19c10c5fd2ef8b7d8f329eceacb55cda9b2f8581
[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: string;
28
29     entryDefinitionKeys: string[] = ['template_tags', 'user-groups',
30         'author-email', 'template_version', 'template_name', 'template_author'];
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                     console.log(filename);
69                     zip.files[filename].async('string').then((fileData) => {
70                         if (fileData) {
71                             if (filename.includes('Scripts/')) {
72                                 this.setScripts(filename, fileData);
73                             } else if (filename.includes('Templates/')) {
74                                 if (filename.includes('-mapping.')) {
75                                     this.setMapping(filename, fileData);
76                                 } else if (filename.includes('-template.')) {
77                                     this.setTemplates(filename, fileData);
78                                 }
79                             } else if (filename.includes('Definitions/')) {
80                                 this.setImports(filename, fileData);
81                             } else if (filename.includes('TOSCA-Metadata/')) {
82                                 const metaDataTabInfo: MetaDataTabModel = this.getMetaDataTabInfo(fileData);
83                                 // console.log(metaDataTabInfo);
84                                 this.setMetaData(metaDataTabInfo, bluePrintDetailModels[0]);
85                             }
86                         }
87                     });
88                 });
89             });
90         });
91     }
92
93     private setScripts(filename: string, fileData: any) {
94         this.packageCreationStore.addScripts(filename, fileData);
95     }
96
97     private setImports(filename: string, fileData: any) {
98         if (filename.includes('blueprint.json') || filename.includes('vLB_CDS.json')) {
99             let definition = new VlbDefinition();
100             definition = fileData as VlbDefinition;
101             definition = JSON.parse(fileData);
102             const dslDefinition = new DslDefinition();
103             dslDefinition.content = this.packageCreationUtils.transformToJson(definition.dsl_definitions);
104             const mapOfCustomKeys = new Map<string, string>();
105             for (const metadataKey in definition.metadata) {
106                 if (!this.entryDefinitionKeys.includes(metadataKey + '')) {
107                     mapOfCustomKeys.set(metadataKey + '', definition.metadata[metadataKey + '']);
108                 }
109             }
110             this.packageCreationStore.changeDslDefinition(dslDefinition);
111             this.packageCreationStore.setCustomKeys(mapOfCustomKeys);
112             // console.log(definition.topology_template.content);
113             if (definition.topology_template.content) {
114                 this.designerStore.saveSourceContent(definition.topology_template.content);
115             }
116         } else {
117             this.packageCreationStore.addDefinition(filename, fileData);
118
119         }
120     }
121
122     private setTemplates(filename: string, fileData: any) {
123         this.packageCreationStore.addTemplate(filename, fileData);
124     }
125
126     private setMapping(fileName: string, fileData: string) {
127         this.packageCreationStore.addMapping(fileName, fileData);
128     }
129
130     editBluePrint() {
131         this.packageCreationStore.state$.subscribe(
132             cbaPackage => {
133                 console.log(cbaPackage);
134                 FilesContent.clear();
135                 let packageCreationModes: PackageCreationModes;
136                 cbaPackage = PackageCreationModes.mapModeType(cbaPackage);
137                 cbaPackage.metaData = PackageCreationModes.setEntryPoint(cbaPackage.metaData);
138                 packageCreationModes = PackageCreationBuilder.getCreationMode(cbaPackage);
139                 packageCreationModes.execute(cbaPackage, this.packageCreationUtils);
140                 this.filesData.push(this.folder.TREE_DATA);
141                 this.saveBluePrintToDataBase();
142             });
143     }
144
145     private setMetaData(metaDataObject: MetaDataTabModel, bluePrintDetailModel: BluePrintDetailModel) {
146         metaDataObject.description = bluePrintDetailModel.artifactDescription;
147         this.packageCreationStore.changeMetaData(metaDataObject);
148
149     }
150
151     saveMetaData() {
152         this.metadataTabComponent.saveMetaDataToStore();
153     }
154
155     getMetaDataTabInfo(fileData: string) {
156         const metaDataTabModel = new MetaDataTabModel();
157         const arrayOfLines = fileData.split('\n');
158         metaDataTabModel.entryFileName = arrayOfLines[3].split(':')[1];
159         metaDataTabModel.name = arrayOfLines[4].split(':')[1];
160         metaDataTabModel.version = arrayOfLines[5].split(':')[1];
161         metaDataTabModel.mode = arrayOfLines[6].split(':')[1];
162         metaDataTabModel.templateTags = new Set<string>(arrayOfLines[7].split(':')[1].split(','));
163         console.log(metaDataTabModel.mode);
164         return metaDataTabModel;
165     }
166
167     saveBluePrintToDataBase() {
168         this.create();
169         this.zipFile.generateAsync({type: 'blob'})
170             .then(blob => {
171                 this.packageCreationStore.saveBluePrint(blob);
172                 this.router.navigate(['/packages']);
173             });
174     }
175
176
177     create() {
178         FilesContent.getMapOfFilesNamesAndContent().forEach((value, key) => {
179             this.zipFile.folder(key.split('/')[0]);
180             this.zipFile.file(key, value);
181         });
182
183     }
184
185     goBacktoDashboard() {
186         this.router.navigate(['/packages']);
187     }
188
189     downloadPackage(artifactName: string, artifactVersion: string) {
190         this.configurationDashboardService.downloadResource(artifactName + '/' + artifactVersion).subscribe(response => {
191             const blob = new Blob([response], {type: 'application/octet-stream'});
192             saveAs(blob, artifactName + '-' + artifactVersion + '-CBA.zip');
193         });
194     }
195
196     deployCurrentPackage() {
197         console.log('happened');
198         /*   this.zipFile.generateAsync({type: 'blob'})
199                .then(blob => {
200                    const formData = new FormData();
201                    formData.append('file', this.currentBlob);
202                    this.configurationDashboardService.deployPost(formData)
203                        .subscribe(data => {
204                        }, error => {
205                        });
206                    this.router.navigate(['/packages']);
207                });
208    */
209         this.router.navigate(['/packages']);
210     }
211
212     goToDesignerMode(id) {
213       //  this.designerService.setActionName(this.customActionName);
214         this.router.navigate(['/packages/designer', id, {actionName: this.customActionName}]);
215      }
216 }