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, PropertyFEModel} from 'app/models';
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';
28 export class UIDropDownSourceTypesElement extends DropdownValue {
30 assignedLabel: string;
33 constructor(input?: any) {
34 super(input ? input.value || '' : "", input ? input.label || '' : "");
36 this.options = input.options;
37 this.assignedLabel = input.assignedLabel;
38 this.type = input.type;
43 // tslint:disable-next-line:max-classes-per-file
45 selector: 'service-dependencies-editor',
46 templateUrl: './service-dependencies-editor.component.html',
47 styleUrls: ['./service-dependencies-editor.component.less'],
48 providers: [ServiceServiceNg2]
51 export class ServiceDependenciesEditorComponent {
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[]
64 currentServiceName: string;
65 selectedServiceProperties: PropertyBEModel[];
66 selectedPropertyObj: PropertyFEModel;
67 ddValueSelectedServicePropertiesNames: DropdownValue[];
68 operatorTypes: DropdownValue[];
69 functionTypes: DropdownValue[];
70 sourceTypes: UIDropDownSourceTypesElement[] = [];
71 currentRule: ConstraintObjectUI;
73 listOfValuesToAssign: DropdownValue[];
74 listOfSourceOptions: PropertyBEModel[];
75 assignedValueLabel: string;
76 serviceRulesList: ConstraintObjectUI[];
79 STATIC: {label: 'Static', value: 'static'},
80 SERVICE_PROPERTY: {label: 'Service Property', value: 'property'},
81 SERVICE_INPUT: {label: 'Service Input', value: 'service_input'}
84 constructor(private propertiesUtils: PropertiesUtils) {}
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();
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,
108 constraintOperator: OPERATOR_TYPES.EQUAL
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,
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}];
127 onServicePropertyChanged() {
128 this.updateSelectedPropertyObj();
129 this.updateOperatorTypesList();
130 this.currentRule.sourceName = "";
131 this.currentRule.value = "";
134 onSelectFunctionType(value: any) {
135 this.currentRule.sourceName = "";
136 this.listOfValuesToAssign = [];
137 this.currentRule.sourceType = value;
138 this.loadSourceTypesData();
139 this.updateSourceTypesRelatedValues();
142 onSelectSourceType(value: any) {
143 this.currentRule.sourceName = value;
144 this.updateSourceTypesRelatedValues();
145 if (this.listOfValuesToAssign) {
146 this.currentRule.value = this.listOfValuesToAssign[0].value
150 private loadSourceTypesData() {
152 if (this.SOURCE_TYPES.SERVICE_INPUT.value === this.currentRule.sourceType) {
153 this.currentRule.sourceName = SELF;
155 this.sourceTypes = [];
156 this.sourceTypes.push({
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)
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({
172 options: sib.properties || [],
173 assignedLabel: this.SOURCE_TYPES.SERVICE_PROPERTY.label,
174 type: this.SOURCE_TYPES.SERVICE_PROPERTY.value
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;
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;
193 this.updateSelectedPropertyObj();
194 this.updateOperatorTypesList();
195 this.updateSourceTypesRelatedValues();
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;
203 this.operatorTypes = this.input.operatorTypes;
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
212 if (selectedSourceType) {
213 this.listOfSourceOptions = [];
214 this.listOfSourceOptions = selectedSourceType.options || [];
215 this.assignedValueLabel = selectedSourceType.assignedLabel || this.SOURCE_TYPES.STATIC.label;
216 this.filterOptionsByType();
221 onChangePage(newIndex:any) {
222 if (newIndex >= 0 && newIndex < this.input.serviceRules.length) {
223 this.currentIndex = newIndex;
224 this.currentRule = this.serviceRulesList[newIndex];
229 filterOptionsByType() {
230 if (!this.selectedPropertyObj) {
231 this.listOfValuesToAssign = [];
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));
242 onValueChange(isValidValue:any) {
243 this.currentRule.updateValidity(isValidValue);
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);
252 // for update all rules
253 return this.serviceRulesList.every((rule) => rule.isValidRule(rule.sourceName === this.SOURCE_TYPES.STATIC.value));
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);
264 this.selectedPropertyObj = newProp})
265 this.selectedPropertyObj = newProp;
269 isStaticSource(): boolean {
270 return this.currentRule.sourceType === this.SOURCE_TYPES.STATIC.value
273 isPropertyFunctionSelected(): boolean {
274 return this.currentRule.sourceType === this.SOURCE_TYPES.SERVICE_PROPERTY.value;
277 isComplexListMapType(): boolean {
278 return this.selectedPropertyObj && this.selectedPropertyObj.derivedDataType > 0;
281 updateComplexListMapTypeRuleValue(): void {
282 let value = PropertyFEModel.cleanValueObj(this.selectedPropertyObj.valueObj);
283 this.currentRule.value = JSON.stringify(value);
284 this.onValueChange(this.selectedPropertyObj.valueObjIsValid);