Renaming Files having BluePrint to have Blueprint
[ccsdk/cds.git] / cds-ui / designer-client / src / app / modules / feature-modules / packages / package-creation / package-creation.service.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 {Injectable} from '@angular/core';
23
24 import {Observable, Subject} from 'rxjs';
25 import {ApiService} from '../../../../common/core/services/api.service';
26 import {BlueprintURLs, ResourceDictionaryURLs} from '../../../../common/constants/app-constants';
27 import {PackagesApiService} from '../packages-api.service';
28 import {PackagesStore} from '../packages.store';
29 import {ResourceDictionary} from './mapping-models/ResourceDictionary.model';
30 import {FilesContent, FolderNodeElement} from './mapping-models/metadata/MetaDataTab.model';
31 import {PackageCreationModes} from './creationModes/PackageCreationModes';
32 import {PackageCreationBuilder} from './creationModes/PackageCreationBuilder';
33 import {PackageCreationStore} from './package-creation.store';
34 import {CBAPackage} from './mapping-models/CBAPacakge.model';
35 import {PackageCreationUtils} from './package-creation.utils';
36 import * as JSZip from 'jszip';
37 import {DesignerStore} from '../designer/designer.store';
38
39 @Injectable({
40     providedIn: 'root'
41 })
42 export class PackageCreationService {
43     private cbaPackage: CBAPackage;
44     folder: FolderNodeElement = new FolderNodeElement();
45     filesData: any = [];
46     zipFile: JSZip = new JSZip();
47
48     constructor(private api: ApiService, private packagesListService: PackagesApiService,
49                 private packagesStore: PackagesStore, private designerStore: DesignerStore,
50                 private packageCreationStore: PackageCreationStore, private packageCreationUtils: PackageCreationUtils
51     ) {
52         this.packageCreationStore.state$.subscribe(
53             cbaPackage => {
54                 this.cbaPackage = cbaPackage;
55             });
56     }
57
58     private saveBlueprint(body: any | null, options?: any): Observable<string> {
59         return this.api.post(BlueprintURLs.save, body, {responseType: 'text'});
60     }
61
62     private enrichBlueprint(body: any | null, options?: any): Observable<any> {
63         return this.api.post(BlueprintURLs.enrich, body, {responseType: 'blob'});
64     }
65
66     private enrichandpublish(body: any | null, options?: any): Observable<any> {
67         return this.api.post(BlueprintURLs.enrichandpublish, body, {responseType: 'text'});
68     }
69
70     private deployBlueprint(body: any | null, options?: any): Observable<any> {
71         return this.api.post(BlueprintURLs.deploy, body, {responseType: 'text'});
72     }
73
74     async checkBlueprintNameAndVersion(name: string, version: string): Promise<boolean> {
75         return await this.packagesListService.checkBlueprintIfItExists(name, version)
76             .then(bluePrintModelsResult => bluePrintModelsResult != null && bluePrintModelsResult.length > 0);
77     }
78
79     refreshPackages() {
80         this.packagesStore.getAll();
81     }
82
83     public savePackage(blob): Observable<string> {
84         const formData = this.getFormData(blob);
85         return this.saveBlueprint(formData);
86     }
87
88     enrichPackage(blob) {
89         const formData = this.getFormData(blob);
90         return this.enrichBlueprint(formData);
91     }
92
93     enrichAndDeployPackage(blob) {
94         const formData = this.getFormData(blob);
95         return this.enrichandpublish(formData);
96     }
97
98     deploy(blob) {
99         const formData = this.getFormData(blob);
100         return this.deployBlueprint(formData);
101     }
102
103     private getFormData(blob) {
104         const formData = new FormData();
105         formData.append('file', blob);
106         return formData;
107     }
108
109     getTemplateAndMapping(variables: string[]): Observable<ResourceDictionary[]> {
110         return this.api.post(ResourceDictionaryURLs.searchResourceDictionaryByNames, variables);
111     }
112
113     downloadPackage(id) {
114         return this.api.getCustomized(BlueprintURLs.download + id, {responseType: 'blob'});
115     }
116
117     public saveBlueprintToDataBase(): Observable<string> {
118         this.formTreeData();
119         this.create();
120         const subject = new Subject<any>();
121         this.zipFile.generateAsync({type: 'blob'})
122             .then(blob => {
123                 this.savePackage(blob).subscribe(bluePrintModel => {
124                     subject.next(bluePrintModel);
125                 });
126             });
127         return subject.asObservable();
128     }
129
130     public deployCurrentPackage() {
131         this.formTreeData();
132         this.create();
133         const subject = new Subject<any>();
134         this.zipFile.generateAsync({type: 'blob'})
135             .then(blob => {
136                 this.deploy(blob).subscribe(bluePrintModel => {
137                     subject.next(bluePrintModel);
138                 });
139             });
140         return subject.asObservable();
141     }
142
143     public enrichCurrentPackage() {
144         this.formTreeData();
145         this.create();
146         const subject = new Subject<any>();
147         return this.zipFile.generateAsync({type: 'blob'})
148             .then(blob => {
149                 return this.enrichPackage(blob).pipe();
150             });
151         //  return subject.asObservable();
152
153     }
154
155     private create() {
156         this.zipFile = new JSZip();
157         FilesContent.getMapOfFilesNamesAndContent().forEach((value, key) => {
158             this.zipFile.folder(key.split('/')[0]);
159             this.zipFile.file(key, value);
160         });
161
162     }
163
164     private formTreeData() {
165
166         FilesContent.clear();
167         let packageCreationModes: PackageCreationModes;
168         this.cbaPackage = PackageCreationModes.mapModeType(this.cbaPackage);
169         this.cbaPackage.metaData = PackageCreationModes.setEntryPoint(this.cbaPackage.metaData);
170         packageCreationModes = PackageCreationBuilder.getCreationMode(this.cbaPackage);
171         this.designerStore.state$.subscribe(state => {
172             this.cbaPackage.templateTopology.content = this.packageCreationUtils.transformToJson(state.template);
173         });
174         packageCreationModes.execute(this.cbaPackage, this.packageCreationUtils);
175         this.filesData.push(this.folder.TREE_DATA);
176     }
177
178 }