Fix 'Primitive types should not be shown under data types in catalog'-bug
[sdc.git] / catalog-ui / src / app / ng2 / services / catalog.service.ts
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 import { Injectable, Inject } from "@angular/core";
22 import { Observable } from "rxjs/Observable";
23 import { SdcConfigToken, ISdcConfig } from "../config/sdc-config.config";
24 import {Component, DataTypeModel, IApi, IComponentsArray} from "app/models";
25 import { ComponentFactory } from 'app/utils/component-factory';
26 import {ResourceType} from "../../utils/constants";
27 import {SharingService} from "./sharing.service";
28 import { HttpClient, HttpParams } from "@angular/common/http";
29 import {DataTypesService} from "../../services/data-types-service";
30 import {DataTypeCatalogComponent} from "../../models/data-type-catalog-component";
31 import {zip} from "rxjs";
32 import {map} from "rxjs/operators";
33
34 @Injectable()
35 export class CatalogService {
36     protected api:IApi;
37     protected baseUrl:string;
38     protected baseMicroServiceUrl:string;
39
40     constructor(private http: HttpClient,
41                 @Inject(SdcConfigToken) sdcConfig:ISdcConfig,
42                 private componentFactory:ComponentFactory,
43                 private dataTypesService:DataTypesService,
44                 private sharingService:SharingService) {
45         this.api = sdcConfig.api;
46         this.baseUrl = sdcConfig.api.root ;
47         this.baseMicroServiceUrl = sdcConfig.api.uicache_root;
48     }
49
50     public getCatalog(): Observable<Array<Component | DataTypeCatalogComponent>> {
51         let searchParams = new HttpParams();
52         searchParams = searchParams.append('excludeTypes', ResourceType.VFCMT).append('excludeTypes', ResourceType.CONFIGURATION);
53         const observableComponents = this.http.get<IComponentsArray>(this.baseMicroServiceUrl + this.api.GET_uicache_catalog, {params: searchParams});
54         const observableDataTypes = this.dataTypesService.getDataTypesFromAllModelExcludePrimitives();
55         return zip(observableComponents, observableDataTypes)
56         .pipe(map(res => this.processComponentsResponse(res, true)));
57     }
58
59     public getArchiveCatalog() {
60         return this.http.get<IComponentsArray>(this.baseUrl + '/v1/catalog/archive/', {})
61             .map(res => this.processComponentsResponse(res[0]));
62     }
63
64     private processComponentsResponse(componentsArr: [IComponentsArray, DataTypeModel[]], addSharing:boolean = false) {
65         const componentsList:Array<Component | DataTypeCatalogComponent> = [];
66         if (componentsArr[0].resources) {
67             componentsList.push(...this.getResourceItems(componentsArr[0].resources));
68         }
69         if (componentsArr[0].services) {
70             componentsList.push(...this.getServiceItems(componentsArr[0].services));
71         }
72         if (componentsArr[1]) {
73             componentsList.push(...this.getDataTypesItems(componentsArr[1]));
74         }
75         if (addSharing) {
76             componentsList.forEach((item) => this.sharingService.addUuidValue(item.uniqueId, item.uuid));
77         }
78         return componentsList;
79     }
80     
81     private getResourceItems(resources){
82         let resourceItems = resources.map((resource)=>{
83             return this.componentFactory.createResource(resource)
84         })
85         return resourceItems;
86     }
87
88     private getServiceItems(services){
89         let serviceItems = services.map((service)=>{
90             return this.componentFactory.createService(service)
91         })
92         return serviceItems;
93     }
94
95     private getDataTypesItems(dataTypes: Array<DataTypeModel>):Array<DataTypeCatalogComponent> {
96         return dataTypes.map(dataType => new DataTypeCatalogComponent(dataType));
97     }
98
99 }