[SDC-29] rebase continue work to align source
[sdc.git] / catalog-ui / src / app / view-models / workspace / tabs / composition / tabs / artifacts / artifacts-view-model.ts
1 'use strict';
2 import {
3     ArtifactModel,
4     Service,
5     IAppConfigurtaion,
6     Resource,
7     Component,
8     ComponentInstance,
9     ArtifactGroupModel,
10     IFileDownload
11 } from "app/models";
12 import {ICompositionViewModelScope} from "../../composition-view-model";
13 import {ArtifactsUtils, ModalsHandler, ArtifactGroupType} from "app/utils";
14 import {GRAPH_EVENTS} from "app/utils/constants";
15 import {EventListenerService} from "app/services/event-listener-service";
16
17 export interface IArtifactsViewModelScope extends ICompositionViewModelScope {
18     artifacts:Array<ArtifactModel>;
19     artifactType:string;
20     downloadFile:IFileDownload;
21     isLoading:boolean;
22
23     getTitle():string;
24     addOrUpdate(artifact:ArtifactModel):void;
25     delete(artifact:ArtifactModel):void;
26     download(artifact:ArtifactModel):void;
27     openEditEnvParametersModal(artifact:ArtifactModel):void;
28     getEnvArtifact(heatArtifact:ArtifactModel):any;
29     getEnvArtifactName(artifact:ArtifactModel):string;
30     isLicenseArtifact(artifact:ArtifactModel):boolean;
31     isVFiArtifact(artifact:ArtifactModel):boolean;
32 }
33
34 export class ResourceArtifactsViewModel {
35
36     static '$inject' = [
37         '$scope',
38         '$filter',
39         '$state',
40         'sdcConfig',
41         'ArtifactsUtils',
42         'ModalsHandler',
43         '$q',
44         'EventListenerService'
45     ];
46
47     constructor(private $scope:IArtifactsViewModelScope,
48                 private $filter:ng.IFilterService,
49                 private $state:any,
50                 private sdcConfig:IAppConfigurtaion,
51                 private artifactsUtils:ArtifactsUtils,
52                 private ModalsHandler:ModalsHandler,
53                 private $q:ng.IQService,
54                 private eventListenerService: EventListenerService) {
55
56         this.initScope();
57     }
58
59
60     private initArtifactArr = (artifactType:string):void => {
61         let artifacts:Array<ArtifactModel> = [];
62
63         if (this.$scope.selectedComponent) {
64             if ('interface' == artifactType) {
65                 let interfaces = this.$scope.currentComponent.interfaces;
66                 if (interfaces && interfaces.standard && interfaces.standard.operations) {
67
68                     angular.forEach(interfaces.standard.operations, (operation:any, interfaceName:string):void => {
69                         let item:ArtifactModel = <ArtifactModel>{};
70                         if (operation.implementation) {
71                             item = <ArtifactModel> operation.implementation;
72                         }
73                         item.artifactDisplayName = interfaceName;
74                         item.artifactLabel = interfaceName;
75                         item.mandatory = false;
76                         artifacts.push(item);
77                     });
78                 }
79             } else {
80                 //init normal artifacts, deployment or api artifacts
81                 let artifactsObj:ArtifactGroupModel;
82                 switch (artifactType) {
83                     case "api":
84                         artifactsObj = (<Service>this.$scope.currentComponent).serviceApiArtifacts;
85                         break;
86                     case "deployment":
87                         if (!this.$scope.isComponentInstanceSelected()) {
88                             artifactsObj = this.$scope.currentComponent.deploymentArtifacts;
89                         } else {
90                             artifactsObj = this.$scope.currentComponent.selectedInstance.deploymentArtifacts;
91                         }
92                         break;
93                     default:
94                         //artifactsObj = this.$scope.selectedComponent.artifacts;
95                         if (!this.$scope.isComponentInstanceSelected()) {
96                             artifactsObj = this.$scope.currentComponent.artifacts;
97                         } else {
98                             artifactsObj = this.$scope.currentComponent.selectedInstance.artifacts;
99                         }
100                         break;
101                 }
102                 _.forEach(artifactsObj, (artifact:ArtifactModel, key) => {
103                     artifacts.push(artifact);
104                 });
105             }
106         }
107         this.$scope.artifacts = artifacts;
108     };
109
110
111     private convertToArtifactUrl = (artifactType:string):string => {
112
113         switch (artifactType) {
114             case 'deployment':
115                 return 'DEPLOYMENT';
116             case 'api':
117                 return 'SERVICE_API';
118             default:
119                 return 'INFORMATIONAL';
120         }
121
122     }
123
124     private loadComponentArtifactIfNeeded = (forceLoad?: boolean) => {
125
126         let onGetComponentArtifactsSuccess = (artifacts:ArtifactGroupModel)=> {
127             switch (this.$scope.artifactType) {
128                 case 'deployment':
129                     this.$scope.currentComponent.deploymentArtifacts = artifacts;
130                     break;
131                 case 'api':
132                     (<Service>this.$scope.currentComponent).serviceApiArtifacts = artifacts;
133                     break;
134                 default:
135                     this.$scope.currentComponent.artifacts = artifacts;
136                     break;
137             }
138             this.$scope.isLoading = false;
139             this.initArtifactArr(this.$scope.artifactType);
140         }
141
142         let onError = ()=> {
143             this.$scope.isLoading = false;
144         };
145
146         switch (this.$scope.artifactType) {
147             case 'deployment':
148                 if(forceLoad || !this.$scope.currentComponent.deploymentArtifacts) {
149                     this.$scope.component.getArtifactByGroupType(this.convertToArtifactUrl(this.$scope.artifactType)).then(onGetComponentArtifactsSuccess, onError);
150                 } else {
151                     this.initArtifactArr(this.$scope.artifactType);
152                 }
153
154                 break;
155             case 'api':
156                 if(!(<Service>this.$scope.currentComponent).serviceApiArtifacts) {
157                     this.$scope.component.getArtifactByGroupType(this.convertToArtifactUrl(this.$scope.artifactType)).then(onGetComponentArtifactsSuccess, onError);
158                 } else {
159                     this.initArtifactArr(this.$scope.artifactType);
160                 }
161                 break;
162             default:
163                 if(!this.$scope.currentComponent.artifacts) {
164                     this.$scope.component.getArtifactByGroupType(this.convertToArtifactUrl(this.$scope.artifactType)).then(onGetComponentArtifactsSuccess, onError);
165                 } else {
166                     this.initArtifactArr(this.$scope.artifactType);
167                 }
168                 break;
169         }
170     }
171     private loadArtifacts = (forceLoad?: boolean):void => {
172
173         let onGetInstanceArtifactsSuccess = (artifacts:ArtifactGroupModel)=> {
174             switch (this.$scope.artifactType) {
175                 case 'deployment':
176                     this.$scope.currentComponent.selectedInstance.deploymentArtifacts = artifacts;
177                     break;
178                 default:
179                     this.$scope.currentComponent.selectedInstance.artifacts = artifacts;
180                     break;
181             }
182             this.initArtifactArr(this.$scope.artifactType);
183         };
184
185         let onError = ()=> {
186             this.$scope.isLoading = false;
187         };
188
189         this.$scope.isLoading = true;
190         if (this.$scope.isComponentInstanceSelected()) {
191             this.$scope.component.getComponentInstanceArtifactsByGroupType(this.$scope.component.selectedInstance.uniqueId, this.convertToArtifactUrl(this.$scope.artifactType)).then(onGetInstanceArtifactsSuccess, onError);
192         } else {
193             this.loadComponentArtifactIfNeeded(forceLoad);
194         }
195     }
196
197     private updateArtifactsIfNeeded = ():void => {
198         if (this.$scope.artifactType === "deployment") {
199             this.loadArtifacts(true);
200         } else {
201             this.initArtifactArr(this.$scope.artifactType);
202         }
203     };
204
205     private openEditArtifactModal = (artifact:ArtifactModel):void => {
206         this.ModalsHandler.openArtifactModal(artifact, this.$scope.currentComponent).then(():void => {
207             this.updateArtifactsIfNeeded();
208         });
209     };
210
211     private initScope = ():void => {
212
213         this.$scope.isLoading = false;
214         this.$scope.artifactType = this.artifactsUtils.getArtifactTypeByState(this.$state.current.name);
215         this.loadArtifacts();
216         this.$scope.getTitle = ():string => {
217             return this.artifactsUtils.getTitle(this.$scope.artifactType, this.$scope.currentComponent);
218         };
219
220         this.$scope.isVFiArtifact = (artifact:ArtifactModel):boolean=> {
221             if (artifact.artifactGroupType === ArtifactGroupType.INFORMATION) {//fix DE256847
222                 return this.$scope.currentComponent.artifacts && (!this.$scope.currentComponent.artifacts[artifact.artifactLabel] || !this.$scope.currentComponent.artifacts[artifact.artifactLabel].artifactName);
223             }
224             return this.$scope.currentComponent.deploymentArtifacts && (!this.$scope.currentComponent.deploymentArtifacts[artifact.artifactLabel]);//fix DE251314
225         };
226
227         this.$scope.addOrUpdate = (artifact:ArtifactModel):void => {
228             this.artifactsUtils.setArtifactType(artifact, this.$scope.artifactType);
229             let artifactCopy = new ArtifactModel(artifact);
230             this.openEditArtifactModal(artifactCopy);
231         };
232
233
234         this.$scope.delete = (artifact:ArtifactModel):void => {
235
236             let onOk = ():void => {
237                 this.$scope.isLoading = true;
238                 this.artifactsUtils.removeArtifact(artifact, this.$scope.artifacts);
239
240                 let success = (responseArtifact:ArtifactModel):void => {
241                     this.initArtifactArr(this.$scope.artifactType);
242                     this.$scope.isLoading = false;
243                 };
244
245                 let error = (error:any):void => {
246                     console.log('Delete artifact returned error:', error);
247                     this.initArtifactArr(this.$scope.artifactType);
248                     this.$scope.isLoading = false;
249                 };
250                 if (this.$scope.isComponentInstanceSelected()) {
251                     this.$scope.currentComponent.deleteInstanceArtifact(artifact.uniqueId, artifact.artifactLabel).then(success, error);
252                 } else {
253                     this.$scope.currentComponent.deleteArtifact(artifact.uniqueId, artifact.artifactLabel).then(success, error);//TODO simulate error (make sure error returns)
254                 }
255             };
256             let title:string = this.$filter('translate')("ARTIFACT_VIEW_DELETE_MODAL_TITLE");
257             let message:string = this.$filter('translate')("ARTIFACT_VIEW_DELETE_MODAL_TEXT", "{'name': '" + artifact.artifactDisplayName + "'}");
258             this.ModalsHandler.openConfirmationModal(title, message, false).then(onOk);
259         };
260
261
262         this.$scope.getEnvArtifact = (heatArtifact:ArtifactModel):any=> {
263             return _.find(this.$scope.artifacts, (item:ArtifactModel)=> {
264                 return item.generatedFromId === heatArtifact.uniqueId;
265             });
266         };
267
268         this.$scope.getEnvArtifactName = (artifact:ArtifactModel):string => {
269             let envArtifact = this.$scope.getEnvArtifact(artifact);
270             if (envArtifact) {
271                 return envArtifact.artifactDisplayName;
272             }
273         };
274
275         this.$scope.isLicenseArtifact = (artifact:ArtifactModel):boolean => {
276             let isLicense:boolean = false;
277             if (this.$scope.component.isResource() && (<Resource>this.$scope.component).isCsarComponent()) {
278                 isLicense = this.artifactsUtils.isLicenseType(artifact.artifactType);
279             }
280
281             return isLicense;
282         };
283
284         this.$scope.openEditEnvParametersModal = (artifact:ArtifactModel):void => {
285             this.ModalsHandler.openEditEnvParametersModal(artifact, this.$scope.currentComponent).then(()=> {
286                 this.updateArtifactsIfNeeded();
287             }, ()=> {
288                 // ERROR
289             });
290         };
291
292         this.eventListenerService.registerObserverCallback(GRAPH_EVENTS.ON_NODE_SELECTED, this.loadArtifacts);
293         this.eventListenerService.registerObserverCallback(GRAPH_EVENTS.ON_GRAPH_BACKGROUND_CLICKED, this.loadArtifacts);
294
295         this.$scope.$on('$destroy', () => {
296
297             this.eventListenerService.unRegisterObserver(GRAPH_EVENTS.ON_NODE_SELECTED, this.loadArtifacts);
298             this.eventListenerService.unRegisterObserver(GRAPH_EVENTS.ON_GRAPH_BACKGROUND_CLICKED, this.loadArtifacts);
299         });
300     }
301 }