blob: 75ea036b3e80c97597d8a6854123da526876d901 [file] [log] [blame]
Michael Landodd603392017-07-12 00:54:52 +03001/*-
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 Landoed64b5e2017-06-09 03:19:04 +030021'use strict';
Michael Lando75aacbb2017-07-17 21:12:03 +030022import { Service, IApi, IAppConfigurtaion, Resource, Component} from "../models";
Michael Landoed64b5e2017-06-09 03:19:04 +030023import {SharingService} from "./sharing-service";
24import {ComponentFactory} from "../utils/component-factory";
25import {CacheService} from "./cache-service";
26
27interface IEntityService {
28 getAllComponents():ng.IPromise<Array<Component>>;
29}
30
31interface IComponentsArray {
32 services:Array<Service>;
33 resources:Array<Resource>;
Michael Landoed64b5e2017-06-09 03:19:04 +030034}
35
36export class EntityService implements IEntityService {
37 static '$inject' = ['$http', '$q', 'sdcConfig', 'Sdc.Services.SharingService', 'ComponentFactory', 'Sdc.Services.CacheService'];
38 private api:IApi;
39
40 constructor(private $http:ng.IHttpService,
41 private $q:ng.IQService,
42 private sdcConfig:IAppConfigurtaion,
43 private sharingService:SharingService,
44 private ComponentFactory:ComponentFactory,
45 private cacheService:CacheService) {
46 this.api = sdcConfig.api;
47 }
48
49 getCatalog = ():ng.IPromise<Array<Component>> => {
50 let defer = this.$q.defer<Array<Component>>();
51 this.$http.get(this.api.root + this.api.GET_catalog)
52 .then((response:any) => {
53 let followedResponse: IComponentsArray = response.data;
54 let componentsList:Array<Component> = new Array();
55
56 followedResponse.services.forEach((serviceResponse:Service) => {
57 let component:Service = this.ComponentFactory.createService(serviceResponse); // new Service(serviceResponse);
58 componentsList.push(component);
59 this.sharingService.addUuidValue(component.uniqueId, component.uuid);
60 });
61
62 followedResponse.resources.forEach((resourceResponse:Resource) => {
63 let component:Resource = this.ComponentFactory.createResource(resourceResponse);
64 componentsList.push(component);
65 this.sharingService.addUuidValue(component.uniqueId, component.uuid);
66 });
67
Michael Landoed64b5e2017-06-09 03:19:04 +030068 this.cacheService.set('breadcrumbsComponents', componentsList);
69 defer.resolve(componentsList);
70 },(responce) => {
71 defer.reject(responce);
72 });
73 return defer.promise;
74 };
75
76 getAllComponents = ():ng.IPromise<Array<Component>> => {
77 let defer = this.$q.defer<Array<Component>>();
78 this.$http.get(this.api.root + this.api.GET_element)
79 .then((response:any) => {
80 let componentResponse:IComponentsArray = response.data;
81 let componentsList:Array<Component> = [];
82
83 componentResponse.services && componentResponse.services.forEach((serviceResponse:Service) => {
84 let component:Service = this.ComponentFactory.createService(serviceResponse);
85 componentsList.push(component);
86 this.sharingService.addUuidValue(component.uniqueId, component.uuid);
87 });
88
89 componentResponse.resources && componentResponse.resources.forEach((resourceResponse:Resource) => {
90 let component:Resource = this.ComponentFactory.createResource(resourceResponse);
91 componentsList.push(component);
92 this.sharingService.addUuidValue(component.uniqueId, component.uuid);
93 });
Michael Lando75aacbb2017-07-17 21:12:03 +030094
Michael Landoed64b5e2017-06-09 03:19:04 +030095 this.cacheService.set('breadcrumbsComponents', componentsList);
96 defer.resolve(componentsList);
97 });
98
99 return defer.promise;
100 };
101}