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