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.PartyEditFactory;
020import com.echothree.control.user.party.common.edit.PersonalTitleEdit;
021import com.echothree.control.user.party.common.form.EditPersonalTitleForm;
022import com.echothree.control.user.party.common.result.PartyResultFactory;
023import com.echothree.control.user.party.common.spec.PersonalTitleSpec;
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 EditPersonalTitleCommand
039        extends BaseEditCommand<PersonalTitleSpec, PersonalTitleEdit> {
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("PersonalTitleId", 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 EditPersonalTitleCommand */
061    public EditPersonalTitleCommand() {
062        super(null, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
063    }
064    
065    @Override
066    protected BaseResult execute() {
067        var result = PartyResultFactory.getEditPersonalTitleResult();
068        
069        if(editMode.equals(EditMode.LOCK)) {
070            var personalTitleId = spec.getPersonalTitleId();
071            var personalTitle = partyControl.convertPersonalTitleIdToEntity(personalTitleId, EntityPermission.READ_ONLY);
072            
073            if(personalTitle != null) {
074                result.setPersonalTitle(partyControl.getPersonalTitleTransfer(getUserVisit(), personalTitle));
075                
076                if(lockEntity(personalTitle)) {
077                    var edit = PartyEditFactory.getPersonalTitleEdit();
078                    var personalTitleDetail = personalTitle.getLastDetail();
079                    
080                    result.setEdit(edit);
081                    edit.setDescription(personalTitleDetail.getDescription());
082                    edit.setIsDefault(personalTitleDetail.getIsDefault().toString());
083                    edit.setSortOrder(personalTitleDetail.getSortOrder().toString());
084                } else {
085                    addExecutionError(ExecutionErrors.EntityLockFailed.name());
086                }
087                
088                result.setEntityLock(getEntityLockTransfer(personalTitle));
089            } else {
090                addExecutionError(ExecutionErrors.UnknownPersonalTitleId.name(), personalTitleId);
091            }
092        } else if(editMode.equals(EditMode.UPDATE)) {
093            var personalTitleId = spec.getPersonalTitleId();
094            var personalTitlePK = partyControl.convertPersonalTitleIdToPK(personalTitleId);
095            
096            if(personalTitlePK != null) {
097                if(lockEntityForUpdate(personalTitlePK)) {
098                    try {
099                        var partyPK = getPartyPK();
100                        var personalTitleDetailValue = partyControl.getPersonalTitleDetailValueByPKForUpdate(personalTitlePK);
101                        
102                        personalTitleDetailValue.setDescription(edit.getDescription());
103                        personalTitleDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault()));
104                        personalTitleDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder()));
105                        
106                        partyControl.updatePersonalTitleFromValue(personalTitleDetailValue, partyPK);
107                    } finally {
108                        unlockEntity(personalTitlePK);
109                    }
110                } else {
111                    addExecutionError(ExecutionErrors.EntityLockStale.name());
112                }
113            } else {
114                addExecutionError(ExecutionErrors.UnknownPersonalTitleId.name(), personalTitleId);
115            }
116        }
117        
118        return result;
119    }
120    
121}