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.form.CreateMoodForm; 020import com.echothree.model.control.icon.common.IconConstants; 021import com.echothree.model.control.icon.server.control.IconControl; 022import com.echothree.model.control.party.server.control.PartyControl; 023import com.echothree.model.data.icon.server.entity.Icon; 024import com.echothree.model.data.icon.server.entity.IconUsage; 025import com.echothree.model.data.icon.server.entity.IconUsageType; 026import com.echothree.model.data.party.server.entity.Mood; 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.server.control.BaseSimpleCommand; 033import com.echothree.util.server.persistence.Session; 034import java.util.Arrays; 035import java.util.Collections; 036import java.util.List; 037 038public class CreateMoodCommand 039 extends BaseSimpleCommand<CreateMoodForm> { 040 041 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 042 043 static { 044 FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 045 new FieldDefinition("MoodName", FieldType.ENTITY_NAME, true, null, null), 046 new FieldDefinition("IconName", FieldType.ENTITY_NAME, false, null, null), 047 new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null), 048 new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null), 049 new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L) 050 )); 051 } 052 053 /** Creates a new instance of CreateMoodCommand */ 054 public CreateMoodCommand(UserVisitPK userVisitPK, CreateMoodForm form) { 055 super(userVisitPK, form, null, FORM_FIELD_DEFINITIONS, false); 056 } 057 058 @Override 059 protected BaseResult execute() { 060 var partyControl = Session.getModelController(PartyControl.class); 061 String moodName = form.getMoodName(); 062 Mood mood = partyControl.getMoodByName(moodName); 063 064 if(mood == null) { 065 var iconControl = Session.getModelController(IconControl.class); 066 String iconName = form.getIconName(); 067 Icon icon = iconName == null? null: iconControl.getIconByName(iconName); 068 069 if(iconName == null || icon != null) { 070 if(icon != null) { 071 IconUsageType iconUsageType = iconControl.getIconUsageTypeByName(IconConstants.IconUsageType_MOOD); 072 IconUsage iconUsage = iconControl.getIconUsage(iconUsageType, icon); 073 074 if(iconUsage == null) { 075 addExecutionError(ExecutionErrors.UnknownIconUsage.name()); 076 } 077 } 078 079 if(!hasExecutionErrors()) { 080 var partyPK = getPartyPK(); 081 var isDefault = Boolean.valueOf(form.getIsDefault()); 082 var sortOrder = Integer.valueOf(form.getSortOrder()); 083 var description = form.getDescription(); 084 085 mood = partyControl.createMood(moodName, icon, isDefault, sortOrder, getPartyPK()); 086 087 if(description != null) { 088 partyControl.createMoodDescription(mood, getPreferredLanguage(), description, partyPK); 089 } 090 } 091 } else { 092 addExecutionError(ExecutionErrors.UnknownIconName.name(), iconName); 093 } 094 } else { 095 addExecutionError(ExecutionErrors.DuplicateMoodName.name(), moodName); 096 } 097 098 return null; 099 } 100 101}