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