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 enrichandpublish(body: any | null, options?: any): Observable<any> {
67 return this.api.post(BlueprintURLs.enrichandpublish, body, {responseType: 'blob'});
70 private deployBluePrint(body: any | null, options?: any): Observable<any> {
71 return this.api.post(BlueprintURLs.deploy, body, {responseType: 'text'});
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);
80 this.packagesStore.getAll();
83 public savePackage(blob): Observable<string> {
84 const formData = this.getFormData(blob);
85 return this.saveBlueprint(formData);
89 const formData = this.getFormData(blob);
90 return this.enrichBlueprint(formData);
93 enrichAndDeployPackage(blob) {
94 const formData = this.getFormData(blob);
95 return this.enrichandpublish(formData);
99 const formData = this.getFormData(blob);
100 return this.deployBluePrint(formData);
103 private getFormData(blob) {
104 const formData = new FormData();
105 formData.append('file', blob);
109 getTemplateAndMapping(variables: string[]): Observable<ResourceDictionary[]> {
110 return this.api.post(ResourceDictionaryURLs.searchResourceDictionaryByNames, variables);
113 downloadPackage(id) {
114 return this.api.getCustomized(BlueprintURLs.download + id, {responseType: 'blob'});
117 public saveBluePrintToDataBase(): Observable<string> {
120 const subject = new Subject<any>();
121 this.zipFile.generateAsync({type: 'blob'})
123 this.savePackage(blob).subscribe(bluePrintModel => {
124 subject.next(bluePrintModel);
127 return subject.asObservable();
130 public deployCurrentPackage() {
133 const subject = new Subject<any>();
134 this.zipFile.generateAsync({type: 'blob'})
136 this.deploy(blob).subscribe(bluePrintModel => {
137 subject.next(bluePrintModel);
140 return subject.asObservable();
143 public enrichCurrentPackage() {
146 const subject = new Subject<any>();
147 return this.zipFile.generateAsync({type: 'blob'})
149 return this.enrichPackage(blob).pipe();
151 // return subject.asObservable();
156 this.zipFile = new JSZip();
157 FilesContent.getMapOfFilesNamesAndContent().forEach((value, key) => {
158 this.zipFile.folder(key.split('/')[0]);
159 this.zipFile.file(key, value);
164 private formTreeData() {
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);
174 packageCreationModes.execute(this.cbaPackage, this.packageCreationUtils);
175 this.filesData.push(this.folder.TREE_DATA);