1 import { Component, ElementRef, OnDestroy, 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 } 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, CBADefinition } from '../package-creation/mapping-models/definitions/CBADefinition';
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 import { distinctUntilChanged, takeUntil } from 'rxjs/operators';
22 import { Subject, throwError } from 'rxjs';
23 import { NgxUiLoaderService } from 'ngx-ui-loader';
26 selector: 'app-configuration-dashboard',
27 templateUrl: './configuration-dashboard.component.html',
28 styleUrls: ['./configuration-dashboard.component.css'],
30 export class ConfigurationDashboardComponent extends ComponentCanDeactivate implements OnInit, OnDestroy {
31 viewedPackage: BluePrintDetailModel = new BluePrintDetailModel();
32 @ViewChild(MetadataTabComponent, { static: false })
33 metadataTabComponent: MetadataTabComponent;
34 public customActionName = '';
36 entryDefinitionKeys: string[] = ['template_tags', 'user-groups',
37 'author-email', 'template_version', 'template_name', 'template_author', 'template_description'];
38 @ViewChild('nameit', { static: true })
39 elementRef: ElementRef;
41 zipFile: JSZip = new JSZip();
43 folder: FolderNodeElement = new FolderNodeElement();
46 currentBlob = new Blob();
47 vlbDefinition: CBADefinition = new CBADefinition();
48 isSaveEnabled = false;
49 versionPattern = '^(\\d+\\.)?(\\d+\\.)?(\\*|\\d+)$';
50 metadataClasses = 'nav-item nav-link active';
51 private cbaPackage: CBAPackage = new CBAPackage();
53 ngUnsubscribe = new Subject();
54 private designerState: any;
58 private route: ActivatedRoute,
59 private configurationDashboardService: ConfigurationDashboardService,
60 private packageCreationStore: PackageCreationStore,
61 private packageCreationService: PackageCreationService,
62 private packageCreationUtils: PackageCreationUtils,
63 private router: Router,
64 private designerStore: DesignerStore,
65 private toastService: ToastrService,
66 private ngxService: NgxUiLoaderService,
67 private packageCreationExtractionService: PackageCreationExtractionService,
68 private activatedRoute: ActivatedRoute,
76 this.ngxService.start();
77 this.vlbDefinition.topology_template = new TemplateTopology();
78 this.packageCreationStore.state$
79 .pipe(distinctUntilChanged((a: any, b: any) => JSON.stringify(a) === JSON.stringify(b)),
80 takeUntil(this.ngUnsubscribe))
83 this.cbaPackage = cbaPackage;
85 this.designerStore.state$.pipe(
86 distinctUntilChanged((a: any, b: any) => JSON.stringify(a) === JSON.stringify(b)),
87 takeUntil(this.ngUnsubscribe))
89 this.designerState = state;
90 this.vlbDefinition.topology_template.content = this.packageCreationUtils.transformToJson(state.template);
92 this.elementRef.nativeElement.focus();
93 this.refreshCurrentPackage();
94 const regexp = RegExp(this.versionPattern);
95 if (this.cbaPackage && this.cbaPackage.metaData && this.cbaPackage.metaData.description
96 && this.cbaPackage.metaData.name && this.cbaPackage.metaData.version &&
97 regexp.test(this.cbaPackage.metaData.version)) {
98 if (!this.metadataClasses.includes('complete')) {
99 this.metadataClasses += ' complete';
102 this.metadataClasses = this.metadataClasses.replace('complete', '');
103 this.isSaveEnabled = false;
105 this.activatedRoute.paramMap.subscribe(res => {
106 this.packageId = res.get('id');
112 private refreshCurrentPackage(id?) {
113 this.id = this.route.snapshot.paramMap.get('id');
114 console.log(this.id);
115 id = id ? id : this.id;
116 this.configurationDashboardService.getPagedPackages(id).subscribe(
117 (bluePrintDetailModels) => {
118 if (bluePrintDetailModels) {
119 this.viewedPackage = bluePrintDetailModels[0];
120 this.downloadCBAPackage(bluePrintDetailModels);
121 this.packageCreationStore.clear();
125 // this.ngxService.stop();
129 private downloadCBAPackage(bluePrintDetailModels: BluePrintDetailModel) {
130 this.configurationDashboardService.downloadResource(
131 this.viewedPackage.artifactName + '/' + this.viewedPackage.artifactVersion).subscribe(response => {
132 const blob = new Blob([response], { type: 'application/octet-stream' });
133 this.currentBlob = blob;
134 this.packageCreationExtractionService.extractBlobToStore(blob);
137 this.ngxService.stop();
142 this.ngxService.start();
143 this.configurationDashboardService.deletePackage(this.packageId).subscribe(res => {
145 this.saveBluePrintToDataBase();
150 private formTreeData() {
151 FilesContent.clear();
152 let packageCreationModes: PackageCreationModes;
153 this.cbaPackage = PackageCreationModes.mapModeType(this.cbaPackage);
154 this.cbaPackage.metaData = PackageCreationModes.setEntryPoint(this.cbaPackage.metaData);
155 packageCreationModes = PackageCreationBuilder.getCreationMode(this.cbaPackage);
156 packageCreationModes.execute(this.cbaPackage, this.packageCreationUtils);
157 this.filesData.push(this.folder.TREE_DATA);
161 this.metadataTabComponent.saveMetaDataToStore();
164 saveBluePrintToDataBase() {
166 this.zipFile.generateAsync({ type: 'blob' })
168 this.packageCreationService.savePackage(blob).subscribe(
169 bluePrintDetailModels => {
170 if (bluePrintDetailModels) {
171 const id = bluePrintDetailModels.toString().split('id')[1].split(':')[1].split('"')[1];
172 this.toastService.info('Package Updated Successfully ');
173 this.isSaveEnabled = false;
174 this.router.navigate(['/packages/package/' + id]);
175 this.refreshCurrentPackage(id);
178 this.toastService.error('Error occured when editing ' + error.message);
179 console.log('Error -' + error.message);
181 this.ngxService.stop();
187 this.configurationDashboardService.deletePackage(this.id).subscribe(res => {
188 console.log('Deleted');
190 this.isSaveEnabled = false;
191 this.router.navigate(['/packages']);
198 this.zipFile = new JSZip();
199 FilesContent.getMapOfFilesNamesAndContent().forEach((value, key) => {
200 this.zipFile.folder(key.split('/')[0]);
201 this.zipFile.file(key, value);
207 this.refreshCurrentPackage();
210 downloadPackage(artifactName: string, artifactVersion: string) {
211 this.ngxService.start();
212 this.configurationDashboardService.downloadResource(artifactName + '/' + artifactVersion).subscribe(response => {
213 const blob = new Blob([response], { type: 'application/octet-stream' });
214 saveAs(blob, artifactName + '-' + artifactVersion + '-CBA.zip');
216 }, err => { }, () => {
217 this.ngxService.stop();
221 deployCurrentPackage() {
222 this.ngxService.start();
224 this.deployPackage();
228 goToDesignerMode(id) {
229 this.router.navigate(['/packages/designer', id, { actionName: this.customActionName }]);
232 public dropped(files: NgxFileDropEntry[]) {
236 public fileOver(event) {
240 public fileLeave(event) {
244 textChanged($event: {}) {
245 this.cbaPackage.templateTopology.node_templates = this.designerState.template.node_templates;
246 this.cbaPackage.templateTopology.workflows = this.designerState.template.workflows;
247 this.cbaPackage.templateTopology.content = this.vlbDefinition.topology_template.content;
251 this.ngxService.start();
252 this.packageCreationStore.addTopologyTemplate(this.cbaPackage.templateTopology);
254 this.enrichPackage();
258 private enrichPackage() {
260 this.zipFile.generateAsync({ type: 'blob' })
262 this.packageCreationService.enrichPackage(blob).subscribe(response => {
263 console.log('success');
264 const blobInfo = new Blob([response], { type: 'application/octet-stream' });
265 this.currentBlob = blobInfo;
266 this.packageCreationStore.clear();
267 this.packageCreationExtractionService.extractBlobToStore(this.currentBlob);
268 this.isSaveEnabled = true;
269 this.toastService.success('Enriched Done Successfully');
271 this.handleError(err);
273 this.ngxService.stop();
276 this.toastService.error('Error occured during enrichment process' + error.message);
277 console.error('Error -' + error.message);
279 this.ngxService.stop();
283 private deployPackage() {
285 this.zipFile.generateAsync({ type: 'blob' })
287 this.packageCreationService.deploy(blob).subscribe(response => {
288 this.toastService.info('Package Deployed Successfully ');
289 const id = response.toString().split('id')[1].split(':')[1].split('"')[1];
290 this.isSaveEnabled = false;
291 this.router.navigate(['/packages/package/' + id]);
293 this.handleError(err);
295 this.ngxService.stop();
298 this.handleError(error);
300 this.ngxService.stop();
305 this.isSaveEnabled = true;
308 canDeactivate(): boolean {
309 return this.isSaveEnabled;
313 this.ngUnsubscribe.next();
314 this.ngUnsubscribe.complete();
317 checkSkipTypesOfAction() {
318 console.log(this.cbaPackage);
319 if (this.cbaPackage.templateTopology && this.cbaPackage.templateTopology.node_templates
320 && this.cbaPackage.templateTopology.workflows) {
321 this.goToDesignerMode(this.id);
323 this.dataTarget = '#exampleModalLong';
328 let errorMessage = '';
329 if (error.error instanceof ErrorEvent) {
331 errorMessage = `Error: ${error.error.message}`;
334 errorMessage = `Error Code: ${error.status}\nMessage: ${error.message}`;
336 this.toastService.error('Error occured when deploying ' + errorMessage);
337 console.log('Error -' + errorMessage);
338 this.ngxService.stop();
339 this.toastService.error('Error occured when deploying' + error.message);
340 return throwError(errorMessage);