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";
17 export interface IArtifactsViewModelScope extends ICompositionViewModelScope {
18 artifacts:Array<ArtifactModel>;
20 downloadFile:IFileDownload;
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;
34 export class ResourceArtifactsViewModel {
44 'EventListenerService'
47 constructor(private $scope:IArtifactsViewModelScope,
48 private $filter:ng.IFilterService,
50 private sdcConfig:IAppConfigurtaion,
51 private artifactsUtils:ArtifactsUtils,
52 private ModalsHandler:ModalsHandler,
53 private $q:ng.IQService,
54 private eventListenerService: EventListenerService) {
60 private initArtifactArr = (artifactType:string):void => {
61 let artifacts:Array<ArtifactModel> = [];
63 if (this.$scope.selectedComponent) {
64 if ('interface' == artifactType) {
65 let interfaces = this.$scope.currentComponent.interfaces;
66 if (interfaces && interfaces.standard && interfaces.standard.operations) {
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;
73 item.artifactDisplayName = interfaceName;
74 item.artifactLabel = interfaceName;
75 item.mandatory = false;
80 //init normal artifacts, deployment or api artifacts
81 let artifactsObj:ArtifactGroupModel;
82 switch (artifactType) {
84 artifactsObj = (<Service>this.$scope.currentComponent).serviceApiArtifacts;
87 if (!this.$scope.isComponentInstanceSelected()) {
88 artifactsObj = this.$scope.currentComponent.deploymentArtifacts;
90 artifactsObj = this.$scope.currentComponent.selectedInstance.deploymentArtifacts;
94 //artifactsObj = this.$scope.selectedComponent.artifacts;
95 if (!this.$scope.isComponentInstanceSelected()) {
96 artifactsObj = this.$scope.currentComponent.artifacts;
98 artifactsObj = this.$scope.currentComponent.selectedInstance.artifacts;
102 _.forEach(artifactsObj, (artifact:ArtifactModel, key) => {
103 artifacts.push(artifact);
107 this.$scope.artifacts = artifacts;
111 private convertToArtifactUrl = (artifactType:string):string => {
113 switch (artifactType) {
117 return 'SERVICE_API';
119 return 'INFORMATIONAL';
124 private loadComponentArtifactIfNeeded = (forceLoad?: boolean) => {
126 let onGetComponentArtifactsSuccess = (artifacts:ArtifactGroupModel)=> {
127 switch (this.$scope.artifactType) {
129 this.$scope.currentComponent.deploymentArtifacts = artifacts;
132 (<Service>this.$scope.currentComponent).serviceApiArtifacts = artifacts;
135 this.$scope.currentComponent.artifacts = artifacts;
138 this.$scope.isLoading = false;
139 this.initArtifactArr(this.$scope.artifactType);
143 this.$scope.isLoading = false;
146 switch (this.$scope.artifactType) {
148 if(forceLoad || !this.$scope.currentComponent.deploymentArtifacts) {
149 this.$scope.component.getArtifactByGroupType(this.convertToArtifactUrl(this.$scope.artifactType)).then(onGetComponentArtifactsSuccess, onError);
151 this.initArtifactArr(this.$scope.artifactType);
156 if(!(<Service>this.$scope.currentComponent).serviceApiArtifacts) {
157 this.$scope.component.getArtifactByGroupType(this.convertToArtifactUrl(this.$scope.artifactType)).then(onGetComponentArtifactsSuccess, onError);
159 this.initArtifactArr(this.$scope.artifactType);
163 if(!this.$scope.currentComponent.artifacts) {
164 this.$scope.component.getArtifactByGroupType(this.convertToArtifactUrl(this.$scope.artifactType)).then(onGetComponentArtifactsSuccess, onError);
166 this.initArtifactArr(this.$scope.artifactType);
171 private loadArtifacts = (forceLoad?: boolean):void => {
173 let onGetInstanceArtifactsSuccess = (artifacts:ArtifactGroupModel)=> {
174 switch (this.$scope.artifactType) {
176 this.$scope.currentComponent.selectedInstance.deploymentArtifacts = artifacts;
179 this.$scope.currentComponent.selectedInstance.artifacts = artifacts;
182 this.initArtifactArr(this.$scope.artifactType);
186 this.$scope.isLoading = false;
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);
193 this.loadComponentArtifactIfNeeded(forceLoad);
197 private updateArtifactsIfNeeded = ():void => {
198 if (this.$scope.artifactType === "deployment") {
199 this.loadArtifacts(true);
201 this.initArtifactArr(this.$scope.artifactType);
205 private openEditArtifactModal = (artifact:ArtifactModel):void => {
206 this.ModalsHandler.openArtifactModal(artifact, this.$scope.currentComponent).then(():void => {
207 this.updateArtifactsIfNeeded();
211 private initScope = ():void => {
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);
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);
224 return this.$scope.currentComponent.deploymentArtifacts && (!this.$scope.currentComponent.deploymentArtifacts[artifact.artifactLabel]);//fix DE251314
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);
234 this.$scope.delete = (artifact:ArtifactModel):void => {
236 let onOk = ():void => {
237 this.$scope.isLoading = true;
238 this.artifactsUtils.removeArtifact(artifact, this.$scope.artifacts);
240 let success = (responseArtifact:ArtifactModel):void => {
241 this.initArtifactArr(this.$scope.artifactType);
242 this.$scope.isLoading = false;
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;
250 if (this.$scope.isComponentInstanceSelected()) {
251 this.$scope.currentComponent.deleteInstanceArtifact(artifact.uniqueId, artifact.artifactLabel).then(success, error);
253 this.$scope.currentComponent.deleteArtifact(artifact.uniqueId, artifact.artifactLabel).then(success, error);//TODO simulate error (make sure error returns)
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);
262 this.$scope.getEnvArtifact = (heatArtifact:ArtifactModel):any=> {
263 return _.find(this.$scope.artifacts, (item:ArtifactModel)=> {
264 return item.generatedFromId === heatArtifact.uniqueId;
268 this.$scope.getEnvArtifactName = (artifact:ArtifactModel):string => {
269 let envArtifact = this.$scope.getEnvArtifact(artifact);
271 return envArtifact.artifactDisplayName;
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);
284 this.$scope.openEditEnvParametersModal = (artifact:ArtifactModel):void => {
285 this.ModalsHandler.openEditEnvParametersModal(artifact, this.$scope.currentComponent).then(()=> {
286 this.updateArtifactsIfNeeded();
292 this.eventListenerService.registerObserverCallback(GRAPH_EVENTS.ON_NODE_SELECTED, this.loadArtifacts);
293 this.eventListenerService.registerObserverCallback(GRAPH_EVENTS.ON_GRAPH_BACKGROUND_CLICKED, this.loadArtifacts);
295 this.$scope.$on('$destroy', () => {
297 this.eventListenerService.unRegisterObserver(GRAPH_EVENTS.ON_NODE_SELECTED, this.loadArtifacts);
298 this.eventListenerService.unRegisterObserver(GRAPH_EVENTS.ON_GRAPH_BACKGROUND_CLICKED, this.loadArtifacts);