Michael Lando | dd60339 | 2017-07-12 00:54:52 +0300 | [diff] [blame] | 1 | /*- |
| 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 Amit | 6187c94 | 2018-04-15 19:19:08 +0300 | [diff] [blame] | 10 | * |
Michael Lando | dd60339 | 2017-07-12 00:54:52 +0300 | [diff] [blame] | 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
Idan Amit | 6187c94 | 2018-04-15 19:19:08 +0300 | [diff] [blame] | 12 | * |
Michael Lando | dd60339 | 2017-07-12 00:54:52 +0300 | [diff] [blame] | 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========================================================= |
| 19 | */ |
ys9693 | 16a9fce | 2020-01-19 13:50:02 +0200 | [diff] [blame] | 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'; |
Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 30 | |
| 31 | export class ChangeLifecycleStateHandler { |
| 32 | |
| 33 | static '$inject' = [ |
| 34 | 'sdcConfig', |
| 35 | 'sdcMenu', |
| 36 | 'ComponentFactory', |
| 37 | '$filter', |
| 38 | 'ModalsHandler', |
Idan Amit | 6187c94 | 2018-04-15 19:19:08 +0300 | [diff] [blame] | 39 | 'ServiceServiceNg2', |
ys9693 | 16a9fce | 2020-01-19 13:50:02 +0200 | [diff] [blame] | 40 | 'EventBusService', |
| 41 | 'ModalServiceSdcUI', |
| 42 | 'ValidationUtils', |
| 43 | 'EventListenerService' |
Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 44 | ]; |
| 45 | |
ys9693 | 16a9fce | 2020-01-19 13:50:02 +0200 | [diff] [blame] | 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) { |
Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 56 | } |
| 57 | |
ys9693 | 16a9fce | 2020-01-19 13:50:02 +0200 | [diff] [blame] | 58 | public changeLifecycleState = (component: Component, data: any, scope: any, onSuccessCallback?: Function, onErrorCallback?: Function) => { |
Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 59 | if (data.conformanceLevelModal) { |
| 60 | this.validateConformanceLevel(component, data, scope, onSuccessCallback, onErrorCallback); |
| 61 | } else { |
| 62 | this.actualChangeLifecycleState(component, data, scope, onSuccessCallback, onErrorCallback); |
| 63 | } |
| 64 | } |
| 65 | |
ys9693 | 16a9fce | 2020-01-19 13:50:02 +0200 | [diff] [blame] | 66 | 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 Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 147 | // Validate conformance level if defined in menu.json |
ys9693 | 16a9fce | 2020-01-19 13:50:02 +0200 | [diff] [blame] | 148 | this.serviceServiceNg2.validateConformanceLevel(component as Service).subscribe((res: boolean) => { |
Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 149 | if (res === true) { |
ys9693 | 16a9fce | 2020-01-19 13:50:02 +0200 | [diff] [blame] | 150 | // Conformance level is ok - continue |
Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 151 | this.actualChangeLifecycleState(component, data, scope, onSuccessCallback, onErrorCallback); |
Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 152 | } else { |
ys9693 | 16a9fce | 2020-01-19 13:50:02 +0200 | [diff] [blame] | 153 | // 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 Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 164 | } |
| 165 | }); |
| 166 | } |
| 167 | } |