60fccebc87fd8e569587dc8b50f94bd3d5322d92
[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, OnInit} from '@angular/core';
23 import {FilesContent, FolderNodeElement, MetaDataFile, MetaDataTab} from './mapping-models/metadata/MetaDataTab.model';
24 // import {saveAs} from 'file-saver/dist/FileSaver';
25 import * as JSZip from 'jszip';
26 import {Observable} from 'rxjs';
27 import {ApiService} from '../../../../common/core/services/api.service';
28 import {BlueprintURLs} from '../../../../common/constants/app-constants';
29 import {Import, Metadata, VlbDefinition} from './mapping-models/definitions/VlbDefinition';
30 import {JsonConvert} from 'json2typescript';
31 import {JsonPipe} from '@angular/common';
32 import {PackageCreationService} from './package-creation.service';
33 import {PackageCreationUtils} from './package-creation.utils';
34
35 @Component({
36     selector: 'app-package-creation',
37     templateUrl: './package-creation.component.html',
38     styleUrls: ['./package-creation.component.css']
39 })
40 export class PackageCreationComponent implements OnInit {
41
42     modes: object[] = [
43         {name: 'Designer Mode', style: 'mode-icon icon-designer-mode'},
44         {name: 'Scripting Mode', style: 'mode-icon icon-scripting-mode'}];
45
46     dictionaryLibraryInstances: string[] = ['x', 'y'];
47     private container: HTMLElement;
48     private elements: HTMLCollection;
49     private newElement: HTMLElement;
50     private metaDataTab: MetaDataTab = new MetaDataTab();
51
52     private result: string;
53
54     private folder: FolderNodeElement = new FolderNodeElement();
55     private zipFile: JSZip = new JSZip();
56     private filesData: any = [];
57
58
59     constructor(private packageCreationService: PackageCreationService, private packageCreationUtils: PackageCreationUtils) {
60     }
61
62     ngOnInit() {
63     }
64
65
66     createAnotherCustomKeyDiv() {
67         console.log(this.metaDataTab);
68         this.newElement = document.getElementById('target');
69         const copiedElement = this.newElement.cloneNode(true);
70         /* this.newElement.setAttribute('class', 'alert-dark');*/
71         this.container = document.getElementById('container');
72         this.container.appendChild(copiedElement);
73         this.elements = this.container.children;
74         /*this.metaDataTab = new MetaDataTab();
75         this.metaDataTab.name = 'klfdj';
76         this.metaDataTab.entryFileName = 'Definitions/vLB_CDS.json';
77         this.metaDataTab.description = 'rere';
78         this.metaDataTab.tags = 'ffsssssss';
79         this.metaDataTab.version = '1.01.10';
80         this.metaDataTab.templateName = 'test';
81
82
83         this.saveToFileSystem(MetaDataFile.getObjectInstance(this.metaDataTab));*/
84     }
85
86     validatePacakgeName() {
87
88     }
89
90     getDictionaryLibraryInstances() {
91
92     }
93
94     saveMetaData() {
95
96
97     }
98
99     private saveToFileSystem(response) {
100
101         const filename = 'TOSCA.meta';
102         FilesContent.putData(filename, response);
103
104         const filenameEntry = 'vLB_CDS.json';
105         const vlbDefinition: VlbDefinition = new VlbDefinition();
106         const metadata: Metadata = new Metadata();
107
108         metadata.templateAuthor = ' lldkslds';
109         metadata.templateName = ' lldkslds';
110         metadata.templateTags = ' lldkslds';
111         metadata.templateVersion = ' lldkslds';
112         metadata['author-email'] = ' lldkslds';
113         metadata['user-groups'] = ' lldkslds';
114         vlbDefinition.metadata = metadata;
115
116         vlbDefinition.imports = [{
117             file: 'Definitions/data_types.json'
118         }];
119
120         const value = this.packageCreationUtils.transformToJson(vlbDefinition);
121         FilesContent.putData(filenameEntry, value);
122
123         this.filesData.push(this.folder.TREE_DATA);
124         this.saveToBackend();
125     }
126
127
128     saveToBackend() {
129         this.create();
130         this.zipFile.generateAsync({type: 'blob'})
131             .then(blob => {
132                 const formData = new FormData();
133                 formData.append('file', blob);
134                 this.packageCreationService.saveBlueprint(formData)
135                     .subscribe(
136                         data => {
137                             console.log('Success:' + JSON.stringify(data));
138                         }, error => {
139                             console.log('Error -' + error.message);
140                         });
141
142             });
143     }
144
145
146     create() {
147         this.folder.TREE_DATA.forEach((path) => {
148
149             const name = path.name;
150             if (path.children) {
151                 this.zipFile.folder(name);
152                 path.children.forEach(children => {
153                     const name2 = children.name;
154                     console.log(FilesContent.getMapOfFilesNamesAndContent());
155                     console.log(name2);
156                     if (FilesContent.getMapOfFilesNamesAndContent().has(name2)) {
157                         this.zipFile.file(name + '/' + name2, FilesContent.getMapOfFilesNamesAndContent().get(name2));
158                     } else {
159                     }
160
161                 });
162
163             }
164         });
165     }
166
167
168     searchPackages($event: Event) {
169         /* const searchQuery = event.target.value;
170          searchQuery = searchQuery.trim();
171          this.packagesStore.search(searchQuery);*/
172     }
173
174     deleteCustomKey(event) {
175         this.container = document.getElementById('container');
176         const element = event.parentElement.parentElement.parentElement;
177         this.container.removeChild(element);
178         console.log('removed');
179     }
180 }