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.MoodDescriptionEdit; 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.MoodDescriptionSpec; 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 com.echothree.util.server.persistence.Session; 031import java.util.List; 032import javax.enterprise.context.Dependent; 033 034@Dependent 035public class EditMoodDescriptionCommand 036 extends BaseEditCommand<MoodDescriptionSpec, MoodDescriptionEdit> { 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("MoodName", 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 /** Creates a new instance of EditMoodDescriptionCommand */ 053 public EditMoodDescriptionCommand() { 054 super(null, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 055 } 056 057 @Override 058 protected BaseResult execute() { 059 var partyControl = Session.getModelController(PartyControl.class); 060 var result = PartyResultFactory.getEditMoodDescriptionResult(); 061 var moodName = spec.getMoodName(); 062 var mood = partyControl.getMoodByName(moodName); 063 064 if(mood != null) { 065 var languageIsoName = spec.getLanguageIsoName(); 066 var language = partyControl.getLanguageByIsoName(languageIsoName); 067 068 if(language != null) { 069 if(editMode.equals(EditMode.LOCK)) { 070 var moodDescription = partyControl.getMoodDescription(mood, language); 071 072 if(moodDescription != null) { 073 result.setMoodDescription(partyControl.getMoodDescriptionTransfer(getUserVisit(), moodDescription)); 074 075 if(lockEntity(mood)) { 076 var edit = PartyEditFactory.getMoodDescriptionEdit(); 077 078 result.setEdit(edit); 079 edit.setDescription(moodDescription.getDescription()); 080 } else { 081 addExecutionError(ExecutionErrors.EntityLockFailed.name()); 082 } 083 084 result.setEntityLock(getEntityLockTransfer(mood)); 085 } else { 086 addExecutionError(ExecutionErrors.UnknownMoodDescription.name()); 087 } 088 } else if(editMode.equals(EditMode.UPDATE)) { 089 var moodDescriptionValue = partyControl.getMoodDescriptionValueForUpdate(mood, language); 090 091 if(moodDescriptionValue != null) { 092 if(lockEntityForUpdate(mood)) { 093 try { 094 var description = edit.getDescription(); 095 096 moodDescriptionValue.setDescription(description); 097 098 partyControl.updateMoodDescriptionFromValue(moodDescriptionValue, getPartyPK()); 099 } finally { 100 unlockEntity(mood); 101 } 102 } else { 103 addExecutionError(ExecutionErrors.EntityLockStale.name()); 104 } 105 } else { 106 addExecutionError(ExecutionErrors.UnknownMoodDescription.name()); 107 } 108 } 109 } else { 110 addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), languageIsoName); 111 } 112 } else { 113 addExecutionError(ExecutionErrors.UnknownMoodName.name(), moodName); 114 } 115 116 return result; 117 } 118 119}