57c84976173bbbccf5b8c514b044f851db11ff90
[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(private packageCreationStore: PackageCreationStore,
47                 private packageCreationUtils: PackageCreationUtils,
48                 private router: Router,
49                 private toastService: ToastrService) {
50     }
51
52     counter = 0;
53     modes: object[] = [
54         {name: 'Designer Mode', style: 'mode-icon icon-designer-mode'},
55         {name: 'Scripting Mode', style: 'mode-icon icon-scripting-mode'}];
56     private metaDataTab: MetaDataTabModel = new MetaDataTabModel();
57     private folder: FolderNodeElement = new FolderNodeElement();
58     private zipFile: JSZip = new JSZip();
59     private filesData: any = [];
60     private definition: Definition = new Definition();
61
62     @ViewChild(MetadataTabComponent, {static: false})
63     private metadataTabComponent: MetadataTabComponent;
64
65     @ViewChild('nameit', {static: true})
66     private elementRef: ElementRef;
67
68     ngOnInit() {
69         this.elementRef.nativeElement.focus();
70     }
71
72     saveBluePrint() {
73         this.packageCreationStore.state$.subscribe(
74             cbaPackage => {
75                 console.log(cbaPackage);
76                 FilesContent.clear();
77                 let packageCreationModes: PackageCreationModes;
78                 cbaPackage = PackageCreationModes.mapModeType(cbaPackage);
79                 cbaPackage.metaData = PackageCreationModes.setEntryPoint(cbaPackage.metaData);
80                 packageCreationModes = PackageCreationBuilder.getCreationMode(cbaPackage);
81                 packageCreationModes.execute(cbaPackage, this.packageCreationUtils);
82                 this.filesData.push(this.folder.TREE_DATA);
83                 this.saveBluePrintToDataBase();
84             });
85
86
87     }
88
89
90     saveBluePrintToDataBase() {
91         this.create();
92         this.zipFile.generateAsync({type: 'blob'})
93             .then(blob => {
94                 this.packageCreationStore.saveBluePrint(blob).subscribe(
95                     bluePrintDetailModels => {
96                         if (bluePrintDetailModels) {
97                             const id = bluePrintDetailModels.toString().split('id')[1].split(':')[1].split('"')[1];
98                             this.toastService.info('package updated successfully ');
99                             this.router.navigate(['/packages/package/' + id]);
100                         }
101                     }, error => {
102                        // this.toastService.error('error happened when editing ' + error.message);
103                         console.log('Error -' + error.message);
104                     });
105             });
106     }
107
108
109     create() {
110         FilesContent.getMapOfFilesNamesAndContent().forEach((value, key) => {
111             this.zipFile.folder(key.split('/')[0]);
112             this.zipFile.file(key, value);
113         });
114
115     }
116
117     test() {
118         this.metadataTabComponent.saveMetaDataToStore();
119     }
120
121     goBackToDashBorad() {
122         this.router.navigate(['/packages']);
123     }
124 }