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.MoodDescriptionEdit;
020import com.echothree.control.user.party.common.edit.PartyEditFactory;
021import com.echothree.control.user.party.common.form.EditMoodDescriptionForm;
022import com.echothree.control.user.party.common.result.EditMoodDescriptionResult;
023import com.echothree.control.user.party.common.result.PartyResultFactory;
024import com.echothree.control.user.party.common.spec.MoodDescriptionSpec;
025import com.echothree.model.control.party.server.control.PartyControl;
026import com.echothree.model.data.party.server.entity.Language;
027import com.echothree.model.data.party.server.entity.Mood;
028import com.echothree.model.data.party.server.entity.MoodDescription;
029import com.echothree.model.data.party.server.value.MoodDescriptionValue;
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 EditMoodDescriptionCommand
043        extends BaseEditCommand<MoodDescriptionSpec, MoodDescriptionEdit> {
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("MoodName", 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 EditMoodDescriptionCommand */
060    public EditMoodDescriptionCommand(UserVisitPK userVisitPK, EditMoodDescriptionForm 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        EditMoodDescriptionResult result = PartyResultFactory.getEditMoodDescriptionResult();
068        String moodName = spec.getMoodName();
069        Mood mood = partyControl.getMoodByName(moodName);
070        
071        if(mood != null) {
072            String languageIsoName = spec.getLanguageIsoName();
073            Language language = partyControl.getLanguageByIsoName(languageIsoName);
074            
075            if(language != null) {
076                if(editMode.equals(EditMode.LOCK)) {
077                    MoodDescription moodDescription = partyControl.getMoodDescription(mood, language);
078                    
079                    if(moodDescription != null) {
080                        result.setMoodDescription(partyControl.getMoodDescriptionTransfer(getUserVisit(), moodDescription));
081                        
082                        if(lockEntity(mood)) {
083                            MoodDescriptionEdit edit = PartyEditFactory.getMoodDescriptionEdit();
084                            
085                            result.setEdit(edit);
086                            edit.setDescription(moodDescription.getDescription());
087                        } else {
088                            addExecutionError(ExecutionErrors.EntityLockFailed.name());
089                        }
090                        
091                        result.setEntityLock(getEntityLockTransfer(mood));
092                    } else {
093                        addExecutionError(ExecutionErrors.UnknownMoodDescription.name());
094                    }
095                } else if(editMode.equals(EditMode.UPDATE)) {
096                    MoodDescriptionValue moodDescriptionValue = partyControl.getMoodDescriptionValueForUpdate(mood, language);
097                    
098                    if(moodDescriptionValue != null) {
099                        if(lockEntityForUpdate(mood)) {
100                            try {
101                                String description = edit.getDescription();
102                                
103                                moodDescriptionValue.setDescription(description);
104                                
105                                partyControl.updateMoodDescriptionFromValue(moodDescriptionValue, getPartyPK());
106                            } finally {
107                                unlockEntity(mood);
108                            }
109                        } else {
110                            addExecutionError(ExecutionErrors.EntityLockStale.name());
111                        }
112                    } else {
113                        addExecutionError(ExecutionErrors.UnknownMoodDescription.name());
114                    }
115                }
116            } else {
117                addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), languageIsoName);
118            }
119        } else {
120            addExecutionError(ExecutionErrors.UnknownMoodName.name(), moodName);
121        }
122        
123        return result;
124    }
125    
126}