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