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.MoodEdit; 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.MoodSpec; 023import com.echothree.model.control.icon.common.IconConstants; 024import com.echothree.model.control.icon.server.control.IconControl; 025import com.echothree.model.control.party.server.control.PartyControl; 026import com.echothree.util.common.command.BaseResult; 027import com.echothree.util.common.command.EditMode; 028import com.echothree.util.common.message.ExecutionErrors; 029import com.echothree.util.common.validation.FieldDefinition; 030import com.echothree.util.common.validation.FieldType; 031import com.echothree.util.server.control.BaseEditCommand; 032import com.echothree.util.server.persistence.Session; 033import java.util.List; 034import javax.enterprise.context.Dependent; 035 036@Dependent 037public class EditMoodCommand 038 extends BaseEditCommand<MoodSpec, MoodEdit> { 039 040 private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS; 041 private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS; 042 043 static { 044 SPEC_FIELD_DEFINITIONS = List.of( 045 new FieldDefinition("MoodName", FieldType.ENTITY_NAME, true, null, null) 046 ); 047 048 EDIT_FIELD_DEFINITIONS = List.of( 049 new FieldDefinition("MoodName", FieldType.ENTITY_NAME, true, null, null), 050 new FieldDefinition("IconName", FieldType.ENTITY_NAME, false, null, null), 051 new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null), 052 new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null), 053 new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L) 054 ); 055 } 056 057 /** Creates a new instance of EditMoodCommand */ 058 public EditMoodCommand() { 059 super(null, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 060 } 061 062 @Override 063 protected BaseResult execute() { 064 var partyControl = Session.getModelController(PartyControl.class); 065 var result = PartyResultFactory.getEditMoodResult(); 066 067 if(editMode.equals(EditMode.LOCK)) { 068 var moodName = spec.getMoodName(); 069 var mood = partyControl.getMoodByName(moodName); 070 071 if(mood != null) { 072 result.setMood(partyControl.getMoodTransfer(getUserVisit(), mood)); 073 074 if(lockEntity(mood)) { 075 var moodDescription = partyControl.getMoodDescription(mood, getPreferredLanguage()); 076 var edit = PartyEditFactory.getMoodEdit(); 077 var moodDetail = mood.getLastDetail(); 078 var icon = moodDetail.getIcon(); 079 080 result.setEdit(edit); 081 edit.setMoodName(moodDetail.getMoodName()); 082 edit.setIconName(icon == null? null: icon.getLastDetail().getIconName()); 083 edit.setIsDefault(moodDetail.getIsDefault().toString()); 084 edit.setSortOrder(moodDetail.getSortOrder().toString()); 085 086 if(moodDescription != null) 087 edit.setDescription(moodDescription.getDescription()); 088 } else { 089 addExecutionError(ExecutionErrors.EntityLockFailed.name()); 090 } 091 092 result.setEntityLock(getEntityLockTransfer(mood)); 093 } else { 094 addExecutionError(ExecutionErrors.UnknownMoodName.name(), moodName); 095 } 096 } else if(editMode.equals(EditMode.UPDATE)) { 097 var moodName = spec.getMoodName(); 098 var mood = partyControl.getMoodByNameForUpdate(moodName); 099 100 if(mood != null) { 101 moodName = edit.getMoodName(); 102 var duplicateMood = partyControl.getMoodByName(moodName); 103 104 if(duplicateMood == null || mood.equals(duplicateMood)) { 105 var iconControl = Session.getModelController(IconControl.class); 106 var iconName = edit.getIconName(); 107 var icon = iconName == null? null: iconControl.getIconByName(iconName); 108 109 if(iconName == null || icon != null) { 110 if(icon != null) { 111 var iconUsageType = iconControl.getIconUsageTypeByName(IconConstants.IconUsageType_MOOD); 112 var iconUsage = iconControl.getIconUsage(iconUsageType, icon); 113 114 if(iconUsage == null) { 115 addExecutionError(ExecutionErrors.UnknownIconUsage.name()); 116 } 117 } 118 119 if(!hasExecutionErrors()) { 120 if(lockEntityForUpdate(mood)) { 121 try { 122 var partyPK = getPartyPK(); 123 var moodDetailValue = partyControl.getMoodDetailValueForUpdate(mood); 124 var moodDescription = partyControl.getMoodDescriptionForUpdate(mood, getPreferredLanguage()); 125 var description = edit.getDescription(); 126 127 moodDetailValue.setMoodName(edit.getMoodName()); 128 moodDetailValue.setIconPK(icon == null? null: icon.getPrimaryKey()); 129 moodDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault())); 130 moodDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder())); 131 132 partyControl.updateMoodFromValue(moodDetailValue, partyPK); 133 134 if(moodDescription == null && description != null) { 135 partyControl.createMoodDescription(mood, getPreferredLanguage(), description, partyPK); 136 } else if(moodDescription != null && description == null) { 137 partyControl.deleteMoodDescription(moodDescription, partyPK); 138 } else if(moodDescription != null && description != null) { 139 var moodDescriptionValue = partyControl.getMoodDescriptionValue(moodDescription); 140 141 moodDescriptionValue.setDescription(description); 142 partyControl.updateMoodDescriptionFromValue(moodDescriptionValue, partyPK); 143 } 144 } finally { 145 unlockEntity(mood); 146 } 147 } else { 148 addExecutionError(ExecutionErrors.EntityLockStale.name()); 149 } 150 } 151 } else { 152 addExecutionError(ExecutionErrors.UnknownIconName.name(), iconName); 153 } 154 } else { 155 addExecutionError(ExecutionErrors.DuplicateMoodName.name(), moodName); 156 } 157 } else { 158 addExecutionError(ExecutionErrors.UnknownMoodName.name(), moodName); 159 } 160 161 if(hasExecutionErrors()) { 162 result.setMood(partyControl.getMoodTransfer(getUserVisit(), mood)); 163 result.setEntityLock(getEntityLockTransfer(mood)); 164 } 165 } 166 167 return result; 168 } 169 170}