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