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.term.server.command; 018 019import com.echothree.control.user.term.common.edit.PartyTermEdit; 020import com.echothree.control.user.term.common.edit.TermEditFactory; 021import com.echothree.control.user.term.common.result.EditPartyTermResult; 022import com.echothree.control.user.term.common.result.TermResultFactory; 023import com.echothree.control.user.term.common.spec.PartyTermSpec; 024import com.echothree.model.control.party.common.PartyTypes; 025import com.echothree.model.control.party.server.logic.PartyChainLogic; 026import com.echothree.model.control.party.server.logic.PartyLogic; 027import com.echothree.model.control.term.server.control.TermControl; 028import com.echothree.model.control.term.server.logic.TermLogic; 029import com.echothree.model.data.party.server.entity.Party; 030import com.echothree.model.data.term.server.entity.PartyTerm; 031import com.echothree.model.data.term.server.entity.Term; 032import com.echothree.util.common.validation.FieldDefinition; 033import com.echothree.util.common.validation.FieldType; 034import com.echothree.util.server.control.BaseEditCommand; 035import java.util.List; 036import javax.enterprise.context.Dependent; 037import javax.inject.Inject; 038 039@Dependent 040public class EditPartyTermCommand 041 extends BaseEditCommand<PartyTermSpec, PartyTermEdit, EditPartyTermResult, PartyTerm, Party> { 042 043 private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS; 044 private final static List<FieldDefinition> CUSTOMER_EDIT_FIELD_DEFINITIONS; 045 private final static List<FieldDefinition> VENDOR_EDIT_FIELD_DEFINITIONS; 046 047 static { 048 SPEC_FIELD_DEFINITIONS = List.of( 049 new FieldDefinition("PartyName", FieldType.ENTITY_NAME, true, null, null) 050 ); 051 052 CUSTOMER_EDIT_FIELD_DEFINITIONS = List.of( 053 new FieldDefinition("TermName", FieldType.ENTITY_NAME, true, null, null), 054 new FieldDefinition("Taxable", FieldType.BOOLEAN, true, null, null) 055 ); 056 057 VENDOR_EDIT_FIELD_DEFINITIONS = List.of( 058 new FieldDefinition("TermName", FieldType.ENTITY_NAME, true, null, null), 059 new FieldDefinition("Taxable", FieldType.NULL, false, null, null) 060 ); 061 } 062 063 private Party party; 064 private boolean partyResolved; 065 private Term term; 066 067 @Inject 068 TermControl termControl; 069 070 @Inject 071 PartyChainLogic partyChainLogic; 072 073 @Inject 074 PartyLogic partyLogic; 075 076 @Inject 077 TermLogic termLogic; 078 079 /** Creates a new instance of EditPartyTermCommand */ 080 public EditPartyTermCommand() { 081 super(null, SPEC_FIELD_DEFINITIONS, null); 082 } 083 084 private Party resolveParty() { 085 if(!partyResolved) { 086 party = partyLogic.getPartyByName(this, spec.getPartyName(), PartyTypes.CUSTOMER.name(), PartyTypes.VENDOR.name()); 087 partyResolved = true; 088 } 089 090 return party; 091 } 092 093 @Override 094 protected List<FieldDefinition> getEditFieldDefinitions() { 095 var party = resolveParty(); 096 097 return party != null && PartyTypes.VENDOR.name().equals(party.getLastDetail().getPartyType().getPartyTypeName()) 098 ? VENDOR_EDIT_FIELD_DEFINITIONS : CUSTOMER_EDIT_FIELD_DEFINITIONS; 099 } 100 101 @Override 102 public EditPartyTermResult getResult() { 103 return TermResultFactory.getEditPartyTermResult(); 104 } 105 106 @Override 107 public PartyTermEdit getEdit() { 108 return TermEditFactory.getPartyTermEdit(); 109 } 110 111 @Override 112 public PartyTerm getEntity(EditPartyTermResult result) { 113 var party = resolveParty(); 114 PartyTerm partyTerm = null; 115 116 if(!hasExecutionErrors()) { 117 partyTerm = termLogic.getPartyTerm(this, party, editModeToEntityPermission(editMode)); 118 } 119 120 return partyTerm; 121 } 122 123 @Override 124 public Party getLockEntity(PartyTerm partyTerm) { 125 return partyTerm.getParty(); 126 } 127 128 @Override 129 public void fillInResult(EditPartyTermResult result, PartyTerm partyTerm) { 130 result.setPartyTerm(termControl.getPartyTermTransfer(getUserVisit(), partyTerm)); 131 } 132 133 @Override 134 public void doLock(PartyTermEdit edit, PartyTerm partyTerm) { 135 var taxable = partyTerm.getTaxable(); 136 137 edit.setTermName(partyTerm.getTerm().getLastDetail().getTermName()); 138 edit.setTaxable(taxable == null ? null : taxable.toString()); 139 } 140 141 @Override 142 public void canUpdate(PartyTerm partyTerm) { 143 term = termLogic.getTermByName(this, edit.getTermName()); 144 } 145 146 @Override 147 public void doUpdate(PartyTerm partyTerm) { 148 var partyTermValue = termControl.getPartyTermValue(partyTerm); 149 var taxable = edit.getTaxable(); 150 151 partyTermValue.setTermPK(term.getPrimaryKey()); 152 partyTermValue.setTaxable(taxable == null ? null : Boolean.valueOf(taxable)); 153 154 if(partyTermValue.hasBeenModified()) { 155 var party = partyTerm.getParty(); 156 var partyTypeName = party.getLastDetail().getPartyType().getPartyTypeName(); 157 var updatedBy = getPartyPK(); 158 159 termControl.updatePartyTermFromValue(partyTermValue, updatedBy); 160 161 if(PartyTypes.CUSTOMER.name().equals(partyTypeName)) { 162 // Throw on chain errors so that the transaction is rolled back. 163 partyChainLogic.createPartyTermChangedChainInstance(null, party, updatedBy); 164 } 165 } 166 } 167 168}