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.
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';
23 export class UIDropDownSourceTypesElement extends DropdownValue{
25 assignedLabel: string;
27 constructor(input?: any){
29 let value = input.value || '';
30 let label = input.label || '';
32 this.options = input.options;
33 this.assignedLabel = input.assignedLabel;
34 this.type = input.type;
40 selector: 'service-dependencies-editor',
41 templateUrl: './service-dependencies-editor.component.html',
42 styleUrls:['./service-dependencies-editor.component.less'],
43 providers: [ServiceServiceNg2]
46 export class ServiceDependenciesEditorComponent {
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>
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;
66 listOfValuesToAssign: Array<DropdownValue>;
67 listOfSourceOptions: Array<PropertyBEModel>;
68 assignedValueLabel: string;
69 serviceRulesList: Array<ConstraintObjectUI>;
73 STATIC: {label: 'Static', value: 'static'},
74 SERVICE_PROPERTY: {label: 'Service Property', value: 'property'}
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();
90 this.updateSourceTypesRelatedValues();
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
103 _.forEach(this.input.selectedInstanceSiblings, sib =>
104 this.sourceTypes.push({
107 options: sib.properties || [],
108 assignedLabel: this.SOURCE_TYPES.SERVICE_PROPERTY.label,
109 type: this.SOURCE_TYPES.SERVICE_PROPERTY.value
115 if(!this.currentRule.sourceName && this.currentRule.sourceType === this.SOURCE_TYPES.STATIC.value) {
116 this.currentRule.sourceName = this.SOURCE_TYPES.STATIC.value;
118 this.selectedPropertyObj = _.find(this.selectedServiceProperties, prop => prop.name === this.currentRule.servicePropertyName);
119 this.updateOperatorTypesList();
120 this.updateSourceTypesRelatedValues();
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;
129 this.operatorTypes = this.input.operatorTypes;
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
138 this.listOfSourceOptions = selectedSourceType.options || [];
139 this.assignedValueLabel = selectedSourceType.assignedLabel || this.SOURCE_TYPES.STATIC.label;
140 this.filterOptionsByType();
144 onChangePage(newIndex) {
145 if (newIndex >= 0 && newIndex < this.input.serviceRules.length) {
146 this.currentIndex = newIndex;
147 this.currentRule = this.serviceRulesList[newIndex];
152 onServicePropertyChanged() {
153 this.selectedPropertyObj = _.find(this.selectedServiceProperties, prop => prop.name === this.currentRule.servicePropertyName);
154 this.updateOperatorTypesList();
155 this.filterOptionsByType();
156 this.currentRule.value = '';
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 = '';
167 filterOptionsByType() {
168 if(!this.selectedPropertyObj) {
169 this.listOfValuesToAssign = [];
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));
180 onValueChange(isValidValue) {
181 this.currentRule.updateValidity(isValidValue);
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);
189 //for update all rules
190 return this.serviceRulesList.every(rule => rule.isValidRule(rule.sourceName === this.SOURCE_TYPES.STATIC.value));