Sync Integ to Master
[sdc.git] / catalog-ui / src / app / view-models / forms / property-forms / module-property-modal / module-property-model.ts
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 /**
22  * Created by obarda on 1/18/2017.
23  */
24 'use strict';
25 import * as _ from "lodash";
26 import {PropertyModel, DisplayModule, Component, Resource, Service, ComponentInstance} from "app/models";
27 import {UNIQUE_GROUP_PROPERTIES_NAME} from "app/utils";
28 import {IPropertyFormBaseViewScope, PropertyFormBaseView} from "../base-property-form/property-form-base-model";
29 import {DataTypesService} from "app/services/data-types-service";
30
31 export interface IModulePropertyViewScope extends IPropertyFormBaseViewScope {
32     onValueChange():void;
33 }
34
35 export class ModulePropertyView extends PropertyFormBaseView {
36
37     static '$inject' = [
38         '$scope',
39         '$templateCache',
40         '$uibModalInstance',
41         '$injector',
42         'originalProperty',
43         'component',
44         'selectedModule',
45         'Sdc.Services.DataTypesService',
46         '$q'
47     ];
48
49     constructor(protected $scope:IModulePropertyViewScope,
50                 protected $templateCache:ng.ITemplateCacheService,
51                 protected $uibModalInstance:ng.ui.bootstrap.IModalServiceInstance,
52                 protected $injector:ng.auto.IInjectorService,
53                 protected originalProperty:PropertyModel,
54                 protected component:Component,
55                 private selectedModule:DisplayModule,
56                 protected DataTypesService:DataTypesService,
57                 private $q:ng.IQService) {
58         super($scope, $uibModalInstance, $injector, originalProperty, component, selectedModule.properties, DataTypesService);
59
60         this.$templateCache.put("module-property-view.html", require('app/view-models/forms/property-forms/module-property-modal/module-property-view.html'));
61         this.$scope.innerViewSrcUrl = "module-property-view.html";
62         this.initChildScope();
63     }
64
65     private findPropertyByName = (propertyName:string):PropertyModel => {
66         let property:PropertyModel = _.find(this.filteredProperties, (property:PropertyModel) => {
67             return property.name === propertyName;
68         });
69         return property;
70     };
71
72     save(isNeedToCloseModal):ng.IPromise<boolean> {
73
74         let deferred = this.$q.defer();
75
76         let onSuccess = (properties:Array<PropertyModel>):void => {
77             deferred.resolve(true);
78             if (isNeedToCloseModal === true) {
79                 this.$scope.close();
80             }
81         };
82
83         let onFailed = ():void => {
84             deferred.resolve(false);
85         };
86
87         let property = _.find(this.selectedModule.properties, (property) => {
88             return property.uniqueId === this.$scope.property.uniqueId;
89         });
90
91         if (!property.readonly && property.value !== this.$scope.property.value) {
92             if (this.component.isResource()) {
93                 (<Resource>this.component).updateResourceGroupProperties(this.selectedModule, [this.$scope.property]).then(onSuccess, onFailed); // for now we only update one property at a time
94             }
95             if (this.component.isService()) {
96                 // Find the component instance of the group instance
97                 let componentInstance:ComponentInstance = _.find(this.component.componentInstances, (componentInstance:ComponentInstance) => {
98                     let groupInstance = _.find(componentInstance.groupInstances, {uniqueId: this.selectedModule.groupInstanceUniqueId});
99                     return groupInstance !== undefined;
100
101                 });
102                 (<Service>this.component).updateGroupInstanceProperties(componentInstance.uniqueId, this.selectedModule, [this.$scope.property]).then(onSuccess, onFailed); // for now we only update one property at a time
103             }
104         } else {
105             deferred.resolve(true);
106             if (isNeedToCloseModal === true) {
107                 this.$uibModalInstance.close();
108             }
109
110         }
111         return deferred.promise;
112     }
113
114     onPropertyChange():void {
115         this.initValidation();
116     }
117
118     protected initValidation = ():void => {
119
120         this.$scope.isDeleteDisable = true;
121         this.$scope.isNameDisable = true;
122         this.$scope.isTypeSelectorDisable = true;
123         this.$scope.isDescriptionDisable = true;
124
125         switch (this.$scope.property.name) {
126             case UNIQUE_GROUP_PROPERTIES_NAME.IS_BASE:
127             case UNIQUE_GROUP_PROPERTIES_NAME.VF_MODULE_TYPE:
128             case UNIQUE_GROUP_PROPERTIES_NAME.VOLUME_GROUP:
129             case UNIQUE_GROUP_PROPERTIES_NAME.VF_MODULE_LABEL:
130                 this.$scope.property.readonly = true;
131                 break;
132             case UNIQUE_GROUP_PROPERTIES_NAME.VF_MODULE_DESCRIPTION:
133                 if (this.component.isService()) {
134                     this.$scope.property.readonly = true;
135                 } else {
136                     this.$scope.property.readonly = false;
137                 }
138                 break;
139         }
140     };
141
142     private isUniqueProperty = ():boolean => {
143         return this.$scope.property.name === UNIQUE_GROUP_PROPERTIES_NAME.MIN_VF_MODULE_INSTANCES ||
144             this.$scope.property.name === UNIQUE_GROUP_PROPERTIES_NAME.MAX_VF_MODULE_INSTANCES ||
145             this.$scope.property.name === UNIQUE_GROUP_PROPERTIES_NAME.INITIAL_COUNT;
146     };
147
148
149     private initChildScope = ():void => {
150
151         this.initValidation();
152
153         this.$scope.clearValidationError = ():void => {
154             this.$scope.forms.editForm.$valid = true;
155             this.$scope.forms.editForm.$invalid = false;
156             this.$scope.forms.editForm['value'].$error = {};
157             this.$scope.forms.editForm.$error = {};
158         }
159
160         // put default value when instance value is empty
161         this.$scope.onValueChange = ():void => {
162
163             if (!this.$scope.property.value) { // Resetting to default value
164                 if (this.isPropertyValueOwner()) {
165                     if (this.component.isService()) {
166                         this.$scope.property.value = this.$scope.property.parentValue;
167                     } else {
168                         this.$scope.property.value = this.$scope.property.defaultValue;
169                     }
170                 }
171             }
172
173             if (this.isUniqueProperty()) {
174
175                 let isValid = true;
176                 let maxProperty:PropertyModel = this.findPropertyByName(UNIQUE_GROUP_PROPERTIES_NAME.MAX_VF_MODULE_INSTANCES);
177                 let minProperty:PropertyModel = this.findPropertyByName(UNIQUE_GROUP_PROPERTIES_NAME.MIN_VF_MODULE_INSTANCES);
178                 let initialCountProperty:PropertyModel = this.findPropertyByName(UNIQUE_GROUP_PROPERTIES_NAME.INITIAL_COUNT);
179
180                 let maxPropertyValue = parseInt(maxProperty.value);
181                 let minPropertyValue = parseInt(minProperty.value);
182                 let initialCountPropertyValue = parseInt(initialCountProperty.value);
183                 let propertyValue = parseInt(this.$scope.property.value);
184                 let parentPropertyValue = parseInt(this.$scope.property.parentValue);
185
186                 switch (this.$scope.property.name) {
187
188                     case UNIQUE_GROUP_PROPERTIES_NAME.MIN_VF_MODULE_INSTANCES:
189                         if (isNaN(maxPropertyValue) || maxPropertyValue == null) {
190                             isValid = propertyValue <= initialCountPropertyValue;
191                         }
192                         else {
193                             isValid = propertyValue && (propertyValue <= maxPropertyValue && propertyValue <= initialCountPropertyValue);
194                         }
195                         this.$scope.forms.editForm["value"].$setValidity('maxValidation', isValid);
196                         if (this.component.isService()) {
197                             if (isNaN(parentPropertyValue) || parentPropertyValue == null) {
198                                 isValid = true;
199                             } else {
200                                 isValid = propertyValue >= parentPropertyValue;
201                                 this.$scope.forms.editForm["value"].$setValidity('minValidationVfLevel', isValid);
202                             }
203                         }
204                         break;
205                     case UNIQUE_GROUP_PROPERTIES_NAME.MAX_VF_MODULE_INSTANCES:
206                         if (isNaN(minPropertyValue) || minPropertyValue == null) {
207                             isValid = propertyValue >= initialCountPropertyValue;
208                         } else {
209                             isValid = isNaN(propertyValue) || (propertyValue >= minPropertyValue && propertyValue >= initialCountPropertyValue);
210                         }
211                         this.$scope.forms.editForm["value"].$setValidity('minValidation', isValid);
212                         if (this.component.isService()) {
213                             if (isNaN(parentPropertyValue) || parentPropertyValue == null) {
214                                 isValid = true;
215                             }
216                             else {
217                                 isValid = propertyValue <= parentPropertyValue;
218                                 this.$scope.forms.editForm["value"].$setValidity('maxValidationVfLevel', isValid);
219                             }
220                         }
221                         break;
222                     case UNIQUE_GROUP_PROPERTIES_NAME.INITIAL_COUNT:
223                         if ((isNaN(minPropertyValue) || minPropertyValue == null) && (isNaN(maxPropertyValue) || maxPropertyValue == null)) {
224                             isValid = true;
225                         } else if (isNaN(minPropertyValue) || minPropertyValue == null) {
226                             isValid = propertyValue <= maxPropertyValue;
227                         } else if (isNaN(maxPropertyValue) || maxPropertyValue == null) {
228                             isValid = propertyValue >= minPropertyValue;
229                         } else {
230                             isValid = minPropertyValue <= propertyValue && propertyValue <= maxPropertyValue;
231                         }
232                         this.$scope.forms.editForm["value"].$setValidity('minOrMaxValidation', isValid);
233                         break;
234                 }
235             }
236             ;
237         }
238     }
239 }