[SDC] rebase 1710 code
[sdc.git] / catalog-ui / src / app / view-models / workspace / workspace-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 /**
22  * Created by obarda on 3/30/2016.
23  */
24 'use strict';
25 import {IUserProperties, IAppMenu, Resource, Component} from "app/models";
26 import {
27     WorkspaceMode, ComponentFactory, ChangeLifecycleStateHandler, Role, ComponentState, MenuItemGroup, MenuHandler,
28     MenuItem, ModalsHandler, States, EVENTS, CHANGE_COMPONENT_CSAR_VERSION_FLAG, ResourceType
29 } from "app/utils";
30 import {
31     EventListenerService,
32     EntityService,
33     ProgressService,
34     CacheService,
35     LeftPaletteLoaderService
36 } from "app/services";
37 import {FileUploadModel} from "../../directives/file-upload/file-upload";
38
39
40 export interface IWorkspaceViewModelScope extends ng.IScope {
41
42     isLoading:boolean;
43     isCreateProgress:boolean;
44     component:Component;
45     originComponent:Component;
46     componentType:string;
47     importFile:any;
48     leftBarTabs:MenuItemGroup;
49     isNew:boolean;
50     isFromImport:boolean;
51     isValidForm:boolean;
52     mode:WorkspaceMode;
53     breadcrumbsModel:Array<MenuItemGroup>;
54     sdcMenu:IAppMenu;
55     changeLifecycleStateButtons:any;
56     version:string;
57     versionsList:Array<any>;
58     changeVersion:any;
59     isComposition:boolean;
60     isDeployment:boolean;
61     $state:ng.ui.IStateService;
62     user:IUserProperties;
63     thirdParty:boolean;
64     disabledButtons:boolean;
65     menuComponentTitle:string;
66     progressService:ProgressService;
67     progressMessage:string;
68     // leftPanelComponents:Array<Models.Components.Component>; //this is in order to load the left panel once, and not wait long time when moving to composition
69
70     showChangeStateButton():boolean;
71     getComponent():Component;
72     setComponent(component:Component):void;
73     onMenuItemPressed(state:string):ng.IPromise<boolean>;
74     save():ng.IPromise<boolean>;
75     setValidState(isValid:boolean):void;
76     revert():void;
77     changeLifecycleState(state:string):void;
78     enabledTabs():void
79     isDesigner():boolean;
80     isViewMode():boolean;
81     isEditMode():boolean;
82     isCreateMode():boolean;
83     isDisableMode():boolean;
84     showFullIcons():boolean;
85     goToBreadcrumbHome():void;
86     onVersionChanged(selectedId:string):void;
87     getLatestVersion():void;
88     getStatus():string;
89     showLifecycleIcon():boolean;
90     updateSelectedMenuItem():void;
91     uploadFileChangedInGeneralTab():void;
92     updateMenuComponentName(ComponentName:string):void;
93     getTabTitle():string;
94     reload(component:Component):void;
95 }
96
97 export class WorkspaceViewModel {
98
99     static '$inject' = [
100         '$scope',
101         'injectComponent',
102         'ComponentFactory',
103         '$state',
104         'sdcMenu',
105         '$q',
106         'MenuHandler',
107         'Sdc.Services.CacheService',
108         'ChangeLifecycleStateHandler',
109         'ModalsHandler',
110         'LeftPaletteLoaderService',
111         '$filter',
112         'EventListenerService',
113         'Sdc.Services.EntityService',
114         'Notification',
115         '$stateParams',
116         'Sdc.Services.ProgressService'
117     ];
118
119     constructor(private $scope:IWorkspaceViewModelScope,
120                 private injectComponent:Component,
121                 private ComponentFactory:ComponentFactory,
122                 private $state:ng.ui.IStateService,
123                 private sdcMenu:IAppMenu,
124                 private $q:ng.IQService,
125                 private MenuHandler:MenuHandler,
126                 private cacheService:CacheService,
127                 private ChangeLifecycleStateHandler:ChangeLifecycleStateHandler,
128                 private ModalsHandler:ModalsHandler,
129                 private LeftPaletteLoaderService:LeftPaletteLoaderService,
130                 private $filter:ng.IFilterService,
131                 private EventListenerService:EventListenerService,
132                 private EntityService:EntityService,
133                 private Notification:any,
134                 private $stateParams:any,
135                 private progressService:ProgressService) {
136
137         this.initScope();
138         this.initAfterScope();
139     }
140
141     private role:string;
142     private components:Array<Component>;
143
144     private initViewMode = ():WorkspaceMode => {
145         let mode = WorkspaceMode.VIEW;
146
147         if (!this.$state.params['id']) {   //&& !this.$state.params['vspComponent']
148             mode = WorkspaceMode.CREATE;
149         } else {
150             if (this.$scope.component.lifecycleState === ComponentState.NOT_CERTIFIED_CHECKOUT &&
151                 this.$scope.component.lastUpdaterUserId === this.cacheService.get("user").userId) {
152                 if ((this.$scope.component.isService() || this.$scope.component.isResource()) && this.role == Role.DESIGNER) {
153                     mode = WorkspaceMode.EDIT;
154                 }
155             }
156         }
157         return mode;
158     };
159
160     private initChangeLifecycleStateButtons = ():void => {
161         let state = this.$scope.component.isService() && (Role.OPS == this.role || Role.GOVERNOR == this.role) ? this.$scope.component.distributionStatus : this.$scope.component.lifecycleState;
162         this.$scope.changeLifecycleStateButtons = this.sdcMenu.roles[this.role].changeLifecycleStateButtons[state];
163     };
164
165     private isNeedSave = ():boolean => {
166         return this.$scope.isEditMode() &&
167             this.$state.current.data && this.$state.current.data.unsavedChanges;
168     };
169
170     private initLeftPalette = ():void => {
171         this.LeftPaletteLoaderService.loadLeftPanel(this.$scope.component);
172     };
173
174     private initScope = ():void => {
175
176         this.$scope.component = this.injectComponent;
177         this.initLeftPalette();
178         this.$scope.menuComponentTitle = this.$scope.component.name;
179         this.$scope.disabledButtons = false;
180         this.$scope.originComponent = this.ComponentFactory.createComponent(this.$scope.component);
181         this.$scope.componentType = this.$scope.component.componentType;
182         this.$scope.version = this.cacheService.get('version');
183         this.$scope.user = this.cacheService.get("user");
184         this.role = this.$scope.user.role;
185         this.$scope.mode = this.initViewMode();
186         this.$scope.isValidForm = true;
187         this.initChangeLifecycleStateButtons();
188         this.initVersionObject();
189         this.$scope.$state = this.$state;
190         this.$scope.isLoading = false;
191         this.$scope.isComposition = (this.$state.current.name.indexOf(States.WORKSPACE_COMPOSITION) > -1);
192         this.$scope.isDeployment = this.$state.current.name == States.WORKSPACE_DEPLOYMENT;
193         this.$scope.progressService = this.progressService;
194
195         this.$scope.getComponent = ():Component => {
196             return this.$scope.component;
197         };
198
199         this.$scope.updateMenuComponentName = (ComponentName:string):void => {
200             this.$scope.menuComponentTitle = ComponentName;
201         };
202
203         this.$scope.sdcMenu = this.sdcMenu;
204         // Will be called from each step after save to update the resource.
205         this.$scope.setComponent = (component:Component):void => {
206             this.$scope.component = component;
207         };
208
209         this.$scope.uploadFileChangedInGeneralTab = ():void => {
210             // In case user select browse file, and in update mode, need to disable submit for testing and checkin buttons.
211             if (this.$scope.isEditMode() && this.$scope.component.isResource() && (<Resource>this.$scope.component).resourceType == ResourceType.VF) {
212                 this.$scope.disabledButtons = true;
213             }
214         };
215
216         this.$scope.onMenuItemPressed = (state:string):ng.IPromise<boolean> => {
217             let deferred = this.$q.defer();
218             let goToState = ():void => {
219                 this.$state.go(state, {
220                     id: this.$scope.component.uniqueId,
221                     type: this.$scope.component.componentType.toLowerCase(),
222                     components: this.components
223                 });
224                 deferred.resolve(true);
225             };
226             if (this.isNeedSave()) {
227                 if (this.$scope.isValidForm) {
228                     this.$scope.save().then(goToState);
229                 } else {
230                     console.log('form is not valid');
231                     deferred.reject(false);
232                 }
233             } else if (this.$scope.isEditMode() && //this is a workaround for amdocs - we need to get the artifact in order to avoid saving the vf when moving from their tabs
234                 (this.$state.current.name === States.WORKSPACE_MANAGEMENT_WORKFLOW || this.$state.current.name === States.WORKSPACE_NETWORK_CALL_FLOW)) {
235                 let onGetSuccess = (component:Component) => {
236                     this.$scope.isLoading = false;
237                     // Update the components
238                     this.$scope.component = component;
239                     goToState();
240                 };
241                 let onFailed = () => {
242                     this.EventListenerService.notifyObservers(EVENTS.ON_WORKSPACE_SAVE_BUTTON_ERROR);
243                     this.$scope.isLoading = false; // stop the progress.
244                     deferred.reject(false);
245                 };
246                 this.$scope.component.getComponent().then(onGetSuccess, onFailed);
247             } else {
248                 goToState();
249             }
250             return deferred.promise;
251         };
252
253         this.$scope.setValidState = (isValid:boolean):void => {
254             this.$scope.isValidForm = isValid;
255         };
256
257         this.$scope.onVersionChanged = (selectedId:string):void => {
258             if (this.$state.current.data && this.$state.current.data.unsavedChanges) {
259                 this.$scope.changeVersion.selectedVersion = _.find(this.$scope.versionsList, (versionObj)=> {
260                    return versionObj.versionId === this.$scope.component.uniqueId;
261                 });
262             }
263             this.$scope.isLoading = true;
264             this.$state.go(this.$state.current.name, {
265                 id: selectedId,
266                 type: this.$scope.componentType.toLowerCase(),
267                 mode: WorkspaceMode.VIEW,
268                 components: this.$state.params['components']
269             }, {reload: true});
270
271         };
272
273         this.$scope.getLatestVersion = ():void => {
274             this.$scope.onVersionChanged(_.first(this.$scope.versionsList).versionId);
275         };
276
277         this.$scope.save = (state?:string):ng.IPromise<boolean> => {
278             this.EventListenerService.notifyObservers(EVENTS.ON_WORKSPACE_SAVE_BUTTON_CLICK);
279
280             this.progressService.initCreateComponentProgress(this.$scope.component.uniqueId);
281
282             let deferred = this.$q.defer();
283             let modalInstance:ng.ui.bootstrap.IModalServiceInstance;
284
285             let onFailed = () => {
286                 _.first(this.$scope.leftBarTabs.menuItems).isDisabled = false;//enabled click on general tab (DE246274)
287                 this.EventListenerService.notifyObservers(EVENTS.ON_WORKSPACE_SAVE_BUTTON_ERROR);
288                 this.progressService.deleteProgressValue(this.$scope.component.uniqueId);
289                 modalInstance && modalInstance.close();  // Close the modal in case it is opened.
290                 this.$scope.component.tags = _.without(this.$scope.component.tags, this.$scope.component.name);// for fix DE246217
291                 this.$scope.isCreateProgress = false;
292                 this.$scope.isLoading = false; // stop the progress.
293
294                 this.$scope.setValidState(true);  // Set the form valid (if sent form is valid, the error from server).
295                 if (!this.$scope.isCreateMode()) {
296                     this.$scope.component = this.ComponentFactory.createComponent(this.$scope.originComponent); // Set the component back to the original.
297                     this.enableMenuItems();  // Enable the menu items (left tabs), so user can press on them.
298                     this.$scope.disabledButtons = false;  // Enable "submit for testing" & checking buttons.
299                 }
300
301                 deferred.reject(false);
302             };
303
304             let onSuccessCreate = (component:Component) => {
305
306                 this.showSuccessNotificationMessage();
307                 this.progressService.deleteProgressValue(this.$scope.component.uniqueId);
308                 //update components for breadcrumbs
309                 this.components.unshift(component);
310                 this.$state.go(States.WORKSPACE_GENERAL, {
311                     id: component.uniqueId,
312                     type: component.componentType.toLowerCase(),
313                     components: this.components
314                 });
315
316                 deferred.resolve(true);
317             };
318
319             let onSuccessUpdate = (component:Component) => {
320                 this.$scope.isCreateProgress = false;
321                 this.$scope.disabledButtons = false;
322                 this.showSuccessNotificationMessage();
323                 this.progressService.deleteProgressValue(this.$scope.component.uniqueId);
324
325                 // Stop the circle loader.
326                 this.$scope.isLoading = false;
327
328                 component.tags = _.reject(component.tags, (item)=> {
329                     return item === component.name
330                 });
331
332                 // Update the components
333                 this.$scope.component = component;
334                 this.$scope.originComponent = this.ComponentFactory.createComponent(this.$scope.component);
335
336                 //update components for breadcrumbs
337                 this.components.unshift(component);
338
339                 // Enable left tags
340                 this.$scope.enabledTabs();
341
342
343                 if (this.$state.current.data) {
344                     this.$state.current.data.unsavedChanges = false;
345                 }
346
347                 deferred.resolve(true);
348             };
349
350             if (this.$scope.isCreateMode()) {
351                 this.$scope.progressMessage = "Creating Asset...";
352                 // CREATE MODE
353                 this.$scope.isCreateProgress = true;
354
355                 _.first(this.$scope.leftBarTabs.menuItems).isDisabled = true;//disabled click on general tab (DE246274)
356
357                 // Start creating the component
358                 this.ComponentFactory.createComponentOnServer(this.$scope.component).then(onSuccessCreate, onFailed);
359
360                 // In case we import CSAR. Notify user that import VF will take long time (the create is performed in the background).
361                 if (this.$scope.component.isResource() && (<Resource>this.$scope.component).csarUUID) {
362                     this.Notification.info({
363                         message: this.$filter('translate')("IMPORT_VF_MESSAGE_CREATE_TAKES_LONG_TIME_DESCRIPTION"),
364                         title: this.$filter('translate')("IMPORT_VF_MESSAGE_CREATE_TAKES_LONG_TIME_TITLE")
365                     });
366                 }
367             } else {
368                 // UPDATE MODE
369                 this.$scope.isCreateProgress = true;
370                 this.$scope.progressMessage = "Updating Asset...";
371                 this.disableMenuItems();
372
373
374                 // Work around to change the csar version
375                 if (this.cacheService.get(CHANGE_COMPONENT_CSAR_VERSION_FLAG)) {
376                     (<Resource>this.$scope.component).csarVersion = this.cacheService.get(CHANGE_COMPONENT_CSAR_VERSION_FLAG);
377                     this.cacheService.remove(CHANGE_COMPONENT_CSAR_VERSION_FLAG);
378                 }
379
380                 this.$scope.component.updateComponent().then(onSuccessUpdate, onFailed);
381             }
382             return deferred.promise;
383         };
384
385         this.$scope.revert = ():void => {
386             //in state of import file leave the file in place
387             if (this.$scope.component.isResource() && (<Resource>this.$scope.component).importedFile) {
388                 let tempFile:FileUploadModel = (<Resource>this.$scope.component).importedFile;
389                 this.$scope.component = this.ComponentFactory.createComponent(this.$scope.originComponent);
390                 (<Resource>this.$scope.component).importedFile = tempFile;
391             } else {
392                 this.$scope.component = this.ComponentFactory.createComponent(this.$scope.originComponent);
393             }
394
395         };
396
397         this.$scope.changeLifecycleState = (state:string):void => {
398             if (this.isNeedSave() && state !== 'deleteVersion') {
399                 this.$scope.save().then(() => {
400                     changeLifecycleState(state);
401                 })
402             } else {
403                 changeLifecycleState(state);
404             }
405         };
406
407         let defaultActionAfterChangeLifecycleState = ():void => {
408             if (this.$state.current.data && this.$state.current.data.unsavedChanges) {
409                 this.$state.current.data.unsavedChanges = false;
410             }
411             this.$state.go('dashboard');
412         };
413
414         let changeLifecycleState = (state:string) => {
415             if ('monitor' === state) {
416                 this.$state.go('workspace.distribution');
417                 return;
418             }
419
420             let data = this.$scope.changeLifecycleStateButtons[state];
421             let onSuccess = (component:Component, url:string):void => {
422                 //Updating the component from server response
423
424                 //the server returns only metaData (small component) except checkout (Full component)  ,so we update only the statuses of distribution & lifecycle
425                 this.$scope.component.lifecycleState = component.lifecycleState;
426                 this.$scope.component.distributionStatus = component.distributionStatus;
427
428                 switch (url) {
429                     case 'lifecycleState/CHECKOUT':
430                         // only checkOut get the full component from server
431                         //   this.$scope.component = component;
432                         // Work around to change the csar version
433                         if (this.cacheService.get(CHANGE_COMPONENT_CSAR_VERSION_FLAG)) {
434                             (<Resource>this.$scope.component).csarVersion = this.cacheService.get(CHANGE_COMPONENT_CSAR_VERSION_FLAG);
435                         }
436
437                         //when checking out a minor version uuid remains
438                         let bcComponent:Component = _.find(this.components, (item) => {
439                             return item.uuid === component.uuid;
440                         });
441                         if (bcComponent) {
442                             this.components[this.components.indexOf(bcComponent)] = component;
443                         } else {
444                             //when checking out a major(certified) version
445                             this.components.unshift(component);
446                         }
447                         // this.$state.go(this.$state.current.name, {
448                         //     id: component.uniqueId,
449                         //     type: component.componentType.toLowerCase(),
450                         //     components: this.components
451                         // });
452                         this.$scope.mode = this.initViewMode();
453                         this.initChangeLifecycleStateButtons();
454                         this.initVersionObject();
455                         this.$scope.isLoading = false;
456                         this.EventListenerService.notifyObservers(EVENTS.ON_CHECKOUT, component);
457                         this.Notification.success({
458                             message: this.$filter('translate')("CHECKOUT_SUCCESS_MESSAGE_TEXT"),
459                             title: this.$filter('translate')("CHECKOUT_SUCCESS_MESSAGE_TITLE")
460                         });
461                         break;
462                     case 'lifecycleState/CHECKIN':
463                         defaultActionAfterChangeLifecycleState();
464                         this.Notification.success({
465                             message: this.$filter('translate')("CHECKIN_SUCCESS_MESSAGE_TEXT"),
466                             title: this.$filter('translate')("CHECKIN_SUCCESS_MESSAGE_TITLE")
467                         });
468                         break;
469                     case 'lifecycleState/UNDOCHECKOUT':
470                         defaultActionAfterChangeLifecycleState();
471                         this.Notification.success({
472                             message: this.$filter('translate')("DELETE_SUCCESS_MESSAGE_TEXT"),
473                             title: this.$filter('translate')("DELETE_SUCCESS_MESSAGE_TITLE")
474                         });
475                         break;
476                     case 'lifecycleState/certificationRequest':
477                         defaultActionAfterChangeLifecycleState();
478                         this.Notification.success({
479                             message: this.$filter('translate')("SUBMIT_FOR_TESTING_SUCCESS_MESSAGE_TEXT"),
480                             title: this.$filter('translate')("SUBMIT_FOR_TESTING_SUCCESS_MESSAGE_TITLE")
481                         });
482                         break;
483                     //Tester Role
484                     case 'lifecycleState/failCertification':
485                         defaultActionAfterChangeLifecycleState();
486                         this.Notification.success({
487                             message: this.$filter('translate')("REJECT_SUCCESS_MESSAGE_TEXT"),
488                             title: this.$filter('translate')("REJECT_SUCCESS_MESSAGE_TITLE")
489                         });
490                         break;
491                     case 'lifecycleState/certify':
492                         defaultActionAfterChangeLifecycleState();
493                         this.Notification.success({
494                             message: this.$filter('translate')("ACCEPT_TESTING_SUCCESS_MESSAGE_TEXT"),
495                             title: this.$filter('translate')("ACCEPT_TESTING_SUCCESS_MESSAGE_TITLE")
496                         });
497                         break;
498                     //DE203504 Bug Fix Start
499                     case 'lifecycleState/startCertification':
500                         this.initChangeLifecycleStateButtons();
501                         this.Notification.success({
502                             message: this.$filter('translate')("START_TESTING_SUCCESS_MESSAGE_TEXT"),
503                             title: this.$filter('translate')("START_TESTING_SUCCESS_MESSAGE_TITLE")
504                         });
505                         break;
506                     case  'lifecycleState/cancelCertification':
507                         this.initChangeLifecycleStateButtons();
508                         this.Notification.success({
509                             message: this.$filter('translate')("CANCEL_TESTING_SUCCESS_MESSAGE_TEXT"),
510                             title: this.$filter('translate')("CANCEL_TESTING_SUCCESS_MESSAGE_TITLE")
511                         });
512                         break;
513                     //Ops Role
514                     case  'distribution/PROD/activate':
515                         this.initChangeLifecycleStateButtons();
516                         this.Notification.success({
517                             message: this.$filter('translate')("DISTRIBUTE_SUCCESS_MESSAGE_TEXT"),
518                             title: this.$filter('translate')("DISTRIBUTE_SUCCESS_MESSAGE_TITLE")
519                         });
520                         break;
521                     //Governor Role
522                     case  'distribution-state/reject':
523                         this.initChangeLifecycleStateButtons();
524                         this.Notification.success({
525                             message: this.$filter('translate')("REJECT_SUCCESS_MESSAGE_TEXT"),
526                             title: this.$filter('translate')("REJECT_SUCCESS_MESSAGE_TITLE")
527                         });
528                         break;
529                     case  'distribution-state/approve':
530                         this.initChangeLifecycleStateButtons();
531                         this.$state.go('catalog');
532                         this.Notification.success({
533                             message: this.$filter('translate')("APPROVE_SUCCESS_MESSAGE_TEXT"),
534                             title: this.$filter('translate')("APPROVE_SUCCESS_MESSAGE_TITLE")
535                         });
536                         break;
537                     //DE203504 Bug Fix End
538
539                     default :
540                         defaultActionAfterChangeLifecycleState();
541
542                 }
543                 if (data.url != 'lifecycleState/CHECKOUT') {
544                     this.$scope.isLoading = false;
545                 }
546             };
547             //this.$scope.isLoading = true;
548             this.ChangeLifecycleStateHandler.changeLifecycleState(this.$scope.component, data, this.$scope, onSuccess);
549         };
550
551         this.$scope.enabledTabs = ():void => {
552             this.$scope.leftBarTabs.menuItems.forEach((item:MenuItem) => {
553                 item.isDisabled = false;
554             });
555         };
556
557         this.$scope.isViewMode = ():boolean => {
558             return this.$scope.mode === WorkspaceMode.VIEW;
559         };
560
561         this.$scope.isDesigner = ():boolean => {
562             return this.role == Role.DESIGNER;
563         };
564
565         this.$scope.isDisableMode = ():boolean => {
566             return this.$scope.mode === WorkspaceMode.VIEW && this.$scope.component.lifecycleState === ComponentState.NOT_CERTIFIED_CHECKIN;
567         };
568
569         this.$scope.showFullIcons = ():boolean => {
570             //we show revert and save icons only in general view
571             return this.$state.current.name === States.WORKSPACE_GENERAL;
572         };
573
574         this.$scope.isCreateMode = ():boolean => {
575             return this.$scope.mode === WorkspaceMode.CREATE;
576         };
577
578         this.$scope.isEditMode = ():boolean => {
579             return this.$scope.mode === WorkspaceMode.EDIT;
580         };
581
582         this.$scope.goToBreadcrumbHome = ():void => {
583             let bcHome:MenuItemGroup = this.$scope.breadcrumbsModel[0];
584             this.$state.go(bcHome.menuItems[bcHome.selectedIndex].state);
585         };
586
587         this.$scope.showLifecycleIcon = ():boolean => {
588             return this.role == Role.DESIGNER;
589         };
590
591         this.$scope.getStatus = ():string => {
592             if (this.$scope.isCreateMode()) {
593                 return 'IN DESIGN';
594             }
595
596             return this.$scope.component.getStatus(this.sdcMenu);
597         };
598
599         this.initMenuItems();
600
601         this.$scope.showChangeStateButton = ():boolean => {
602             let result:boolean = true;
603             if (!this.$scope.component.isLatestVersion() && Role.OPS != this.role && Role.GOVERNOR != this.role) {
604                 result = false;
605             }
606             if (ComponentState.NOT_CERTIFIED_CHECKOUT === this.$scope.component.lifecycleState && this.$scope.isViewMode()) {
607                 result = false;
608             }
609             if (ComponentState.CERTIFIED != this.$scope.component.lifecycleState &&
610                 (Role.OPS == this.role || Role.GOVERNOR == this.role)) {
611                 result = false;
612             }
613             return result;
614         };
615
616         this.$scope.updateSelectedMenuItem = ():void => {
617             let selectedItem:MenuItem = _.find(this.$scope.leftBarTabs.menuItems, (item:MenuItem) => {
618                 return item.state === this.$state.current.name;
619             });
620             this.$scope.leftBarTabs.selectedIndex = selectedItem ? this.$scope.leftBarTabs.menuItems.indexOf(selectedItem) : 0;
621         };
622
623         this.$scope.$watch('$state.current.name', (newVal:string):void => {
624             if (newVal) {
625                 this.$scope.isComposition = (newVal.indexOf(States.WORKSPACE_COMPOSITION) > -1);
626                 this.$scope.isDeployment = newVal == States.WORKSPACE_DEPLOYMENT;
627             }
628         });
629
630         this.$scope.getTabTitle = ():string => {
631             return this.$scope.leftBarTabs.menuItems.find((menuItem:MenuItem)=>{
632                 return menuItem.state == this.$scope.$state.current.name;
633             }).text;
634         };
635
636         this.$scope.reload = (component:Component):void => {
637             this.$state.go(this.$state.current.name,{id:component.uniqueId},{reload:true});
638         };
639         
640     };
641
642     private initAfterScope = ():void => {
643         // In case user select csar from the onboarding modal, need to disable checkout and submit for testing.
644         if (this.$state.params['disableButtons'] === true) {
645             this.$scope.uploadFileChangedInGeneralTab();
646         }
647     };
648
649     private initVersionObject = ():void => {
650         this.$scope.versionsList = (this.$scope.component.getAllVersionsAsSortedArray()).reverse();
651         this.$scope.changeVersion = {
652             selectedVersion: _.find(this.$scope.versionsList, (versionObj)=> {
653                 return versionObj.versionId === this.$scope.component.uniqueId;
654             })
655         };
656     };
657
658     private getNewComponentBreadcrumbItem = ():MenuItem => {
659         let text = "";
660         if (this.$scope.component.isResource() && (<Resource>this.$scope.component).isCsarComponent()) {
661             text = this.$scope.component.getComponentSubType() + ': ' + this.$scope.component.name;
662         } else {
663             text = 'Create new ' + this.$state.params['type'];
664         }
665         return new MenuItem(text, null, States.WORKSPACE_GENERAL, 'goToState', [this.$state.params]);
666     };
667
668     private updateMenuItemByRole = (menuItems:Array<MenuItem>, role:string) => {
669         let tempMenuItems:Array<MenuItem> = new Array<MenuItem>();
670         menuItems.forEach((item:MenuItem) => {
671             //remove item if role is disabled
672             if (!(item.disabledRoles && item.disabledRoles.indexOf(role) > -1)) {
673                 tempMenuItems.push(item);
674             }
675         });
676         return tempMenuItems;
677     };
678
679     private initBreadcrumbs = () => {
680         this.components = this.cacheService.get('breadcrumbsComponents');
681         let breadcrumbsComponentsLvl = this.MenuHandler.generateBreadcrumbsModelFromComponents(this.components, this.$scope.component);
682
683         if (this.$scope.isCreateMode()) {
684             let createItem = this.getNewComponentBreadcrumbItem();
685             if (!breadcrumbsComponentsLvl.menuItems) {
686                 breadcrumbsComponentsLvl.menuItems = [];
687             }
688             breadcrumbsComponentsLvl.menuItems.unshift(createItem);
689             breadcrumbsComponentsLvl.selectedIndex = 0;
690         }
691
692         this.$scope.breadcrumbsModel = [breadcrumbsComponentsLvl, this.$scope.leftBarTabs];
693     };
694
695     private initMenuItems() {
696
697         let inCreateMode = this.$scope.isCreateMode();
698         this.$scope.leftBarTabs = new MenuItemGroup();
699         this.$scope.leftBarTabs.menuItems = this.updateMenuItemByRole(this.sdcMenu.component_workspace_menu_option[this.$scope.component.getComponentSubType()], this.role);
700
701         this.$scope.leftBarTabs.menuItems.forEach((item:MenuItem) => {
702             item.params = [item.state];
703             item.callback = this.$scope.onMenuItemPressed;
704             item.isDisabled = (inCreateMode && States.WORKSPACE_GENERAL != item.state) ||
705                 (States.WORKSPACE_DEPLOYMENT === item.state && this.$scope.component.groups && this.$scope.component.groups.length === 0 && this.$scope.component.isResource());
706         });
707
708         if (this.cacheService.get('breadcrumbsComponents')) {
709             this.initBreadcrumbs();
710         } else {
711             let onSuccess = (components:Array<Component>) => {
712                 this.cacheService.set('breadcrumbsComponents', components);
713                 this.initBreadcrumbs();
714             };
715             this.EntityService.getCatalog().then(onSuccess); //getAllComponents() doesnt return components from catalog
716         }
717     }
718
719     private disableMenuItems() {
720         this.$scope.leftBarTabs.menuItems.forEach((item:MenuItem) => {
721             item.params = [item.state];
722             item.callback = this.$scope.onMenuItemPressed;
723             item.isDisabled = (States.WORKSPACE_GENERAL != item.state);
724         });
725     }
726
727     private enableMenuItems() {
728         this.$scope.leftBarTabs.menuItems.forEach((item:MenuItem) => {
729             item.params = [item.state];
730             item.callback = this.$scope.onMenuItemPressed;
731             item.isDisabled = false;
732         });
733     }
734
735     private showSuccessNotificationMessage = ():void => {
736         this.Notification.success({
737             message: this.$filter('translate')("IMPORT_VF_MESSAGE_CREATE_FINISHED_DESCRIPTION"),
738             title: this.$filter('translate')("IMPORT_VF_MESSAGE_CREATE_FINISHED_TITLE")
739         });
740     };
741
742 }
743
744