db5e7a9a31607ae6c0e47f3a1dc80f5736c8d2dd
[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 {ServiceServiceNg2} from "app/ng2/services/component-services/service.service";
18 import {ConstraintObjectUI, OPERATOR_TYPES} from 'app/ng2/components/logic/service-dependencies/service-dependencies.component';
19 import {ServiceInstanceObject, PropertyBEModel} from 'app/models';
20 import { PROPERTY_DATA } from 'app/utils';
21 import {DropdownValue} from 'app/ng2/components/ui/form-components/dropdown/ui-element-dropdown.component';
22
23 export class UIDropDownSourceTypesElement extends DropdownValue{
24     options: Array<any>;
25     assignedLabel: string;
26     type: string;
27     constructor(input?: any){
28         if(input) {
29             let value = input.value || '';
30             let label = input.label || '';
31             super(value, label);
32             this.options = input.options;
33             this.assignedLabel = input.assignedLabel;
34             this.type = input.type;
35         }
36     }
37 }
38
39 @Component({
40     selector: 'service-dependencies-editor',
41     templateUrl: './service-dependencies-editor.component.html',
42     styleUrls:['./service-dependencies-editor.component.less'],
43     providers: [ServiceServiceNg2]
44 })
45
46 export class ServiceDependenciesEditorComponent {
47
48     input: {
49         serviceRuleIndex: number,
50         serviceRules: Array<ConstraintObjectUI>,
51         compositeServiceName: string,
52         currentServiceName: string,
53         compositeServiceProperties: Array<PropertyBEModel>,
54         selectedInstanceProperties: Array<PropertyBEModel>,
55         operatorTypes: Array<DropdownValue>,
56         selectedInstanceSiblings: Array<ServiceInstanceObject>
57     };
58     currentServiceName: string;
59     selectedServiceProperties: Array<PropertyBEModel>;
60     selectedPropertyObj: PropertyBEModel;
61     ddValueSelectedServicePropertiesNames: Array<DropdownValue>;
62     operatorTypes: Array<DropdownValue>;
63     sourceTypes: Array<UIDropDownSourceTypesElement> = [];
64     currentRule: ConstraintObjectUI;
65     currentIndex: number;
66     listOfValuesToAssign: Array<DropdownValue>;
67     listOfSourceOptions: Array<PropertyBEModel>;
68     assignedValueLabel: string;
69     serviceRulesList: Array<ConstraintObjectUI>;
70
71
72     SOURCE_TYPES = {
73         STATIC: {label: 'Static', value: 'static'},
74         SERVICE_PROPERTY: {label: 'Service Property', value: 'property'}
75     };
76
77
78     ngOnInit() {
79         this.currentIndex = this.input.serviceRuleIndex;
80         this.serviceRulesList = this.input.serviceRules;
81         this.currentRule = this.serviceRulesList && this.input.serviceRuleIndex >= 0 ?
82             this.serviceRulesList[this.input.serviceRuleIndex]:
83             new ConstraintObjectUI({sourceName: this.SOURCE_TYPES.STATIC.value, sourceType: this.SOURCE_TYPES.STATIC.value, value: "", constraintOperator: OPERATOR_TYPES.EQUAL});
84         this.currentServiceName = this.input.currentServiceName;
85         this.operatorTypes = this.input.operatorTypes;
86         this.selectedServiceProperties = this.input.selectedInstanceProperties;
87         this.ddValueSelectedServicePropertiesNames = _.map(this.input.selectedInstanceProperties, prop => new DropdownValue(prop.name, prop.name));
88         this.initSourceTypes();
89         this.syncRuleData();
90         this.updateSourceTypesRelatedValues();
91     }
92
93     initSourceTypes() {
94         this.sourceTypes.push({label: this.SOURCE_TYPES.STATIC.label, value: this.SOURCE_TYPES.STATIC.value,
95             options: [], assignedLabel: this.SOURCE_TYPES.STATIC.label, type: this.SOURCE_TYPES.STATIC.value});
96         this.sourceTypes.push({
97             label: this.input.compositeServiceName,
98             value: this.input.compositeServiceName,
99             assignedLabel: this.SOURCE_TYPES.SERVICE_PROPERTY.label,
100             type: this.SOURCE_TYPES.SERVICE_PROPERTY.value,
101             options: this.input.compositeServiceProperties
102         });
103         _.forEach(this.input.selectedInstanceSiblings, sib =>
104             this.sourceTypes.push({
105                 label: sib.name,
106                 value: sib.name,
107                 options: sib.properties || [],
108                 assignedLabel: this.SOURCE_TYPES.SERVICE_PROPERTY.label,
109                 type: this.SOURCE_TYPES.SERVICE_PROPERTY.value
110             })
111         );
112     }
113
114     syncRuleData() {
115         if(!this.currentRule.sourceName && this.currentRule.sourceType === this.SOURCE_TYPES.STATIC.value) {
116             this.currentRule.sourceName = this.SOURCE_TYPES.STATIC.value;
117         }
118         this.selectedPropertyObj = _.find(this.selectedServiceProperties, prop => prop.name === this.currentRule.servicePropertyName);
119         this.updateOperatorTypesList();
120         this.updateSourceTypesRelatedValues();
121     }
122
123     updateOperatorTypesList() {
124         if (this.selectedPropertyObj && PROPERTY_DATA.SIMPLE_TYPES_COMPARABLE.indexOf(this.selectedPropertyObj.type) === -1) {
125             this.operatorTypes = [{label: "=", value: OPERATOR_TYPES.EQUAL}];
126             this.currentRule.constraintOperator = OPERATOR_TYPES.EQUAL;
127         }
128         else {
129             this.operatorTypes = this.input.operatorTypes;
130         }
131     }
132
133     updateSourceTypesRelatedValues() {
134         if(this.currentRule.sourceName) {
135             let selectedSourceType: UIDropDownSourceTypesElement = this.sourceTypes.find(
136                 t => t.value === this.currentRule.sourceName && t.type === this.currentRule.sourceType
137             );
138             this.listOfSourceOptions = selectedSourceType.options || [];
139             this.assignedValueLabel = selectedSourceType.assignedLabel || this.SOURCE_TYPES.STATIC.label;
140             this.filterOptionsByType();
141         }
142     }
143
144     onChangePage(newIndex) {
145         if (newIndex >= 0 && newIndex < this.input.serviceRules.length) {
146             this.currentIndex = newIndex;
147             this.currentRule = this.serviceRulesList[newIndex];
148             this.syncRuleData();
149         }
150     }
151
152     onServicePropertyChanged() {
153         this.selectedPropertyObj = _.find(this.selectedServiceProperties, prop => prop.name === this.currentRule.servicePropertyName);
154         this.updateOperatorTypesList();
155         this.filterOptionsByType();
156         this.currentRule.value = '';
157     }
158
159     onSelectSourceType() {
160         this.currentRule.sourceType = this.currentRule.sourceName === this.SOURCE_TYPES.STATIC.value ?
161             this.SOURCE_TYPES.STATIC.value :
162             this.SOURCE_TYPES.SERVICE_PROPERTY.value;
163         this.updateSourceTypesRelatedValues();
164         this.currentRule.value = '';
165     }
166
167     filterOptionsByType() {
168         if(!this.selectedPropertyObj) {
169             this.listOfValuesToAssign = [];
170             return;
171         }
172         this.listOfValuesToAssign =  this.listOfSourceOptions.reduce((result, op:PropertyBEModel) => {
173             if(op.type === this.selectedPropertyObj.type && op.schemaType === this.selectedPropertyObj.schemaType) {
174                 result.push(new DropdownValue(op.name, op.name));
175             }
176             return result;
177         }, []);
178     }
179
180     onValueChange(isValidValue) {
181         this.currentRule.updateValidity(isValidValue);
182     }
183
184     checkFormValidForSubmit() {
185         if(!this.serviceRulesList) { //for create modal
186             let isStatic = this.currentRule.sourceName === this.SOURCE_TYPES.STATIC.value;
187             return this.currentRule.isValidRule(isStatic);
188         }
189         //for update all rules
190         return this.serviceRulesList.every(rule => rule.isValidRule(rule.sourceName === this.SOURCE_TYPES.STATIC.value));
191     }
192 }