0c5885cefb42f3dd84a0d91003489f0e28d64733
[ccsdk/cds.git] /
1 /*
2 ============LICENSE_START==========================================
3 ===================================================================
4 Copyright (C) 2019 Orange. All rights reserved.
5 ===================================================================
6
7 Unless otherwise specified, all software contained herein is licensed
8 under the Apache License, Version 2.0 (the License);
9 you may not use this software except in compliance with the License.
10 You may obtain a copy of the License at
11
12     http://www.apache.org/licenses/LICENSE-2.0
13
14 Unless required by applicable law or agreed to in writing, software
15 distributed under the License is distributed on an "AS IS" BASIS,
16 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 See the License for the specific language governing permissions and
18 limitations under the License.
19 ============LICENSE_END============================================
20 */
21
22 import { Component, ElementRef, OnInit, ViewChild } from '@angular/core';
23 import { FilesContent, FolderNodeElement, MetaDataTabModel } from './mapping-models/metadata/MetaDataTab.model';
24
25 import * as JSZip from 'jszip';
26 import { PackageCreationStore } from './package-creation.store';
27 import { Definition } from './mapping-models/CBAPacakge.model';
28 import { PackageCreationModes } from './creationModes/PackageCreationModes';
29 import { PackageCreationBuilder } from './creationModes/PackageCreationBuilder';
30 import { PackageCreationUtils } from './package-creation.utils';
31 import { MetadataTabComponent } from './metadata-tab/metadata-tab.component';
32 import { Router } from '@angular/router';
33 import { ToastrService } from 'ngx-toastr';
34
35
36 @Component({
37     selector: 'app-package-creation',
38     templateUrl: './package-creation.component.html',
39     styleUrls: ['./package-creation.component.css']
40 })
41 export class PackageCreationComponent implements OnInit {
42
43     // adding initial referencing to designer mode
44
45
46     constructor(
47         private packageCreationStore: PackageCreationStore,
48         private packageCreationUtils: PackageCreationUtils,
49         private router: Router,
50         private toastService: ToastrService) {
51     }
52
53     counter = 0;
54     modes: object[] = [
55         { name: 'Designer Mode', style: 'mode-icon icon-designer-mode' },
56         { name: 'Scripting Mode', style: 'mode-icon icon-scripting-mode' }];
57     metaDataTab: MetaDataTabModel = new MetaDataTabModel();
58     folder: FolderNodeElement = new FolderNodeElement();
59     zipFile: JSZip = new JSZip();
60     filesData: any = [];
61     definition: Definition = new Definition();
62
63     @ViewChild(MetadataTabComponent, { static: false })
64     metadataTabComponent: MetadataTabComponent;
65
66     @ViewChild('nameit', { static: true })
67     elementRef: ElementRef;
68
69     ngOnInit() {
70         this.elementRef.nativeElement.focus();
71     }
72
73     saveBluePrint() {
74         this.packageCreationStore.state$.subscribe(
75             cbaPackage => {
76                 console.log(cbaPackage);
77                 FilesContent.clear();
78                 let packageCreationModes: PackageCreationModes;
79                 cbaPackage = PackageCreationModes.mapModeType(cbaPackage);
80                 cbaPackage.metaData = PackageCreationModes.setEntryPoint(cbaPackage.metaData);
81                 packageCreationModes = PackageCreationBuilder.getCreationMode(cbaPackage);
82                 packageCreationModes.execute(cbaPackage, this.packageCreationUtils);
83                 this.filesData.push(this.folder.TREE_DATA);
84                 this.saveBluePrintToDataBase();
85             });
86
87
88     }
89
90
91     saveBluePrintToDataBase() {
92         this.create();
93         this.zipFile.generateAsync({ type: 'blob' })
94             .then(blob => {
95                 this.packageCreationStore.saveBluePrint(blob).subscribe(
96                     bluePrintDetailModels => {
97                         if (bluePrintDetailModels) {
98                             const id = bluePrintDetailModels.toString().split('id')[1].split(':')[1].split('"')[1];
99                             this.toastService.info('package updated successfully ');
100                             this.router.navigate(['/packages/package/' + id]);
101                         }
102                     }, error => {
103                         // this.toastService.error('error happened when editing ' + error.message);
104                         console.log('Error -' + error.message);
105                     });
106             });
107     }
108
109
110     create() {
111         FilesContent.getMapOfFilesNamesAndContent().forEach((value, key) => {
112             this.zipFile.folder(key.split('/')[0]);
113             this.zipFile.file(key, value);
114         });
115
116     }
117
118     test() {
119         this.metadataTabComponent.saveMetaDataToStore();
120     }
121
122     goBackToDashBorad() {
123         this.router.navigate(['/packages']);
124     }
125 }