blob: 6a37864fe186a2ff9f6ddafe800c807936bf55a8 [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 */
ys969316a9fce2020-01-19 13:50:02 +020020import { ServiceServiceNg2 } from 'app/ng2/services/component-services/service.service';
21import { EventBusService } from 'app/ng2/services/event-bus.service';
22import { EVENTS, ValidationUtils } from 'app/utils';
23import { SdcUiCommon, SdcUiComponents, SdcUiServices } from 'onap-ui-angular';
24import { Component, IAppConfigurtaion, IAppMenu, Service } from '../models';
25import { AsdcComment } from '../models/comments';
26import { CommentModalComponent } from '../ng2/components/modals/comment-modal/comment-modal.component';
27import { EventListenerService } from '../services/event-listener-service';
28import { ComponentFactory } from './component-factory';
29import { ModalsHandler } from './modals-handler';
Michael Landoed64b5e2017-06-09 03:19:04 +030030
31export class ChangeLifecycleStateHandler {
32
33 static '$inject' = [
34 'sdcConfig',
35 'sdcMenu',
36 'ComponentFactory',
37 '$filter',
38 'ModalsHandler',
Idan Amit6187c942018-04-15 19:19:08 +030039 'ServiceServiceNg2',
ys969316a9fce2020-01-19 13:50:02 +020040 'EventBusService',
41 'ModalServiceSdcUI',
42 'ValidationUtils',
43 'EventListenerService'
Michael Landoed64b5e2017-06-09 03:19:04 +030044 ];
45
ys969316a9fce2020-01-19 13:50:02 +020046 constructor(private sdcConfig: IAppConfigurtaion,
47 private sdcMenu: IAppMenu,
48 private componentFactory: ComponentFactory,
49 private $filter: ng.IFilterService,
50 private modalsHandler: ModalsHandler,
51 private serviceServiceNg2: ServiceServiceNg2,
52 private eventBusService: EventBusService,
53 private modalService: SdcUiServices.ModalService,
54 private validationUtils: ValidationUtils,
55 private eventListenerService: EventListenerService) {
Michael Landoed64b5e2017-06-09 03:19:04 +030056 }
57
ys969316a9fce2020-01-19 13:50:02 +020058 public changeLifecycleState = (component: Component, data: any, scope: any, onSuccessCallback?: Function, onErrorCallback?: Function) => {
Michael Landoed64b5e2017-06-09 03:19:04 +030059 if (data.conformanceLevelModal) {
60 this.validateConformanceLevel(component, data, scope, onSuccessCallback, onErrorCallback);
61 } else {
62 this.actualChangeLifecycleState(component, data, scope, onSuccessCallback, onErrorCallback);
63 }
64 }
65
ys969316a9fce2020-01-19 13:50:02 +020066 private actualChangeLifecycleState = (component: Component, data: any, scope: any, onSuccessCallback?: Function, onErrorCallback?: Function) => {
67 const self = this;
68
69 const onSuccess = (newComponent: Component) => {
70 if (onSuccessCallback) {
71 onSuccessCallback(self.componentFactory.createComponent(newComponent), data.url);
72 if (data.url === 'distribution/PROD/activate') {
73 this.eventListenerService.notifyObservers(EVENTS.ON_DISTRIBUTION_SUCCESS);
74 }
75 }
76 };
77
78 const onError = (error) => {
79 scope.isLoading = false;
80 if (onErrorCallback) {
81 onErrorCallback(error);
82 }
83 };
84
85 const comment: AsdcComment = new AsdcComment();
86 if (data.alertModal) {
87 // Show alert dialog if defined in menu.json
88 const onOk: Function = (confirmationText) => {
89 comment.userRemarks = confirmationText;
90 scope.isLoading = true;
91 component.changeLifecycleState(data.url, comment).then(onSuccess, onError);
92 };
93
94 const modalTitle = this.sdcMenu.alertMessages[data.alertModal].title;
95 const modalMessage = this.sdcMenu.alertMessages[data.alertModal].message.format([component.componentType.toLowerCase()]);
96 const modalButton = {
97 testId: 'OK',
98 text: this.sdcMenu.alertMessages.okButton,
99 type: SdcUiCommon.ButtonType.warning,
100 callback: onOk,
101 closeModal: true
102 } as SdcUiComponents.ModalButtonComponent;
103 this.modalService.openWarningModal(modalTitle, modalMessage, 'alert-modal', [modalButton]);
104 } else if (data.confirmationModal) {
105 // Show confirmation dialog if defined in menu.json
106 let commentModalInstance: SdcUiComponents.ModalComponent;
107 const onOk = () => {
108 const confirmationText: string = commentModalInstance.innerModalContent.instance.comment.text;
109 commentModalInstance.closeModal();
110 comment.userRemarks = this.validationUtils.stripAndSanitize(confirmationText);
111
112 if (data.url === 'lifecycleState/CHECKIN') {
113 this.eventBusService.notify('CHECK_IN').subscribe(() => {
114 scope.isLoading = true;
115 component.changeLifecycleState(data.url, comment).then(onSuccess, onError);
116 });
117 } else {
118 scope.isLoading = true;
119 component.changeLifecycleState(data.url, comment).then(onSuccess, onError);
120 }
121 };
122
123 const modalTitle = this.sdcMenu.confirmationMessages[data.confirmationModal].title;
124 const modalMessage = this.sdcMenu.confirmationMessages[data.confirmationModal].message.format([component.componentType.toLowerCase()]);
125 const modalConfig = {
126 size: 'md',
127 title: modalTitle,
128 type: SdcUiCommon.ModalType.custom,
129 testId: 'confirm-modal',
130 buttons: [
131 { id: 'OK', text: 'OK', callback: onOk, closeModal: false, testId: 'OK' },
132 { id: 'cancel', text: 'Cancel', size: 'x-small', type: 'secondary', closeModal: true, testId: 'Cancel' }
133 ] as SdcUiCommon.IModalButtonComponent[]
134 } as SdcUiCommon.IModalConfig;
135 commentModalInstance = this.modalService.openCustomModal(modalConfig, CommentModalComponent, { message: modalMessage });
136 commentModalInstance.innerModalContent.instance.onValidationChange.subscribe((isValid) => {
137 commentModalInstance.getButtonById('OK').disabled = !isValid;
138 });
139 } else {
140 // Submit to server only (no modal is shown).
141 scope.isLoading = true;
142 component.changeLifecycleState(data.url, comment).then(onSuccess, onError);
143 }
144 }
145
146 private validateConformanceLevel = (component: Component, data: any, scope: any, onSuccessCallback?: Function, onErrorCallback?: Function) => {
Michael Landoed64b5e2017-06-09 03:19:04 +0300147 // Validate conformance level if defined in menu.json
ys969316a9fce2020-01-19 13:50:02 +0200148 this.serviceServiceNg2.validateConformanceLevel(component as Service).subscribe((res: boolean) => {
Michael Landoed64b5e2017-06-09 03:19:04 +0300149 if (res === true) {
ys969316a9fce2020-01-19 13:50:02 +0200150 // Conformance level is ok - continue
Michael Landoed64b5e2017-06-09 03:19:04 +0300151 this.actualChangeLifecycleState(component, data, scope, onSuccessCallback, onErrorCallback);
Michael Landoed64b5e2017-06-09 03:19:04 +0300152 } else {
ys969316a9fce2020-01-19 13:50:02 +0200153 // Show warning modal
154 const onContinue: Function = () => {
155 this.actualChangeLifecycleState(component, data, scope, onSuccessCallback, onErrorCallback);
156 };
157 const reject: Function = () => {
158 this.actualChangeLifecycleState(component, data.conformanceLevelModal, scope, onSuccessCallback, onErrorCallback);
159 };
160 const continueButton = {testId: 'Continue', text: 'Continue', type: SdcUiCommon.ButtonType.primary, callback: onContinue, closeModal: true} as SdcUiComponents.ModalButtonComponent;
161 const rejectButton = {testId: 'Reject', text: 'Reject', type: SdcUiCommon.ButtonType.secondary, callback: reject, closeModal: true} as SdcUiComponents.ModalButtonComponent;
162 this.modalService.openInfoModal(this.$filter('translate')('CONFORMANCE_LEVEL_MODAL_TITLE'),
163 this.$filter('translate')('CONFORMANCE_LEVEL_MODAL_TEXT'), 'conformance-modal', [continueButton, rejectButton]);
Michael Landoed64b5e2017-06-09 03:19:04 +0300164 }
165 });
166 }
167}