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=========================================================
22 import * as _ from "lodash";
33 import {ICompositionViewModelScope} from "../../composition-view-model";
34 import {ArtifactsUtils, ModalsHandler, ArtifactGroupType} from "app/utils";
35 import {GRAPH_EVENTS} from "app/utils/constants";
36 import {EventListenerService} from "app/services/event-listener-service";
37 import {Dictionary} from "../../../../../../utils/dictionary/dictionary";
39 export interface IArtifactsViewModelScope extends ICompositionViewModelScope {
40 artifacts:Array<ArtifactModel>;
42 downloadFile:IFileDownload;
44 allowDeleteAndUpdateArtifactMap:Dictionary<string, boolean>;
46 addOrUpdate(artifact:ArtifactModel):void;
47 delete(artifact:ArtifactModel):void;
48 download(artifact:ArtifactModel):void;
49 openEditEnvParametersModal(artifact:ArtifactModel):void;
50 getEnvArtifact(heatArtifact:ArtifactModel):any;
51 getEnvArtifactName(artifact:ArtifactModel):string;
52 isLicenseArtifact(artifact:ArtifactModel):boolean;
53 //isVFiArtifact(artifact:ArtifactModel):boolean;
56 export class ResourceArtifactsViewModel {
66 'EventListenerService'
69 constructor(private $scope:IArtifactsViewModelScope,
70 private $filter:ng.IFilterService,
72 private sdcConfig:IAppConfigurtaion,
73 private artifactsUtils:ArtifactsUtils,
74 private ModalsHandler:ModalsHandler,
75 private $q:ng.IQService,
76 private eventListenerService: EventListenerService) {
82 private initArtifactArr = (artifactType:string):void => {
83 let artifacts:Array<ArtifactModel> = [];
85 if (this.$scope.selectedComponent) {
86 if ('interface' == artifactType) {
87 let interfaces = this.$scope.currentComponent.interfaces;
88 if (interfaces && interfaces.standard && interfaces.standard.operations) {
90 angular.forEach(interfaces.standard.operations, (operation:any, interfaceName:string):void => {
91 let item:ArtifactModel = <ArtifactModel>{};
92 if (operation.implementation) {
93 item = <ArtifactModel> operation.implementation;
95 item.artifactDisplayName = interfaceName;
96 item.artifactLabel = interfaceName;
97 item.mandatory = false;
102 //init normal artifacts, deployment or api artifacts
103 let artifactsObj:ArtifactGroupModel;
104 switch (artifactType) {
106 artifactsObj = (<Service>this.$scope.currentComponent).serviceApiArtifacts;
109 if (!this.$scope.isComponentInstanceSelected()) {
110 artifactsObj = this.$scope.currentComponent.deploymentArtifacts;
112 artifactsObj = this.$scope.currentComponent.selectedInstance.deploymentArtifacts;
116 //artifactsObj = this.$scope.selectedComponent.artifacts;
117 if (!this.$scope.isComponentInstanceSelected()) {
118 artifactsObj = this.$scope.currentComponent.artifacts;
120 artifactsObj = this.$scope.currentComponent.selectedInstance.artifacts;
124 _.forEach(artifactsObj, (artifact:ArtifactModel, key) => {
125 artifacts.push(artifact);
129 this.$scope.artifacts = artifacts;
130 this.$scope.allowDeleteAndUpdateArtifactMap = new Dictionary<string, boolean>();
131 _.forEach(this.$scope.artifacts, (artifact:ArtifactModel)=>{
132 this.$scope.allowDeleteAndUpdateArtifactMap[artifact.artifactLabel] = this.allowDeleteAndUpdateArtifact(artifact);
134 this.$scope.isLoading = false;
135 this.$scope.preventMoveTab(false);
139 private convertToArtifactUrl = (artifactType:string):string => {
141 switch (artifactType) {
145 return 'SERVICE_API';
147 return 'INFORMATIONAL';
152 private loadComponentArtifactIfNeeded = (forceLoad?: boolean) => {
154 let onGetComponentArtifactsSuccess = (artifacts:ArtifactGroupModel)=> {
155 switch (this.$scope.artifactType) {
157 this.$scope.currentComponent.deploymentArtifacts = artifacts;
160 (<Service>this.$scope.currentComponent).serviceApiArtifacts = artifacts;
163 this.$scope.currentComponent.artifacts = artifacts;
166 this.$scope.isLoading = false;
167 this.initArtifactArr(this.$scope.artifactType);
171 this.$scope.isLoading = false;
174 switch (this.$scope.artifactType) {
176 if(forceLoad || !this.$scope.currentComponent.deploymentArtifacts) {
177 this.$scope.component.getArtifactByGroupType(this.convertToArtifactUrl(this.$scope.artifactType)).then(onGetComponentArtifactsSuccess, onError);
179 this.initArtifactArr(this.$scope.artifactType);
184 if(!(<Service>this.$scope.currentComponent).serviceApiArtifacts) {
185 this.$scope.component.getArtifactByGroupType(this.convertToArtifactUrl(this.$scope.artifactType)).then(onGetComponentArtifactsSuccess, onError);
187 this.initArtifactArr(this.$scope.artifactType);
191 if(!this.$scope.currentComponent.artifacts) {
192 this.$scope.component.getArtifactByGroupType(this.convertToArtifactUrl(this.$scope.artifactType)).then(onGetComponentArtifactsSuccess, onError);
194 this.initArtifactArr(this.$scope.artifactType);
199 private loadArtifacts = (forceLoad?: boolean):void => {
201 let onGetInstanceArtifactsSuccess = (artifacts:ArtifactGroupModel)=> {
202 switch (this.$scope.artifactType) {
204 this.$scope.currentComponent.selectedInstance.deploymentArtifacts = artifacts;
207 this.$scope.currentComponent.selectedInstance.artifacts = artifacts;
210 this.initArtifactArr(this.$scope.artifactType);
214 this.$scope.isLoading = false;
217 this.$scope.isLoading = true;
218 this.$scope.preventMoveTab(true);
219 if (this.$scope.isComponentInstanceSelected()) {
220 this.$scope.component.getComponentInstanceArtifactsByGroupType(this.$scope.component.selectedInstance.uniqueId, this.convertToArtifactUrl(this.$scope.artifactType)).then(onGetInstanceArtifactsSuccess, onError);
222 this.loadComponentArtifactIfNeeded(forceLoad);
226 private updateArtifactsIfNeeded = ():void => {
227 if (this.$scope.artifactType === "deployment") {
228 this.loadArtifacts(true);
230 this.initArtifactArr(this.$scope.artifactType);
234 private openEditArtifactModal = (artifact:ArtifactModel):void => {
235 this.ModalsHandler.openArtifactModal(artifact, this.$scope.currentComponent).then(():void => {
236 this.updateArtifactsIfNeeded();
240 private allowDeleteAndUpdateArtifact = (artifact:ArtifactModel):boolean => {
241 if(!this.$scope.isViewMode()){
242 if(this.$scope.isComponentInstanceSelected()){//is artifact of instance
243 return !this.$scope.selectedComponent.deploymentArtifacts || !this.$scope.selectedComponent.deploymentArtifacts[artifact.artifactLabel];//if the artifact is not from instance parent
244 }else{//is artifact of main component
245 return (!artifact.isHEAT() && !artifact.isThirdParty() && !this.$scope.isLicenseArtifact(artifact));
251 private initScope = ():void => {
253 this.$scope.isLoading = false;
254 this.$scope.artifactType = this.artifactsUtils.getArtifactTypeByState(this.$state.current.name);
255 this.$scope.getTitle = ():string => {
256 return this.artifactsUtils.getTitle(this.$scope.artifactType, this.$scope.currentComponent);
259 // Bug 310499 - user should be unable to delete RI artifact. (also talked to David and agreed this function isn't necessary)
260 // this.$scope.isVFiArtifact = (artifact:ArtifactModel):boolean=> {
261 // if (artifact.artifactGroupType === ArtifactGroupType.INFORMATION) {//fix DE256847
262 // return this.$scope.currentComponent.artifacts && (!this.$scope.currentComponent.artifacts[artifact.artifactLabel] || !this.$scope.currentComponent.artifacts[artifact.artifactLabel].artifactName);
264 // return this.$scope.currentComponent.selectedInstance && this.$scope.currentComponent.selectedInstance.deploymentArtifacts && this.$scope.currentComponent.selectedInstance.deploymentArtifacts[artifact.artifactLabel];
267 this.$scope.addOrUpdate = (artifact:ArtifactModel):void => {
268 this.artifactsUtils.setArtifactType(artifact, this.$scope.artifactType);
269 let artifactCopy = new ArtifactModel(artifact);
270 this.openEditArtifactModal(artifactCopy);
274 this.$scope.delete = (artifact:ArtifactModel):void => {
276 let onOk = ():void => {
277 this.$scope.isLoading = true;
278 this.artifactsUtils.removeArtifact(artifact, this.$scope.artifacts);
280 let success = (responseArtifact:ArtifactModel):void => {
281 this.initArtifactArr(this.$scope.artifactType);
282 this.$scope.isLoading = false;
285 let error = (error:any):void => {
286 console.log('Delete artifact returned error:', error);
287 this.initArtifactArr(this.$scope.artifactType);
288 this.$scope.isLoading = false;
290 if (this.$scope.isComponentInstanceSelected()) {
291 this.$scope.currentComponent.deleteInstanceArtifact(artifact.uniqueId, artifact.artifactLabel).then(success, error);
293 this.$scope.currentComponent.deleteArtifact(artifact.uniqueId, artifact.artifactLabel).then(success, error);//TODO simulate error (make sure error returns)
296 let title:string = this.$filter('translate')("ARTIFACT_VIEW_DELETE_MODAL_TITLE");
297 let message:string = this.$filter('translate')("ARTIFACT_VIEW_DELETE_MODAL_TEXT", "{'name': '" + artifact.artifactDisplayName + "'}");
298 this.ModalsHandler.openConfirmationModal(title, message, false).then(onOk);
302 this.$scope.getEnvArtifact = (heatArtifact:ArtifactModel):any=> {
303 return _.find(this.$scope.artifacts, (item:ArtifactModel)=> {
304 return item.generatedFromId === heatArtifact.uniqueId;
308 this.$scope.getEnvArtifactName = (artifact:ArtifactModel):string => {
309 let envArtifact = this.$scope.getEnvArtifact(artifact);
311 return envArtifact.artifactDisplayName;
315 this.$scope.isLicenseArtifact = (artifact:ArtifactModel):boolean => {
316 let isLicense:boolean = false;
317 if (this.$scope.component.isResource() && (<Resource>this.$scope.component).isCsarComponent()) {
318 isLicense = this.artifactsUtils.isLicenseType(artifact.artifactType);
324 this.$scope.openEditEnvParametersModal = (artifact:ArtifactModel):void => {
325 this.ModalsHandler.openEditEnvParametersModal(artifact, this.$scope.currentComponent).then(()=> {
326 this.updateArtifactsIfNeeded();
332 this.eventListenerService.registerObserverCallback(GRAPH_EVENTS.ON_NODE_SELECTED, this.loadArtifacts);
333 this.eventListenerService.registerObserverCallback(GRAPH_EVENTS.ON_GRAPH_BACKGROUND_CLICKED, this.loadArtifacts);
335 this.$scope.$on('$destroy', () => {
337 this.eventListenerService.unRegisterObserver(GRAPH_EVENTS.ON_NODE_SELECTED, this.loadArtifacts);
338 this.eventListenerService.unRegisterObserver(GRAPH_EVENTS.ON_GRAPH_BACKGROUND_CLICKED, this.loadArtifacts);
341 this.loadArtifacts();