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 com.echothree.util.server.persistence.Session;
034import java.util.List;
035import javax.enterprise.context.Dependent;
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    /** Creates a new instance of EditPersonalTitleCommand */
057    public EditPersonalTitleCommand() {
058        super(null, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
059    }
060    
061    @Override
062    protected BaseResult execute() {
063        var partyControl = Session.getModelController(PartyControl.class);
064        var result = PartyResultFactory.getEditPersonalTitleResult();
065        
066        if(editMode.equals(EditMode.LOCK)) {
067            var personalTitleId = spec.getPersonalTitleId();
068            var personalTitle = partyControl.convertPersonalTitleIdToEntity(personalTitleId, EntityPermission.READ_ONLY);
069            
070            if(personalTitle != null) {
071                result.setPersonalTitle(partyControl.getPersonalTitleTransfer(getUserVisit(), personalTitle));
072                
073                if(lockEntity(personalTitle)) {
074                    var edit = PartyEditFactory.getPersonalTitleEdit();
075                    var personalTitleDetail = personalTitle.getLastDetail();
076                    
077                    result.setEdit(edit);
078                    edit.setDescription(personalTitleDetail.getDescription());
079                    edit.setIsDefault(personalTitleDetail.getIsDefault().toString());
080                    edit.setSortOrder(personalTitleDetail.getSortOrder().toString());
081                } else {
082                    addExecutionError(ExecutionErrors.EntityLockFailed.name());
083                }
084                
085                result.setEntityLock(getEntityLockTransfer(personalTitle));
086            } else {
087                addExecutionError(ExecutionErrors.UnknownPersonalTitleId.name(), personalTitleId);
088            }
089        } else if(editMode.equals(EditMode.UPDATE)) {
090            var personalTitleId = spec.getPersonalTitleId();
091            var personalTitlePK = partyControl.convertPersonalTitleIdToPK(personalTitleId);
092            
093            if(personalTitlePK != null) {
094                if(lockEntityForUpdate(personalTitlePK)) {
095                    try {
096                        var partyPK = getPartyPK();
097                        var personalTitleDetailValue = partyControl.getPersonalTitleDetailValueByPKForUpdate(personalTitlePK);
098                        
099                        personalTitleDetailValue.setDescription(edit.getDescription());
100                        personalTitleDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault()));
101                        personalTitleDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder()));
102                        
103                        partyControl.updatePersonalTitleFromValue(personalTitleDetailValue, partyPK);
104                    } finally {
105                        unlockEntity(personalTitlePK);
106                    }
107                } else {
108                    addExecutionError(ExecutionErrors.EntityLockStale.name());
109                }
110            } else {
111                addExecutionError(ExecutionErrors.UnknownPersonalTitleId.name(), personalTitleId);
112            }
113        }
114        
115        return result;
116    }
117    
118}