blob: 143c3c5360885fd8fe7566a4f1bd23d9551e66a5 [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 }
Idan Amit5668d532018-04-23 18:43:58 +0300113 else {
114 component.changeLifecycleState(data.url, comment).then(onSuccess, onError);
115 }
Michael Landoed64b5e2017-06-09 03:19:04 +0300116 };
117
118 let onCancel = ():void => {
119 console.info('Cancel pressed');
120 scope.isLoading = false;
121 };
122
123 let modalTitle = this.sdcMenu.confirmationMessages[data.confirmationModal].title;
124 let modalMessage = this.sdcMenu.confirmationMessages[data.confirmationModal].message.format([component.componentType.toLowerCase()]);
125 let modalShowComment = this.sdcMenu.confirmationMessages[data.confirmationModal].showComment;
126 this.ModalsHandler.openConfirmationModal(modalTitle, modalMessage, modalShowComment).then(onOk, onCancel);
127
128 } else if (data.emailModal) {
129 // Show email dialog if defined in menu.json
130 //-------------------------------------------------
131 let onOk = (resource):void => {
Idan Amit6187c942018-04-15 19:19:08 +0300132 if (data.url === "lifecycleState/certificationRequest") {
133 this.eventBusService.notify("SUBMIT_FOR_TESTING").subscribe(() => {
134 if (resource) {
135 onSuccess(resource);
136 } else {
137 onError("Error changing life cycle state");
138 }
139 });
Michael Landoed64b5e2017-06-09 03:19:04 +0300140 }
Idan Amit5668d532018-04-23 18:43:58 +0300141 else {
142 if (resource) {
143 onSuccess(resource);
144 } else {
145 onError("Error changing life cycle state");
146 }
147 }
Michael Landoed64b5e2017-06-09 03:19:04 +0300148 };
149
150 let onCancel = ():void => {
151 scope.isLoading = false;
152 };
153
154 let emailModel:IEmailModalModel = <IEmailModalModel>{};
155 emailModel.email = <IEmailModalModel_Email>{};
156 emailModel.data = <IEmailModalModel_Data>{};
157 emailModel.title = this.$filter('translate')("EMAIL_MODAL_TITLE");
158 emailModel.email.to = getContacts(component);
159 emailModel.email.subject = this.$filter('translate')("EMAIL_MODAL_SUBJECT", "{'entityName': '" + this.$filter('resourceName')(component.name) + "','entityVersion': '" + component.version + "'}");
160 emailModel.email.message = '';
161 emailModel.data.component = component;
162 emailModel.data.stateUrl = data.url;
163
164 this.ModalsHandler.openEmailModal(emailModel).then(onOk, onCancel);
165
166 } else {
167 // Submit to server only (no modal is shown).
168 scope.isLoading = true;
169 component.changeLifecycleState(data.url, comment).then(onSuccess, onError);
170 }
171
172 }
173
174 public changeLifecycleState = (component:Component, data:any, scope:any, onSuccessCallback?:Function, onErrorCallback?:Function):void => {
175
176 if (data.conformanceLevelModal) {
177 this.validateConformanceLevel(component, data, scope, onSuccessCallback, onErrorCallback);
178 } else {
179 this.actualChangeLifecycleState(component, data, scope, onSuccessCallback, onErrorCallback);
180 }
181 }
182
183 private validateConformanceLevel = (component:Component, data:any, scope:any, onSuccessCallback?:Function, onErrorCallback?:Function):void => {
184 // Validate conformance level if defined in menu.json
185 //-------------------------------------------------
186 this.ServiceServiceNg2.validateConformanceLevel(<Service>component).subscribe((res:boolean) => {
187 if (res === true) {
188 //conformance level is ok - continue
189 this.actualChangeLifecycleState(component, data, scope, onSuccessCallback, onErrorCallback);
190
191 } else {
192 //show warning modal
193 this.ModalsHandler.openConformanceLevelModal()
194 .then(() => {
195 //continue distribute
196 this.actualChangeLifecycleState(component, data, scope, onSuccessCallback, onErrorCallback);
197
198 }).catch(() => {
199 //reject distribution
200 this.actualChangeLifecycleState(component, data.conformanceLevelModal, scope, onSuccessCallback, onErrorCallback);
201 });
202 }
203 });
204 }
205}