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