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