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, Input, Output, EventEmitter, ComponentRef} from '@angular/core';
17 import {ModalService} from 'app/ng2/services/modal.service';
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';
33 export class ConstraintObject {
34 servicePropertyName: string;
35 constraintOperator: string;
40 constructor(input?: any) {
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;
51 export class ConstraintObjectUI extends ConstraintObject{
52 isValidValue: boolean;
54 constructor(input?: any) {
57 this.isValidValue = input.isValidValue ? input.isValidValue : input.value !== '';
61 public updateValidity(isValidValue: boolean) {
62 this.isValidValue = isValidValue;
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;
72 export const OPERATOR_TYPES = {
74 GREATER_THAN: 'greater_than',
75 LESS_THAN: 'less_than'
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;
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");
108 selector: 'service-dependencies',
109 templateUrl: './service-dependencies.component.html',
110 styleUrls: ['service-dependencies.component.less'],
111 providers: [ModalService, TranslateService]
114 export class ServiceDependenciesComponent {
115 modalInstance: ComponentRef<ModalComponent>;
116 isDependent: boolean;
118 parentServiceInputs: Array<InputBEModel> = [];
119 rulesList: Array<ConstraintObject> = [];
120 operatorTypes: Array<any>;
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>();
133 constructor(private componentServiceNg2: ComponentServiceNg2, private ModalServiceNg2: ModalService, private translateService: TranslateService) {
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}
143 this.componentServiceNg2.getComponentInputsWithProperties(this.compositeService).subscribe((result: ComponentGenericResponse) => {
144 this.parentServiceInputs = result.inputs;
147 this.translateService.languageChangedObservable.subscribe(lang => {
148 I18nTexts.translateTexts(this.translateService);
152 ngOnChanges(changes) {
153 if(changes.currentServiceInstance) {
154 this.currentServiceInstance = changes.currentServiceInstance.currentValue;
155 this.isDependent = this.currentServiceInstance.isDependent();
157 if(changes.selectedInstanceConstraints && changes.selectedInstanceConstraints.currentValue !== changes.selectedInstanceConstraints.previousValue) {
158 this.selectedInstanceConstraints = changes.selectedInstanceConstraints.currentValue;
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);
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,
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);
189 onCloseRemoveDependencyModal = () => {
190 this.isDependent = true;
191 this.ModalServiceNg2.closeCurrentModal();
194 onCheckDependency = () => {
195 let isDepOrig = this.isDependent;
196 let rulesListOrig = this.rulesList;
197 this.currentServiceInstance.markAsDependent();
199 this.updateComponentInstance(isDepOrig, rulesListOrig);
202 onMarkAsDependent() {
203 if(!this.currentServiceInstance.isDependent()) {
204 this.onCheckDependency();
207 this.openRemoveDependencyModal().instance.open();
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();
220 this.isLoading = false;
222 this.isDependent = isDependent_origVal;
223 this.rulesList = rulesList_orig;
224 this.isLoading = false;
225 console.log('An error has occurred.');
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(
236 ServiceDependenciesEditorComponent,
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
246 this.modalInstance.instance.open();
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(
256 ServiceDependenciesEditorComponent,
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
268 this.modalInstance.instance.open();
271 getDisabled = ():boolean => {
272 return !this.modalInstance.instance.dynamicContent.instance.checkFormValidForSubmit();
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,
282 ).subscribe( (response) => {
283 this.updateRulesListEvent.emit(response.properties);
284 this.isLoading = false;
286 this.isLoading = false;
288 this.ModalServiceNg2.closeCurrentModal();
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,
298 ).subscribe((response) => {
299 this.updateRulesListEvent.emit(response.properties);
300 this.isLoading = false;
302 this.isLoading = false;
304 this.ModalServiceNg2.closeCurrentModal();
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 '>';
315 onDeleteRule = (index:number) => {
316 this.isLoading = true;
317 this.componentServiceNg2.deleteServiceFilterConstraints(
318 this.compositeService,
319 this.currentServiceInstance,
321 ).subscribe( (response) => {
322 this.updateRulesListEvent.emit(response.properties);
323 this.isLoading = false;
325 this.isLoading = false;
327 this.ModalServiceNg2.closeCurrentModal();
330 openDeleteModal = (index:number) => {
331 this.ModalServiceNg2.createActionModal(I18nTexts.deleteRuleTxt, I18nTexts.deleteRuleMsg,
332 I18nTexts.modalDelete, () => this.onDeleteRule(index), I18nTexts.modalCancel).instance.open();