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 |
| 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 | |
Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 21 | import {ComponentFactory} from "./component-factory"; |
| 22 | import {Component, Service,IAppMenu, IAppConfigurtaion} from "../models"; |
| 23 | import {IEmailModalModel, IEmailModalModel_Email, IEmailModalModel_Data} from "../view-models/modals/email-modal/email-modal-view-model"; |
| 24 | import {AsdcComment} from "../models/comments"; |
| 25 | import {ModalsHandler} from "./modals-handler"; |
| 26 | import {ServiceServiceNg2} from "../ng2/services/component-services/service.service"; |
| 27 | |
| 28 | /** |
| 29 | * Created by obarda on 2/11/2016. |
| 30 | */ |
| 31 | |
| 32 | export class ChangeLifecycleStateHandler { |
| 33 | |
| 34 | static '$inject' = [ |
| 35 | 'sdcConfig', |
| 36 | 'sdcMenu', |
| 37 | 'ComponentFactory', |
| 38 | '$filter', |
| 39 | 'ModalsHandler', |
| 40 | 'ServiceServiceNg2' |
| 41 | ]; |
| 42 | |
| 43 | constructor(private sdcConfig:IAppConfigurtaion, |
| 44 | private sdcMenu:IAppMenu, |
| 45 | private ComponentFactory:ComponentFactory, |
| 46 | private $filter:ng.IFilterService, |
| 47 | private ModalsHandler:ModalsHandler, |
| 48 | private ServiceServiceNg2:ServiceServiceNg2) { |
| 49 | |
| 50 | } |
| 51 | |
| 52 | private actualChangeLifecycleState = (component:Component, data:any, scope:any, onSuccessCallback?:Function, onErrorCallback?:Function):void => { |
| 53 | |
| 54 | let self = this; |
| 55 | |
| 56 | let getContacts = (component:Component):string => { |
| 57 | let testers = this.sdcConfig.testers; |
| 58 | let result:string = testers[component.componentType][component.categories[0].name] ? |
| 59 | testers[component.componentType][component.categories[0].name] : |
| 60 | testers[component.componentType]['default']; |
| 61 | return result; |
| 62 | }; |
| 63 | |
| 64 | let onSuccess = (newComponent:Component):void => { |
| 65 | //scope.isLoading = false; |
| 66 | console.info(component.componentType.toLowerCase + ' change state ', newComponent); |
| 67 | if (onSuccessCallback) { |
| 68 | onSuccessCallback(self.ComponentFactory.createComponent(newComponent), data.url); |
| 69 | } |
| 70 | }; |
| 71 | |
| 72 | let onError = (error):void => { |
| 73 | scope.isLoading = false; |
| 74 | console.info('Failed to changeLifecycleState to ', data.url); |
| 75 | if (onErrorCallback) { |
| 76 | onErrorCallback(error); |
| 77 | } |
| 78 | }; |
| 79 | |
| 80 | let comment:AsdcComment = new AsdcComment(); |
| 81 | if (data.alertModal) { |
| 82 | // Show alert dialog if defined in menu.json |
| 83 | //------------------------------------------------- |
| 84 | let onOk = (confirmationText):void => { |
| 85 | comment.userRemarks = confirmationText; |
| 86 | scope.isLoading = true; |
| 87 | component.changeLifecycleState(data.url, comment).then(onSuccess, onError); |
| 88 | }; |
| 89 | |
| 90 | let onCancel = ():void => { |
| 91 | console.info('Cancel pressed'); |
| 92 | scope.isLoading = false; |
| 93 | }; |
| 94 | |
| 95 | let modalTitle = this.sdcMenu.alertMessages[data.alertModal].title; |
| 96 | let modalMessage = this.sdcMenu.alertMessages[data.alertModal].message.format([component.componentType.toLowerCase()]); |
| 97 | this.ModalsHandler.openAlertModal(modalTitle, modalMessage).then(onOk, onCancel); |
| 98 | } else if (data.confirmationModal) { |
| 99 | // Show confirmation dialog if defined in menu.json |
| 100 | //------------------------------------------------- |
| 101 | let onOk = (confirmationText):void => { |
| 102 | comment.userRemarks = confirmationText; |
| 103 | scope.isLoading = true; |
| 104 | component.changeLifecycleState(data.url, comment).then(onSuccess, onError); |
| 105 | }; |
| 106 | |
| 107 | let onCancel = ():void => { |
| 108 | console.info('Cancel pressed'); |
| 109 | scope.isLoading = false; |
| 110 | }; |
| 111 | |
| 112 | let modalTitle = this.sdcMenu.confirmationMessages[data.confirmationModal].title; |
| 113 | let modalMessage = this.sdcMenu.confirmationMessages[data.confirmationModal].message.format([component.componentType.toLowerCase()]); |
| 114 | let modalShowComment = this.sdcMenu.confirmationMessages[data.confirmationModal].showComment; |
| 115 | this.ModalsHandler.openConfirmationModal(modalTitle, modalMessage, modalShowComment).then(onOk, onCancel); |
| 116 | |
| 117 | } else if (data.emailModal) { |
| 118 | // Show email dialog if defined in menu.json |
| 119 | //------------------------------------------------- |
| 120 | let onOk = (resource):void => { |
| 121 | if (resource) { |
| 122 | onSuccess(resource); |
| 123 | } else { |
| 124 | onError("Error changing life cycle state"); |
| 125 | } |
| 126 | }; |
| 127 | |
| 128 | let onCancel = ():void => { |
| 129 | scope.isLoading = false; |
| 130 | }; |
| 131 | |
| 132 | let emailModel:IEmailModalModel = <IEmailModalModel>{}; |
| 133 | emailModel.email = <IEmailModalModel_Email>{}; |
| 134 | emailModel.data = <IEmailModalModel_Data>{}; |
| 135 | emailModel.title = this.$filter('translate')("EMAIL_MODAL_TITLE"); |
| 136 | emailModel.email.to = getContacts(component); |
| 137 | emailModel.email.subject = this.$filter('translate')("EMAIL_MODAL_SUBJECT", "{'entityName': '" + this.$filter('resourceName')(component.name) + "','entityVersion': '" + component.version + "'}"); |
| 138 | emailModel.email.message = ''; |
| 139 | emailModel.data.component = component; |
| 140 | emailModel.data.stateUrl = data.url; |
| 141 | |
| 142 | this.ModalsHandler.openEmailModal(emailModel).then(onOk, onCancel); |
| 143 | |
| 144 | } else { |
| 145 | // Submit to server only (no modal is shown). |
| 146 | scope.isLoading = true; |
| 147 | component.changeLifecycleState(data.url, comment).then(onSuccess, onError); |
| 148 | } |
| 149 | |
| 150 | } |
| 151 | |
| 152 | public changeLifecycleState = (component:Component, data:any, scope:any, onSuccessCallback?:Function, onErrorCallback?:Function):void => { |
| 153 | |
| 154 | if (data.conformanceLevelModal) { |
| 155 | this.validateConformanceLevel(component, data, scope, onSuccessCallback, onErrorCallback); |
| 156 | } else { |
| 157 | this.actualChangeLifecycleState(component, data, scope, onSuccessCallback, onErrorCallback); |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | private validateConformanceLevel = (component:Component, data:any, scope:any, onSuccessCallback?:Function, onErrorCallback?:Function):void => { |
| 162 | // Validate conformance level if defined in menu.json |
| 163 | //------------------------------------------------- |
| 164 | this.ServiceServiceNg2.validateConformanceLevel(<Service>component).subscribe((res:boolean) => { |
| 165 | if (res === true) { |
| 166 | //conformance level is ok - continue |
| 167 | this.actualChangeLifecycleState(component, data, scope, onSuccessCallback, onErrorCallback); |
| 168 | |
| 169 | } else { |
| 170 | //show warning modal |
| 171 | this.ModalsHandler.openConformanceLevelModal() |
| 172 | .then(() => { |
| 173 | //continue distribute |
| 174 | this.actualChangeLifecycleState(component, data, scope, onSuccessCallback, onErrorCallback); |
| 175 | |
| 176 | }).catch(() => { |
| 177 | //reject distribution |
| 178 | this.actualChangeLifecycleState(component, data.conformanceLevelModal, scope, onSuccessCallback, onErrorCallback); |
| 179 | }); |
| 180 | } |
| 181 | }); |
| 182 | } |
| 183 | } |