2 * Copyright © 2016-2018 European Support Limited
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
13 * or implied. See the License for the specific language governing
14 * permissions and limitations under the License.
18 import {ICompositionViewModelScope} from "../../composition-view-model";
23 ServiceInstanceObject,
30 import {ComponentGenericResponse} from "app/ng2/services/responses/component-generic-response";
31 import {ServiceServiceNg2} from "app/ng2/services/component-services/service.service";
33 interface IServiceConsumptionViewModelScope extends ICompositionViewModelScope {
35 instancesMappedList: Array<ServiceInstanceObject>;
36 componentInstancesProperties: PropertiesGroup;
37 componentInstancesInputs: InputsGroup;
38 componentInstancesInterfaces: Map<string, Array<InterfaceModel>>;
39 componentInputs: Array<InputBEModel>;
40 componentCapabilities: Array<Capability>;
41 instancesCapabilitiesMap: Map<string, Array<Capability>>;
45 export class ServiceConsumptionViewModel {
52 constructor(private $scope:IServiceConsumptionViewModelScope, private ServiceServiceNg2:ServiceServiceNg2) {
53 this.$scope.service = <Service>this.$scope.currentComponent;
58 private initInstances = ():void => {
59 this.ServiceServiceNg2.getServiceConsumptionData(this.$scope.service).subscribe((genericResponse:ComponentGenericResponse) => {
60 this.$scope.componentInstancesProperties = genericResponse.componentInstancesProperties;
61 this.$scope.componentInstancesInputs = genericResponse.componentInstancesInputs;
62 this.$scope.componentInstancesInterfaces = genericResponse.componentInstancesInterfaces;
63 this.$scope.componentInputs = genericResponse.inputs;
64 this.buildInstancesCapabilitiesMap(genericResponse.componentInstances);
65 this.updateInstanceAttributes();
69 buildInstancesCapabilitiesMap = (componentInstances: Array<ComponentInstance>): void => {
70 this.$scope.instancesCapabilitiesMap = new Map();
71 let flattenCapabilities = [];
72 _.forEach(componentInstances, componentInstance => {
73 flattenCapabilities = CapabilitiesGroup.getFlattenedCapabilities(componentInstance.capabilities);
74 this.$scope.instancesCapabilitiesMap[componentInstance.uniqueId] = _.filter(flattenCapabilities, cap => cap.properties && cap.ownerId === componentInstance.uniqueId);
78 private updateInstanceAttributes = ():void => {
79 if (this.$scope.isComponentInstanceSelected() && this.$scope.componentInstancesProperties) {
80 this.$scope.instancesMappedList = this.$scope.service.componentInstances.map(coInstance => new ServiceInstanceObject({
81 id: coInstance.uniqueId,
82 name: coInstance.name,
83 properties: this.$scope.componentInstancesProperties[coInstance.uniqueId] || [],
84 inputs: this.$scope.componentInstancesInputs[coInstance.uniqueId] || [],
85 interfaces: this.$scope.componentInstancesInterfaces[coInstance.uniqueId] || []
90 private initScope = ():void => {
91 this.$scope.$watch('currentComponent.selectedInstance', ():void => {
92 this.updateInstanceAttributes();
95 this.$scope.registerCreateInstanceEvent(() => {
99 this.$scope.$on('$destroy', this.$scope.unregisterCreateInstanceEvent);