adding import package basic functionalities
[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} 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
31 @Injectable({
32     providedIn: 'root'
33 })
34 export class PackageCreationService {
35
36
37     constructor(private api: ApiService, private packagesListService: PackagesApiService, private packagesStore: PackagesStore) {
38     }
39
40     private saveBlueprint(body: any | null, options?: any): Observable<any> {
41         return this.api.post(BlueprintURLs.save, body, {responseType: 'text'});
42     }
43
44     private enrichBlueprint(body: any | null, options?: any): Observable<any> {
45         return this.api.post(BlueprintURLs.enrich, body, { responseType: 'blob' });
46     }
47
48     private deployBluePrint(body: any | null, options?: any): Observable<any> {
49         return this.api.post(BlueprintURLs.deploy, body, {responseType: 'text'});
50     }
51
52     async checkBluePrintNameAndVersion(name: string, version: string): Promise<boolean> {
53         return await this.packagesListService.checkBluePrintIfItExists(name, version)
54             .then(bluePrintModelsResult => bluePrintModelsResult != null && bluePrintModelsResult.length > 0);
55     }
56
57     refreshPackages() {
58         this.packagesStore.getAll();
59     }
60
61     savePackage(blob) {
62         const formData = this.getFormData(blob);
63         return this.saveBlueprint(formData);
64     }
65
66     enrichPackage(blob) {
67         const formData = this.getFormData(blob);
68         return this.enrichBlueprint(formData);
69     }
70
71     deploy(blob) {
72         const formData = this.getFormData(blob);
73         return this.deployBluePrint(formData);
74     }
75
76     private getFormData(blob) {
77         const formData = new FormData();
78         formData.append('file', blob);
79         return formData;
80     }
81
82     getTemplateAndMapping(variables: string[]): Observable<ResourceDictionary[]> {
83         return this.api.post(ResourceDictionaryURLs.searchResourceDictionaryByNames, variables);
84     }
85
86
87
88
89 }