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