fix bug - Service dependency - Can't select parent inputs that came from other instances
[sdc.git] / catalog-ui / src / app / ng2 / components / logic / service-dependencies / service-dependencies.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, Input, Output, EventEmitter, ComponentRef} from '@angular/core';
17 import {ModalService} from 'app/ng2/services/modal.service';
18 import {
19     Service,
20     ComponentInstance,
21     ModalModel,
22     ButtonModel,
23     PropertyBEModel,
24     InputBEModel,
25     ServiceInstanceObject
26 } from 'app/models';
27 import {ServiceDependenciesEditorComponent} from 'app/ng2/pages/service-dependencies-editor/service-dependencies-editor.component';
28 import {ModalComponent} from 'app/ng2/components/ui/modal/modal.component';
29 import {ComponentServiceNg2} from 'app/ng2/services/component-services/component.service';
30 import {TranslateService} from 'app/ng2/shared/translator/translate.service';
31 import {ComponentGenericResponse} from 'app/ng2/services/responses/component-generic-response';
32
33 export class ConstraintObject {
34     servicePropertyName: string;
35     constraintOperator: string;
36     sourceType: string;
37     sourceName: string;
38     value: string;
39
40     constructor(input?: any) {
41         if (input) {
42             this.servicePropertyName = input.servicePropertyName;
43             this.constraintOperator = input.constraintOperator;
44             this.sourceType = input.sourceType;
45             this.sourceName = input.sourceName;
46             this.value = input.value;
47         }
48     }
49 }
50
51 export class ConstraintObjectUI extends ConstraintObject{
52     isValidValue: boolean;
53
54     constructor(input?: any) {
55         super(input);
56         if(input) {
57             this.isValidValue = input.isValidValue ? input.isValidValue : input.value !== '';
58         }
59     }
60
61     public updateValidity(isValidValue: boolean) {
62         this.isValidValue = isValidValue;
63     }
64
65     public isValidRule(isStatic) {
66         let isValidValue = isStatic ? this.isValidValue : true;
67         return this.servicePropertyName != null && this.servicePropertyName !== ''
68             && this.value != null && this.value !== '' && isValidValue;
69     }
70 }
71
72 export const OPERATOR_TYPES = {
73     EQUAL: 'equal',
74     GREATER_THAN: 'greater_than',
75     LESS_THAN: 'less_than'
76 };
77
78 class I18nTexts {
79     static uncheckModalTitle: string;
80     static uncheckModalText: string;
81     static modalApprove: string;
82     static modalCancel: string;
83     static modalCreate: string;
84     static modalSave: string;
85     static modalDelete: string;
86     static addRuleTxt: string;
87     static updateRuleTxt: string;
88     static deleteRuleTxt: string;
89     static deleteRuleMsg: string;
90
91     public static translateTexts(translateService) {
92             I18nTexts.uncheckModalTitle = translateService.translate("SERVICE_DEPENDENCY_UNCHECK_TITLE");
93             I18nTexts.uncheckModalText = translateService.translate("SERVICE_DEPENDENCY_UNCHECK_TEXT");
94             I18nTexts.modalApprove = translateService.translate("MODAL_APPROVE");
95             I18nTexts.modalCancel = translateService.translate("MODAL_CANCEL");
96             I18nTexts.modalCreate = translateService.translate("MODAL_CREATE");
97             I18nTexts.modalSave = translateService.translate("MODAL_SAVE");
98             I18nTexts.modalDelete = translateService.translate("MODAL_DELETE");
99             I18nTexts.addRuleTxt = translateService.translate("SERVICE_DEPENDENCY_ADD_RULE");
100             I18nTexts.updateRuleTxt = translateService.translate("SERVICE_DEPENDENCY_UPDATE_RULE");
101             I18nTexts.deleteRuleTxt = translateService.translate("SERVICE_DEPENDENCY_DELETE_RULE");
102             I18nTexts.deleteRuleMsg = translateService.translate("SERVICE_DEPENDENCY_DELETE_RULE_MSG");
103     }
104 }
105
106
107 @Component({
108     selector: 'service-dependencies',
109     templateUrl: './service-dependencies.component.html',
110     styleUrls: ['service-dependencies.component.less'],
111     providers: [ModalService, TranslateService]
112 })
113
114 export class ServiceDependenciesComponent {
115     modalInstance: ComponentRef<ModalComponent>;
116     isDependent: boolean;
117     isLoading: boolean;
118     parentServiceInputs: Array<InputBEModel> = [];
119     rulesList: Array<ConstraintObject> = [];
120     operatorTypes: Array<any>;
121
122     @Input() readonly: boolean;
123     @Input() compositeService: Service;
124     @Input() currentServiceInstance: ComponentInstance;
125     @Input() selectedInstanceSiblings: Array<ServiceInstanceObject>;
126     @Input() selectedInstanceConstraints: Array<ConstraintObject> = [];
127     @Input() selectedInstanceProperties: Array<PropertyBEModel> = [];
128     @Output() updateRulesListEvent:EventEmitter<Array<ConstraintObject>> = new EventEmitter<Array<ConstraintObject>>();
129     @Output() loadRulesListEvent:EventEmitter<any> = new EventEmitter();
130     @Output() dependencyStatus = new EventEmitter<boolean>();
131
132
133     constructor(private componentServiceNg2: ComponentServiceNg2, private ModalServiceNg2: ModalService, private translateService: TranslateService) {
134     }
135
136     ngOnInit() {
137         this.isLoading = false;
138         this.operatorTypes = [
139             {label: ">", value: OPERATOR_TYPES.GREATER_THAN},
140             {label: "<", value: OPERATOR_TYPES.LESS_THAN},
141             {label: "=", value: OPERATOR_TYPES.EQUAL}
142         ];
143         this.componentServiceNg2.getComponentInputsWithProperties(this.compositeService).subscribe((result: ComponentGenericResponse) => {
144             this.parentServiceInputs = result.inputs;
145         });
146         this.loadRules();
147         this.translateService.languageChangedObservable.subscribe(lang => {
148             I18nTexts.translateTexts(this.translateService);
149         });
150     }
151
152     ngOnChanges(changes) {
153         if(changes.currentServiceInstance) {
154             this.currentServiceInstance = changes.currentServiceInstance.currentValue;
155             this.isDependent = this.currentServiceInstance.isDependent();
156         }
157         if(changes.selectedInstanceConstraints && changes.selectedInstanceConstraints.currentValue !== changes.selectedInstanceConstraints.previousValue) {
158             this.selectedInstanceConstraints = changes.selectedInstanceConstraints.currentValue;
159             this.loadRules();
160         }
161     }
162
163     public openRemoveDependencyModal = (): ComponentRef<ModalComponent> => {
164         let actionButton: ButtonModel = new ButtonModel(I18nTexts.modalApprove, 'blue', this.onUncheckDependency);
165         let cancelButton: ButtonModel = new ButtonModel(I18nTexts.modalCancel, 'grey', this.onCloseRemoveDependencyModal);
166         let modalModel: ModalModel = new ModalModel('sm', I18nTexts.uncheckModalTitle, I18nTexts.uncheckModalText, [actionButton, cancelButton]);
167         return this.ModalServiceNg2.createCustomModal(modalModel);
168     }
169
170     loadRules() {
171         this.rulesList = this.selectedInstanceConstraints && this.selectedInstanceConstraints.map((co: ConstraintObject) => ({
172                 servicePropertyName: co.servicePropertyName,
173                 constraintOperator: co.constraintOperator,
174                 sourceType: co.sourceType,
175                 sourceName: co.sourceName !== 'SELF' ? co.sourceName : this.compositeService.name,
176                 value: co.value,
177             }));
178     }
179
180     onUncheckDependency = () => {
181         this.ModalServiceNg2.closeCurrentModal();
182         this.isLoading = true;
183         let isDepOrig = this.isDependent;
184         let rulesListOrig = this.rulesList;
185         this.currentServiceInstance.unmarkAsDependent();
186         this.updateComponentInstance(isDepOrig, rulesListOrig);
187     }
188
189     onCloseRemoveDependencyModal = () => {
190         this.isDependent = true;
191         this.ModalServiceNg2.closeCurrentModal();
192     }
193
194     onCheckDependency = () => {
195         let isDepOrig = this.isDependent;
196         let rulesListOrig = this.rulesList;
197         this.currentServiceInstance.markAsDependent();
198         this.rulesList = [];
199         this.updateComponentInstance(isDepOrig, rulesListOrig);
200     }
201
202     onMarkAsDependent() {
203         if(!this.currentServiceInstance.isDependent()) {
204             this.onCheckDependency();
205         }
206         else {
207             this.openRemoveDependencyModal().instance.open();
208         }
209     }
210
211     updateComponentInstance(isDependent_origVal : boolean, rulesList_orig: Array<ConstraintObject>) {
212         this.isLoading = true;
213         this.componentServiceNg2.updateComponentInstance(this.compositeService, this.currentServiceInstance).subscribe((updatedServiceIns: ComponentInstance) => {
214             this.currentServiceInstance = new ComponentInstance(updatedServiceIns);
215             this.isDependent = this.currentServiceInstance.isDependent();
216             this.dependencyStatus.emit(this.isDependent);
217             if(this.isDependent) {
218                 this.loadRulesListEvent.emit();
219             }
220             this.isLoading = false;
221         }, err=> {
222             this.isDependent = isDependent_origVal;
223             this.rulesList = rulesList_orig;
224             this.isLoading = false;
225             console.log('An error has occurred.');
226         });
227     }
228
229     onAddRule () {
230         let cancelButton: ButtonModel = new ButtonModel(I18nTexts.modalCancel, 'outline white', this.ModalServiceNg2.closeCurrentModal);
231         let saveButton: ButtonModel = new ButtonModel(I18nTexts.modalCreate, 'blue', this.createRule, this.getDisabled);
232         let modalModel: ModalModel = new ModalModel('l', I18nTexts.addRuleTxt, '', [saveButton, cancelButton], 'standard');
233         this.modalInstance = this.ModalServiceNg2.createCustomModal(modalModel);
234         this.ModalServiceNg2.addDynamicContentToModal(
235             this.modalInstance,
236             ServiceDependenciesEditorComponent,
237             {
238                 currentServiceName: this.currentServiceInstance.name,
239                 operatorTypes: this.operatorTypes,
240                 compositeServiceName: this.compositeService.name,
241                 parentServiceInputs: this.parentServiceInputs,
242                 selectedInstanceProperties: this.selectedInstanceProperties,
243                 selectedInstanceSiblings: this.selectedInstanceSiblings
244             }
245         );
246         this.modalInstance.instance.open();
247     }
248
249     onSelectRule(index: number) {
250         let cancelButton: ButtonModel = new ButtonModel(I18nTexts.modalCancel, 'outline white', this.ModalServiceNg2.closeCurrentModal);
251         let saveButton: ButtonModel = new ButtonModel(I18nTexts.modalSave, 'blue', () => this.updateRules(), this.getDisabled);
252         let modalModel: ModalModel = new ModalModel('l', I18nTexts.updateRuleTxt, '', [saveButton, cancelButton], 'standard');
253         this.modalInstance = this.ModalServiceNg2.createCustomModal(modalModel);
254         this.ModalServiceNg2.addDynamicContentToModal(
255             this.modalInstance,
256             ServiceDependenciesEditorComponent,
257             {
258                 serviceRuleIndex: index,
259                 serviceRules: _.map(this.rulesList, rule => new ConstraintObjectUI(rule)),
260                 currentServiceName: this.currentServiceInstance.name,
261                 operatorTypes: this.operatorTypes,
262                 compositeServiceName: this.compositeService.name,
263                 parentServiceInputs: this.parentServiceInputs,
264                 selectedInstanceProperties: this.selectedInstanceProperties,
265                 selectedInstanceSiblings: this.selectedInstanceSiblings
266             }
267         );
268         this.modalInstance.instance.open();
269     }
270
271     getDisabled = ():boolean =>  {
272         return !this.modalInstance.instance.dynamicContent.instance.checkFormValidForSubmit();
273     };
274
275     createRule  = ():void => {
276         let newRuleToCreate: ConstraintObject = new ConstraintObject(this.modalInstance.instance.dynamicContent.instance.currentRule);
277         this.isLoading = true;
278         this.componentServiceNg2.createServiceFilterConstraints(
279             this.compositeService,
280             this.currentServiceInstance,
281             newRuleToCreate
282         ).subscribe( (response) => {
283             this.updateRulesListEvent.emit(response.properties);
284             this.isLoading = false;
285         }, err=> {
286             this.isLoading = false;
287         });
288         this.ModalServiceNg2.closeCurrentModal();
289     };
290
291     updateRules = ():void => {
292         let allRulesToUpdate: Array<ConstraintObject> = this.modalInstance.instance.dynamicContent.instance.serviceRulesList.map(rule => new ConstraintObject(rule));
293         this.isLoading = true;
294         this.componentServiceNg2.updateServiceFilterConstraints(
295             this.compositeService,
296             this.currentServiceInstance,
297             allRulesToUpdate
298         ).subscribe((response) => {
299             this.updateRulesListEvent.emit(response.properties);
300             this.isLoading = false;
301         }, err => {
302             this.isLoading = false;
303         });
304         this.ModalServiceNg2.closeCurrentModal();
305     }
306
307     getSymbol(constraintOperator) {
308         switch (constraintOperator) {
309             case OPERATOR_TYPES.LESS_THAN: return '<';
310             case OPERATOR_TYPES.EQUAL: return '=';
311             case OPERATOR_TYPES.GREATER_THAN: return '>';
312         }
313     }
314
315     onDeleteRule = (index:number) => {
316         this.isLoading = true;
317         this.componentServiceNg2.deleteServiceFilterConstraints(
318             this.compositeService,
319             this.currentServiceInstance,
320             index
321         ).subscribe( (response) => {
322             this.updateRulesListEvent.emit(response.properties);
323             this.isLoading = false;
324         }, err=> {
325             this.isLoading = false;
326         });
327         this.ModalServiceNg2.closeCurrentModal();
328     };
329
330     openDeleteModal = (index:number) => {
331         this.ModalServiceNg2.createActionModal(I18nTexts.deleteRuleTxt, I18nTexts.deleteRuleMsg,
332             I18nTexts.modalDelete, () => this.onDeleteRule(index), I18nTexts.modalCancel).instance.open();
333     }
334 }