1 import {ComponentFactory} from "./component-factory";
2 import {Component, Service,IAppMenu, IAppConfigurtaion} from "../models";
3 import {IEmailModalModel, IEmailModalModel_Email, IEmailModalModel_Data} from "../view-models/modals/email-modal/email-modal-view-model";
4 import {AsdcComment} from "../models/comments";
5 import {ModalsHandler} from "./modals-handler";
6 import {ServiceServiceNg2} from "../ng2/services/component-services/service.service";
9 * Created by obarda on 2/11/2016.
12 export class ChangeLifecycleStateHandler {
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) {
32 private actualChangeLifecycleState = (component:Component, data:any, scope:any, onSuccessCallback?:Function, onErrorCallback?:Function):void => {
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'];
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);
52 let onError = (error):void => {
53 scope.isLoading = false;
54 console.info('Failed to changeLifecycleState to ', data.url);
55 if (onErrorCallback) {
56 onErrorCallback(error);
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);
70 let onCancel = ():void => {
71 console.info('Cancel pressed');
72 scope.isLoading = false;
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);
87 let onCancel = ():void => {
88 console.info('Cancel pressed');
89 scope.isLoading = false;
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);
97 } else if (data.emailModal) {
98 // Show email dialog if defined in menu.json
99 //-------------------------------------------------
100 let onOk = (resource):void => {
104 onError("Error changing life cycle state");
108 let onCancel = ():void => {
109 scope.isLoading = false;
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;
122 this.ModalsHandler.openEmailModal(emailModel).then(onOk, onCancel);
125 // Submit to server only (no modal is shown).
126 scope.isLoading = true;
127 component.changeLifecycleState(data.url, comment).then(onSuccess, onError);
132 public changeLifecycleState = (component:Component, data:any, scope:any, onSuccessCallback?:Function, onErrorCallback?:Function):void => {
134 if (data.conformanceLevelModal) {
135 this.validateConformanceLevel(component, data, scope, onSuccessCallback, onErrorCallback);
137 this.actualChangeLifecycleState(component, data, scope, onSuccessCallback, onErrorCallback);
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) => {
146 //conformance level is ok - continue
147 this.actualChangeLifecycleState(component, data, scope, onSuccessCallback, onErrorCallback);
151 this.ModalsHandler.openConformanceLevelModal()
153 //continue distribute
154 this.actualChangeLifecycleState(component, data, scope, onSuccessCallback, onErrorCallback);
157 //reject distribution
158 this.actualChangeLifecycleState(component, data.conformanceLevelModal, scope, onSuccessCallback, onErrorCallback);