1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
|
/*-
* ============LICENSE_START=======================================================
* SDC
* ================================================================================
* Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ============LICENSE_END=========================================================
*/
import { ServiceServiceNg2 } from 'app/ng2/services/component-services/service.service';
import { EventBusService } from 'app/ng2/services/event-bus.service';
import { EVENTS, ValidationUtils } from 'app/utils';
import { SdcUiCommon, SdcUiComponents, SdcUiServices } from 'onap-ui-angular';
import { Component, IAppConfigurtaion, IAppMenu, Service } from '../models';
import { AsdcComment } from '../models/comments';
import { CommentModalComponent } from '../ng2/components/modals/comment-modal/comment-modal.component';
import { EventListenerService } from '../services/event-listener-service';
import { ComponentFactory } from './component-factory';
import { ModalsHandler } from './modals-handler';
export class ChangeLifecycleStateHandler {
static '$inject' = [
'sdcConfig',
'sdcMenu',
'ComponentFactory',
'$filter',
'ModalsHandler',
'ServiceServiceNg2',
'EventBusService',
'ModalServiceSdcUI',
'ValidationUtils',
'EventListenerService'
];
constructor(private sdcConfig: IAppConfigurtaion,
private sdcMenu: IAppMenu,
private componentFactory: ComponentFactory,
private $filter: ng.IFilterService,
private modalsHandler: ModalsHandler,
private serviceServiceNg2: ServiceServiceNg2,
private eventBusService: EventBusService,
private modalService: SdcUiServices.ModalService,
private validationUtils: ValidationUtils,
private eventListenerService: EventListenerService) {
}
public changeLifecycleState = (component: Component, data: any, scope: any, onSuccessCallback?: Function, onErrorCallback?: Function) => {
if (data.conformanceLevelModal) {
this.validateConformanceLevel(component, data, scope, onSuccessCallback, onErrorCallback);
} else {
this.actualChangeLifecycleState(component, data, scope, onSuccessCallback, onErrorCallback);
}
}
private actualChangeLifecycleState = (component: Component, data: any, scope: any, onSuccessCallback?: Function, onErrorCallback?: Function) => {
const self = this;
const onSuccess = (newComponent: Component) => {
if (onSuccessCallback) {
onSuccessCallback(self.componentFactory.createComponent(newComponent), data.url);
if (data.url === 'distribution/PROD/activate') {
this.eventListenerService.notifyObservers(EVENTS.ON_DISTRIBUTION_SUCCESS);
}
}
};
const onError = (error) => {
scope.isLoading = false;
if (onErrorCallback) {
onErrorCallback(error);
}
};
const comment: AsdcComment = new AsdcComment();
if (data.alertModal) {
// Show alert dialog if defined in menu.json
const onOk: Function = (confirmationText) => {
comment.userRemarks = confirmationText;
scope.isLoading = true;
component.changeLifecycleState(data.url, comment).then(onSuccess, onError);
};
const modalTitle = this.sdcMenu.alertMessages[data.alertModal].title;
const modalMessage = this.sdcMenu.alertMessages[data.alertModal].message.format([component.componentType.toLowerCase()]);
const modalButton = {
testId: 'OK',
text: this.sdcMenu.alertMessages.okButton,
type: SdcUiCommon.ButtonType.warning,
callback: onOk,
closeModal: true
} as SdcUiComponents.ModalButtonComponent;
this.modalService.openWarningModal(modalTitle, modalMessage, 'alert-modal', [modalButton]);
} else if (data.confirmationModal) {
// Show confirmation dialog if defined in menu.json
let commentModalInstance: SdcUiComponents.ModalComponent;
const onOk = () => {
const confirmationText: string = commentModalInstance.innerModalContent.instance.comment.text;
commentModalInstance.closeModal();
comment.userRemarks = this.validationUtils.stripAndSanitize(confirmationText);
if (data.url === 'lifecycleState/CHECKIN') {
this.eventBusService.notify('CHECK_IN').subscribe(() => {
scope.isLoading = true;
component.changeLifecycleState(data.url, comment).then(onSuccess, onError);
});
} else {
scope.isLoading = true;
component.changeLifecycleState(data.url, comment).then(onSuccess, onError);
}
};
const modalTitle = this.sdcMenu.confirmationMessages[data.confirmationModal].title;
const modalMessage = this.sdcMenu.confirmationMessages[data.confirmationModal].message.format([component.componentType.toLowerCase()]);
const modalConfig = {
size: 'md',
title: modalTitle,
type: SdcUiCommon.ModalType.custom,
testId: 'confirm-modal',
buttons: [
{ id: 'OK', text: 'OK', callback: onOk, closeModal: false, testId: 'OK' },
{ id: 'cancel', text: 'Cancel', size: 'x-small', type: 'secondary', closeModal: true, testId: 'Cancel' }
] as SdcUiCommon.IModalButtonComponent[]
} as SdcUiCommon.IModalConfig;
commentModalInstance = this.modalService.openCustomModal(modalConfig, CommentModalComponent, { message: modalMessage });
commentModalInstance.innerModalContent.instance.onValidationChange.subscribe((isValid) => {
commentModalInstance.getButtonById('OK').disabled = !isValid;
});
} else {
// Submit to server only (no modal is shown).
scope.isLoading = true;
component.changeLifecycleState(data.url, comment).then(onSuccess, onError);
}
}
private validateConformanceLevel = (component: Component, data: any, scope: any, onSuccessCallback?: Function, onErrorCallback?: Function) => {
// Validate conformance level if defined in menu.json
this.serviceServiceNg2.validateConformanceLevel(component as Service).subscribe((res: boolean) => {
if (res === true) {
// Conformance level is ok - continue
this.actualChangeLifecycleState(component, data, scope, onSuccessCallback, onErrorCallback);
} else {
// Show warning modal
const onContinue: Function = () => {
this.actualChangeLifecycleState(component, data, scope, onSuccessCallback, onErrorCallback);
};
const reject: Function = () => {
this.actualChangeLifecycleState(component, data.conformanceLevelModal, scope, onSuccessCallback, onErrorCallback);
};
const continueButton = {testId: 'Continue', text: 'Continue', type: SdcUiCommon.ButtonType.primary, callback: onContinue, closeModal: true} as SdcUiComponents.ModalButtonComponent;
const rejectButton = {testId: 'Reject', text: 'Reject', type: SdcUiCommon.ButtonType.secondary, callback: reject, closeModal: true} as SdcUiComponents.ModalButtonComponent;
this.modalService.openInfoModal(this.$filter('translate')('CONFORMANCE_LEVEL_MODAL_TITLE'),
this.$filter('translate')('CONFORMANCE_LEVEL_MODAL_TEXT'), 'conformance-modal', [continueButton, rejectButton]);
}
});
}
}
|