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.GenderEdit;
020import com.echothree.control.user.party.common.edit.PartyEditFactory;
021import com.echothree.control.user.party.common.form.EditGenderForm;
022import com.echothree.control.user.party.common.result.PartyResultFactory;
023import com.echothree.control.user.party.common.spec.GenderSpec;
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.Session;
033import java.util.ArrayList;
034import java.util.List;
035import javax.enterprise.context.Dependent;
036
037@Dependent
038public class EditGenderCommand
039        extends BaseEditCommand<GenderSpec, GenderEdit> {
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("GenderName", FieldType.ENTITY_NAME, true, null, null)
047                );
048        
049        EDIT_FIELD_DEFINITIONS = List.of(
050                new FieldDefinition("GenderName", FieldType.ENTITY_NAME, true, null, null),
051                new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null),
052                new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null),
053                new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L)
054                );
055    }
056    
057    /** Creates a new instance of EditGenderCommand */
058    public EditGenderCommand() {
059        super(null, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
060    }
061    
062    @Override
063    protected BaseResult execute() {
064        var partyControl = Session.getModelController(PartyControl.class);
065        var result = PartyResultFactory.getEditGenderResult();
066        
067        if(editMode.equals(EditMode.LOCK)) {
068            var genderName = spec.getGenderName();
069            var gender = partyControl.getGenderByName(genderName);
070            
071            if(gender != null) {
072                result.setGender(partyControl.getGenderTransfer(getUserVisit(), gender));
073                
074                if(lockEntity(gender)) {
075                    var genderDescription = partyControl.getGenderDescription(gender, getPreferredLanguage());
076                    var edit = PartyEditFactory.getGenderEdit();
077                    var genderDetail = gender.getLastDetail();
078                    
079                    result.setEdit(edit);
080                    edit.setGenderName(genderDetail.getGenderName());
081                    edit.setIsDefault(genderDetail.getIsDefault().toString());
082                    edit.setSortOrder(genderDetail.getSortOrder().toString());
083                    
084                    if(genderDescription != null)
085                        edit.setDescription(genderDescription.getDescription());
086                } else {
087                    addExecutionError(ExecutionErrors.EntityLockFailed.name());
088                }
089                
090                result.setEntityLock(getEntityLockTransfer(gender));
091            } else {
092                addExecutionError(ExecutionErrors.UnknownGenderName.name(), genderName);
093            }
094        } else if(editMode.equals(EditMode.UPDATE)) {
095            var genderName = spec.getGenderName();
096            var gender = partyControl.getGenderByNameForUpdate(genderName);
097            
098            if(gender != null) {
099                genderName = edit.getGenderName();
100                var duplicateGender = partyControl.getGenderByName(genderName);
101                
102                if(duplicateGender == null || gender.equals(duplicateGender)) {
103                    if(lockEntityForUpdate(gender)) {
104                        try {
105                            var partyPK = getPartyPK();
106                            var genderDetailValue = partyControl.getGenderDetailValueForUpdate(gender);
107                            var genderDescription = partyControl.getGenderDescriptionForUpdate(gender, getPreferredLanguage());
108                            var description = edit.getDescription();
109                            
110                            genderDetailValue.setGenderName(edit.getGenderName());
111                            genderDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault()));
112                            genderDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder()));
113                            
114                            partyControl.updateGenderFromValue(genderDetailValue, partyPK);
115                            
116                            if(genderDescription == null && description != null) {
117                                partyControl.createGenderDescription(gender, getPreferredLanguage(), description, partyPK);
118                            } else if(genderDescription != null && description == null) {
119                                partyControl.deleteGenderDescription(genderDescription, partyPK);
120                            } else if(genderDescription != null && description != null) {
121                                var genderDescriptionValue = partyControl.getGenderDescriptionValue(genderDescription);
122                                
123                                genderDescriptionValue.setDescription(description);
124                                partyControl.updateGenderDescriptionFromValue(genderDescriptionValue, partyPK);
125                            }
126                        } finally {
127                            unlockEntity(gender);
128                        }
129                    } else {
130                        addExecutionError(ExecutionErrors.EntityLockStale.name());
131                    }
132                } else {
133                    addExecutionError(ExecutionErrors.DuplicateGenderName.name(), genderName);
134                }
135            } else {
136                addExecutionError(ExecutionErrors.UnknownGenderName.name(), genderName);
137            }
138            
139            if(hasExecutionErrors()) {
140                result.setGender(partyControl.getGenderTransfer(getUserVisit(), gender));
141                result.setEntityLock(getEntityLockTransfer(gender));
142            }
143        }
144        
145        return result;
146    }
147    
148}