001// -------------------------------------------------------------------------------- 002// Copyright 2002-2026 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.carrier.server.command; 018 019import com.echothree.control.user.carrier.common.edit.CarrierEditFactory; 020import com.echothree.control.user.carrier.common.edit.PartyCarrierAccountEdit; 021import com.echothree.control.user.carrier.common.form.EditPartyCarrierAccountForm; 022import com.echothree.control.user.carrier.common.result.CarrierResultFactory; 023import com.echothree.control.user.carrier.common.result.EditPartyCarrierAccountResult; 024import com.echothree.control.user.carrier.common.spec.PartyCarrierAccountSpec; 025import com.echothree.model.control.carrier.server.control.CarrierControl; 026import com.echothree.model.control.party.common.PartyTypes; 027import com.echothree.model.control.party.server.control.PartyControl; 028import com.echothree.model.control.security.common.SecurityRoleGroups; 029import com.echothree.model.control.security.common.SecurityRoles; 030import com.echothree.model.data.carrier.server.entity.PartyCarrierAccount; 031import com.echothree.model.data.user.common.pk.UserVisitPK; 032import com.echothree.util.common.message.ExecutionErrors; 033import com.echothree.util.common.validation.FieldDefinition; 034import com.echothree.util.common.validation.FieldType; 035import com.echothree.util.common.command.EditMode; 036import com.echothree.util.server.control.BaseAbstractEditCommand; 037import com.echothree.util.server.control.CommandSecurityDefinition; 038import com.echothree.util.server.control.PartyTypeDefinition; 039import com.echothree.util.server.control.SecurityRoleDefinition; 040import java.util.List; 041import javax.enterprise.context.Dependent; 042import javax.inject.Inject; 043 044@Dependent 045public class EditPartyCarrierAccountCommand 046 extends BaseAbstractEditCommand<PartyCarrierAccountSpec, PartyCarrierAccountEdit, EditPartyCarrierAccountResult, PartyCarrierAccount, PartyCarrierAccount> { 047 048 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 049 private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS; 050 private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS; 051 052 static { 053 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of( 054 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 055 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of( 056 new SecurityRoleDefinition(SecurityRoleGroups.PartyCarrierAccount.name(), SecurityRoles.Edit.name()) 057 )) 058 )); 059 060 SPEC_FIELD_DEFINITIONS = List.of( 061 new FieldDefinition("PartyName", FieldType.ENTITY_NAME, true, null, null), 062 new FieldDefinition("CarrierName", FieldType.ENTITY_NAME, true, null, null) 063 ); 064 065 EDIT_FIELD_DEFINITIONS = List.of( 066 new FieldDefinition("Account", FieldType.ENTITY_NAME, true, null, null), 067 new FieldDefinition("AlwaysUseThirdPartyBilling", FieldType.BOOLEAN, true, null, null) 068 ); 069 } 070 071 @Inject 072 CarrierControl carrierControl; 073 074 @Inject 075 PartyControl partyControl; 076 077 078 /** Creates a new instance of EditPartyCarrierAccountCommand */ 079 public EditPartyCarrierAccountCommand() { 080 super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 081 } 082 083 @Override 084 public EditPartyCarrierAccountResult getResult() { 085 return CarrierResultFactory.getEditPartyCarrierAccountResult(); 086 } 087 088 @Override 089 public PartyCarrierAccountEdit getEdit() { 090 return CarrierEditFactory.getPartyCarrierAccountEdit(); 091 } 092 093 @Override 094 public PartyCarrierAccount getEntity(EditPartyCarrierAccountResult result) { 095 PartyCarrierAccount partyCarrierAccount = null; 096 var partyName = spec.getPartyName(); 097 var party = partyControl.getPartyByName(partyName); 098 099 if(party != null) { 100 var carrierName = spec.getCarrierName(); 101 var carrier = carrierControl.getCarrierByName(carrierName); 102 103 if(carrier != null) { 104 var carrierParty = carrier.getParty(); 105 106 if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) { 107 partyCarrierAccount = carrierControl.getPartyCarrierAccount(party, carrierParty); 108 } else { // EditMode.UPDATE 109 partyCarrierAccount = carrierControl.getPartyCarrierAccountForUpdate(party, carrierParty); 110 } 111 112 if(partyCarrierAccount != null) { 113 result.setPartyCarrierAccount(carrierControl.getPartyCarrierAccountTransfer(getUserVisit(), partyCarrierAccount)); 114 } else { 115 addExecutionError(ExecutionErrors.UnknownPartyCarrierAccount.name(), partyName, carrierName); 116 } 117 } else { 118 addExecutionError(ExecutionErrors.UnknownCarrierName.name(), carrierName); 119 } 120 } else { 121 addExecutionError(ExecutionErrors.UnknownPartyName.name(), partyName); 122 } 123 124 return partyCarrierAccount; 125 } 126 127 @Override 128 public PartyCarrierAccount getLockEntity(PartyCarrierAccount partyCarrierAccount) { 129 return partyCarrierAccount; 130 } 131 132 @Override 133 public void fillInResult(EditPartyCarrierAccountResult result, PartyCarrierAccount partyCarrierAccount) { 134 result.setPartyCarrierAccount(carrierControl.getPartyCarrierAccountTransfer(getUserVisit(), partyCarrierAccount)); 135 } 136 137 @Override 138 public void doLock(PartyCarrierAccountEdit edit, PartyCarrierAccount partyCarrierAccount) { 139 var partyCarrierAccountDetail = partyCarrierAccount.getLastDetail(); 140 141 edit.setAccount(partyCarrierAccountDetail.getAccount()); 142 edit.setAlwaysUseThirdPartyBilling(partyCarrierAccountDetail.getAlwaysUseThirdPartyBilling().toString()); 143 } 144 145 @Override 146 public void doUpdate(PartyCarrierAccount partyCarrierAccount) { 147 var partyCarrierAccountDetailValue = carrierControl.getPartyCarrierAccountDetailValueForUpdate(partyCarrierAccount); 148 149 partyCarrierAccountDetailValue.setAccount(edit.getAccount()); 150 partyCarrierAccountDetailValue.setAlwaysUseThirdPartyBilling(Boolean.valueOf(edit.getAlwaysUseThirdPartyBilling())); 151 152 carrierControl.updatePartyCarrierAccountFromValue(partyCarrierAccountDetailValue, getPartyPK()); 153 } 154 155}