1aea85a61866ead49a5b46e5a32ef12109819f9c
[sdc.git] / catalog-ui / src / app / ng2 / pages / service-dependencies-editor / service-dependencies-editor.component.ts
1 /*!
2  * Copyright © 2016-2018 European Support Limited
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 import {Component} from '@angular/core';
17 import {InputBEModel, PropertyBEModel, PropertyFEModel} from 'app/models';
18 import {
19   ConstraintObjectUI,
20   OPERATOR_TYPES
21 } from 'app/ng2/components/logic/service-dependencies/service-dependencies.component';
22 import {DropdownValue} from 'app/ng2/components/ui/form-components/dropdown/ui-element-dropdown.component';
23 import {ServiceServiceNg2} from 'app/ng2/services/component-services/service.service';
24 import {PROPERTY_DATA} from 'app/utils';
25 import {ServiceInstanceObject} from '../../../models/service-instance-properties-and-interfaces';
26 import { PropertiesUtils } from '../properties-assignment/services/properties.utils';
27
28 export class UIDropDownSourceTypesElement extends DropdownValue {
29   options: any[];
30   assignedLabel: string;
31   type: string;
32
33   constructor(input?: any) {
34     super(input ? input.value || '' : "", input ? input.label || '' : "");
35     if (input) {
36       this.options = input.options;
37       this.assignedLabel = input.assignedLabel;
38       this.type = input.type;
39     }
40   }
41 }
42
43 // tslint:disable-next-line:max-classes-per-file
44 @Component({
45   selector: 'service-dependencies-editor',
46   templateUrl: './service-dependencies-editor.component.html',
47   styleUrls: ['./service-dependencies-editor.component.less'],
48   providers: [ServiceServiceNg2]
49 })
50
51 export class ServiceDependenciesEditorComponent {
52
53   input: {
54     serviceRuleIndex: number,
55     serviceRules: ConstraintObjectUI[],
56     compositeServiceName: string,
57     currentServiceName: string,
58     parentServiceInputs: InputBEModel[],
59     parentServiceProperties: PropertyBEModel[];
60     selectedInstanceProperties: PropertyBEModel[],
61     operatorTypes: DropdownValue[],
62     selectedInstanceSiblings: ServiceInstanceObject[]
63   };
64   currentServiceName: string;
65   selectedServiceProperties: PropertyBEModel[];
66   selectedPropertyObj: PropertyFEModel;
67   ddValueSelectedServicePropertiesNames: DropdownValue[];
68   operatorTypes: DropdownValue[];
69   functionTypes: DropdownValue[];
70   sourceTypes: UIDropDownSourceTypesElement[] = [];
71   currentRule: ConstraintObjectUI;
72   currentIndex: number;
73   listOfValuesToAssign: DropdownValue[];
74   listOfSourceOptions: PropertyBEModel[];
75   assignedValueLabel: string;
76   serviceRulesList: ConstraintObjectUI[];
77
78   SOURCE_TYPES = {
79     STATIC: {label: 'Static', value: 'static'},
80     SERVICE_PROPERTY: {label: 'Service Property', value: 'property'},
81     SERVICE_INPUT: {label: 'Service Input', value: 'service_input'}
82   };
83
84   constructor(private propertiesUtils: PropertiesUtils) {}
85
86   ngOnInit() {
87     this.currentIndex = this.input.serviceRuleIndex;
88     this.serviceRulesList = this.input.serviceRules;
89     this.initFunctionTypes();
90     this.initCurrentRule();
91     this.currentServiceName = this.input.currentServiceName;
92     this.operatorTypes = this.input.operatorTypes;
93     this.selectedServiceProperties = this.input.selectedInstanceProperties;
94     this.ddValueSelectedServicePropertiesNames = _.map(this.input.selectedInstanceProperties, (prop) => new DropdownValue(prop.name, prop.name));
95     if (this.SOURCE_TYPES.STATIC.value !== this.currentRule.sourceType) {
96       this.loadSourceTypesData();
97     }
98     this.syncRuleData();
99   }
100
101   private initCurrentRule() {
102     this.currentRule = this.serviceRulesList && this.input.serviceRuleIndex >= 0 ?
103         this.serviceRulesList[this.input.serviceRuleIndex] :
104         new ConstraintObjectUI({
105           sourceName: this.SOURCE_TYPES.STATIC.value,
106           sourceType: this.SOURCE_TYPES.STATIC.value,
107           value: '',
108           constraintOperator: OPERATOR_TYPES.EQUAL
109         });
110     if (this.currentRule && this.currentRule.sourceType === this.SOURCE_TYPES.STATIC.value){
111       this.sourceTypes.push({
112         label: this.SOURCE_TYPES.STATIC.label,
113         value: this.SOURCE_TYPES.STATIC.value,
114         assignedLabel: this.SOURCE_TYPES.STATIC.value,
115         type: this.SOURCE_TYPES.STATIC.value,
116         options: []});
117     }
118   }
119
120   private initFunctionTypes() {
121     this.functionTypes = [
122       {label: this.SOURCE_TYPES.STATIC.label, value: this.SOURCE_TYPES.STATIC.value},
123       {label: this.SOURCE_TYPES.SERVICE_PROPERTY.label, value: this.SOURCE_TYPES.SERVICE_PROPERTY.value},
124       {label: this.SOURCE_TYPES.SERVICE_INPUT.label, value: this.SOURCE_TYPES.SERVICE_INPUT.value}];
125   }
126
127   onServicePropertyChanged() {
128     this.updateSelectedPropertyObj();
129     this.updateOperatorTypesList();
130     this.currentRule.sourceName = "";
131     this.currentRule.value = "";
132   }
133
134   onSelectFunctionType(value: any) {
135     this.currentRule.sourceName = "";
136     this.listOfValuesToAssign = [];
137     this.currentRule.sourceType = value;
138     this.loadSourceTypesData();
139     this.updateSourceTypesRelatedValues();
140   }
141
142   onSelectSourceType(value: any) {
143     this.currentRule.sourceName = value;
144     this.updateSourceTypesRelatedValues();
145     if (this.listOfValuesToAssign) {
146       this.currentRule.value = this.listOfValuesToAssign[0].value
147     }
148   }
149
150   private loadSourceTypesData() {
151     const SELF = "SELF";
152     if (this.SOURCE_TYPES.SERVICE_INPUT.value === this.currentRule.sourceType) {
153       this.currentRule.sourceName = SELF;
154     }
155     this.sourceTypes = [];
156     this.sourceTypes.push({
157       label: SELF,
158       value: SELF,
159       assignedLabel: this.currentRule.sourceType == this.SOURCE_TYPES.SERVICE_PROPERTY.value
160           ? this.SOURCE_TYPES.SERVICE_PROPERTY.label : this.SOURCE_TYPES.SERVICE_INPUT.label,
161       type: this.currentRule.sourceType == this.SOURCE_TYPES.SERVICE_PROPERTY.value
162           ? this.SOURCE_TYPES.SERVICE_PROPERTY.value : this.SOURCE_TYPES.SERVICE_INPUT.value,
163       options: this.loadSourceTypeBySelectedFunction().get(this.currentRule.sourceType)
164     });
165
166     if (this.currentRule.sourceType !== this.SOURCE_TYPES.SERVICE_INPUT.value) {
167       if (this.input.selectedInstanceSiblings && this.isPropertyFunctionSelected) {
168         _.forEach(this.input.selectedInstanceSiblings, (sib) =>
169             this.sourceTypes.push({
170               label: sib.name,
171               value: sib.name,
172               options: sib.properties || [],
173               assignedLabel: this.SOURCE_TYPES.SERVICE_PROPERTY.label,
174               type: this.SOURCE_TYPES.SERVICE_PROPERTY.value
175             })
176         );
177       }
178     }
179   }
180
181   loadSourceTypeBySelectedFunction = (): any => {
182     let parentDataMap = new Map();
183     parentDataMap.set(this.SOURCE_TYPES.SERVICE_PROPERTY.value, this.input.parentServiceProperties);
184     parentDataMap.set(this.SOURCE_TYPES.SERVICE_INPUT.value , this.input.parentServiceInputs);
185     return parentDataMap;
186   }
187
188   syncRuleData() {
189     if (!this.currentRule.sourceName || this.currentRule.sourceType === this.SOURCE_TYPES.STATIC.value) {
190       this.currentRule.sourceName = this.SOURCE_TYPES.STATIC.value;
191       this.currentRule.sourceType = this.SOURCE_TYPES.STATIC.value;
192     }
193     this.updateSelectedPropertyObj();
194     this.updateOperatorTypesList();
195     this.updateSourceTypesRelatedValues();
196   }
197
198   updateOperatorTypesList() {
199     if (this.selectedPropertyObj && PROPERTY_DATA.SIMPLE_TYPES_COMPARABLE.indexOf(this.selectedPropertyObj.type) === -1) {
200       this.operatorTypes = [{label: '=', value: OPERATOR_TYPES.EQUAL}];
201       this.currentRule.constraintOperator = OPERATOR_TYPES.EQUAL;
202     } else {
203       this.operatorTypes = this.input.operatorTypes;
204     }
205   }
206
207   updateSourceTypesRelatedValues() {
208     if (this.currentRule.sourceName) {
209       const selectedSourceType: UIDropDownSourceTypesElement = this.sourceTypes.find(
210           (t) => t.value === this.currentRule.sourceName && t.type === this.currentRule.sourceType
211       );
212       if (selectedSourceType) {
213         this.listOfSourceOptions = [];
214         this.listOfSourceOptions = selectedSourceType.options || [];
215         this.assignedValueLabel = selectedSourceType.assignedLabel || this.SOURCE_TYPES.STATIC.label;
216         this.filterOptionsByType();
217       }
218     }
219   }
220
221   onChangePage(newIndex:any) {
222     if (newIndex >= 0 && newIndex < this.input.serviceRules.length) {
223       this.currentIndex = newIndex;
224       this.currentRule = this.serviceRulesList[newIndex];
225       this.syncRuleData();
226     }
227   }
228
229   filterOptionsByType() {
230     if (!this.selectedPropertyObj) {
231       this.listOfValuesToAssign = [];
232       return;
233     }
234     this.listOfValuesToAssign = this.listOfSourceOptions.reduce((result, op: PropertyBEModel) => {
235       if (op.type === this.selectedPropertyObj.type && (!op.schemaType || op.schemaType === this.selectedPropertyObj.schemaType)) {
236         result.push(new DropdownValue(op.name, op.name));
237       }
238       return result;
239     }, []);
240   }
241
242   onValueChange(isValidValue:any) {
243     this.currentRule.updateValidity(isValidValue);
244   }
245
246   checkFormValidForSubmit() {
247     if (!this.serviceRulesList) { // for create modal
248       const isStatic = this.currentRule.sourceName === this.SOURCE_TYPES.STATIC.value;
249       return this.currentRule.isValidRule(isStatic);
250     }
251
252     // for update all rules
253     return this.serviceRulesList.every((rule) => rule.isValidRule(rule.sourceName === this.SOURCE_TYPES.STATIC.value));
254   }
255
256   updateSelectedPropertyObj(): void {
257     this.selectedPropertyObj = null;
258     if (this.currentRule.servicePropertyName) {
259       let newProp = new PropertyFEModel(_.find(this.selectedServiceProperties, (prop) => prop.name === this.currentRule.servicePropertyName));
260       newProp.value = JSON.stringify(this.currentRule.value);
261       this.propertiesUtils.initValueObjectRef(newProp);
262       console.log("TEST" + newProp.value);
263       setTimeout(() => {
264         this.selectedPropertyObj = newProp})
265       this.selectedPropertyObj = newProp;
266     }
267   }
268
269   isStaticSource(): boolean {
270     return this.currentRule.sourceType === this.SOURCE_TYPES.STATIC.value
271   }
272
273   isPropertyFunctionSelected(): boolean {
274     return this.currentRule.sourceType === this.SOURCE_TYPES.SERVICE_PROPERTY.value;
275   }
276
277   isComplexListMapType(): boolean {
278     return this.selectedPropertyObj && this.selectedPropertyObj.derivedDataType > 0;
279   }
280
281   updateComplexListMapTypeRuleValue(): void {
282     let value = PropertyFEModel.cleanValueObj(this.selectedPropertyObj.valueObj);
283     this.currentRule.value = JSON.stringify(value);
284     this.onValueChange(this.selectedPropertyObj.valueObjIsValid);
285   }
286
287 }