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.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.PartyResultFactory; 023import com.echothree.control.user.party.common.spec.MoodDescriptionSpec; 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 EditMoodDescriptionCommand 040 extends BaseEditCommand<MoodDescriptionSpec, MoodDescriptionEdit> { 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<>(2); 047 temp.add(new FieldDefinition("MoodName", FieldType.ENTITY_NAME, true, null, null)); 048 temp.add(new FieldDefinition("LanguageIsoName", FieldType.ENTITY_NAME, true, null, null)); 049 SPEC_FIELD_DEFINITIONS = Collections.unmodifiableList(temp); 050 051 temp = new ArrayList<>(1); 052 temp.add(new FieldDefinition("Description", FieldType.STRING, true, 1L, 132L)); 053 EDIT_FIELD_DEFINITIONS = Collections.unmodifiableList(temp); 054 } 055 056 /** Creates a new instance of EditMoodDescriptionCommand */ 057 public EditMoodDescriptionCommand() { 058 super(null, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 059 } 060 061 @Override 062 protected BaseResult execute() { 063 var partyControl = Session.getModelController(PartyControl.class); 064 var result = PartyResultFactory.getEditMoodDescriptionResult(); 065 var moodName = spec.getMoodName(); 066 var mood = partyControl.getMoodByName(moodName); 067 068 if(mood != null) { 069 var languageIsoName = spec.getLanguageIsoName(); 070 var language = partyControl.getLanguageByIsoName(languageIsoName); 071 072 if(language != null) { 073 if(editMode.equals(EditMode.LOCK)) { 074 var moodDescription = partyControl.getMoodDescription(mood, language); 075 076 if(moodDescription != null) { 077 result.setMoodDescription(partyControl.getMoodDescriptionTransfer(getUserVisit(), moodDescription)); 078 079 if(lockEntity(mood)) { 080 var edit = PartyEditFactory.getMoodDescriptionEdit(); 081 082 result.setEdit(edit); 083 edit.setDescription(moodDescription.getDescription()); 084 } else { 085 addExecutionError(ExecutionErrors.EntityLockFailed.name()); 086 } 087 088 result.setEntityLock(getEntityLockTransfer(mood)); 089 } else { 090 addExecutionError(ExecutionErrors.UnknownMoodDescription.name()); 091 } 092 } else if(editMode.equals(EditMode.UPDATE)) { 093 var moodDescriptionValue = partyControl.getMoodDescriptionValueForUpdate(mood, language); 094 095 if(moodDescriptionValue != null) { 096 if(lockEntityForUpdate(mood)) { 097 try { 098 var description = edit.getDescription(); 099 100 moodDescriptionValue.setDescription(description); 101 102 partyControl.updateMoodDescriptionFromValue(moodDescriptionValue, getPartyPK()); 103 } finally { 104 unlockEntity(mood); 105 } 106 } else { 107 addExecutionError(ExecutionErrors.EntityLockStale.name()); 108 } 109 } else { 110 addExecutionError(ExecutionErrors.UnknownMoodDescription.name()); 111 } 112 } 113 } else { 114 addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), languageIsoName); 115 } 116 } else { 117 addExecutionError(ExecutionErrors.UnknownMoodName.name(), moodName); 118 } 119 120 return result; 121 } 122 123}