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.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.form.EditPartyTermForm; 022import com.echothree.control.user.term.common.result.EditPartyTermResult; 023import com.echothree.control.user.term.common.result.TermResultFactory; 024import com.echothree.control.user.term.common.spec.PartyTermSpec; 025import com.echothree.model.control.party.common.PartyTypes; 026import com.echothree.model.control.party.server.control.PartyControl; 027import com.echothree.model.control.party.server.logic.PartyChainLogic; 028import com.echothree.model.control.term.server.control.TermControl; 029import com.echothree.model.data.party.common.pk.PartyPK; 030import com.echothree.model.data.party.server.entity.Party; 031import com.echothree.model.data.term.server.entity.PartyTerm; 032import com.echothree.model.data.term.server.entity.Term; 033import com.echothree.model.data.term.server.value.PartyTermValue; 034import com.echothree.model.data.user.common.pk.UserVisitPK; 035import com.echothree.util.common.message.ExecutionErrors; 036import com.echothree.util.common.validation.FieldDefinition; 037import com.echothree.util.common.validation.FieldType; 038import com.echothree.util.common.command.BaseResult; 039import com.echothree.util.common.command.EditMode; 040import com.echothree.util.server.control.BaseEditCommand; 041import com.echothree.util.server.persistence.Session; 042import java.util.Arrays; 043import java.util.Collections; 044import java.util.List; 045 046public class EditPartyTermCommand 047 extends BaseEditCommand<PartyTermSpec, PartyTermEdit> { 048 049 private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS; 050 private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS; 051 052 static { 053 SPEC_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 054 new FieldDefinition("PartyName", FieldType.ENTITY_NAME, true, null, null) 055 )); 056 057 EDIT_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 058 new FieldDefinition("TermName", FieldType.ENTITY_NAME, true, null, null), 059 new FieldDefinition("Taxable", FieldType.BOOLEAN, true, null, null) 060 )); 061 } 062 063 /** Creates a new instance of EditPartyTermCommand */ 064 public EditPartyTermCommand(UserVisitPK userVisitPK, EditPartyTermForm form) { 065 super(userVisitPK, form, null, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 066 } 067 068 @Override 069 protected BaseResult execute() { 070 var partyControl = Session.getModelController(PartyControl.class); 071 EditPartyTermResult result = TermResultFactory.getEditPartyTermResult(); 072 String partyName = spec.getPartyName(); 073 Party party = partyControl.getPartyByName(partyName); 074 075 if(party != null) { 076 var termControl = Session.getModelController(TermControl.class); 077 078 if(editMode.equals(EditMode.LOCK)) { 079 PartyTerm partyTerm = termControl.getPartyTerm(party); 080 081 if(partyTerm != null) { 082 result.setPartyTerm(termControl.getPartyTermTransfer(getUserVisit(), partyTerm)); 083 084 if(lockEntity(party)) { 085 PartyTermEdit edit = TermEditFactory.getPartyTermEdit(); 086 087 result.setEdit(edit); 088 edit.setTermName(partyTerm.getTerm().getLastDetail().getTermName()); 089 edit.setTaxable(partyTerm.getTaxable().toString()); 090 } else { 091 addExecutionError(ExecutionErrors.EntityLockFailed.name()); 092 } 093 094 result.setEntityLock(getEntityLockTransfer(party)); 095 } else { 096 addExecutionError(ExecutionErrors.UnknownPartyTerm.name()); 097 } 098 } else if(editMode.equals(EditMode.UPDATE)) { 099 PartyTermValue partyTermValue = termControl.getPartyTermValueForUpdate(party); 100 101 if(partyTermValue != null) { 102 if(lockEntityForUpdate(party)) { 103 String termName = edit.getTermName(); 104 Term term = termControl.getTermByName(termName); 105 106 if(term != null) { 107 Boolean taxable = Boolean.valueOf(edit.getTaxable()); 108 109 try { 110 partyTermValue.setTermPK(term.getPrimaryKey()); 111 partyTermValue.setTaxable(taxable); 112 113 if(partyTermValue.hasBeenModified()) { 114 String partyTypeName = party.getLastDetail().getPartyType().getPartyTypeName(); 115 PartyPK updatedBy = getPartyPK(); 116 117 termControl.updatePartyTermFromValue(partyTermValue, updatedBy); 118 119 if(partyTypeName.equals(PartyTypes.CUSTOMER.name())) { 120 // ExecutionErrorAccumulator is passed in as null so that an Exception will be thrown if there is an error. 121 PartyChainLogic.getInstance().createPartyTermChangedChainInstance(null, party, updatedBy); 122 } 123 } 124 } finally { 125 unlockEntity(party); 126 } 127 } else { 128 addExecutionError(ExecutionErrors.UnknownTermName.name(), termName); 129 } 130 } else { 131 addExecutionError(ExecutionErrors.EntityLockStale.name()); 132 } 133 } else { 134 addExecutionError(ExecutionErrors.UnknownPartyTerm.name()); 135 } 136 } 137 } else { 138 addExecutionError(ExecutionErrors.UnknownPartyName.name(), partyName); 139 } 140 141 return result; 142 } 143 144}