e86a2c500c53d9005d935a86eaa60681ee990c7b
[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 {CBAPackage} 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 import {PackageCreationService} from '../package-creation/package-creation.service';
19 import {ComponentCanDeactivate} from '../../../../common/core/canDactivate/ComponentCanDeactivate';
20 import {PackageCreationExtractionService} from '../package-creation/package-creation-extraction.service';
21
22 @Component({
23     selector: 'app-configuration-dashboard',
24     templateUrl: './configuration-dashboard.component.html',
25     styleUrls: ['./configuration-dashboard.component.css'],
26 })
27 export class ConfigurationDashboardComponent extends ComponentCanDeactivate implements OnInit {
28     viewedPackage: BluePrintDetailModel = new BluePrintDetailModel();
29     @ViewChild(MetadataTabComponent, {static: false})
30     metadataTabComponent: MetadataTabComponent;
31     public customActionName = '';
32
33     entryDefinitionKeys: string[] = ['template_tags', 'user-groups',
34         'author-email', 'template_version', 'template_name', 'template_author', 'template_description'];
35     @ViewChild('nameit', {static: true})
36     elementRef: ElementRef;
37     uploadedFiles = [];
38     zipFile: JSZip = new JSZip();
39     filesData: any = [];
40     folder: FolderNodeElement = new FolderNodeElement();
41     id: any;
42
43     currentBlob = new Blob();
44     vlbDefinition: VlbDefinition = new VlbDefinition();
45     isSaveEnabled = false;
46     versionPattern = '^(\\d+\\.)?(\\d+\\.)?(\\*|\\d+)$';
47     metadataClasses = 'nav-item nav-link active';
48     private cbaPackage: CBAPackage = new CBAPackage();
49     dataTarget: any = '';
50
51     constructor(
52         private route: ActivatedRoute,
53         private configurationDashboardService: ConfigurationDashboardService,
54         private packageCreationStore: PackageCreationStore,
55         private packageCreationService: PackageCreationService,
56         private packageCreationUtils: PackageCreationUtils,
57         private router: Router,
58         private designerStore: DesignerStore,
59         private toastService: ToastrService,
60         private packageCreationExtractionService: PackageCreationExtractionService
61     ) {
62         super();
63
64         this.packageCreationStore.state$.subscribe(
65             cbaPackage => {
66                 this.cbaPackage = cbaPackage;
67             });
68
69
70     }
71
72     ngOnInit() {
73         this.vlbDefinition.topology_template = new TemplateTopology();
74
75         this.elementRef.nativeElement.focus();
76         this.refreshCurrentPackage();
77         const regexp = RegExp(this.versionPattern);
78         if (this.cbaPackage && this.cbaPackage.metaData && this.cbaPackage.metaData.description
79             && this.cbaPackage.metaData.name && this.cbaPackage.metaData.version &&
80             regexp.test(this.cbaPackage.metaData.version)) {
81             if (!this.metadataClasses.includes('complete')) {
82                 this.metadataClasses += ' complete';
83             }
84         } else {
85             this.metadataClasses = this.metadataClasses.replace('complete', '');
86             this.isSaveEnabled = false;
87         }
88
89
90     }
91
92     private refreshCurrentPackage(id?) {
93         this.id = this.route.snapshot.paramMap.get('id');
94         console.log(this.id);
95         id = id ? id : this.id;
96         this.configurationDashboardService.getPagedPackages(id).subscribe(
97             (bluePrintDetailModels) => {
98                 if (bluePrintDetailModels) {
99                     this.viewedPackage = bluePrintDetailModels[0];
100                     this.downloadCBAPackage(bluePrintDetailModels);
101                     this.packageCreationStore.clear();
102                 }
103             });
104     }
105
106     private downloadCBAPackage(bluePrintDetailModels: BluePrintDetailModel) {
107         this.configurationDashboardService.downloadResource(
108             this.viewedPackage.artifactName + '/' + this.viewedPackage.artifactVersion).subscribe(response => {
109             const blob = new Blob([response], {type: 'application/octet-stream'});
110             this.currentBlob = blob;
111             this.packageCreationExtractionService.extractBlobToStore(blob);
112         });
113     }
114
115     editBluePrint() {
116         this.configurationDashboardService.deletePackage(this.viewedPackage.id).subscribe(res => {
117             this.formTreeData();
118             this.saveBluePrintToDataBase();
119
120         });
121     }
122
123     private formTreeData() {
124         FilesContent.clear();
125         let packageCreationModes: PackageCreationModes;
126         this.cbaPackage = PackageCreationModes.mapModeType(this.cbaPackage);
127         this.cbaPackage.metaData = PackageCreationModes.setEntryPoint(this.cbaPackage.metaData);
128         packageCreationModes = PackageCreationBuilder.getCreationMode(this.cbaPackage);
129         packageCreationModes.execute(this.cbaPackage, this.packageCreationUtils);
130         this.filesData.push(this.folder.TREE_DATA);
131     }
132
133     saveMetaData() {
134         this.metadataTabComponent.saveMetaDataToStore();
135     }
136
137     saveBluePrintToDataBase() {
138         this.create();
139         this.zipFile.generateAsync({type: 'blob'})
140             .then(blob => {
141                 this.packageCreationService.savePackage(blob).subscribe(
142                     bluePrintDetailModels => {
143                         if (bluePrintDetailModels) {
144                             const id = bluePrintDetailModels.toString().split('id')[1].split(':')[1].split('"')[1];
145                             this.toastService.info('package updated successfully ');
146                             this.isSaveEnabled = false;
147                             this.router.navigate(['/packages/package/' + id]);
148                             this.refreshCurrentPackage(id);
149                         }
150                     }, error => {
151                         this.toastService.error('error happened when editing ' + error.message);
152                         console.log('Error -' + error.message);
153                     });
154             });
155     }
156
157     deletePackage() {
158         this.configurationDashboardService.deletePackage(this.id).subscribe(res => {
159             console.log('Deleted');
160             console.log(res);
161             this.isSaveEnabled = false;
162             this.router.navigate(['/packages']);
163         }, err => {
164             console.log(err);
165         });
166     }
167
168     create() {
169         this.zipFile = new JSZip();
170         FilesContent.getMapOfFilesNamesAndContent().forEach((value, key) => {
171             this.zipFile.folder(key.split('/')[0]);
172             this.zipFile.file(key, value);
173         });
174
175     }
176
177     discardChanges() {
178         this.refreshCurrentPackage();
179     }
180
181     downloadPackage(artifactName: string, artifactVersion: string) {
182         this.configurationDashboardService.downloadResource(artifactName + '/' + artifactVersion).subscribe(response => {
183             const blob = new Blob([response], {type: 'application/octet-stream'});
184             saveAs(blob, artifactName + '-' + artifactVersion + '-CBA.zip');
185         });
186     }
187
188     deployCurrentPackage() {
189         this.formTreeData();
190         this.deployPackage();
191
192     }
193
194     goToDesignerMode(id) {
195         this.router.navigate(['/packages/designer', id, {actionName: this.customActionName}]);
196     }
197
198     public dropped(files: NgxFileDropEntry[]) {
199
200     }
201
202     public fileOver(event) {
203         console.log(event);
204     }
205
206     public fileLeave(event) {
207         console.log(event);
208     }
209
210     textChanged($event: {}) {
211         this.packageCreationStore.addTopologyTemplate(this.vlbDefinition.topology_template);
212     }
213
214     enrichBluePrint() {
215
216         this.formTreeData();
217         this.enrichPackage();
218     }
219
220
221     private enrichPackage() {
222         this.create();
223         this.zipFile.generateAsync({type: 'blob'})
224             .then(blob => {
225                 this.packageCreationService.enrichPackage(blob).subscribe(response => {
226                     console.log('success');
227                     const blobInfo = new Blob([response], {type: 'application/octet-stream'});
228                     this.currentBlob = blobInfo;
229                     this.packageCreationStore.clear();
230                     this.packageCreationExtractionService.extractBlobToStore(this.currentBlob);
231                     this.isSaveEnabled = true;
232                     this.toastService.info('enriched successfully ');
233                 });
234             }, error => {
235                 this.toastService.error('error happened when enrich ' + error.message);
236                 console.error('Error -' + error.message);
237             });
238     }
239
240     private deployPackage() {
241         this.create();
242         this.zipFile.generateAsync({type: 'blob'})
243             .then(blob => {
244                 this.packageCreationService.deploy(blob).subscribe(response => {
245                     this.toastService.info('deployed successfully ');
246                     const id = response.toString().split('id')[1].split(':')[1].split('"')[1];
247                     this.isSaveEnabled = false;
248                     this.router.navigate(['/packages/package/' + id]);
249                 });
250             }, error => {
251                 this.toastService.error('error happened when deploying ' + error.message);
252                 console.log('Error -' + error.message);
253             });
254     }
255
256     clickEvent() {
257         this.isSaveEnabled = true;
258     }
259
260     canDeactivate(): boolean {
261         return this.isSaveEnabled;
262     }
263
264     checkSkipTypesOfAction() {
265         if (this.cbaPackage.templateTopology.node_templates && this.cbaPackage.templateTopology.workflows) {
266             this.goToDesignerMode(this.id);
267         } else {
268             this.dataTarget = '#exampleModalLong';
269         }
270     }
271 }