CSIT Fix for SDC-2585
[sdc.git] / catalog-ui / src / app / view-models / workspace / tabs / information-artifacts / information-artifacts-view-model.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
21 'use strict';
22 import * as _ from "lodash";
23 import {ModalsHandler} from "app/utils";
24 import {SharingService} from "app/services";
25 import {IAppConfigurtaion, ArtifactModel, IFileDownload} from "app/models";
26 import {IWorkspaceViewModelScope} from "app/view-models/workspace/workspace-view-model";
27 import {ComponentServiceNg2} from "../../../../ng2/services/component-services/component.service";
28 import {ArtifactGroupModel} from "../../../../models/artifacts";
29 import {ComponentGenericResponse} from "../../../../ng2/services/responses/component-generic-response";
30
31 export interface IInformationArtifactsScope extends IWorkspaceViewModelScope {
32     artifacts:Array<ArtifactModel>;
33     tableHeadersList:Array<any>;
34     artifactType:string;
35     isResourceInstance:boolean;
36     downloadFile:IFileDownload;
37     isLoading:boolean;
38     sortBy:string;
39     reverse:boolean;
40
41     getTitle():string;
42     addOrUpdate(artifact:ArtifactModel):void;
43     delete(artifact:ArtifactModel):void;
44     download(artifact:ArtifactModel):void;
45     clickArtifactName(artifact:any):void;
46     openEditEnvParametersModal(artifactResource:ArtifactModel):void;
47     sort(sortBy:string):void;
48     showNoArtifactMessage():boolean;
49 }
50
51 export class InformationArtifactsViewModel {
52
53     static '$inject' = [
54         '$scope',
55         '$filter',
56         '$state',
57         'sdcConfig',
58         'ModalsHandler',
59         'ComponentServiceNg2'
60     ];
61
62     constructor(private $scope:IInformationArtifactsScope,
63                 private $filter:ng.IFilterService,
64                 private $state:any,
65                 private sdcConfig:IAppConfigurtaion,
66                 private ModalsHandler:ModalsHandler,
67                 private ComponentServiceNg2: ComponentServiceNg2) {
68         this.initInformationalArtifacts();
69     }
70
71     private initInformationalArtifacts = ():void => {
72         if(!this.$scope.component.artifacts) {
73             this.$scope.isLoading = true;
74             this.ComponentServiceNg2.getComponentInformationalArtifacts(this.$scope.component).subscribe((response:ComponentGenericResponse) => {
75                 this.$scope.component.artifacts = response.artifacts;
76                 this.initScope();
77                 this.$scope.isLoading = false;
78             });
79         } else {
80             this.initScope();
81         }
82     }
83
84     private initScope = ():void => {
85
86         this.$scope.isLoading = false;
87         this.$scope.sortBy = 'artifactDisplayName';
88         this.$scope.reverse = false;
89         this.$scope.setValidState(true);
90         this.$scope.artifactType = 'informational';
91         this.$scope.getTitle = ():string => {
92             return this.$filter("resourceName")(this.$scope.component.name) + ' Artifacts';
93
94         };
95
96         this.$scope.tableHeadersList = [
97             {title: 'Name', property: 'artifactDisplayName'},
98             {title: 'Type', property: 'artifactType'},
99             {title: 'Version', property: 'artifactVersion'},
100             {title: 'UUID', property: 'artifactUUID'}
101         ];
102
103         this.$scope.artifacts = <ArtifactModel[]>_.values(this.$scope.component.artifacts);
104         this.$scope.sort = (sortBy:string):void => {
105             this.$scope.reverse = (this.$scope.sortBy === sortBy) ? !this.$scope.reverse : false;
106             this.$scope.sortBy = sortBy;
107         };
108
109
110         this.$scope.addOrUpdate = (artifact:ArtifactModel):void => {
111             artifact.artifactGroupType = 'INFORMATIONAL';
112             this.ModalsHandler.openArtifactModal(artifact, this.$scope.component).then(() => {
113                 this.$scope.artifacts = <ArtifactModel[]>_.values(this.$scope.component.artifacts);
114             });
115         };
116
117         this.$scope.showNoArtifactMessage = ():boolean => {
118             let artifacts:any = [];
119             artifacts = _.filter(this.$scope.artifacts, (artifact:ArtifactModel)=> {
120                 return artifact.esId;
121             });
122
123             if (artifacts.length === 0) {
124                 return true;
125             }
126             return false;
127         };
128
129         this.$scope.delete = (artifact:ArtifactModel):void => {
130
131             let onOk = ():void => {
132                 this.$scope.isLoading = true;
133                 let onSuccess = ():void => {
134                     this.$scope.isLoading = false;
135                     this.$scope.artifacts = <ArtifactModel[]>_.values(this.$scope.component.artifacts);
136                 };
137
138                 let onFailed = (error:any):void => {
139                     console.log('Delete artifact returned error:', error);
140                     this.$scope.isLoading = false;
141                 };
142
143                 this.$scope.component.deleteArtifact(artifact.uniqueId, artifact.artifactLabel).then(onSuccess, onFailed);
144             };
145
146             let title:string = this.$filter('translate')("ARTIFACT_VIEW_DELETE_MODAL_TITLE");
147             let message:string = this.$filter('translate')("ARTIFACT_VIEW_DELETE_MODAL_TEXT", "{'name': '" + artifact.artifactDisplayName + "'}");
148             this.ModalsHandler.openConfirmationModal(title, message, false).then(onOk);
149         };
150
151         this.$scope.clickArtifactName = (artifact:any) => {
152             if (!artifact.esId) {
153                 this.$scope.addOrUpdate(artifact);
154             }
155
156         };
157     }
158 }