Catalog alignment
[sdc.git] / catalog-ui / src / app / ng2 / services / home.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 import { HttpClient } from '@angular/common/http';
21 import { Inject, Injectable } from '@angular/core';
22 import { Component, IApi, Resource, Service } from 'app/models';
23 import { ComponentFactory } from 'app/utils/component-factory';
24 import { Observable } from 'rxjs';
25 import { ISdcConfig, SdcConfigToken } from '../config/sdc-config.config';
26 import { SharingService } from './sharing.service';
27
28 // tslint:disable-next-line:interface-name
29 interface IComponentsArray {
30     services: Service[];
31     resources: Resource[];
32 }
33
34 @Injectable()
35 export class HomeService {
36     private api: IApi;
37     private smallObjectAttributes = [
38         'uniqueId', 'name', 'componentType', 'resourceType', 'lastUpdateDate', 'lifecycleState', 'distributionStatus',
39         'icon', 'version'
40     ];
41
42     constructor(private http: HttpClient,
43                 @Inject(SdcConfigToken) private sdcConfig: ISdcConfig,
44                 private sharingService: SharingService,
45                 private componentFactory: ComponentFactory) {
46         this.api = sdcConfig.api;
47     }
48
49     public getAllComponents(smallObjects?: boolean): Observable<Component[]> {
50         return this.http.get<IComponentsArray>(this.api.root + this.api.GET_element)
51             .map((response) => {
52                 const componentResponse: IComponentsArray = response;
53                 let componentsList: Component[] = [];
54
55                 componentResponse.services && componentResponse.services.forEach((serviceResponse: Service) => {
56                     serviceResponse = (smallObjects) ? _.pick(serviceResponse, this.smallObjectAttributes) : serviceResponse;
57                     const component: Service = this.componentFactory.createService(serviceResponse);
58                     componentsList.push(component);
59                     this.sharingService.addUuidValue(component.uniqueId, component.uuid);
60                 });
61
62                 componentResponse.resources && componentResponse.resources.forEach((resourceResponse: Resource) => {
63                     resourceResponse = (smallObjects) ? _.pick(resourceResponse, this.smallObjectAttributes) : resourceResponse;
64                     const component: Resource = this.componentFactory.createResource(resourceResponse);
65                     componentsList.push(component);
66                     this.sharingService.addUuidValue(component.uniqueId, component.uuid);
67                 });
68
69                 componentsList = _.orderBy(componentsList, ['lastUpdateDate'], ['desc']);
70
71                 return componentsList;
72             });
73     }
74 }