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 com.echothree.util.server.persistence.Session; 041import java.util.List; 042import javax.enterprise.context.Dependent; 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 /** Creates a new instance of EditPartyCarrierAccountCommand */ 072 public EditPartyCarrierAccountCommand() { 073 super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 074 } 075 076 @Override 077 public EditPartyCarrierAccountResult getResult() { 078 return CarrierResultFactory.getEditPartyCarrierAccountResult(); 079 } 080 081 @Override 082 public PartyCarrierAccountEdit getEdit() { 083 return CarrierEditFactory.getPartyCarrierAccountEdit(); 084 } 085 086 @Override 087 public PartyCarrierAccount getEntity(EditPartyCarrierAccountResult result) { 088 var partyControl = Session.getModelController(PartyControl.class); 089 PartyCarrierAccount partyCarrierAccount = null; 090 var partyName = spec.getPartyName(); 091 var party = partyControl.getPartyByName(partyName); 092 093 if(party != null) { 094 var carrierControl = Session.getModelController(CarrierControl.class); 095 var carrierName = spec.getCarrierName(); 096 var carrier = carrierControl.getCarrierByName(carrierName); 097 098 if(carrier != null) { 099 var carrierParty = carrier.getParty(); 100 101 if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) { 102 partyCarrierAccount = carrierControl.getPartyCarrierAccount(party, carrierParty); 103 } else { // EditMode.UPDATE 104 partyCarrierAccount = carrierControl.getPartyCarrierAccountForUpdate(party, carrierParty); 105 } 106 107 if(partyCarrierAccount != null) { 108 result.setPartyCarrierAccount(carrierControl.getPartyCarrierAccountTransfer(getUserVisit(), partyCarrierAccount)); 109 } else { 110 addExecutionError(ExecutionErrors.UnknownPartyCarrierAccount.name(), partyName, carrierName); 111 } 112 } else { 113 addExecutionError(ExecutionErrors.UnknownCarrierName.name(), carrierName); 114 } 115 } else { 116 addExecutionError(ExecutionErrors.UnknownPartyName.name(), partyName); 117 } 118 119 return partyCarrierAccount; 120 } 121 122 @Override 123 public PartyCarrierAccount getLockEntity(PartyCarrierAccount partyCarrierAccount) { 124 return partyCarrierAccount; 125 } 126 127 @Override 128 public void fillInResult(EditPartyCarrierAccountResult result, PartyCarrierAccount partyCarrierAccount) { 129 var carrierControl = Session.getModelController(CarrierControl.class); 130 131 result.setPartyCarrierAccount(carrierControl.getPartyCarrierAccountTransfer(getUserVisit(), partyCarrierAccount)); 132 } 133 134 @Override 135 public void doLock(PartyCarrierAccountEdit edit, PartyCarrierAccount partyCarrierAccount) { 136 var partyCarrierAccountDetail = partyCarrierAccount.getLastDetail(); 137 138 edit.setAccount(partyCarrierAccountDetail.getAccount()); 139 edit.setAlwaysUseThirdPartyBilling(partyCarrierAccountDetail.getAlwaysUseThirdPartyBilling().toString()); 140 } 141 142 @Override 143 public void doUpdate(PartyCarrierAccount partyCarrierAccount) { 144 var carrierControl = Session.getModelController(CarrierControl.class); 145 var partyCarrierAccountDetailValue = carrierControl.getPartyCarrierAccountDetailValueForUpdate(partyCarrierAccount); 146 147 partyCarrierAccountDetailValue.setAccount(edit.getAccount()); 148 partyCarrierAccountDetailValue.setAlwaysUseThirdPartyBilling(Boolean.valueOf(edit.getAlwaysUseThirdPartyBilling())); 149 150 carrierControl.updatePartyCarrierAccountFromValue(partyCarrierAccountDetailValue, getPartyPK()); 151 } 152 153}