Michael Lando | dd60339 | 2017-07-12 00:54:52 +0300 | [diff] [blame] | 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 | |
Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 21 | 'use strict'; |
Michael Lando | 5b59349 | 2018-07-29 16:13:45 +0300 | [diff] [blame] | 22 | import * as _ from "lodash"; |
Michael Lando | 75aacbb | 2017-07-17 21:12:03 +0300 | [diff] [blame] | 23 | import { Service, IApi, IAppConfigurtaion, Resource, Component} from "../models"; |
Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 24 | import {SharingService} from "./sharing-service"; |
| 25 | import {ComponentFactory} from "../utils/component-factory"; |
| 26 | import {CacheService} from "./cache-service"; |
Tal Gitelman | 51d50f0 | 2017-12-10 18:55:03 +0200 | [diff] [blame] | 27 | import {ResourceType} from "app/utils"; |
Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 28 | |
| 29 | interface IEntityService { |
Michael Lando | 5b59349 | 2018-07-29 16:13:45 +0300 | [diff] [blame] | 30 | getAllComponents(smallObjects?:boolean):ng.IPromise<Array<Component>>; |
Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | interface IComponentsArray { |
| 34 | services:Array<Service>; |
| 35 | resources:Array<Resource>; |
Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | export class EntityService implements IEntityService { |
| 39 | static '$inject' = ['$http', '$q', 'sdcConfig', 'Sdc.Services.SharingService', 'ComponentFactory', 'Sdc.Services.CacheService']; |
Michael Lando | 5b59349 | 2018-07-29 16:13:45 +0300 | [diff] [blame] | 40 | private _smallObjectAttributes = ['uniqueId', 'name', 'componentType', 'resourceType', 'lastUpdateDate', 'lifecycleState', 'distributionStatus', 'icon', 'version']; |
Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 41 | private api:IApi; |
| 42 | |
| 43 | constructor(private $http:ng.IHttpService, |
| 44 | private $q:ng.IQService, |
| 45 | private sdcConfig:IAppConfigurtaion, |
| 46 | private sharingService:SharingService, |
| 47 | private ComponentFactory:ComponentFactory, |
| 48 | private cacheService:CacheService) { |
| 49 | this.api = sdcConfig.api; |
| 50 | } |
| 51 | |
| 52 | getCatalog = ():ng.IPromise<Array<Component>> => { |
| 53 | let defer = this.$q.defer<Array<Component>>(); |
Tal Gitelman | 51d50f0 | 2017-12-10 18:55:03 +0200 | [diff] [blame] | 54 | this.$http.get(this.api.root + this.api.GET_catalog, {params: {excludeTypes: [ResourceType.VFCMT, ResourceType.CONFIGURATION]}}) |
Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 55 | .then((response:any) => { |
| 56 | let followedResponse: IComponentsArray = response.data; |
| 57 | let componentsList:Array<Component> = new Array(); |
| 58 | |
Michael Lando | a544510 | 2018-03-04 14:53:33 +0200 | [diff] [blame] | 59 | followedResponse.services && followedResponse.services.forEach((serviceResponse:Service) => { |
Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 60 | let component:Service = this.ComponentFactory.createService(serviceResponse); // new Service(serviceResponse); |
| 61 | componentsList.push(component); |
| 62 | this.sharingService.addUuidValue(component.uniqueId, component.uuid); |
| 63 | }); |
| 64 | |
Michael Lando | a544510 | 2018-03-04 14:53:33 +0200 | [diff] [blame] | 65 | followedResponse.resources && followedResponse.resources.forEach((resourceResponse:Resource) => { |
Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 66 | let component:Resource = this.ComponentFactory.createResource(resourceResponse); |
| 67 | componentsList.push(component); |
| 68 | this.sharingService.addUuidValue(component.uniqueId, component.uuid); |
| 69 | }); |
| 70 | |
Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 71 | defer.resolve(componentsList); |
| 72 | },(responce) => { |
| 73 | defer.reject(responce); |
| 74 | }); |
| 75 | return defer.promise; |
| 76 | }; |
| 77 | |
Michael Lando | 5b59349 | 2018-07-29 16:13:45 +0300 | [diff] [blame] | 78 | getAllComponents = (smallObjects?:boolean):ng.IPromise<Array<Component>> => { |
Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 79 | let defer = this.$q.defer<Array<Component>>(); |
| 80 | this.$http.get(this.api.root + this.api.GET_element) |
| 81 | .then((response:any) => { |
| 82 | let componentResponse:IComponentsArray = response.data; |
| 83 | let componentsList:Array<Component> = []; |
| 84 | |
| 85 | componentResponse.services && componentResponse.services.forEach((serviceResponse:Service) => { |
Michael Lando | 5b59349 | 2018-07-29 16:13:45 +0300 | [diff] [blame] | 86 | serviceResponse = (smallObjects) ? _.pick(serviceResponse, this._smallObjectAttributes) : serviceResponse; |
Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 87 | let component:Service = this.ComponentFactory.createService(serviceResponse); |
| 88 | componentsList.push(component); |
| 89 | this.sharingService.addUuidValue(component.uniqueId, component.uuid); |
| 90 | }); |
| 91 | |
| 92 | componentResponse.resources && componentResponse.resources.forEach((resourceResponse:Resource) => { |
Michael Lando | 5b59349 | 2018-07-29 16:13:45 +0300 | [diff] [blame] | 93 | resourceResponse = (smallObjects) ? _.pick(resourceResponse, this._smallObjectAttributes) : resourceResponse; |
Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 94 | let component:Resource = this.ComponentFactory.createResource(resourceResponse); |
| 95 | componentsList.push(component); |
| 96 | this.sharingService.addUuidValue(component.uniqueId, component.uuid); |
| 97 | }); |
Michael Lando | 75aacbb | 2017-07-17 21:12:03 +0300 | [diff] [blame] | 98 | |
Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 99 | defer.resolve(componentsList); |
| 100 | }); |
| 101 | |
| 102 | return defer.promise; |
| 103 | }; |
| 104 | } |