[sdc] update code of sdc
[sdc.git] / catalog-ui / src / app / view-models / forms / property-forms / module-property-modal / module-property-model.ts
1 /**
2  * Created by obarda on 1/18/2017.
3  */
4 'use strict';
5 import {PropertyModel, DisplayModule, Component, Resource, Service, ComponentInstance} from "app/models";
6 import {UNIQUE_GROUP_PROPERTIES_NAME} from "app/utils";
7 import {IPropertyFormBaseViewScope, PropertyFormBaseView} from "../base-property-form/property-form-base-model";
8 import {DataTypesService} from "app/services/data-types-service";
9
10 export interface IModulePropertyViewScope extends IPropertyFormBaseViewScope {
11     onValueChange():void;
12 }
13
14 export class ModulePropertyView extends PropertyFormBaseView {
15
16     static '$inject' = [
17         '$scope',
18         '$templateCache',
19         '$uibModalInstance',
20         '$injector',
21         'originalProperty',
22         'component',
23         'selectedModule',
24         'Sdc.Services.DataTypesService',
25         '$q'
26     ];
27
28     constructor(protected $scope:IModulePropertyViewScope,
29                 protected $templateCache:ng.ITemplateCacheService,
30                 protected $uibModalInstance:ng.ui.bootstrap.IModalServiceInstance,
31                 protected $injector:ng.auto.IInjectorService,
32                 protected originalProperty:PropertyModel,
33                 protected component:Component,
34                 private selectedModule:DisplayModule,
35                 protected DataTypesService:DataTypesService,
36                 private $q:ng.IQService) {
37         super($scope, $uibModalInstance, $injector, originalProperty, component, selectedModule.properties, DataTypesService);
38
39         this.$templateCache.put("module-property-view.html", require('app/view-models/forms/property-forms/module-property-modal/module-property-view.html'));
40         this.$scope.innerViewSrcUrl = "module-property-view.html";
41         this.initChildScope();
42     }
43
44     private findPropertyByName = (propertyName:string):PropertyModel => {
45         let property:PropertyModel = _.find(this.filteredProperties, (property:PropertyModel) => {
46             return property.name === propertyName;
47         });
48         return property;
49     };
50
51     save(isNeedToCloseModal):ng.IPromise<boolean> {
52
53         let deferred = this.$q.defer();
54
55         let onSuccess = (properties:Array<PropertyModel>):void => {
56             deferred.resolve(true);
57             if (isNeedToCloseModal === true) {
58                 this.$scope.close();
59             }
60         };
61
62         let onFailed = ():void => {
63             deferred.resolve(false);
64         };
65
66         let property = _.find(this.selectedModule.properties, (property) => {
67             return property.uniqueId === this.$scope.property.uniqueId;
68         });
69         if (property.value !== this.$scope.property.value) {
70             if (this.component.isResource()) {
71                 (<Resource>this.component).updateResourceGroupProperties(this.selectedModule, [this.$scope.property]).then(onSuccess, onFailed); // for now we only update one property at a time
72             }
73             if (this.component.isService()) {
74                 // Find the component instance of the group instance
75                 let componentInstance:ComponentInstance = _.find(this.component.componentInstances, (componentInstance:ComponentInstance) => {
76                     let groupInstance = _.find(componentInstance.groupInstances, {uniqueId: this.selectedModule.groupInstanceUniqueId});
77                     return groupInstance !== undefined;
78
79                 });
80                 (<Service>this.component).updateGroupInstanceProperties(componentInstance.uniqueId, this.selectedModule, [this.$scope.property]).then(onSuccess, onFailed); // for now we only update one property at a time
81             }
82         } else {
83             deferred.resolve(true);
84         }
85         return deferred.promise;
86     }
87
88     onPropertyChange():void {
89         this.initValidation();
90     }
91
92     protected initValidation = ():void => {
93
94         this.$scope.isDeleteDisable = true;
95         this.$scope.isNameDisable = true;
96         this.$scope.isTypeSelectorDisable = true;
97         this.$scope.isDescriptionDisable = true;
98
99         switch (this.$scope.property.name) {
100             case UNIQUE_GROUP_PROPERTIES_NAME.IS_BASE:
101             case UNIQUE_GROUP_PROPERTIES_NAME.VF_MODULE_TYPE:
102             case UNIQUE_GROUP_PROPERTIES_NAME.VOLUME_GROUP:
103             case UNIQUE_GROUP_PROPERTIES_NAME.VF_MODULE_LABEL:
104                 this.$scope.property.readonly = true;
105                 break;
106             case UNIQUE_GROUP_PROPERTIES_NAME.VF_MODULE_DESCRIPTION:
107                 if (this.component.isService()) {
108                     this.$scope.property.readonly = true;
109                 } else {
110                     this.$scope.property.readonly = false;
111                 }
112                 break;
113         }
114     };
115
116     private isUniqueProperty = ():boolean => {
117         return this.$scope.property.name === UNIQUE_GROUP_PROPERTIES_NAME.MIN_VF_MODULE_INSTANCES ||
118             this.$scope.property.name === UNIQUE_GROUP_PROPERTIES_NAME.MAX_VF_MODULE_INSTANCES ||
119             this.$scope.property.name === UNIQUE_GROUP_PROPERTIES_NAME.INITIAL_COUNT;
120     };
121
122
123     private initChildScope = ():void => {
124
125         this.initValidation();
126
127         // put default value when instance value is empty
128         this.$scope.onValueChange = ():void => {
129
130             if (!this.$scope.property.value) { // Resetting to default value
131                 if (this.isPropertyValueOwner()) {
132                     if (this.component.isService()) {
133                         this.$scope.property.value = this.$scope.property.parentValue;
134                     } else {
135                         this.$scope.property.value = this.$scope.property.defaultValue;
136                     }
137                 }
138             }
139
140             if (this.isUniqueProperty()) {
141
142                 let isValid = true;
143                 let maxProperty:PropertyModel = this.findPropertyByName(UNIQUE_GROUP_PROPERTIES_NAME.MAX_VF_MODULE_INSTANCES);
144                 let minProperty:PropertyModel = this.findPropertyByName(UNIQUE_GROUP_PROPERTIES_NAME.MIN_VF_MODULE_INSTANCES);
145                 let initialCountProperty:PropertyModel = this.findPropertyByName(UNIQUE_GROUP_PROPERTIES_NAME.INITIAL_COUNT);
146
147                 let maxPropertyValue = parseInt(maxProperty.value);
148                 let minPropertyValue = parseInt(minProperty.value);
149                 let initialCountPropertyValue = parseInt(initialCountProperty.value);
150                 let propertyValue = parseInt(this.$scope.property.value);
151                 let parentPropertyValue = parseInt(this.$scope.property.parentValue);
152
153                 switch (this.$scope.property.name) {
154
155                     case UNIQUE_GROUP_PROPERTIES_NAME.MIN_VF_MODULE_INSTANCES:
156                         if (isNaN(maxPropertyValue) || maxPropertyValue == null) {
157                             isValid = propertyValue <= initialCountPropertyValue;
158                         }
159                         else {
160                             isValid = propertyValue && (propertyValue <= maxPropertyValue && propertyValue <= initialCountPropertyValue);
161                         }
162                         this.$scope.forms.editForm["value"].$setValidity('maxValidation', isValid);
163                         if (this.component.isService()) {
164                             if (isNaN(parentPropertyValue) || parentPropertyValue == null) {
165                                 isValid = true;
166                             } else {
167                                 isValid = propertyValue >= parentPropertyValue;
168                                 this.$scope.forms.editForm["value"].$setValidity('minValidationVfLevel', isValid);
169                             }
170                         }
171                         break;
172                     case UNIQUE_GROUP_PROPERTIES_NAME.MAX_VF_MODULE_INSTANCES:
173                         if (isNaN(minPropertyValue) || minPropertyValue == null) {
174                             isValid = propertyValue >= initialCountPropertyValue;
175                         } else {
176                             isValid = !propertyValue || (propertyValue >= minPropertyValue && propertyValue >= initialCountPropertyValue);
177                         }
178                         this.$scope.forms.editForm["value"].$setValidity('minValidation', isValid);
179                         if (this.component.isService()) {
180                             if (isNaN(parentPropertyValue) || parentPropertyValue == null) {
181                                 isValid = true;
182                             }
183                             else {
184                                 isValid = propertyValue <= parentPropertyValue;
185                                 this.$scope.forms.editForm["value"].$setValidity('maxValidationVfLevel', isValid);
186                             }
187                         }
188                         break;
189                     case UNIQUE_GROUP_PROPERTIES_NAME.INITIAL_COUNT:
190                         if ((isNaN(minPropertyValue) || minPropertyValue == null) && (isNaN(maxPropertyValue) || maxPropertyValue == null)) {
191                             isValid = true;
192                         } else if (isNaN(minPropertyValue) || minPropertyValue == null) {
193                             isValid = propertyValue <= maxPropertyValue;
194                         } else if (isNaN(maxPropertyValue) || maxPropertyValue == null) {
195                             isValid = propertyValue >= minPropertyValue;
196                         } else {
197                             isValid = minPropertyValue <= propertyValue && propertyValue <= maxPropertyValue;
198                         }
199                         this.$scope.forms.editForm["value"].$setValidity('minOrMaxValidation', isValid);
200                         break;
201                 }
202             }
203             ;
204         }
205     }
206 }