43511e2deb7ceaa60baf2a7112011ec1072d25ec
[sdc.git] /
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 /// <reference path="../../../../references"/>
21 module Sdc.ViewModels {
22     'use strict';
23     import Resource = Sdc.Models.Components.Resource;
24     import ArtifactModel = Sdc.Models.ArtifactModel;
25
26     interface IDeploymentArtifactsViewModelScope extends IWorkspaceViewModelScope {
27         tableHeadersList: Array<any>;
28         reverse: boolean;
29         sortBy:string;
30         artifacts: Array<Models.ArtifactModel>;
31         editForm:ng.IFormController;
32         isLoading:boolean;
33         artifactDescriptions:any;
34         updateInProgress:boolean;
35
36         addOrUpdate(artifact:Models.ArtifactModel): void;
37         update(artifact:Models.ArtifactModel): void;
38         delete(artifact:Models.ArtifactModel): void;
39         sort(sortBy:string): void;
40         noArtifactsToShow():boolean;
41         getValidationPattern(validationType:string, parameterType?:string):RegExp;
42         validateJson(json:string):boolean;
43         resetValue(parameter:any):void;
44         viewModeOrCsarComponent():boolean;
45         isLicenseArtifact(artifact:Models.ArtifactModel): boolean;
46
47     }
48
49     export class DeploymentArtifactsViewModel {
50
51         static '$inject' = [
52             '$scope',
53             '$filter',
54             '$modal',
55             '$templateCache',
56             'ValidationUtils',
57             'ArtifactsUtils',
58             'ModalsHandler'
59         ];
60
61         constructor(private $scope:IDeploymentArtifactsViewModelScope,
62                     private $filter:ng.IFilterService,
63                     private $modal:ng.ui.bootstrap.IModalService,
64                     private $templateCache:ng.ITemplateCacheService,
65                     private validationUtils:Sdc.Utils.ValidationUtils,
66                     private artifactsUtils:Sdc.Utils.ArtifactsUtils,
67                     private ModalsHandler:Utils.ModalsHandler) {
68             this.initScope();
69             this.$scope.updateSelectedMenuItem();
70         }
71
72         private initDescriptions = ():void => {
73             this.$scope.artifactDescriptions = {};
74             _.forEach(this.$scope.component.deploymentArtifacts, (artifact:Models.ArtifactModel):void => {
75                 this.$scope.artifactDescriptions[artifact.artifactLabel] = artifact.description;
76             });
77         };
78
79
80         private setArtifact = (artifact:Models.ArtifactModel):void => {
81             if (artifact.heatParameters) {
82                 artifact.heatParameters.forEach((parameter:any):void => {
83                     if (!parameter.currentValue && parameter.defaultValue) {
84                         parameter.currentValue = parameter.defaultValue;
85                     } else if ("" === parameter.currentValue) {
86                         parameter.currentValue = null;
87                     }
88                 });
89             }
90             if (!artifact.description || !this.$scope.getValidationPattern('string').test(artifact.description)) {
91                 artifact.description = this.$scope.artifactDescriptions[artifact.artifactLabel];
92             }
93         };
94
95         private updateAll = ():void => {
96             let artifacts:Array<Models.ArtifactModel> = [];
97             _.forEach(this.$scope.component.deploymentArtifacts, (artifact:Models.ArtifactModel):void => {
98                 if (artifact.selected) {
99                     this.setArtifact(artifact);
100                     artifacts.push(artifact);
101                 }
102             });
103             this.$scope.component.updateMultipleArtifacts(artifacts);
104         };
105
106
107         private initScope = ():void => {
108             let self = this;
109             this.$scope.isLoading = false;
110             this.$scope.updateInProgress = false;
111             this.initDescriptions();
112             this.$scope.artifacts = <ArtifactModel[]>_.values(this.$scope.component.deploymentArtifacts);
113             this.$scope.setValidState(true);
114
115             this.$scope.tableHeadersList = [
116                 {title: 'Name', property: 'artifactDisplayName'},
117                 {title: 'Type', property: 'artifactType'},
118                 {title: 'Deployment timeout', property: 'timeout'}
119             ];
120
121             this.$scope.isLicenseArtifact = (artifact:Models.ArtifactModel) :boolean => {
122                 let isLicense:boolean = false;
123                 if(this.$scope.component.isResource() && (<Resource>this.$scope.component).isCsarComponent()) {
124
125                    isLicense = this.artifactsUtils.isLicenseType(artifact.artifactType);
126                 }
127
128                 return isLicense;
129             };
130
131             this.$scope.sort = (sortBy:string):void => {
132                 this.$scope.reverse = (this.$scope.sortBy === sortBy) ? !this.$scope.reverse : false;
133                 this.$scope.sortBy = sortBy;
134             };
135
136             this.$scope.getValidationPattern = (validationType:string, parameterType?:string):RegExp => {
137                 return this.validationUtils.getValidationPattern(validationType, parameterType);
138             };
139
140             this.$scope.validateJson = (json:string):boolean => {
141                 if(!json){
142                     return true;
143                 }
144                 return this.validationUtils.validateJson(json);
145             };
146
147             this.$scope.viewModeOrCsarComponent = ():boolean => {
148               return this.$scope.isViewMode() || (this.$scope.component.isResource() && (<Resource>this.$scope.component).isCsarComponent());
149             };
150
151
152             this.$scope.addOrUpdate = (artifact:Models.ArtifactModel):void => {
153                 artifact.artifactGroupType = 'DEPLOYMENT';
154                 let artifactCopy = new Models.ArtifactModel(artifact);
155
156                 let success = (response:any):void => {
157                     self.$scope.artifactDescriptions[artifactCopy.artifactLabel] = artifactCopy.description;
158                     self.$scope.artifacts = <ArtifactModel[]>_.values(self.$scope.component.deploymentArtifacts);
159                 };
160
161                 let error = (err:any):void =>{
162                     console.log(err);
163                     self.$scope.artifacts = <ArtifactModel[]>_.values(self.$scope.component.deploymentArtifacts);
164                 };
165
166
167                 this.ModalsHandler.openWizardArtifactModal(artifactCopy, self.$scope.component).then(success, error);
168             };
169
170             this.$scope.noArtifactsToShow = ():boolean => {
171                 return !_.some(this.$scope.artifacts, 'esId');
172             };
173
174
175             this.$scope.resetValue = (parameter:any):void => {
176                 if (!parameter.currentValue && parameter.defaultValue) {
177                     parameter.currentValue = parameter.defaultValue;
178                 }
179                 else if ('boolean' == parameter.type) {
180                     parameter.currentValue = parameter.currentValue.toUpperCase();
181                 }
182             };
183
184
185             this.$scope.$watch('editForm.$valid', ():void => {
186                 if (this.$scope.editForm) {
187                     //    this.$scope.setValidState(this.$scope.editForm.$valid);
188                 }
189             });
190
191             this.$scope.update = (artifact:Models.ArtifactModel):void => {
192                 if (false == this.$scope.isLoading) {
193                     if (artifact.selected && !this.$scope.isViewMode()) {
194                         this.$scope.isLoading = true;
195                         this.$scope.updateInProgress = true;
196                         let onSuccess = (responseArtifact:Models.ArtifactModel):void => {
197                             this.$scope.artifactDescriptions[responseArtifact.artifactLabel] = responseArtifact.description;
198                             this.$scope.artifacts = <ArtifactModel[]>_.values(this.$scope.component.deploymentArtifacts);
199                             this.$scope.isLoading = false;
200                             artifact.selected = !artifact.selected;
201                             this.$scope.updateInProgress = false;
202                         };
203
204                         let onFailed = (error:any):void => {
205                             console.log('Delete artifact returned error:', error);
206                             this.$scope.isLoading = false;
207                             artifact.selected = !artifact.selected;
208                             this.$scope.updateInProgress = false;
209                         };
210
211                         this.setArtifact(artifact);
212                         this.$scope.component.addOrUpdateArtifact(artifact).then(onSuccess, onFailed);
213                     } else {
214                         artifact.selected = !artifact.selected;
215
216                     }
217                 }
218             };
219
220             this.$scope.delete = (artifact:Models.ArtifactModel):void => {
221                 let onOk = ():void => {
222                     this.$scope.isLoading = true;
223                     let onSuccess = ():void => {
224                         this.$scope.isLoading = false;
225                         this.$scope.artifacts = <ArtifactModel[]>_.values(this.$scope.component.deploymentArtifacts);
226                     };
227
228                     let onFailed = (error:any):void => {
229                         this.$scope.isLoading = false;
230                         console.log('Delete artifact returned error:', error);
231                     };
232
233                     this.$scope.component.deleteArtifact(artifact.uniqueId, artifact.artifactLabel).then(onSuccess, onFailed);
234                 };
235
236                 let title:string = self.$filter('translate')("ARTIFACT_VIEW_DELETE_MODAL_TITLE");
237                 let message:string = self.$filter('translate')("ARTIFACT_VIEW_DELETE_MODAL_TEXT", "{'name': '" + artifact.artifactDisplayName + "'}");
238                 this.ModalsHandler.openConfirmationModal(title, message, false).then(onOk);
239             };
240         };
241
242         public save = (callback:Function):void => {
243             this.updateAll();
244             this.$scope.setComponent(this.$scope.component);
245             callback(true);
246         };
247
248         public back = (callback:Function):void => {
249             this.$scope.setComponent(this.$scope.component);
250             callback(true);
251         }
252     }
253 }