add package creation component and fixing designer compilation and linting
[ccsdk/cds.git] / cds-ui / designer-client / src / app / modules / feature-modules / packages / package-creation / package-creation.component.ts
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: string[] = ['Designer Mode', 'Scripting Mode'];
43     dictionaryLibraryInstances: string[] = ['x', 'y'];
44     private target: HTMLElement;
45     private newElement: HTMLElement;
46     private metaDataTab: MetaDataTab = new MetaDataTab();
47
48     private result: string;
49
50     private folder: FolderNodeElement = new FolderNodeElement();
51     private zipFile: JSZip = new JSZip();
52     private filesData: any = [];
53
54
55     constructor(private packageCreationService: PackageCreationService, private packageCreationUtils: PackageCreationUtils) {
56     }
57
58     ngOnInit() {
59     }
60
61
62     createAnotherCustomKeyDiv() {
63         console.log(this.metaDataTab);
64         this.newElement = document.createElement('div');
65         this.newElement.setAttribute('class', 'alert-dark');
66         this.target = document.getElementById('target');
67         this.target.appendChild(this.newElement);
68         this.metaDataTab = new MetaDataTab();
69         this.metaDataTab.name = 'klfdj';
70         this.metaDataTab.entryFileName = 'Definitions/vLB_CDS.json';
71         this.metaDataTab.description = 'rere';
72         this.metaDataTab.tags = 'ffsssssss';
73         this.metaDataTab.version = '1.01.10';
74         this.metaDataTab.templateName = 'test';
75
76
77         this.saveToFileSystem(MetaDataFile.getObjectInstance(this.metaDataTab));
78     }
79
80     validatePacakgeName() {
81
82     }
83
84     getDictionaryLibraryInstances() {
85
86     }
87
88     saveMetaData() {
89
90
91     }
92
93     private saveToFileSystem(response) {
94
95         const filename = 'TOSCA.meta';
96         FilesContent.putData(filename, response);
97
98         const filenameEntry = 'vLB_CDS.json';
99         const vlbDefinition: VlbDefinition = new VlbDefinition();
100         const metadata: Metadata = new Metadata();
101
102         metadata.templateAuthor = ' lldkslds';
103         metadata.templateName = ' lldkslds';
104         metadata.templateTags = ' lldkslds';
105         metadata.templateVersion = ' lldkslds';
106         metadata['author-email'] = ' lldkslds';
107         metadata['user-groups'] = ' lldkslds';
108         vlbDefinition.metadata = metadata;
109
110         vlbDefinition.imports = [{
111             file: 'Definitions/data_types.json'
112         }];
113
114         const value = this.packageCreationUtils.transformToJson(vlbDefinition);
115         FilesContent.putData(filenameEntry, value);
116
117         this.filesData.push(this.folder.TREE_DATA);
118         this.saveToBackend();
119     }
120
121
122     saveToBackend() {
123         this.create();
124         this.zipFile.generateAsync({type: 'blob'})
125             .then(blob => {
126                 const formData = new FormData();
127                 formData.append('file', blob);
128                 this.packageCreationService.saveBlueprint(formData)
129                     .subscribe(
130                         data => {
131                             console.log('Success:' + JSON.stringify(data));
132                         }, error => {
133                             console.log('Error -' + error.message);
134                         });
135
136             });
137     }
138
139
140     create() {
141         this.folder.TREE_DATA.forEach((path) => {
142
143             const name = path.name;
144             if (path.children) {
145                 this.zipFile.folder(name);
146                 path.children.forEach(children => {
147                     const name2 = children.name;
148                     console.log(FilesContent.getMapOfFilesNamesAndContent());
149                     console.log(name2);
150                     if (FilesContent.getMapOfFilesNamesAndContent().has(name2)) {
151                         this.zipFile.file(name + '/' + name2, FilesContent.getMapOfFilesNamesAndContent().get(name2));
152                     } else {
153                     }
154
155                 });
156
157             }
158         });
159     }
160
161
162     searchPackages($event: Event) {
163         const searchQuery = event.target.value;
164         searchQuery = searchQuery.trim();
165         this.packagesStore.search(searchQuery);
166     }
167 }