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.GenderDescriptionEdit;
020import com.echothree.control.user.party.common.edit.PartyEditFactory;
021import com.echothree.control.user.party.common.result.PartyResultFactory;
022import com.echothree.control.user.party.common.spec.GenderDescriptionSpec;
023import com.echothree.model.control.party.server.control.PartyControl;
024import com.echothree.util.common.command.BaseResult;
025import com.echothree.util.common.command.EditMode;
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.server.control.BaseEditCommand;
030import java.util.List;
031import javax.enterprise.context.Dependent;
032import javax.inject.Inject;
033
034@Dependent
035public class EditGenderDescriptionCommand
036        extends BaseEditCommand<GenderDescriptionSpec, GenderDescriptionEdit> {
037    
038    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
039    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
040    
041    static {
042        SPEC_FIELD_DEFINITIONS = List.of(
043                new FieldDefinition("GenderName", FieldType.ENTITY_NAME, true, null, null),
044                new FieldDefinition("LanguageIsoName", FieldType.ENTITY_NAME, true, null, null)
045        );
046        
047        EDIT_FIELD_DEFINITIONS = List.of(
048                new FieldDefinition("Description", FieldType.STRING, true, 1L, 132L)
049        );
050    }
051
052    @Inject
053    PartyControl partyControl;
054
055    
056    /** Creates a new instance of EditGenderDescriptionCommand */
057    public EditGenderDescriptionCommand() {
058        super(null, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
059    }
060    
061    @Override
062    protected BaseResult execute() {
063        var result = PartyResultFactory.getEditGenderDescriptionResult();
064        var genderName = spec.getGenderName();
065        var gender = partyControl.getGenderByName(genderName);
066        
067        if(gender != null) {
068            var languageIsoName = spec.getLanguageIsoName();
069            var language = partyControl.getLanguageByIsoName(languageIsoName);
070            
071            if(language != null) {
072                if(editMode.equals(EditMode.LOCK)) {
073                    var genderDescription = partyControl.getGenderDescription(gender, language);
074                    
075                    if(genderDescription != null) {
076                        result.setGenderDescription(partyControl.getGenderDescriptionTransfer(getUserVisit(), genderDescription));
077                        
078                        if(lockEntity(gender)) {
079                            var edit = PartyEditFactory.getGenderDescriptionEdit();
080                            
081                            result.setEdit(edit);
082                            edit.setDescription(genderDescription.getDescription());
083                        } else {
084                            addExecutionError(ExecutionErrors.EntityLockFailed.name());
085                        }
086                        
087                        result.setEntityLock(getEntityLockTransfer(gender));
088                    } else {
089                        addExecutionError(ExecutionErrors.UnknownGenderDescription.name());
090                    }
091                } else if(editMode.equals(EditMode.UPDATE)) {
092                    var genderDescriptionValue = partyControl.getGenderDescriptionValueForUpdate(gender, language);
093                    
094                    if(genderDescriptionValue != null) {
095                        if(lockEntityForUpdate(gender)) {
096                            try {
097                                var description = edit.getDescription();
098                                
099                                genderDescriptionValue.setDescription(description);
100                                
101                                partyControl.updateGenderDescriptionFromValue(genderDescriptionValue, getPartyPK());
102                            } finally {
103                                unlockEntity(gender);
104                            }
105                        } else {
106                            addExecutionError(ExecutionErrors.EntityLockStale.name());
107                        }
108                    } else {
109                        addExecutionError(ExecutionErrors.UnknownGenderDescription.name());
110                    }
111                }
112            } else {
113                addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), languageIsoName);
114            }
115        } else {
116            addExecutionError(ExecutionErrors.UnknownGenderName.name(), genderName);
117        }
118        
119        return result;
120    }
121    
122}