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