86bc4780488388407eebc7502925fd7da61ac836
[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
21 //@require "./*.html"
22 'use strict';
23 import {IWorkspaceViewModelScope} from "app/view-models/workspace/workspace-view-model";
24 import {ArtifactModel, ArtifactGroupModel, Resource} from "app/models";
25 import {ArtifactsUtils, ModalsHandler, ValidationUtils} from "app/utils";
26 import {ComponentServiceNg2} from "app/ng2/services/component-services/component.service";
27 import {ComponentGenericResponse} from "../../../../ng2/services/responses/component-generic-response";
28
29 interface IDeploymentArtifactsViewModelScope extends IWorkspaceViewModelScope {
30     tableHeadersList:Array<any>;
31     reverse:boolean;
32     sortBy:string;
33     artifacts:Array<ArtifactModel>;
34     editForm:ng.IFormController;
35     isLoading:boolean;
36     artifactDescriptions:any;
37     selectedArtifactId:string;
38     popoverTemplate:string;
39
40     addOrUpdate(artifact:ArtifactModel):void;
41     updateSelectedArtifact():void;
42     delete(artifact:ArtifactModel):void;
43     sort(sortBy:string):void;
44     noArtifactsToShow():boolean;
45     getValidationPattern(validationType:string, parameterType?:string):RegExp;
46     validateJson(json:string):boolean;
47     resetValue(parameter:any):void;
48     viewModeOrCsarComponent():boolean;
49     isLicenseArtifact(artifact:ArtifactModel):void;
50     getEnvArtifact(heatArtifact:ArtifactModel):ArtifactModel;
51     getEnvArtifactName(artifact:ArtifactModel):string;
52     openEditEnvParametersModal(artifact:ArtifactModel):void;
53     openDescriptionPopover(artifactId:string):void;
54     closeDescriptionPopover():void;
55 }
56
57 export class DeploymentArtifactsViewModel {
58
59     static '$inject' = [
60         '$scope',
61         '$templateCache',
62         '$filter',
63         'ValidationUtils',
64         'ArtifactsUtils',
65         'ModalsHandler',
66         'ComponentServiceNg2'
67     ];
68
69     constructor(private $scope:IDeploymentArtifactsViewModelScope,
70                 private $templateCache:ng.ITemplateCacheService,
71                 private $filter:ng.IFilterService,
72                 private validationUtils:ValidationUtils,
73                 private artifactsUtils:ArtifactsUtils,
74                 private ModalsHandler:ModalsHandler,
75                 private ComponentServiceNg2: ComponentServiceNg2) {
76         this.initScope();
77     }
78
79     private initDescriptions = ():void => {
80         this.$scope.artifactDescriptions = {};
81         _.forEach(this.$scope.component.deploymentArtifacts, (artifact:ArtifactModel):void => {
82             this.$scope.artifactDescriptions[artifact.artifactLabel] = artifact.description;
83         });
84     };
85
86     private setArtifact = (artifact:ArtifactModel):void => {
87         if (!artifact.description || !this.$scope.getValidationPattern('string').test(artifact.description)) {
88             artifact.description = this.$scope.artifactDescriptions[artifact.artifactLabel];
89         }
90     };
91
92     private initScopeArtifacts = ()=> {
93         this.$scope.artifacts = <ArtifactModel[]>_.values(this.$scope.component.deploymentArtifacts);
94         _.forEach(this.$scope.artifacts, (artifact:ArtifactModel):void => {
95             artifact.envArtifact = this.getEnvArtifact(artifact);
96         });
97     };
98
99     private initArtifacts = (loadFromServer:boolean):void => {
100         if (loadFromServer) {
101             this.$scope.isLoading = true;
102             this.ComponentServiceNg2.getComponentDeploymentArtifacts(this.$scope.component).subscribe((response:ComponentGenericResponse) => {
103                 this.$scope.component.deploymentArtifacts = response.deploymentArtifacts;
104                 this.initScopeArtifacts();
105                 this.$scope.isLoading = false;
106             });
107         } else {
108             this.initScopeArtifacts();
109         }
110
111     };
112
113     private getEnvArtifact = (heatArtifact:ArtifactModel):ArtifactModel=> {
114         return _.find(this.$scope.artifacts, (item:ArtifactModel)=> {
115             return item.generatedFromId === heatArtifact.uniqueId;
116         });
117     };
118
119     private getCurrentArtifact = ():ArtifactModel => {
120         if (!this.$scope.selectedArtifactId) {
121             return null;
122         }
123         let artifact:ArtifactModel = this.$scope.artifacts.filter((art) => {
124             return art.uniqueId == this.$scope.selectedArtifactId;
125         })[0];
126         return artifact;
127     }
128
129     private initScope = ():void => {
130         let self = this;
131         this.$scope.isLoading = false;
132         this.$scope.selectedArtifactId = null;
133         this.initDescriptions();
134         if(this.$scope.component.deploymentArtifacts) {
135             this.initArtifacts(false);
136         } else {
137             this.initArtifacts(true);
138         }
139         this.$scope.setValidState(true);
140
141         this.$scope.tableHeadersList = [
142             {title: 'Name', property: 'artifactDisplayName'},
143             {title: 'Type', property: 'artifactType'},
144             {title: 'Deployment timeout', property: 'timeout'},
145             {title: 'Version', property: 'artifactVersion'},
146             {title: 'UUID', property: 'artifactUUID'}
147         ];
148
149         this.$templateCache.put("deployment-artifacts-description-popover.html", require('app/view-models/workspace/tabs/deployment-artifacts/deployment-artifacts-description-popover.html'));
150         this.$scope.popoverTemplate = "deployment-artifacts-description-popover.html";
151
152         this.$scope.isLicenseArtifact = (artifact:ArtifactModel):boolean => {
153             let isLicense:boolean = false;
154             if (this.$scope.component.isResource() && (<Resource>this.$scope.component).isCsarComponent()) {
155
156                 isLicense = this.artifactsUtils.isLicenseType(artifact.artifactType);
157             }
158
159             return isLicense;
160         };
161
162         this.$scope.sort = (sortBy:string):void => {
163             this.$scope.reverse = (this.$scope.sortBy === sortBy) ? !this.$scope.reverse : false;
164             this.$scope.sortBy = sortBy;
165         };
166
167         this.$scope.getValidationPattern = (validationType:string, parameterType?:string):RegExp => {
168             return this.validationUtils.getValidationPattern(validationType, parameterType);
169         };
170
171         this.$scope.validateJson = (json:string):boolean => {
172             if (!json) {
173                 return true;
174             }
175             return this.validationUtils.validateJson(json);
176         };
177
178         this.$scope.viewModeOrCsarComponent = ():boolean => {
179             return this.$scope.isViewMode() || (this.$scope.component.isResource() && (<Resource>this.$scope.component).isCsarComponent());
180         };
181
182         this.$scope.addOrUpdate = (artifact:ArtifactModel):void => {
183             artifact.artifactGroupType = 'DEPLOYMENT';
184             let artifactCopy = new ArtifactModel(artifact);
185
186             let success = (response:any):void => {
187                 this.$scope.artifactDescriptions[artifactCopy.artifactLabel] = artifactCopy.description;
188                 this.initArtifacts(true);
189                 //  this.$scope.artifacts = _.values(this.$scope.component.deploymentArtifacts);
190             };
191
192             let error = (err:any):void => {
193                 console.log(err);
194                 this.initArtifacts(true);
195                 //  self.$scope.artifacts = _.values(self.$scope.component.deploymentArtifacts);
196             };
197
198             this.ModalsHandler.openArtifactModal(artifactCopy, this.$scope.component).then(success, error);
199         };
200
201         this.$scope.noArtifactsToShow = ():boolean => {
202             return !_.some(this.$scope.artifacts, 'esId');
203         };
204
205         this.$scope.resetValue = (parameter:any):void => {
206             if (!parameter.currentValue && parameter.defaultValue) {
207                 parameter.currentValue = parameter.defaultValue;
208             }
209             else if ('boolean' == parameter.type) {
210                 parameter.currentValue = parameter.currentValue.toUpperCase();
211             }
212         };
213
214         this.$scope.$watch('editForm.$valid', ():void => {
215             if (this.$scope.editForm) {
216                 //    this.$scope.setValidState(this.$scope.editForm.$valid);
217             }
218         });
219
220         this.$scope.updateSelectedArtifact = ():void => {
221             if (!this.$scope.isViewMode() && !this.$scope.isLoading) {
222                 let artifact:ArtifactModel = this.getCurrentArtifact();
223                 this.setArtifact(artifact); //resets artifact description to original value if invalid.
224                 if (artifact && artifact.originalDescription != artifact.description) {
225                     this.$scope.isLoading = true;
226                     let onSuccess = (responseArtifact:ArtifactModel):void => {
227                         this.$scope.artifactDescriptions[responseArtifact.artifactLabel] = responseArtifact.description;
228                         // this.$scope.artifacts = _.values(this.$scope.component.deploymentArtifacts);
229                         this.initArtifacts(true);
230                         this.$scope.isLoading = false;
231                     };
232
233                     let onFailed = (error:any):void => {
234                         console.log('Delete artifact returned error:', error);
235                         this.$scope.isLoading = false;
236                     };
237
238                     this.$scope.component.addOrUpdateArtifact(artifact).then(onSuccess, onFailed);
239                 }
240             }
241         };
242
243         this.$scope.delete = (artifact:ArtifactModel):void => {
244             let onOk = ():void => {
245                 this.$scope.isLoading = true;
246                 let onSuccess = ():void => {
247                     this.$scope.isLoading = false;
248                     this.initArtifacts(true);
249                     //this.$scope.artifacts = _.values(this.$scope.component.deploymentArtifacts);
250                 };
251
252                 let onFailed = (error:any):void => {
253                     this.$scope.isLoading = false;
254                     console.log('Delete artifact returned error:', error);
255                 };
256
257                 this.$scope.component.deleteArtifact(artifact.uniqueId, artifact.artifactLabel).then(onSuccess, onFailed);
258             };
259
260             let title:string = self.$filter('translate')("ARTIFACT_VIEW_DELETE_MODAL_TITLE");
261             let message:string = self.$filter('translate')("ARTIFACT_VIEW_DELETE_MODAL_TEXT", "{'name': '" + artifact.artifactDisplayName + "'}");
262             this.ModalsHandler.openConfirmationModal(title, message, false).then(onOk);
263         };
264
265         this.$scope.getEnvArtifactName = (artifact:ArtifactModel):string => {
266             let envArtifact = this.$scope.getEnvArtifact(artifact);
267             if (envArtifact) {
268                 return envArtifact.artifactDisplayName;
269             }
270         };
271
272         this.$scope.openEditEnvParametersModal = (artifact:ArtifactModel):void => {
273             this.ModalsHandler.openEditEnvParametersModal(artifact, this.$scope.component).then(()=> {
274                 this.initArtifacts(true);
275             }, ()=> {
276                 this.initArtifacts(true);
277             });
278         };
279
280         this.$scope.openDescriptionPopover = (artifactId:string):void => {
281             if (this.$scope.selectedArtifactId && this.$scope.selectedArtifactId != artifactId) {
282                 this.$scope.updateSelectedArtifact();
283             }
284             this.$scope.selectedArtifactId = artifactId;
285
286         };
287
288         this.$scope.closeDescriptionPopover = ():void => {
289             if (this.$scope.selectedArtifactId) {
290                 this.$scope.updateSelectedArtifact();
291                 this.$scope.selectedArtifactId = null;
292             }
293         };
294     };
295 }