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