blob: dc59e3bb98f23bb4bf325251309f939544142f70 [file] [log] [blame]
Michael Landoed64b5e2017-06-09 03:19:04 +03001import {ComponentFactory} from "./component-factory";
2import {Component, Service,IAppMenu, IAppConfigurtaion} from "../models";
3import {IEmailModalModel, IEmailModalModel_Email, IEmailModalModel_Data} from "../view-models/modals/email-modal/email-modal-view-model";
4import {AsdcComment} from "../models/comments";
5import {ModalsHandler} from "./modals-handler";
6import {ServiceServiceNg2} from "../ng2/services/component-services/service.service";
7
8/**
9 * Created by obarda on 2/11/2016.
10 */
11
12export class ChangeLifecycleStateHandler {
13
14 static '$inject' = [
15 'sdcConfig',
16 'sdcMenu',
17 'ComponentFactory',
18 '$filter',
19 'ModalsHandler',
20 'ServiceServiceNg2'
21 ];
22
23 constructor(private sdcConfig:IAppConfigurtaion,
24 private sdcMenu:IAppMenu,
25 private ComponentFactory:ComponentFactory,
26 private $filter:ng.IFilterService,
27 private ModalsHandler:ModalsHandler,
28 private ServiceServiceNg2:ServiceServiceNg2) {
29
30 }
31
32 private actualChangeLifecycleState = (component:Component, data:any, scope:any, onSuccessCallback?:Function, onErrorCallback?:Function):void => {
33
34 let self = this;
35
36 let getContacts = (component:Component):string => {
37 let testers = this.sdcConfig.testers;
38 let result:string = testers[component.componentType][component.categories[0].name] ?
39 testers[component.componentType][component.categories[0].name] :
40 testers[component.componentType]['default'];
41 return result;
42 };
43
44 let onSuccess = (newComponent:Component):void => {
45 //scope.isLoading = false;
46 console.info(component.componentType.toLowerCase + ' change state ', newComponent);
47 if (onSuccessCallback) {
48 onSuccessCallback(self.ComponentFactory.createComponent(newComponent), data.url);
49 }
50 };
51
52 let onError = (error):void => {
53 scope.isLoading = false;
54 console.info('Failed to changeLifecycleState to ', data.url);
55 if (onErrorCallback) {
56 onErrorCallback(error);
57 }
58 };
59
60 let comment:AsdcComment = new AsdcComment();
61 if (data.alertModal) {
62 // Show alert dialog if defined in menu.json
63 //-------------------------------------------------
64 let onOk = (confirmationText):void => {
65 comment.userRemarks = confirmationText;
66 scope.isLoading = true;
67 component.changeLifecycleState(data.url, comment).then(onSuccess, onError);
68 };
69
70 let onCancel = ():void => {
71 console.info('Cancel pressed');
72 scope.isLoading = false;
73 };
74
75 let modalTitle = this.sdcMenu.alertMessages[data.alertModal].title;
76 let modalMessage = this.sdcMenu.alertMessages[data.alertModal].message.format([component.componentType.toLowerCase()]);
77 this.ModalsHandler.openAlertModal(modalTitle, modalMessage).then(onOk, onCancel);
78 } else if (data.confirmationModal) {
79 // Show confirmation dialog if defined in menu.json
80 //-------------------------------------------------
81 let onOk = (confirmationText):void => {
82 comment.userRemarks = confirmationText;
83 scope.isLoading = true;
84 component.changeLifecycleState(data.url, comment).then(onSuccess, onError);
85 };
86
87 let onCancel = ():void => {
88 console.info('Cancel pressed');
89 scope.isLoading = false;
90 };
91
92 let modalTitle = this.sdcMenu.confirmationMessages[data.confirmationModal].title;
93 let modalMessage = this.sdcMenu.confirmationMessages[data.confirmationModal].message.format([component.componentType.toLowerCase()]);
94 let modalShowComment = this.sdcMenu.confirmationMessages[data.confirmationModal].showComment;
95 this.ModalsHandler.openConfirmationModal(modalTitle, modalMessage, modalShowComment).then(onOk, onCancel);
96
97 } else if (data.emailModal) {
98 // Show email dialog if defined in menu.json
99 //-------------------------------------------------
100 let onOk = (resource):void => {
101 if (resource) {
102 onSuccess(resource);
103 } else {
104 onError("Error changing life cycle state");
105 }
106 };
107
108 let onCancel = ():void => {
109 scope.isLoading = false;
110 };
111
112 let emailModel:IEmailModalModel = <IEmailModalModel>{};
113 emailModel.email = <IEmailModalModel_Email>{};
114 emailModel.data = <IEmailModalModel_Data>{};
115 emailModel.title = this.$filter('translate')("EMAIL_MODAL_TITLE");
116 emailModel.email.to = getContacts(component);
117 emailModel.email.subject = this.$filter('translate')("EMAIL_MODAL_SUBJECT", "{'entityName': '" + this.$filter('resourceName')(component.name) + "','entityVersion': '" + component.version + "'}");
118 emailModel.email.message = '';
119 emailModel.data.component = component;
120 emailModel.data.stateUrl = data.url;
121
122 this.ModalsHandler.openEmailModal(emailModel).then(onOk, onCancel);
123
124 } else {
125 // Submit to server only (no modal is shown).
126 scope.isLoading = true;
127 component.changeLifecycleState(data.url, comment).then(onSuccess, onError);
128 }
129
130 }
131
132 public changeLifecycleState = (component:Component, data:any, scope:any, onSuccessCallback?:Function, onErrorCallback?:Function):void => {
133
134 if (data.conformanceLevelModal) {
135 this.validateConformanceLevel(component, data, scope, onSuccessCallback, onErrorCallback);
136 } else {
137 this.actualChangeLifecycleState(component, data, scope, onSuccessCallback, onErrorCallback);
138 }
139 }
140
141 private validateConformanceLevel = (component:Component, data:any, scope:any, onSuccessCallback?:Function, onErrorCallback?:Function):void => {
142 // Validate conformance level if defined in menu.json
143 //-------------------------------------------------
144 this.ServiceServiceNg2.validateConformanceLevel(<Service>component).subscribe((res:boolean) => {
145 if (res === true) {
146 //conformance level is ok - continue
147 this.actualChangeLifecycleState(component, data, scope, onSuccessCallback, onErrorCallback);
148
149 } else {
150 //show warning modal
151 this.ModalsHandler.openConformanceLevelModal()
152 .then(() => {
153 //continue distribute
154 this.actualChangeLifecycleState(component, data, scope, onSuccessCallback, onErrorCallback);
155
156 }).catch(() => {
157 //reject distribution
158 this.actualChangeLifecycleState(component, data.conformanceLevelModal, scope, onSuccessCallback, onErrorCallback);
159 });
160 }
161 });
162 }
163}