a69e45fcec4288cdaf85ad9aee1ae5ed6262e6bf
[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 {TemplateTopology, 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 {ToastrService} from 'ngx-toastr';
17 import {NgxFileDropEntry} from 'ngx-file-drop';
18
19 @Component({
20     selector: 'app-configuration-dashboard',
21     templateUrl: './configuration-dashboard.component.html',
22     styleUrls: ['./configuration-dashboard.component.css'],
23 })
24 export class ConfigurationDashboardComponent implements OnInit {
25     viewedPackage: BluePrintDetailModel = new BluePrintDetailModel();
26     @ViewChild(MetadataTabComponent, {static: false})
27     metadataTabComponent: MetadataTabComponent;
28     public customActionName = '';
29
30     entryDefinitionKeys: string[] = ['template_tags', 'user-groups',
31         'author-email', 'template_version', 'template_name', 'template_author', 'template_description'];
32     @ViewChild('nameit', {static: true})
33     elementRef: ElementRef;
34     uploadedFiles = [];
35     zipFile: JSZip = new JSZip();
36     filesData: any = [];
37     folder: FolderNodeElement = new FolderNodeElement();
38     id: any;
39
40     currentBlob = new Blob();
41     vlbDefinition: VlbDefinition = new VlbDefinition();
42     isSaveEnabled = false;
43     versionPattern = '^(\\d+\\.)?(\\d+\\.)?(\\*|\\d+)$';
44
45     constructor(
46         private route: ActivatedRoute,
47         private configurationDashboardService: ConfigurationDashboardService,
48         private packageCreationStore: PackageCreationStore,
49         private packageCreationUtils: PackageCreationUtils,
50         private router: Router,
51         private designerStore: DesignerStore,
52         private toastService: ToastrService
53     ) {
54     }
55
56     ngOnInit() {
57         this.vlbDefinition.topology_template = new TemplateTopology();
58
59         this.elementRef.nativeElement.focus();
60         this.refreshCurrentPackage();
61
62         const regexp = RegExp(this.versionPattern);
63         this.packageCreationStore.state$.subscribe(cbaPackage => {
64             if (cbaPackage && cbaPackage.metaData && cbaPackage.metaData.description
65                 && cbaPackage.metaData.name && cbaPackage.metaData.version &&
66                 regexp.test(cbaPackage.metaData.version)) {
67                 this.isSaveEnabled = true;
68             } else {
69                 this.isSaveEnabled = false;
70             }
71         });
72     }
73
74     private refreshCurrentPackage() {
75         this.id = this.route.snapshot.paramMap.get('id');
76         this.configurationDashboardService.getPagedPackages(this.id).subscribe(
77             (bluePrintDetailModels) => {
78                 if (bluePrintDetailModels) {
79                     this.viewedPackage = bluePrintDetailModels[0];
80                     this.downloadCBAPackage(bluePrintDetailModels);
81                     this.packageCreationStore.clear();
82                 }
83             });
84     }
85
86     private downloadCBAPackage(bluePrintDetailModels: BluePrintDetailModel) {
87         this.configurationDashboardService.downloadResource(
88             bluePrintDetailModels[0].artifactName + '/' + bluePrintDetailModels[0].artifactVersion).subscribe(response => {
89             const blob = new Blob([response], {type: 'application/octet-stream'});
90             this.currentBlob = blob;
91             this.zipFile.loadAsync(blob).then((zip) => {
92                 Object.keys(zip.files).forEach((filename) => {
93                     zip.files[filename].async('string').then((fileData) => {
94                         if (fileData) {
95                             if (filename.includes('Scripts/')) {
96                                 this.setScripts(filename, fileData);
97                             } else if (filename.includes('Templates/')) {
98                                 if (filename.includes('-mapping.')) {
99                                     this.setMapping(filename, fileData);
100                                 } else if (filename.includes('-template.')) {
101                                     this.setTemplates(filename, fileData);
102                                 }
103
104                             } else if (filename.includes('Definitions/')) {
105                                 this.setImports(filename, fileData, bluePrintDetailModels);
106                             } else if (filename.includes('TOSCA-Metadata/')) {
107                                 const metaDataTabInfo: MetaDataTabModel = this.getMetaDataTabInfo(fileData);
108                                 this.setMetaData(metaDataTabInfo, bluePrintDetailModels[0]);
109                             }
110                         }
111                     });
112                 });
113             });
114         });
115     }
116
117     setScripts(filename: string, fileData: any) {
118         this.packageCreationStore.addScripts(filename, fileData);
119     }
120
121     setImports(filename: string, fileData: any, bluePrintDetailModels: BluePrintDetailModel) {
122         if (filename.includes(bluePrintDetailModels[0].artifactName)) {
123             let definition = new VlbDefinition();
124             definition = fileData as VlbDefinition;
125             definition = JSON.parse(fileData);
126             const dslDefinition = new DslDefinition();
127             dslDefinition.content = this.packageCreationUtils.transformToJson(definition.dsl_definitions);
128             const mapOfCustomKeys = new Map<string, string>();
129             for (const metadataKey in definition.metadata) {
130                 if (!this.entryDefinitionKeys.includes(metadataKey + '')) {
131                     mapOfCustomKeys.set(metadataKey + '', definition.metadata[metadataKey + '']);
132                 }
133             }
134             this.packageCreationStore.changeDslDefinition(dslDefinition);
135             this.packageCreationStore.setCustomKeys(mapOfCustomKeys);
136             if (definition.topology_template && definition.topology_template.content) {
137                 this.designerStore.saveSourceContent(definition.topology_template.content);
138             }
139
140         }
141         this.packageCreationStore.addDefinition(filename, fileData);
142
143     }
144
145     setTemplates(filename: string, fileData: any) {
146         this.packageCreationStore.addTemplate(filename, fileData);
147     }
148
149     setMapping(fileName: string, fileData: string) {
150         this.packageCreationStore.addMapping(fileName, fileData);
151     }
152
153     editBluePrint() {
154         this.packageCreationStore.state$.subscribe(
155             cbaPackage => {
156                 FilesContent.clear();
157                 let packageCreationModes: PackageCreationModes;
158                 cbaPackage = PackageCreationModes.mapModeType(cbaPackage);
159                 cbaPackage.metaData = PackageCreationModes.setEntryPoint(cbaPackage.metaData);
160                 packageCreationModes = PackageCreationBuilder.getCreationMode(cbaPackage);
161                 packageCreationModes.execute(cbaPackage, this.packageCreationUtils);
162                 this.filesData.push(this.folder.TREE_DATA);
163                 this.saveBluePrintToDataBase();
164             });
165     }
166
167     setMetaData(metaDataObject: MetaDataTabModel, bluePrintDetailModel: BluePrintDetailModel) {
168         metaDataObject.description = bluePrintDetailModel.artifactDescription;
169         this.packageCreationStore.changeMetaData(metaDataObject);
170
171     }
172
173     saveMetaData() {
174         this.metadataTabComponent.saveMetaDataToStore();
175     }
176
177     getMetaDataTabInfo(fileData: string) {
178         const metaDataTabModel = new MetaDataTabModel();
179         const arrayOfLines = fileData.split('\n');
180         metaDataTabModel.entryFileName = arrayOfLines[3].split(':')[1];
181         metaDataTabModel.name = arrayOfLines[4].split(':')[1];
182         metaDataTabModel.version = arrayOfLines[5].split(':')[1];
183         metaDataTabModel.mode = arrayOfLines[6].split(':')[1];
184         metaDataTabModel.templateTags = new Set<string>(arrayOfLines[7].split(':')[1].split(','));
185         return metaDataTabModel;
186     }
187
188     saveBluePrintToDataBase() {
189         this.create();
190         this.zipFile.generateAsync({type: 'blob'})
191             .then(blob => {
192                 this.packageCreationStore.saveBluePrint(blob).subscribe(
193                     bluePrintDetailModels => {
194                         if (bluePrintDetailModels) {
195                             const id = bluePrintDetailModels.toString().split('id')[1].split(':')[1].split('"')[1];
196                             this.toastService.info('package updated successfully ');
197                             this.router.navigate(['/packages/package/' + id]);
198                         }
199                     }, error => {
200                         this.toastService.error('error happened when editing ' + error.message);
201                         console.log('Error -' + error.message);
202                     });
203             });
204     }
205
206     deletePackage() {
207         this.configurationDashboardService.deletePackage(this.id).subscribe(res => {
208             console.log('Deleted');
209             console.log(res);
210             this.router.navigate(['/packages']);
211         }, err => {
212             console.log(err);
213         });
214     }
215
216     create() {
217         this.zipFile = new JSZip();
218         FilesContent.getMapOfFilesNamesAndContent().forEach((value, key) => {
219             this.zipFile.folder(key.split('/')[0]);
220             this.zipFile.file(key, value);
221         });
222
223     }
224
225     discardChanges() {
226         this.refreshCurrentPackage();
227     }
228
229     downloadPackage(artifactName: string, artifactVersion: string) {
230         this.configurationDashboardService.downloadResource(artifactName + '/' + artifactVersion).subscribe(response => {
231             const blob = new Blob([response], {type: 'application/octet-stream'});
232             saveAs(blob, artifactName + '-' + artifactVersion + '-CBA.zip');
233         });
234     }
235
236     deployCurrentPackage() {
237         console.log('happened');
238         this.router.navigate(['/packages']);
239     }
240
241     goToDesignerMode(id) {
242         //  this.designerService.setActionName(this.customActionName);
243         this.router.navigate(['/packages/designer', id, {actionName: this.customActionName}]);
244     }
245
246     public dropped(files: NgxFileDropEntry[]) {
247
248     }
249
250     public fileOver(event) {
251         console.log(event);
252     }
253
254     public fileLeave(event) {
255         console.log(event);
256     }
257
258     textChanged($event: {}) {
259         this.packageCreationStore.addTopologyTemplate(this.vlbDefinition.topology_template);
260     }
261
262     enrichBluePrint() {
263
264         this.packageCreationStore.state$.subscribe(
265             cbaPackage => {
266                 FilesContent.clear();
267                 console.log(cbaPackage);
268
269                 let packageCreationModes: PackageCreationModes;
270                 cbaPackage = PackageCreationModes.mapModeType(cbaPackage);
271                 cbaPackage.metaData = PackageCreationModes.setEntryPoint(cbaPackage.metaData);
272                 packageCreationModes = PackageCreationBuilder.getCreationMode(cbaPackage);
273                 packageCreationModes.execute(cbaPackage, this.packageCreationUtils);
274                 this.filesData.push(this.folder.TREE_DATA);
275                 this.enrichPackage();
276
277             });
278     }
279
280     private enrichPackage() {
281         this.create();
282         this.zipFile.generateAsync({type: 'blob'})
283             .then(blob => {
284                 this.packageCreationStore.enrichBluePrint(blob).subscribe(response => {
285                     console.log('success');
286                     const blobInfo = new Blob([response], {type: 'application/octet-stream'});
287                     saveAs(blobInfo, 'test' + '-' + '1.0.0' + '-CBA.zip');
288                     this.toastService.info('enriched successfully ');
289                 });
290             }, error => {
291                 this.toastService.error('error happened when editing ' + error.message);
292                 console.log('Error -' + error.message);
293             });
294     }
295 }