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 { 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';
24 export class UIDropDownSourceTypesElement extends DropdownValue {
26 assignedLabel: string;
28 constructor(input?: any) {
30 const value = input.value || '';
31 const label = input.label || '';
32 // const hidden = input.hidden || '';
33 // const selected = input.selected || '';
35 this.options = input.options;
36 this.assignedLabel = input.assignedLabel;
37 this.type = input.type;
42 // tslint:disable-next-line:max-classes-per-file
44 selector: 'service-dependencies-editor',
45 templateUrl: './service-dependencies-editor.component.html',
46 styleUrls: ['./service-dependencies-editor.component.less'],
47 providers: [ServiceServiceNg2]
50 export class ServiceDependenciesEditorComponent {
53 serviceRuleIndex: number,
54 serviceRules: ConstraintObjectUI[],
55 compositeServiceName: string,
56 currentServiceName: string,
57 parentServiceInputs: InputBEModel[],
58 selectedInstanceProperties: PropertyBEModel[],
59 operatorTypes: DropdownValue[],
60 selectedInstanceSiblings: ServiceInstanceObject[]
62 currentServiceName: string;
63 selectedServiceProperties: PropertyBEModel[];
64 selectedPropertyObj: PropertyBEModel;
65 ddValueSelectedServicePropertiesNames: DropdownValue[];
66 operatorTypes: DropdownValue[];
67 sourceTypes: UIDropDownSourceTypesElement[] = [];
68 currentRule: ConstraintObjectUI;
70 listOfValuesToAssign: DropdownValue[];
71 listOfSourceOptions: PropertyBEModel[];
72 assignedValueLabel: string;
73 serviceRulesList: ConstraintObjectUI[];
76 STATIC: {label: 'Static', value: 'static'},
77 SERVICE_PROPERTY: {label: 'Service Property', value: 'property'}
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();
92 this.updateSourceTypesRelatedValues();
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
105 _.forEach(this.input.selectedInstanceSiblings, (sib) =>
106 this.sourceTypes.push({
109 options: sib.properties || [],
110 assignedLabel: this.SOURCE_TYPES.SERVICE_PROPERTY.label,
111 type: this.SOURCE_TYPES.SERVICE_PROPERTY.value
117 if (!this.currentRule.sourceName && this.currentRule.sourceType === this.SOURCE_TYPES.STATIC.value) {
118 this.currentRule.sourceName = this.SOURCE_TYPES.STATIC.value;
120 this.selectedPropertyObj = _.find(this.selectedServiceProperties, (prop) => prop.name === this.currentRule.servicePropertyName);
121 this.updateOperatorTypesList();
122 this.updateSourceTypesRelatedValues();
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;
130 this.operatorTypes = this.input.operatorTypes;
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
139 this.listOfSourceOptions = selectedSourceType.options || [];
140 this.assignedValueLabel = selectedSourceType.assignedLabel || this.SOURCE_TYPES.STATIC.label;
141 this.filterOptionsByType();
145 onChangePage(newIndex) {
146 if (newIndex >= 0 && newIndex < this.input.serviceRules.length) {
147 this.currentIndex = newIndex;
148 this.currentRule = this.serviceRulesList[newIndex];
153 onServicePropertyChanged() {
154 this.selectedPropertyObj = _.find(this.selectedServiceProperties, (prop) => prop.name === this.currentRule.servicePropertyName);
155 this.updateOperatorTypesList();
156 this.filterOptionsByType();
157 this.currentRule.value = '';
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 = '';
168 filterOptionsByType() {
169 if (!this.selectedPropertyObj) {
170 this.listOfValuesToAssign = [];
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));
181 onValueChange(isValidValue) {
182 this.currentRule.updateValidity(isValidValue);
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);
190 // for update all rules
191 return this.serviceRulesList.every((rule) => rule.isValidRule(rule.sourceName === this.SOURCE_TYPES.STATIC.value));