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 com.echothree.util.server.persistence.Session; 040import java.util.List; 041import javax.enterprise.context.Dependent; 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 /** Creates a new instance of CreateAppearanceCommand */ 071 public CreateAppearanceCommand() { 072 super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false); 073 } 074 075 @Override 076 protected BaseResult execute() { 077 var result = CoreResultFactory.getCreateAppearanceResult(); 078 var appearanceControl = Session.getModelController(AppearanceControl.class); 079 var appearanceName = form.getAppearanceName(); 080 081 if(appearanceName == null) { 082 var sequenceControl = Session.getModelController(SequenceControl.class); 083 var sequence = sequenceControl.getDefaultSequenceUsingNames(SequenceTypes.APPEARANCE.name()); 084 085 appearanceName = SequenceGeneratorLogic.getInstance().getNextSequenceValue(sequence); 086 } 087 088 var appearance = appearanceControl.getAppearanceByName(appearanceName); 089 090 if(appearance == null) { 091 var colorControl = Session.getModelController(ColorControl.class); 092 var textColorName = form.getTextColorName(); 093 var textColor = textColorName == null ? null : colorControl.getColorByName(textColorName); 094 095 if(textColorName == null || textColor != null) { 096 var backgroundColorName = form.getBackgroundColorName(); 097 var backgroundColor = backgroundColorName == null ? null : colorControl.getColorByName(backgroundColorName); 098 099 if(backgroundColorName == null || backgroundColor != null) { 100 var fontStyleName = form.getFontStyleName(); 101 var fontStyle = fontStyleName == null ? null : FontLogic.getInstance().getFontStyleByName(this, fontStyleName); 102 103 if(!hasExecutionErrors()) { 104 var fontWeightName = form.getFontWeightName(); 105 var fontWeight = fontWeightName == null ? null : FontLogic.getInstance().getFontWeightByName(this, fontWeightName); 106 107 if(!hasExecutionErrors()) { 108 var partyPK = getPartyPK(); 109 var isDefault = Boolean.valueOf(form.getIsDefault()); 110 var sortOrder = Integer.valueOf(form.getSortOrder()); 111 var description = form.getDescription(); 112 113 appearance = appearanceControl.createAppearance(appearanceName, textColor, backgroundColor, fontStyle, fontWeight, isDefault, sortOrder, 114 partyPK); 115 116 if(description != null) { 117 appearanceControl.createAppearanceDescription(appearance, getPreferredLanguage(), description, partyPK); 118 } 119 } 120 } 121 } else { 122 addExecutionError(ExecutionErrors.UnknownBackgroundColorName.name(), backgroundColorName); 123 } 124 } else { 125 addExecutionError(ExecutionErrors.UnknownTextColorName.name(), textColorName); 126 } 127 } else { 128 addExecutionError(ExecutionErrors.DuplicateAppearanceName.name(), appearanceName); 129 } 130 131 if(appearance != null) { 132 result.setAppearanceName(appearance.getLastDetail().getAppearanceName()); 133 result.setEntityRef(appearance.getPrimaryKey().getEntityRef()); 134 } 135 136 return result; 137 } 138 139}