4a92943e3dcbc14bf5c1c153fdbe3e8467c5f6ad
[ccsdk/cds.git] /
1 import {Component, ElementRef, OnInit, ViewChild} from '@angular/core';
2 import {ActivatedRoute, Router} from '@angular/router';
3 import {BluePrintDetailModel} from '../model/BluePrint.detail.model';
4 import {PackageCreationStore} from '../package-creation/package-creation.store';
5 import {FilesContent, FolderNodeElement, MetaDataTabModel} from '../package-creation/mapping-models/metadata/MetaDataTab.model';
6 import {MetadataTabComponent} from '../package-creation/metadata-tab/metadata-tab.component';
7 import * as JSZip from 'jszip';
8 import {ConfigurationDashboardService} from './configuration-dashboard.service';
9 import {VlbDefinition} from '../package-creation/mapping-models/definitions/VlbDefinition';
10 import {DslDefinition} from '../package-creation/mapping-models/CBAPacakge.model';
11 import {PackageCreationUtils} from '../package-creation/package-creation.utils';
12 import {PackageCreationModes} from '../package-creation/creationModes/PackageCreationModes';
13 import {PackageCreationBuilder} from '../package-creation/creationModes/PackageCreationBuilder';
14 import {saveAs} from 'file-saver';
15 import {DesignerStore} from '../designer/designer.store';
16 import {DesignerService} from '../designer/designer.service';
17
18 @Component({
19     selector: 'app-configuration-dashboard',
20     templateUrl: './configuration-dashboard.component.html',
21     styleUrls: ['./configuration-dashboard.component.css'],
22 })
23 export class ConfigurationDashboardComponent implements OnInit {
24     viewedPackage: BluePrintDetailModel = new BluePrintDetailModel();
25     @ViewChild(MetadataTabComponent, {static: false})
26     private metadataTabComponent: MetadataTabComponent;
27     public customActionName = '';
28
29     entryDefinitionKeys: string[] = ['template_tags', 'user-groups',
30         'author-email', 'template_version', 'template_name', 'template_author', 'template_description'];
31     @ViewChild('nameit', {static: true})
32     private elementRef: ElementRef;
33
34     private zipFile: JSZip = new JSZip();
35     private filesData: any = [];
36     private folder: FolderNodeElement = new FolderNodeElement();
37
38     private currentBlob = new Blob();
39
40     constructor(private route: ActivatedRoute, private configurationDashboardService: ConfigurationDashboardService,
41                 private packageCreationStore: PackageCreationStore,
42                 private packageCreationUtils: PackageCreationUtils,
43                 private router: Router,
44                 private designerStore: DesignerStore,
45                 private designerService: DesignerService) {
46     }
47
48     ngOnInit() {
49         this.elementRef.nativeElement.focus();
50         const id = this.route.snapshot.paramMap.get('id');
51         this.configurationDashboardService.getPagedPackages(id).subscribe(
52             (bluePrintDetailModels) => {
53                 if (bluePrintDetailModels) {
54                     this.viewedPackage = bluePrintDetailModels[0];
55                     this.downloadCBAPackage(bluePrintDetailModels);
56                 }
57             });
58     }
59
60
61     private downloadCBAPackage(bluePrintDetailModels: BluePrintDetailModel) {
62         this.configurationDashboardService.downloadResource(
63             bluePrintDetailModels[0].artifactName + '/' + bluePrintDetailModels[0].artifactVersion).subscribe(response => {
64             const blob = new Blob([response], {type: 'application/octet-stream'});
65             this.currentBlob = blob;
66             this.zipFile.loadAsync(blob).then((zip) => {
67                 Object.keys(zip.files).forEach((filename) => {
68                     zip.files[filename].async('string').then((fileData) => {
69                         if (fileData) {
70                             if (filename.includes('Scripts/')) {
71                                 this.setScripts(filename, fileData);
72                             } else if (filename.includes('Templates/')) {
73                                 if (filename.includes('-mapping.')) {
74                                     this.setMapping(filename, fileData);
75                                 } else if (filename.includes('-template.')) {
76                                     this.setTemplates(filename, fileData);
77                                 }
78
79                             } else if (filename.includes('Definitions/')) {
80                                 this.setImports(filename, fileData, bluePrintDetailModels);
81                             } else if (filename.includes('TOSCA-Metadata/')) {
82                                 const metaDataTabInfo: MetaDataTabModel = this.getMetaDataTabInfo(fileData);
83                                 this.setMetaData(metaDataTabInfo, bluePrintDetailModels[0]);
84                             }
85                         }
86                     });
87                 });
88             });
89         });
90     }
91
92     private setScripts(filename: string, fileData: any) {
93         this.packageCreationStore.addScripts(filename, fileData);
94     }
95
96     private setImports(filename: string, fileData: any, bluePrintDetailModels: BluePrintDetailModel) {
97         if (filename.includes(bluePrintDetailModels[0].artifactName)) {
98             let definition = new VlbDefinition();
99             definition = fileData as VlbDefinition;
100             definition = JSON.parse(fileData);
101             const dslDefinition = new DslDefinition();
102             dslDefinition.content = this.packageCreationUtils.transformToJson(definition.dsl_definitions);
103             const mapOfCustomKeys = new Map<string, string>();
104             for (const metadataKey in definition.metadata) {
105                 if (!this.entryDefinitionKeys.includes(metadataKey + '')) {
106                     mapOfCustomKeys.set(metadataKey + '', definition.metadata[metadataKey + '']);
107                 }
108             }
109             this.packageCreationStore.changeDslDefinition(dslDefinition);
110             this.packageCreationStore.setCustomKeys(mapOfCustomKeys);
111             if (definition.topology_template && definition.topology_template.content) {
112                 this.designerStore.saveSourceContent(definition.topology_template.content);
113             }
114         } else {
115             this.packageCreationStore.addDefinition(filename, fileData);
116
117         }
118     }
119
120     private setTemplates(filename: string, fileData: any) {
121         this.packageCreationStore.addTemplate(filename, fileData);
122     }
123
124     private setMapping(fileName: string, fileData: string) {
125         this.packageCreationStore.addMapping(fileName, fileData);
126     }
127
128     editBluePrint() {
129         this.packageCreationStore.state$.subscribe(
130             cbaPackage => {
131                 FilesContent.clear();
132                 let packageCreationModes: PackageCreationModes;
133                 cbaPackage = PackageCreationModes.mapModeType(cbaPackage);
134                 cbaPackage.metaData = PackageCreationModes.setEntryPoint(cbaPackage.metaData);
135                 packageCreationModes = PackageCreationBuilder.getCreationMode(cbaPackage);
136                 packageCreationModes.execute(cbaPackage, this.packageCreationUtils);
137                 this.filesData.push(this.folder.TREE_DATA);
138                 this.saveBluePrintToDataBase();
139             });
140     }
141
142     private setMetaData(metaDataObject: MetaDataTabModel, bluePrintDetailModel: BluePrintDetailModel) {
143         metaDataObject.description = bluePrintDetailModel.artifactDescription;
144         this.packageCreationStore.changeMetaData(metaDataObject);
145
146     }
147
148     saveMetaData() {
149         this.metadataTabComponent.saveMetaDataToStore();
150     }
151
152     getMetaDataTabInfo(fileData: string) {
153         const metaDataTabModel = new MetaDataTabModel();
154         const arrayOfLines = fileData.split('\n');
155         metaDataTabModel.entryFileName = arrayOfLines[3].split(':')[1];
156         metaDataTabModel.name = arrayOfLines[4].split(':')[1];
157         metaDataTabModel.version = arrayOfLines[5].split(':')[1];
158         metaDataTabModel.mode = arrayOfLines[6].split(':')[1];
159         metaDataTabModel.templateTags = new Set<string>(arrayOfLines[7].split(':')[1].split(','));
160         return metaDataTabModel;
161     }
162
163     saveBluePrintToDataBase() {
164         this.create();
165         this.zipFile.generateAsync({type: 'blob'})
166             .then(blob => {
167                 this.packageCreationStore.saveBluePrint(blob);
168                 this.router.navigate(['/packages']);
169             });
170     }
171
172
173     create() {
174         FilesContent.getMapOfFilesNamesAndContent().forEach((value, key) => {
175             this.zipFile.folder(key.split('/')[0]);
176             this.zipFile.file(key, value);
177         });
178
179     }
180
181     goBacktoDashboard() {
182         this.router.navigate(['/packages']);
183     }
184
185     downloadPackage(artifactName: string, artifactVersion: string) {
186         this.configurationDashboardService.downloadResource(artifactName + '/' + artifactVersion).subscribe(response => {
187             const blob = new Blob([response], {type: 'application/octet-stream'});
188             saveAs(blob, artifactName + '-' + artifactVersion + '-CBA.zip');
189         });
190     }
191
192     deployCurrentPackage() {
193         console.log('happened');
194         /*   this.zipFile.generateAsync({type: 'blob'})
195                .then(blob => {
196                    const formData = new FormData();
197                    formData.append('file', this.currentBlob);
198                    this.configurationDashboardService.deployPost(formData)
199                        .subscribe(data => {
200                        }, error => {
201                        });
202                    this.router.navigate(['/packages']);
203                });
204    */
205         this.router.navigate(['/packages']);
206     }
207
208     goToDesignerMode(id) {
209         //  this.designerService.setActionName(this.customActionName);
210         this.router.navigate(['/packages/designer', id, {actionName: this.customActionName}]);
211     }
212 }