Release version 1.13.7
[sdc.git] / catalog-ui / src / app / utils / change-lifecycle-state-handler.ts
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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  */
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';
30
31 export class ChangeLifecycleStateHandler {
32
33     static '$inject' = [
34         'sdcConfig',
35         'sdcMenu',
36         'ComponentFactory',
37         '$filter',
38         'ModalsHandler',
39         'ServiceServiceNg2',
40         'EventBusService',
41         'ModalServiceSdcUI',
42         'ValidationUtils',
43         'EventListenerService'
44     ];
45
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) {
56     }
57
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);
61         } else {
62             this.actualChangeLifecycleState(component, data, scope, onSuccessCallback, onErrorCallback);
63         }
64     }
65
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) => {
147         // Validate conformance level if defined in menu.json
148         this.serviceServiceNg2.validateConformanceLevel(component as Service).subscribe((res: boolean) => {
149             if (res === true) {
150                 // Conformance level is ok - continue
151                 this.actualChangeLifecycleState(component, data, scope, onSuccessCallback, onErrorCallback);
152             } else {
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]);
164             }
165         });
166     }
167 }