[SDC-29] Amdocs OnBoard 1707 initial commit.
[sdc.git] / catalog-ui / app / scripts / view-models / wizard / artifact-deployment-step / artifact-deployment-step.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 /// <reference path="../../../references"/>
21 module Sdc.ViewModels.Wizard {
22     'use strict';
23     import ArtifactModel = Sdc.Models.ArtifactModel;
24
25     interface IArtifactDeploymentStepViewModelScope extends IWizardCreationStepScope {
26         tableHeadersList: Array<any>;
27         reverse: boolean;
28         sortBy:string;
29         component: Models.Components.Component;
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     }
45
46     export class ArtifactDeploymentStepViewModel implements IWizardCreationStep {
47
48         static '$inject' = [
49             '$scope',
50             '$filter',
51             '$modal',
52             '$templateCache',
53             'ValidationUtils',
54             'ModalsHandler'
55         ];
56
57         constructor(
58             private $scope:IArtifactDeploymentStepViewModelScope,
59             private $filter:ng.IFilterService,
60             private $modal:ng.ui.bootstrap.IModalService,
61             private $templateCache:ng.ITemplateCacheService,
62             private validationUtils: Sdc.Utils.ValidationUtils,
63             private ModalsHandler: Utils.ModalsHandler
64         ){
65             this.$scope.registerChild(this);
66             this.$scope.setValidState(true);
67             this.initScope();
68         }
69
70         private initDescriptions = ():void =>{
71             this.$scope.artifactDescriptions = {};
72             _.forEach(this.$scope.component.deploymentArtifacts,(artifact:Models.ArtifactModel):void => {
73                 this.$scope.artifactDescriptions[artifact.artifactLabel] = artifact.description;
74             });
75         };
76
77
78         private setArtifact = (artifact:Models.ArtifactModel):void =>{
79             if(artifact.heatParameters) {
80                 artifact.heatParameters.forEach((parameter:any):void => {
81                     if (!parameter.currentValue && parameter.defaultValue) {
82                         parameter.currentValue = parameter.defaultValue;
83                     } else if ("" === parameter.currentValue) {
84                         parameter.currentValue = null;
85                     }
86                 });
87             }
88             if(!artifact.description || !this.$scope.getValidationPattern('string').test(artifact.description)){
89                 artifact.description = this.$scope.artifactDescriptions[artifact.artifactLabel];
90             }
91         };
92
93         private updateAll = ():void =>{
94             let artifacts:Array<Models.ArtifactModel>= [];
95             _.forEach(this.$scope.component.deploymentArtifacts,(artifact:Models.ArtifactModel): void => {
96                 if(artifact.selected) {
97                     this.setArtifact(artifact);
98                     artifacts.push(artifact);
99                 }
100             });
101             this.$scope.component.updateMultipleArtifacts(artifacts);
102         };
103
104
105
106         private initScope = (): void => {
107             let self = this;
108             this.$scope.isLoading = false;
109             this.$scope.updateInProgress = false;
110             this.$scope.component = this.$scope.getComponent();
111             this.initDescriptions();
112             this.$scope.artifacts = <ArtifactModel[]>_.values(this.$scope.component.deploymentArtifacts);
113
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.sort = (sortBy:string):void => {
122                 this.$scope.reverse = (this.$scope.sortBy === sortBy) ? !this.$scope.reverse : false;
123                 this.$scope.sortBy = sortBy;
124             };
125
126             this.$scope.getValidationPattern = (validationType:string, parameterType?:string):RegExp => {
127                 return this.validationUtils.getValidationPattern(validationType, parameterType);
128             };
129
130             this.$scope.validateJson = (json:string):boolean => {
131                 if(!json){
132                     return true;
133                 }
134                 return this.validationUtils.validateJson(json);
135             };
136
137
138             this.$scope.addOrUpdate = (artifact:Models.ArtifactModel): void => {
139                 artifact.artifactGroupType = 'DEPLOYMENT';
140                 let artifactCopy = new Models.ArtifactModel(artifact);
141                 this.ModalsHandler.openWizardArtifactModal(artifactCopy, this.$scope.component).then(() => {
142                     this.$scope.artifactDescriptions[artifactCopy.artifactLabel]= artifactCopy.description;
143                     this.$scope.artifacts = <ArtifactModel[]>_.values(this.$scope.component.deploymentArtifacts);
144                 })
145             };
146
147             this.$scope.noArtifactsToShow = ():boolean =>{
148                 return !_.some(this.$scope.artifacts, 'esId');
149             };
150
151             this.$scope.resetValue = (parameter:any):void => {
152                 if(!parameter.currentValue && parameter.defaultValue){
153                     parameter.currentValue = parameter.defaultValue;
154                 }
155                 else if('boolean'==parameter.type){
156                     parameter.currentValue = parameter.currentValue.toUpperCase();
157                 }
158             };
159
160
161             this.$scope.$watch('editForm.$valid', ():void => {
162                 if(this.$scope.editForm) {
163                     this.$scope.setValidState(this.$scope.editForm.$valid);
164                 }
165             });
166
167             this.$scope.update = (artifact:Models.ArtifactModel):void =>{
168                 if(false == this.$scope.isLoading) {
169                     if(artifact.selected) {
170                         this.$scope.isLoading = true;
171                         this.$scope.updateInProgress = true;
172                         let onSuccess = (responseArtifact:Models.ArtifactModel):void => {
173                             this.$scope.artifactDescriptions[responseArtifact.artifactLabel] = responseArtifact.description;
174                             this.$scope.artifacts = <ArtifactModel[]>_.values(this.$scope.component.deploymentArtifacts);
175                             this.$scope.isLoading = false;
176                             this.$scope.updateInProgress = false;
177                             artifact.selected = !artifact.selected;
178                         };
179
180                         let onFailed = (error:any):void => {
181                             console.log('Delete artifact returned error:', error);
182                             this.$scope.isLoading = false;
183                             this.$scope.updateInProgress = false;
184                             artifact.selected = !artifact.selected;
185                         };
186
187                         this.setArtifact(artifact);
188                         this.$scope.component.addOrUpdateArtifact(artifact).then(onSuccess, onFailed);
189                     } else {
190                         artifact.selected = !artifact.selected;
191                     }
192                 }
193             };
194
195             this.$scope.delete = (artifact:Models.ArtifactModel):void => {
196                 let onOk = ():void => {
197                     this.$scope.isLoading = true;
198                     let onSuccess = ():void => {
199                         this.$scope.isLoading = false;
200                         this.$scope.artifacts = <ArtifactModel[]>_.values(this.$scope.component.deploymentArtifacts);
201                     };
202
203                     let onFailed = (error:any):void => {
204                         this.$scope.isLoading = false;
205                         console.log('Delete artifact returned error:', error);
206                     };
207
208                     this.$scope.component.deleteArtifact(artifact.uniqueId, artifact.artifactLabel).then(onSuccess, onFailed);
209                 };
210
211                 let title:string = self.$filter('translate')("ARTIFACT_VIEW_DELETE_MODAL_TITLE");
212                 let message:string = self.$filter('translate')("ARTIFACT_VIEW_DELETE_MODAL_TEXT", "{'name': '" + artifact.artifactDisplayName + "'}");
213                 this.ModalsHandler.openConfirmationModal(title, message, false).then(onOk);
214             };
215         };
216
217         public save = (callback:Function):void => {
218             this.updateAll();
219             this.$scope.setComponent(this.$scope.component);
220             callback(true);
221         };
222
223         public back = (callback:Function):void => {
224             this.$scope.setComponent(this.$scope.component);
225             callback(true);
226         }
227     }
228 }