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.party.server.command;
018
019import com.echothree.control.user.party.common.edit.NameSuffixEdit;
020import com.echothree.control.user.party.common.edit.PartyEditFactory;
021import com.echothree.control.user.party.common.form.EditNameSuffixForm;
022import com.echothree.control.user.party.common.result.PartyResultFactory;
023import com.echothree.control.user.party.common.spec.NameSuffixSpec;
024import com.echothree.model.control.party.server.control.PartyControl;
025import com.echothree.model.data.user.common.pk.UserVisitPK;
026import com.echothree.util.common.message.ExecutionErrors;
027import com.echothree.util.common.validation.FieldDefinition;
028import com.echothree.util.common.validation.FieldType;
029import com.echothree.util.common.command.BaseResult;
030import com.echothree.util.common.command.EditMode;
031import com.echothree.util.server.control.BaseEditCommand;
032import com.echothree.util.server.persistence.EntityPermission;
033import java.util.List;
034import javax.enterprise.context.Dependent;
035import javax.inject.Inject;
036
037@Dependent
038public class EditNameSuffixCommand
039        extends BaseEditCommand<NameSuffixSpec, NameSuffixEdit> {
040    
041    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
042    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
043    
044    static {
045        SPEC_FIELD_DEFINITIONS = List.of(
046                new FieldDefinition("NameSuffixId", FieldType.ID, true, null, null)
047        );
048        
049        EDIT_FIELD_DEFINITIONS = List.of(
050                new FieldDefinition("Description", FieldType.STRING, true, 1L, 132L),
051                new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null),
052                new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null)
053        );
054    }
055
056    @Inject
057    PartyControl partyControl;
058
059    
060    /** Creates a new instance of EditNameSuffixCommand */
061    public EditNameSuffixCommand() {
062        super(null, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
063    }
064    
065    @Override
066    protected BaseResult execute() {
067        var result = PartyResultFactory.getEditNameSuffixResult();
068        
069        if(editMode.equals(EditMode.LOCK)) {
070            var nameSuffixId = spec.getNameSuffixId();
071            var nameSuffix = partyControl.convertNameSuffixIdToEntity(nameSuffixId, EntityPermission.READ_ONLY);
072            
073            if(nameSuffix != null) {
074                result.setNameSuffix(partyControl.getNameSuffixTransfer(getUserVisit(), nameSuffix));
075                
076                if(lockEntity(nameSuffix)) {
077                    var edit = PartyEditFactory.getNameSuffixEdit();
078                    var nameSuffixDetail = nameSuffix.getLastDetail();
079                    
080                    result.setEdit(edit);
081                    edit.setDescription(nameSuffixDetail.getDescription());
082                    edit.setIsDefault(nameSuffixDetail.getIsDefault().toString());
083                    edit.setSortOrder(nameSuffixDetail.getSortOrder().toString());
084                } else {
085                    addExecutionError(ExecutionErrors.EntityLockFailed.name());
086                }
087                
088                result.setEntityLock(getEntityLockTransfer(nameSuffix));
089            } else {
090                addExecutionError(ExecutionErrors.UnknownNameSuffixId.name(), nameSuffixId);
091            }
092        } else if(editMode.equals(EditMode.UPDATE)) {
093            var nameSuffixId = spec.getNameSuffixId();
094            var nameSuffixPK = partyControl.convertNameSuffixIdToPK(nameSuffixId);
095            
096            if(nameSuffixPK != null) {
097                if(lockEntityForUpdate(nameSuffixPK)) {
098                    try {
099                        var partyPK = getPartyPK();
100                        var nameSuffixDetailValue = partyControl.getNameSuffixDetailValueByPKForUpdate(nameSuffixPK);
101                        
102                        nameSuffixDetailValue.setDescription(edit.getDescription());
103                        nameSuffixDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault()));
104                        nameSuffixDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder()));
105                        
106                        partyControl.updateNameSuffixFromValue(nameSuffixDetailValue, partyPK);
107                    } finally {
108                        unlockEntity(nameSuffixPK);
109                    }
110                } else {
111                    addExecutionError(ExecutionErrors.EntityLockStale.name());
112                }
113            } else {
114                addExecutionError(ExecutionErrors.UnknownNameSuffixId.name(), nameSuffixId);
115            }
116        }
117        
118        return result;
119    }
120    
121}