blob: f1c6544351d8503c60741a452f067ffc70f5851b [file] [log] [blame]
Michael Landodd603392017-07-12 00:54:52 +03001/*-
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
Idan Amit6187c942018-04-15 19:19:08 +030010 *
Michael Landodd603392017-07-12 00:54:52 +030011 * http://www.apache.org/licenses/LICENSE-2.0
Idan Amit6187c942018-04-15 19:19:08 +030012 *
Michael Landodd603392017-07-12 00:54:52 +030013 * 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
Michael Landoed64b5e2017-06-09 03:19:04 +030021import {ComponentFactory} from "./component-factory";
22import {Component, Service,IAppMenu, IAppConfigurtaion} from "../models";
23import {IEmailModalModel, IEmailModalModel_Email, IEmailModalModel_Data} from "../view-models/modals/email-modal/email-modal-view-model";
24import {AsdcComment} from "../models/comments";
25import {ModalsHandler} from "./modals-handler";
26import {ServiceServiceNg2} from "../ng2/services/component-services/service.service";
Idan Amit6187c942018-04-15 19:19:08 +030027import {EventBusService} from "../ng2/services/event-bus.service";
Michael Landoed64b5e2017-06-09 03:19:04 +030028
29/**
30 * Created by obarda on 2/11/2016.
31 */
32
33export class ChangeLifecycleStateHandler {
34
35 static '$inject' = [
36 'sdcConfig',
37 'sdcMenu',
38 'ComponentFactory',
39 '$filter',
40 'ModalsHandler',
Idan Amit6187c942018-04-15 19:19:08 +030041 'ServiceServiceNg2',
42 'EventBusService'
Michael Landoed64b5e2017-06-09 03:19:04 +030043 ];
44
45 constructor(private sdcConfig:IAppConfigurtaion,
46 private sdcMenu:IAppMenu,
47 private ComponentFactory:ComponentFactory,
48 private $filter:ng.IFilterService,
49 private ModalsHandler:ModalsHandler,
Idan Amit6187c942018-04-15 19:19:08 +030050 private ServiceServiceNg2:ServiceServiceNg2,
51 private eventBusService:EventBusService) {
Michael Landoed64b5e2017-06-09 03:19:04 +030052
53 }
54
55 private actualChangeLifecycleState = (component:Component, data:any, scope:any, onSuccessCallback?:Function, onErrorCallback?:Function):void => {
56
57 let self = this;
58
59 let getContacts = (component:Component):string => {
60 let testers = this.sdcConfig.testers;
61 let result:string = testers[component.componentType][component.categories[0].name] ?
62 testers[component.componentType][component.categories[0].name] :
63 testers[component.componentType]['default'];
64 return result;
65 };
66
67 let onSuccess = (newComponent:Component):void => {
68 //scope.isLoading = false;
69 console.info(component.componentType.toLowerCase + ' change state ', newComponent);
70 if (onSuccessCallback) {
71 onSuccessCallback(self.ComponentFactory.createComponent(newComponent), data.url);
72 }
73 };
74
75 let onError = (error):void => {
76 scope.isLoading = false;
77 console.info('Failed to changeLifecycleState to ', data.url);
78 if (onErrorCallback) {
79 onErrorCallback(error);
80 }
81 };
82
83 let comment:AsdcComment = new AsdcComment();
84 if (data.alertModal) {
85 // Show alert dialog if defined in menu.json
86 //-------------------------------------------------
87 let onOk = (confirmationText):void => {
88 comment.userRemarks = confirmationText;
89 scope.isLoading = true;
90 component.changeLifecycleState(data.url, comment).then(onSuccess, onError);
91 };
92
93 let onCancel = ():void => {
94 console.info('Cancel pressed');
95 scope.isLoading = false;
96 };
97
98 let modalTitle = this.sdcMenu.alertMessages[data.alertModal].title;
99 let modalMessage = this.sdcMenu.alertMessages[data.alertModal].message.format([component.componentType.toLowerCase()]);
100 this.ModalsHandler.openAlertModal(modalTitle, modalMessage).then(onOk, onCancel);
101 } else if (data.confirmationModal) {
102 // Show confirmation dialog if defined in menu.json
103 //-------------------------------------------------
104 let onOk = (confirmationText):void => {
105 comment.userRemarks = confirmationText;
106 scope.isLoading = true;
Idan Amit6187c942018-04-15 19:19:08 +0300107
108 if (data.url === "lifecycleState/CHECKIN") {
109 this.eventBusService.notify("CHECK_IN").subscribe(() => {
110 component.changeLifecycleState(data.url, comment).then(onSuccess, onError);
111 });
112 }
Michael Landoed64b5e2017-06-09 03:19:04 +0300113 };
114
115 let onCancel = ():void => {
116 console.info('Cancel pressed');
117 scope.isLoading = false;
118 };
119
120 let modalTitle = this.sdcMenu.confirmationMessages[data.confirmationModal].title;
121 let modalMessage = this.sdcMenu.confirmationMessages[data.confirmationModal].message.format([component.componentType.toLowerCase()]);
122 let modalShowComment = this.sdcMenu.confirmationMessages[data.confirmationModal].showComment;
123 this.ModalsHandler.openConfirmationModal(modalTitle, modalMessage, modalShowComment).then(onOk, onCancel);
124
125 } else if (data.emailModal) {
126 // Show email dialog if defined in menu.json
127 //-------------------------------------------------
128 let onOk = (resource):void => {
Idan Amit6187c942018-04-15 19:19:08 +0300129 if (data.url === "lifecycleState/certificationRequest") {
130 this.eventBusService.notify("SUBMIT_FOR_TESTING").subscribe(() => {
131 if (resource) {
132 onSuccess(resource);
133 } else {
134 onError("Error changing life cycle state");
135 }
136 });
Michael Landoed64b5e2017-06-09 03:19:04 +0300137 }
138 };
139
140 let onCancel = ():void => {
141 scope.isLoading = false;
142 };
143
144 let emailModel:IEmailModalModel = <IEmailModalModel>{};
145 emailModel.email = <IEmailModalModel_Email>{};
146 emailModel.data = <IEmailModalModel_Data>{};
147 emailModel.title = this.$filter('translate')("EMAIL_MODAL_TITLE");
148 emailModel.email.to = getContacts(component);
149 emailModel.email.subject = this.$filter('translate')("EMAIL_MODAL_SUBJECT", "{'entityName': '" + this.$filter('resourceName')(component.name) + "','entityVersion': '" + component.version + "'}");
150 emailModel.email.message = '';
151 emailModel.data.component = component;
152 emailModel.data.stateUrl = data.url;
153
154 this.ModalsHandler.openEmailModal(emailModel).then(onOk, onCancel);
155
156 } else {
157 // Submit to server only (no modal is shown).
158 scope.isLoading = true;
159 component.changeLifecycleState(data.url, comment).then(onSuccess, onError);
160 }
161
162 }
163
164 public changeLifecycleState = (component:Component, data:any, scope:any, onSuccessCallback?:Function, onErrorCallback?:Function):void => {
165
166 if (data.conformanceLevelModal) {
167 this.validateConformanceLevel(component, data, scope, onSuccessCallback, onErrorCallback);
168 } else {
169 this.actualChangeLifecycleState(component, data, scope, onSuccessCallback, onErrorCallback);
170 }
171 }
172
173 private validateConformanceLevel = (component:Component, data:any, scope:any, onSuccessCallback?:Function, onErrorCallback?:Function):void => {
174 // Validate conformance level if defined in menu.json
175 //-------------------------------------------------
176 this.ServiceServiceNg2.validateConformanceLevel(<Service>component).subscribe((res:boolean) => {
177 if (res === true) {
178 //conformance level is ok - continue
179 this.actualChangeLifecycleState(component, data, scope, onSuccessCallback, onErrorCallback);
180
181 } else {
182 //show warning modal
183 this.ModalsHandler.openConformanceLevelModal()
184 .then(() => {
185 //continue distribute
186 this.actualChangeLifecycleState(component, data, scope, onSuccessCallback, onErrorCallback);
187
188 }).catch(() => {
189 //reject distribution
190 this.actualChangeLifecycleState(component, data.conformanceLevelModal, scope, onSuccessCallback, onErrorCallback);
191 });
192 }
193 });
194 }
195}