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 com.echothree.util.server.persistence.Session;
036import java.util.List;
037import javax.enterprise.context.Dependent;
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    /** Creates a new instance of EditPartyTermCommand */
058    public EditPartyTermCommand() {
059        super(null, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
060    }
061    
062    @Override
063    protected BaseResult execute() {
064        var partyControl = Session.getModelController(PartyControl.class);
065        var result = TermResultFactory.getEditPartyTermResult();
066        var partyName = spec.getPartyName();
067        var party = partyControl.getPartyByName(partyName);
068        
069        if(party != null) {
070            var termControl = Session.getModelController(TermControl.class);
071            
072            if(editMode.equals(EditMode.LOCK)) {
073                var partyTerm = termControl.getPartyTerm(party);
074                
075                if(partyTerm != null) {
076                    result.setPartyTerm(termControl.getPartyTermTransfer(getUserVisit(), partyTerm));
077                    
078                    if(lockEntity(party)) {
079                        var edit = TermEditFactory.getPartyTermEdit();
080                        
081                        result.setEdit(edit);
082                        edit.setTermName(partyTerm.getTerm().getLastDetail().getTermName());
083                        edit.setTaxable(partyTerm.getTaxable().toString());
084                    } else {
085                        addExecutionError(ExecutionErrors.EntityLockFailed.name());
086                    }
087                    
088                    result.setEntityLock(getEntityLockTransfer(party));
089                } else {
090                    addExecutionError(ExecutionErrors.UnknownPartyTerm.name());
091                }
092            } else if(editMode.equals(EditMode.UPDATE)) {
093                var partyTermValue = termControl.getPartyTermValueForUpdate(party);
094                
095                if(partyTermValue != null) {
096                    if(lockEntityForUpdate(party)) {
097                        var termName = edit.getTermName();
098                        var term = termControl.getTermByName(termName);
099                        
100                        if(term != null) {
101                            var taxable = Boolean.valueOf(edit.getTaxable());
102                            
103                            try {
104                                partyTermValue.setTermPK(term.getPrimaryKey());
105                                partyTermValue.setTaxable(taxable);
106                                
107                                if(partyTermValue.hasBeenModified()) {
108                                    var partyTypeName = party.getLastDetail().getPartyType().getPartyTypeName();
109                                    var updatedBy = getPartyPK();
110                                    
111                                    termControl.updatePartyTermFromValue(partyTermValue, updatedBy);
112                                    
113                                    if(partyTypeName.equals(PartyTypes.CUSTOMER.name())) {
114                                        // ExecutionErrorAccumulator is passed in as null so that an Exception will be thrown if there is an error.
115                                        PartyChainLogic.getInstance().createPartyTermChangedChainInstance(null, party, updatedBy);
116                                    }
117                                }
118                            } finally {
119                                unlockEntity(party);
120                            }
121                        } else {
122                            addExecutionError(ExecutionErrors.UnknownTermName.name(), termName);
123                        }
124                    } else {
125                        addExecutionError(ExecutionErrors.EntityLockStale.name());
126                    }
127                } else {
128                    addExecutionError(ExecutionErrors.UnknownPartyTerm.name());
129                }
130            }
131        } else {
132            addExecutionError(ExecutionErrors.UnknownPartyName.name(), partyName);
133        }
134        
135        return result;
136    }
137    
138}