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