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 import {TopologyTemplate} from '../designer/model/designer.topologyTemplate.model';
21 selector: 'app-configuration-dashboard',
22 templateUrl: './configuration-dashboard.component.html',
23 styleUrls: ['./configuration-dashboard.component.css'],
25 export class ConfigurationDashboardComponent implements OnInit {
26 viewedPackage: BluePrintDetailModel = new BluePrintDetailModel();
27 @ViewChild(MetadataTabComponent, {static: false})
28 metadataTabComponent: MetadataTabComponent;
29 public customActionName = '';
31 entryDefinitionKeys: string[] = ['template_tags', 'user-groups',
32 'author-email', 'template_version', 'template_name', 'template_author', 'template_description'];
33 @ViewChild('nameit', {static: true})
34 elementRef: ElementRef;
36 zipFile: JSZip = new JSZip();
38 folder: FolderNodeElement = new FolderNodeElement();
41 currentBlob = new Blob();
42 vlbDefinition: VlbDefinition = new VlbDefinition();
45 private route: ActivatedRoute,
46 private configurationDashboardService: ConfigurationDashboardService,
47 private packageCreationStore: PackageCreationStore,
48 private packageCreationUtils: PackageCreationUtils,
49 private router: Router,
50 private designerStore: DesignerStore,
51 private toastService: ToastrService
56 this.vlbDefinition.topology_template = new TemplateTopology();
58 this.elementRef.nativeElement.focus();
59 this.id = this.route.snapshot.paramMap.get('id');
60 this.configurationDashboardService.getPagedPackages(this.id).subscribe(
61 (bluePrintDetailModels) => {
62 if (bluePrintDetailModels) {
63 this.viewedPackage = bluePrintDetailModels[0];
64 this.downloadCBAPackage(bluePrintDetailModels);
65 this.packageCreationStore.clear();
69 if (this.route.snapshot.paramMap.has('id')) {
70 console.log('The id is equal to ' + this.route.snapshot.paramMap.get('id'));
74 private downloadCBAPackage(bluePrintDetailModels: BluePrintDetailModel) {
75 this.configurationDashboardService.downloadResource(
76 bluePrintDetailModels[0].artifactName + '/' + bluePrintDetailModels[0].artifactVersion).subscribe(response => {
77 const blob = new Blob([response], {type: 'application/octet-stream'});
78 this.currentBlob = blob;
79 this.zipFile.loadAsync(blob).then((zip) => {
80 Object.keys(zip.files).forEach((filename) => {
81 zip.files[filename].async('string').then((fileData) => {
83 if (filename.includes('Scripts/')) {
84 this.setScripts(filename, fileData);
85 } else if (filename.includes('Templates/')) {
86 if (filename.includes('-mapping.')) {
87 this.setMapping(filename, fileData);
88 } else if (filename.includes('-template.')) {
89 this.setTemplates(filename, fileData);
92 } else if (filename.includes('Definitions/')) {
93 this.setImports(filename, fileData, bluePrintDetailModels);
94 } else if (filename.includes('TOSCA-Metadata/')) {
95 const metaDataTabInfo: MetaDataTabModel = this.getMetaDataTabInfo(fileData);
96 this.setMetaData(metaDataTabInfo, bluePrintDetailModels[0]);
105 setScripts(filename: string, fileData: any) {
106 this.packageCreationStore.addScripts(filename, fileData);
109 setImports(filename: string, fileData: any, bluePrintDetailModels: BluePrintDetailModel) {
110 if (filename.includes(bluePrintDetailModels[0].artifactName)) {
111 let definition = new VlbDefinition();
112 definition = fileData as VlbDefinition;
113 definition = JSON.parse(fileData);
114 const dslDefinition = new DslDefinition();
115 dslDefinition.content = this.packageCreationUtils.transformToJson(definition.dsl_definitions);
116 const mapOfCustomKeys = new Map<string, string>();
117 for (const metadataKey in definition.metadata) {
118 if (!this.entryDefinitionKeys.includes(metadataKey + '')) {
119 mapOfCustomKeys.set(metadataKey + '', definition.metadata[metadataKey + '']);
122 this.packageCreationStore.changeDslDefinition(dslDefinition);
123 this.packageCreationStore.setCustomKeys(mapOfCustomKeys);
124 if (definition.topology_template && definition.topology_template.content) {
125 this.designerStore.saveSourceContent(definition.topology_template.content);
129 this.packageCreationStore.addDefinition(filename, fileData);
133 setTemplates(filename: string, fileData: any) {
134 this.packageCreationStore.addTemplate(filename, fileData);
137 setMapping(fileName: string, fileData: string) {
138 this.packageCreationStore.addMapping(fileName, fileData);
142 this.packageCreationStore.state$.subscribe(
144 FilesContent.clear();
145 let packageCreationModes: PackageCreationModes;
146 cbaPackage = PackageCreationModes.mapModeType(cbaPackage);
147 cbaPackage.metaData = PackageCreationModes.setEntryPoint(cbaPackage.metaData);
148 packageCreationModes = PackageCreationBuilder.getCreationMode(cbaPackage);
149 packageCreationModes.execute(cbaPackage, this.packageCreationUtils);
150 this.filesData.push(this.folder.TREE_DATA);
151 this.saveBluePrintToDataBase();
155 setMetaData(metaDataObject: MetaDataTabModel, bluePrintDetailModel: BluePrintDetailModel) {
156 metaDataObject.description = bluePrintDetailModel.artifactDescription;
157 this.packageCreationStore.changeMetaData(metaDataObject);
162 this.metadataTabComponent.saveMetaDataToStore();
165 getMetaDataTabInfo(fileData: string) {
166 const metaDataTabModel = new MetaDataTabModel();
167 const arrayOfLines = fileData.split('\n');
168 metaDataTabModel.entryFileName = arrayOfLines[3].split(':')[1];
169 metaDataTabModel.name = arrayOfLines[4].split(':')[1];
170 metaDataTabModel.version = arrayOfLines[5].split(':')[1];
171 metaDataTabModel.mode = arrayOfLines[6].split(':')[1];
172 metaDataTabModel.templateTags = new Set<string>(arrayOfLines[7].split(':')[1].split(','));
173 return metaDataTabModel;
176 saveBluePrintToDataBase() {
178 this.zipFile.generateAsync({type: 'blob'})
180 this.packageCreationStore.saveBluePrint(blob).subscribe(
181 bluePrintDetailModels => {
182 if (bluePrintDetailModels) {
183 const id = bluePrintDetailModels.toString().split('id')[1].split(':')[1].split('"')[1];
184 this.toastService.info('package updated successfully ');
185 this.router.navigate(['/packages/package/' + id]);
188 this.toastService.error('error happened when editing ' + error.message);
189 console.log('Error -' + error.message);
195 this.configurationDashboardService.deletePackage(this.id).subscribe(res => {
196 console.log('Deleted');
198 this.router.navigate(['/packages']);
205 this.zipFile = new JSZip();
206 FilesContent.getMapOfFilesNamesAndContent().forEach((value, key) => {
207 this.zipFile.folder(key.split('/')[0]);
208 this.zipFile.file(key, value);
213 goBacktoDashboard() {
214 this.router.navigate(['/packages']);
217 downloadPackage(artifactName: string, artifactVersion: string) {
218 this.configurationDashboardService.downloadResource(artifactName + '/' + artifactVersion).subscribe(response => {
219 const blob = new Blob([response], {type: 'application/octet-stream'});
220 saveAs(blob, artifactName + '-' + artifactVersion + '-CBA.zip');
224 deployCurrentPackage() {
225 console.log('happened');
226 this.router.navigate(['/packages']);
229 goToDesignerMode(id) {
230 // this.designerService.setActionName(this.customActionName);
231 this.router.navigate(['/packages/designer', id, {actionName: this.customActionName}]);
234 public dropped(files: NgxFileDropEntry[]) {
238 public fileOver(event) {
242 public fileLeave(event) {
246 textChanged($event: {}) {
247 this.packageCreationStore.addTopologyTemplate(this.vlbDefinition.topology_template);
252 this.packageCreationStore.state$.subscribe(
254 FilesContent.clear();
255 console.log(cbaPackage);
257 let packageCreationModes: PackageCreationModes;
258 cbaPackage = PackageCreationModes.mapModeType(cbaPackage);
259 cbaPackage.metaData = PackageCreationModes.setEntryPoint(cbaPackage.metaData);
260 packageCreationModes = PackageCreationBuilder.getCreationMode(cbaPackage);
261 packageCreationModes.execute(cbaPackage, this.packageCreationUtils);
262 this.filesData.push(this.folder.TREE_DATA);
263 this.enrichPackage();
268 private enrichPackage() {
270 this.zipFile.generateAsync({type: 'blob'})
272 this.packageCreationStore.enrichBluePrint(blob).subscribe(response => {
273 console.log('success');
274 const blobInfo = new Blob([response], {type: 'application/octet-stream'});
275 saveAs(blobInfo, 'test' + '-' + '1.0.0' + '-CBA.zip');
276 this.toastService.info('enriched successfully ');
279 this.toastService.error('error happened when editing ' + error.message);
280 console.log('Error -' + error.message);