2 ============LICENSE_START==========================================
3 ===================================================================
4 Copyright (C) 2019 Orange. All rights reserved.
5 ===================================================================
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
12 http://www.apache.org/licenses/LICENSE-2.0
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============================================
22 import {Injectable} from '@angular/core';
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';
42 export class PackageCreationService {
43 private cbaPackage: CBAPackage;
44 folder: FolderNodeElement = new FolderNodeElement();
46 zipFile: JSZip = new JSZip();
48 constructor(private api: ApiService, private packagesListService: PackagesApiService,
49 private packagesStore: PackagesStore, private designerStore: DesignerStore,
50 private packageCreationStore: PackageCreationStore, private packageCreationUtils: PackageCreationUtils
52 this.packageCreationStore.state$.subscribe(
54 this.cbaPackage = cbaPackage;
58 private saveBlueprint(body: any | null, options?: any): Observable<string> {
59 return this.api.post(BlueprintURLs.save, body, {responseType: 'text'});
62 private enrichBlueprint(body: any | null, options?: any): Observable<any> {
63 return this.api.post(BlueprintURLs.enrich, body, {responseType: 'blob'});
66 private deployBluePrint(body: any | null, options?: any): Observable<any> {
67 return this.api.post(BlueprintURLs.deploy, body, {responseType: 'text'});
70 async checkBluePrintNameAndVersion(name: string, version: string): Promise<boolean> {
71 return await this.packagesListService.checkBluePrintIfItExists(name, version)
72 .then(bluePrintModelsResult => bluePrintModelsResult != null && bluePrintModelsResult.length > 0);
76 this.packagesStore.getAll();
79 public savePackage(blob): Observable<string> {
80 const formData = this.getFormData(blob);
81 return this.saveBlueprint(formData);
85 const formData = this.getFormData(blob);
86 return this.enrichBlueprint(formData);
90 const formData = this.getFormData(blob);
91 return this.deployBluePrint(formData);
94 private getFormData(blob) {
95 const formData = new FormData();
96 formData.append('file', blob);
100 getTemplateAndMapping(variables: string[]): Observable<ResourceDictionary[]> {
101 return this.api.post(ResourceDictionaryURLs.searchResourceDictionaryByNames, variables);
104 downloadPackage(id) {
105 return this.api.getCustomized(BlueprintURLs.download + id, {responseType: 'blob'});
108 public saveBluePrintToDataBase(): Observable<string> {
111 const subject = new Subject<any>();
112 this.zipFile.generateAsync({type: 'blob'})
114 this.savePackage(blob).subscribe(bluePrintModel => {
115 subject.next(bluePrintModel);
118 return subject.asObservable();
121 public deployCurrentPackage() {
124 const subject = new Subject<any>();
125 this.zipFile.generateAsync({type: 'blob'})
127 this.deploy(blob).subscribe(bluePrintModel => {
128 subject.next(bluePrintModel);
131 return subject.asObservable();
134 public enrichCurrentPackage() {
137 const subject = new Subject<any>();
138 return this.zipFile.generateAsync({type: 'blob'})
140 return this.enrichPackage(blob).pipe();
142 // return subject.asObservable();
147 this.zipFile = new JSZip();
148 FilesContent.getMapOfFilesNamesAndContent().forEach((value, key) => {
149 this.zipFile.folder(key.split('/')[0]);
150 this.zipFile.file(key, value);
155 private formTreeData() {
157 FilesContent.clear();
158 let packageCreationModes: PackageCreationModes;
159 this.cbaPackage = PackageCreationModes.mapModeType(this.cbaPackage);
160 this.cbaPackage.metaData = PackageCreationModes.setEntryPoint(this.cbaPackage.metaData);
161 packageCreationModes = PackageCreationBuilder.getCreationMode(this.cbaPackage);
162 this.designerStore.state$.subscribe(state => {
163 this.cbaPackage.templateTopology.content = this.packageCreationUtils.transformToJson(state.template);
165 packageCreationModes.execute(this.cbaPackage, this.packageCreationUtils);
166 this.filesData.push(this.folder.TREE_DATA);