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