2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
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";
29 interface IDeploymentArtifactsViewModelScope extends IWorkspaceViewModelScope {
30 tableHeadersList:Array<any>;
33 artifacts:Array<ArtifactModel>;
34 editForm:ng.IFormController;
36 artifactDescriptions:any;
37 selectedArtifactId:string;
38 popoverTemplate:string;
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;
57 export class DeploymentArtifactsViewModel {
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) {
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;
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];
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);
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;
108 this.initScopeArtifacts();
113 private getEnvArtifact = (heatArtifact:ArtifactModel):ArtifactModel=> {
114 return _.find(this.$scope.artifacts, (item:ArtifactModel)=> {
115 return item.generatedFromId === heatArtifact.uniqueId;
119 private getCurrentArtifact = ():ArtifactModel => {
120 if (!this.$scope.selectedArtifactId) {
123 let artifact:ArtifactModel = this.$scope.artifacts.filter((art) => {
124 return art.uniqueId == this.$scope.selectedArtifactId;
129 private initScope = ():void => {
131 this.$scope.isLoading = false;
132 this.$scope.selectedArtifactId = null;
133 this.initDescriptions();
134 if(this.$scope.component.deploymentArtifacts) {
135 this.initArtifacts(false);
137 this.initArtifacts(true);
139 this.$scope.setValidState(true);
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'}
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";
152 this.$scope.isLicenseArtifact = (artifact:ArtifactModel):boolean => {
153 let isLicense:boolean = false;
154 if (this.$scope.component.isResource() && (<Resource>this.$scope.component).isCsarComponent()) {
156 isLicense = this.artifactsUtils.isLicenseType(artifact.artifactType);
162 this.$scope.sort = (sortBy:string):void => {
163 this.$scope.reverse = (this.$scope.sortBy === sortBy) ? !this.$scope.reverse : false;
164 this.$scope.sortBy = sortBy;
167 this.$scope.getValidationPattern = (validationType:string, parameterType?:string):RegExp => {
168 return this.validationUtils.getValidationPattern(validationType, parameterType);
171 this.$scope.validateJson = (json:string):boolean => {
175 return this.validationUtils.validateJson(json);
178 this.$scope.viewModeOrCsarComponent = ():boolean => {
179 return this.$scope.isViewMode() || (this.$scope.component.isResource() && (<Resource>this.$scope.component).isCsarComponent());
182 this.$scope.addOrUpdate = (artifact:ArtifactModel):void => {
183 artifact.artifactGroupType = 'DEPLOYMENT';
184 let artifactCopy = new ArtifactModel(artifact);
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);
192 let error = (err:any):void => {
194 this.initArtifacts(true);
195 // self.$scope.artifacts = _.values(self.$scope.component.deploymentArtifacts);
198 this.ModalsHandler.openArtifactModal(artifactCopy, this.$scope.component).then(success, error);
201 this.$scope.noArtifactsToShow = ():boolean => {
202 return !_.some(this.$scope.artifacts, 'esId');
205 this.$scope.resetValue = (parameter:any):void => {
206 if (!parameter.currentValue && parameter.defaultValue) {
207 parameter.currentValue = parameter.defaultValue;
209 else if ('boolean' == parameter.type) {
210 parameter.currentValue = parameter.currentValue.toUpperCase();
214 this.$scope.$watch('editForm.$valid', ():void => {
215 if (this.$scope.editForm) {
216 // this.$scope.setValidState(this.$scope.editForm.$valid);
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;
233 let onFailed = (error:any):void => {
234 console.log('Delete artifact returned error:', error);
235 this.$scope.isLoading = false;
238 this.$scope.component.addOrUpdateArtifact(artifact).then(onSuccess, onFailed);
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);
252 let onFailed = (error:any):void => {
253 this.$scope.isLoading = false;
254 console.log('Delete artifact returned error:', error);
257 this.$scope.component.deleteArtifact(artifact.uniqueId, artifact.artifactLabel).then(onSuccess, onFailed);
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);
265 this.$scope.getEnvArtifactName = (artifact:ArtifactModel):string => {
266 let envArtifact = this.$scope.getEnvArtifact(artifact);
268 return envArtifact.artifactDisplayName;
272 this.$scope.openEditEnvParametersModal = (artifact:ArtifactModel):void => {
273 this.ModalsHandler.openEditEnvParametersModal(artifact, this.$scope.component).then(()=> {
274 this.initArtifacts(true);
276 this.initArtifacts(true);
280 this.$scope.openDescriptionPopover = (artifactId:string):void => {
281 if (this.$scope.selectedArtifactId && this.$scope.selectedArtifactId != artifactId) {
282 this.$scope.updateSelectedArtifact();
284 this.$scope.selectedArtifactId = artifactId;
288 this.$scope.closeDescriptionPopover = ():void => {
289 if (this.$scope.selectedArtifactId) {
290 this.$scope.updateSelectedArtifact();
291 this.$scope.selectedArtifactId = null;