2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * 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=========================================================
20 import { ServiceServiceNg2 } from 'app/ng2/services/component-services/service.service';
21 import { EventBusService } from 'app/ng2/services/event-bus.service';
22 import { EVENTS, ValidationUtils } from 'app/utils';
23 import { SdcUiCommon, SdcUiComponents, SdcUiServices } from 'onap-ui-angular';
24 import { Component, IAppConfigurtaion, IAppMenu, Service } from '../models';
25 import { AsdcComment } from '../models/comments';
26 import { CommentModalComponent } from '../ng2/components/modals/comment-modal/comment-modal.component';
27 import { EventListenerService } from '../services/event-listener-service';
28 import { ComponentFactory } from './component-factory';
29 import { ModalsHandler } from './modals-handler';
31 export class ChangeLifecycleStateHandler {
43 'EventListenerService'
46 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) {
58 public changeLifecycleState = (component: Component, data: any, scope: any, onSuccessCallback?: Function, onErrorCallback?: Function) => {
59 if (data.conformanceLevelModal) {
60 this.validateConformanceLevel(component, data, scope, onSuccessCallback, onErrorCallback);
62 this.actualChangeLifecycleState(component, data, scope, onSuccessCallback, onErrorCallback);
66 private actualChangeLifecycleState = (component: Component, data: any, scope: any, onSuccessCallback?: Function, onErrorCallback?: Function) => {
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);
78 const onError = (error) => {
79 scope.isLoading = false;
80 if (onErrorCallback) {
81 onErrorCallback(error);
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);
94 const modalTitle = this.sdcMenu.alertMessages[data.alertModal].title;
95 const modalMessage = this.sdcMenu.alertMessages[data.alertModal].message.format([component.componentType.toLowerCase()]);
98 text: this.sdcMenu.alertMessages.okButton,
99 type: SdcUiCommon.ButtonType.warning,
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;
108 const confirmationText: string = commentModalInstance.innerModalContent.instance.comment.text;
109 commentModalInstance.closeModal();
110 comment.userRemarks = this.validationUtils.stripAndSanitize(confirmationText);
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);
118 scope.isLoading = true;
119 component.changeLifecycleState(data.url, comment).then(onSuccess, onError);
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 = {
128 type: SdcUiCommon.ModalType.custom,
129 testId: 'confirm-modal',
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;
140 // Submit to server only (no modal is shown).
141 scope.isLoading = true;
142 component.changeLifecycleState(data.url, comment).then(onSuccess, onError);
146 private validateConformanceLevel = (component: Component, data: any, scope: any, onSuccessCallback?: Function, onErrorCallback?: Function) => {
147 // Validate conformance level if defined in menu.json
148 this.serviceServiceNg2.validateConformanceLevel(component as Service).subscribe((res: boolean) => {
150 // Conformance level is ok - continue
151 this.actualChangeLifecycleState(component, data, scope, onSuccessCallback, onErrorCallback);
153 // Show warning modal
154 const onContinue: Function = () => {
155 this.actualChangeLifecycleState(component, data, scope, onSuccessCallback, onErrorCallback);
157 const reject: Function = () => {
158 this.actualChangeLifecycleState(component, data.conformanceLevelModal, scope, onSuccessCallback, onErrorCallback);
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]);