001// -------------------------------------------------------------------------------- 002// Copyright 2002-2024 Echo Three, LLC 003// 004// Licensed under the Apache License, Version 2.0 (the "License"); 005// you may not use this file except in compliance with the License. 006// You may obtain a copy of the License at 007// 008// http://www.apache.org/licenses/LICENSE-2.0 009// 010// Unless required by applicable law or agreed to in writing, software 011// distributed under the License is distributed on an "AS IS" BASIS, 012// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013// See the License for the specific language governing permissions and 014// limitations under the License. 015// -------------------------------------------------------------------------------- 016 017package com.echothree.control.user.payment.server.command; 018 019import com.echothree.control.user.payment.common.edit.PaymentEditFactory; 020import com.echothree.control.user.payment.common.edit.PaymentProcessorEdit; 021import com.echothree.control.user.payment.common.form.EditPaymentProcessorForm; 022import com.echothree.control.user.payment.common.result.EditPaymentProcessorResult; 023import com.echothree.control.user.payment.common.result.PaymentResultFactory; 024import com.echothree.control.user.payment.common.spec.PaymentProcessorSpec; 025import com.echothree.model.control.party.common.PartyTypes; 026import com.echothree.model.control.payment.server.control.PaymentProcessorControl; 027import com.echothree.model.control.security.common.SecurityRoleGroups; 028import com.echothree.model.control.security.common.SecurityRoles; 029import com.echothree.model.data.party.common.pk.PartyPK; 030import com.echothree.model.data.payment.server.entity.PaymentProcessor; 031import com.echothree.model.data.payment.server.entity.PaymentProcessorDescription; 032import com.echothree.model.data.payment.server.entity.PaymentProcessorDetail; 033import com.echothree.model.data.payment.server.value.PaymentProcessorDescriptionValue; 034import com.echothree.model.data.payment.server.value.PaymentProcessorDetailValue; 035import com.echothree.model.data.user.common.pk.UserVisitPK; 036import com.echothree.util.common.command.EditMode; 037import com.echothree.util.common.message.ExecutionErrors; 038import com.echothree.util.common.validation.FieldDefinition; 039import com.echothree.util.common.validation.FieldType; 040import com.echothree.util.server.control.BaseAbstractEditCommand; 041import com.echothree.util.server.control.CommandSecurityDefinition; 042import com.echothree.util.server.control.PartyTypeDefinition; 043import com.echothree.util.server.control.SecurityRoleDefinition; 044import com.echothree.util.server.persistence.Session; 045import java.util.Arrays; 046import java.util.Collections; 047import java.util.List; 048 049public class EditPaymentProcessorCommand 050 extends BaseAbstractEditCommand<PaymentProcessorSpec, PaymentProcessorEdit, EditPaymentProcessorResult, PaymentProcessor, PaymentProcessor> { 051 052 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 053 private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS; 054 private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS; 055 056 static { 057 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList( 058 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 059 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList( 060 new SecurityRoleDefinition(SecurityRoleGroups.PaymentProcessor.name(), SecurityRoles.Edit.name()) 061 ))) 062 ))); 063 064 SPEC_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 065 new FieldDefinition("PaymentProcessorName", FieldType.ENTITY_NAME, true, null, null) 066 )); 067 068 EDIT_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 069 new FieldDefinition("PaymentProcessorName", FieldType.ENTITY_NAME, true, null, null), 070 new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null), 071 new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null), 072 new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L) 073 )); 074 } 075 076 /** Creates a new instance of EditPaymentProcessorCommand */ 077 public EditPaymentProcessorCommand(UserVisitPK userVisitPK, EditPaymentProcessorForm form) { 078 super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 079 } 080 081 @Override 082 public EditPaymentProcessorResult getResult() { 083 return PaymentResultFactory.getEditPaymentProcessorResult(); 084 } 085 086 @Override 087 public PaymentProcessorEdit getEdit() { 088 return PaymentEditFactory.getPaymentProcessorEdit(); 089 } 090 091 @Override 092 public PaymentProcessor getEntity(EditPaymentProcessorResult result) { 093 var paymentProcessorControl = Session.getModelController(PaymentProcessorControl.class); 094 PaymentProcessor paymentProcessor = null; 095 String paymentProcessorName = spec.getPaymentProcessorName(); 096 097 if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) { 098 paymentProcessor = paymentProcessorControl.getPaymentProcessorByName(paymentProcessorName); 099 } else { // EditMode.UPDATE 100 paymentProcessor = paymentProcessorControl.getPaymentProcessorByNameForUpdate(paymentProcessorName); 101 } 102 103 if(paymentProcessor != null) { 104 result.setPaymentProcessor(paymentProcessorControl.getPaymentProcessorTransfer(getUserVisit(), paymentProcessor)); 105 } else { 106 addExecutionError(ExecutionErrors.UnknownPaymentProcessorName.name(), paymentProcessorName); 107 } 108 109 return paymentProcessor; 110 } 111 112 @Override 113 public PaymentProcessor getLockEntity(PaymentProcessor paymentProcessor) { 114 return paymentProcessor; 115 } 116 117 @Override 118 public void fillInResult(EditPaymentProcessorResult result, PaymentProcessor paymentProcessor) { 119 var paymentProcessorControl = Session.getModelController(PaymentProcessorControl.class); 120 121 result.setPaymentProcessor(paymentProcessorControl.getPaymentProcessorTransfer(getUserVisit(), paymentProcessor)); 122 } 123 124 @Override 125 public void doLock(PaymentProcessorEdit edit, PaymentProcessor paymentProcessor) { 126 var paymentProcessorControl = Session.getModelController(PaymentProcessorControl.class); 127 PaymentProcessorDescription paymentProcessorDescription = paymentProcessorControl.getPaymentProcessorDescription(paymentProcessor, getPreferredLanguage()); 128 PaymentProcessorDetail paymentProcessorDetail = paymentProcessor.getLastDetail(); 129 130 edit.setPaymentProcessorName(paymentProcessorDetail.getPaymentProcessorName()); 131 edit.setIsDefault(paymentProcessorDetail.getIsDefault().toString()); 132 edit.setSortOrder(paymentProcessorDetail.getSortOrder().toString()); 133 134 if(paymentProcessorDescription != null) { 135 edit.setDescription(paymentProcessorDescription.getDescription()); 136 } 137 } 138 139 @Override 140 public void canUpdate(PaymentProcessor paymentProcessor) { 141 var paymentProcessorControl = Session.getModelController(PaymentProcessorControl.class); 142 String paymentProcessorName = edit.getPaymentProcessorName(); 143 PaymentProcessor duplicatePaymentProcessor = paymentProcessorControl.getPaymentProcessorByName(paymentProcessorName); 144 145 if(duplicatePaymentProcessor != null && !paymentProcessor.equals(duplicatePaymentProcessor)) { 146 addExecutionError(ExecutionErrors.DuplicatePaymentProcessorName.name(), paymentProcessorName); 147 } 148 } 149 150 @Override 151 public void doUpdate(PaymentProcessor paymentProcessor) { 152 var paymentProcessorControl = Session.getModelController(PaymentProcessorControl.class); 153 var partyPK = getPartyPK(); 154 PaymentProcessorDetailValue paymentProcessorDetailValue = paymentProcessorControl.getPaymentProcessorDetailValueForUpdate(paymentProcessor); 155 PaymentProcessorDescription paymentProcessorDescription = paymentProcessorControl.getPaymentProcessorDescriptionForUpdate(paymentProcessor, getPreferredLanguage()); 156 String description = edit.getDescription(); 157 158 paymentProcessorDetailValue.setPaymentProcessorName(edit.getPaymentProcessorName()); 159 paymentProcessorDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault())); 160 paymentProcessorDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder())); 161 162 paymentProcessorControl.updatePaymentProcessorFromValue(paymentProcessorDetailValue, partyPK); 163 164 if(paymentProcessorDescription == null && description != null) { 165 paymentProcessorControl.createPaymentProcessorDescription(paymentProcessor, getPreferredLanguage(), description, partyPK); 166 } else if(paymentProcessorDescription != null && description == null) { 167 paymentProcessorControl.deletePaymentProcessorDescription(paymentProcessorDescription, partyPK); 168 } else if(paymentProcessorDescription != null && description != null) { 169 PaymentProcessorDescriptionValue paymentProcessorDescriptionValue = paymentProcessorControl.getPaymentProcessorDescriptionValue(paymentProcessorDescription); 170 171 paymentProcessorDescriptionValue.setDescription(description); 172 paymentProcessorControl.updatePaymentProcessorDescriptionFromValue(paymentProcessorDescriptionValue, partyPK); 173 } 174 } 175 176}