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.core.server.command;
018
019import com.echothree.control.user.core.common.form.CreateAppearanceForm;
020import com.echothree.control.user.core.common.result.CoreResultFactory;
021import com.echothree.model.control.core.server.control.AppearanceControl;
022import com.echothree.model.control.core.server.control.ColorControl;
023import com.echothree.model.control.core.server.logic.FontLogic;
024import com.echothree.model.control.party.common.PartyTypes;
025import com.echothree.model.control.security.common.SecurityRoleGroups;
026import com.echothree.model.control.security.common.SecurityRoles;
027import com.echothree.model.control.sequence.common.SequenceTypes;
028import com.echothree.model.control.sequence.server.control.SequenceControl;
029import com.echothree.model.control.sequence.server.logic.SequenceGeneratorLogic;
030import com.echothree.model.data.user.common.pk.UserVisitPK;
031import com.echothree.util.common.command.BaseResult;
032import com.echothree.util.common.message.ExecutionErrors;
033import com.echothree.util.common.validation.FieldDefinition;
034import com.echothree.util.common.validation.FieldType;
035import com.echothree.util.server.control.BaseSimpleCommand;
036import com.echothree.util.server.control.CommandSecurityDefinition;
037import com.echothree.util.server.control.PartyTypeDefinition;
038import com.echothree.util.server.control.SecurityRoleDefinition;
039import java.util.List;
040import javax.enterprise.context.Dependent;
041import javax.inject.Inject;
042
043@Dependent
044public class CreateAppearanceCommand
045        extends BaseSimpleCommand<CreateAppearanceForm> {
046    
047    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
048    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
049    
050    static {
051        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
052                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
053                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
054                        new SecurityRoleDefinition(SecurityRoleGroups.Appearance.name(), SecurityRoles.Create.name())
055                ))
056        ));
057        
058        FORM_FIELD_DEFINITIONS = List.of(
059                new FieldDefinition("AppearanceName", FieldType.ENTITY_NAME, false, null, null),
060                new FieldDefinition("TextColorName", FieldType.ENTITY_NAME, false, null, null),
061                new FieldDefinition("BackgroundColorName", FieldType.ENTITY_NAME, false, null, null),
062                new FieldDefinition("FontStyleName", FieldType.ENTITY_NAME, false, null, null),
063                new FieldDefinition("FontWeightName", FieldType.ENTITY_NAME, false, null, null),
064                new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null),
065                new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null),
066                new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L)
067        );
068    }
069
070    @Inject
071    AppearanceControl appearanceControl;
072
073    @Inject
074    ColorControl colorControl;
075
076    @Inject
077    SequenceControl sequenceControl;
078
079    @Inject
080    FontLogic fontLogic;
081
082    @Inject
083    SequenceGeneratorLogic sequenceGeneratorLogic;
084
085    
086    /** Creates a new instance of CreateAppearanceCommand */
087    public CreateAppearanceCommand() {
088        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false);
089    }
090    
091    @Override
092    protected BaseResult execute() {
093        var result = CoreResultFactory.getCreateAppearanceResult();
094        var appearanceName = form.getAppearanceName();
095        
096        if(appearanceName == null) {
097            var sequence = sequenceControl.getDefaultSequenceUsingNames(SequenceTypes.APPEARANCE.name());
098            
099            appearanceName = sequenceGeneratorLogic.getNextSequenceValue(sequence);
100        }
101
102        var appearance = appearanceControl.getAppearanceByName(appearanceName);
103        
104        if(appearance == null) {
105            var textColorName = form.getTextColorName();
106            var textColor = textColorName == null ? null : colorControl.getColorByName(textColorName);
107            
108            if(textColorName == null || textColor != null) {
109                var backgroundColorName = form.getBackgroundColorName();
110                var backgroundColor = backgroundColorName == null ? null : colorControl.getColorByName(backgroundColorName);
111
112                if(backgroundColorName == null || backgroundColor != null) {
113                    var fontStyleName = form.getFontStyleName();
114                    var fontStyle = fontStyleName == null ? null : fontLogic.getFontStyleByName(this, fontStyleName);
115                    
116                    if(!hasExecutionErrors()) {
117                        var fontWeightName = form.getFontWeightName();
118                        var fontWeight = fontWeightName == null ? null : fontLogic.getFontWeightByName(this, fontWeightName);
119
120                        if(!hasExecutionErrors()) {
121                            var partyPK = getPartyPK();
122                            var isDefault = Boolean.valueOf(form.getIsDefault());
123                            var sortOrder = Integer.valueOf(form.getSortOrder());
124                            var description = form.getDescription();
125
126                            appearance = appearanceControl.createAppearance(appearanceName, textColor, backgroundColor, fontStyle, fontWeight, isDefault, sortOrder,
127                                    partyPK);
128
129                            if(description != null) {
130                                appearanceControl.createAppearanceDescription(appearance, getPreferredLanguage(), description, partyPK);
131                            }
132                        }
133                    }
134                } else {
135                addExecutionError(ExecutionErrors.UnknownBackgroundColorName.name(), backgroundColorName);
136                }
137            } else {
138                addExecutionError(ExecutionErrors.UnknownTextColorName.name(), textColorName);
139            }
140        } else {
141            addExecutionError(ExecutionErrors.DuplicateAppearanceName.name(), appearanceName);
142        }
143                
144        if(appearance != null) {
145            result.setAppearanceName(appearance.getLastDetail().getAppearanceName());
146            result.setEntityRef(appearance.getPrimaryKey().getEntityRef());
147        }
148        
149        return result;
150    }
151    
152}