001// -------------------------------------------------------------------------------- 002// Copyright 2002-2025 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.Arrays; 041import java.util.Collections; 042import java.util.List; 043import javax.enterprise.context.RequestScoped; 044 045@RequestScoped 046public class CreateAppearanceCommand 047 extends BaseSimpleCommand<CreateAppearanceForm> { 048 049 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 050 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 051 052 static { 053 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList( 054 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 055 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList( 056 new SecurityRoleDefinition(SecurityRoleGroups.Appearance.name(), SecurityRoles.Create.name()) 057 ))) 058 ))); 059 060 FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 061 new FieldDefinition("AppearanceName", FieldType.ENTITY_NAME, false, null, null), 062 new FieldDefinition("TextColorName", FieldType.ENTITY_NAME, false, null, null), 063 new FieldDefinition("BackgroundColorName", FieldType.ENTITY_NAME, false, null, null), 064 new FieldDefinition("FontStyleName", FieldType.ENTITY_NAME, false, null, null), 065 new FieldDefinition("FontWeightName", FieldType.ENTITY_NAME, false, null, null), 066 new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null), 067 new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null), 068 new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L) 069 )); 070 } 071 072 /** Creates a new instance of CreateAppearanceCommand */ 073 public CreateAppearanceCommand() { 074 super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false); 075 } 076 077 @Override 078 protected BaseResult execute() { 079 var result = CoreResultFactory.getCreateAppearanceResult(); 080 var appearanceControl = Session.getModelController(AppearanceControl.class); 081 var appearanceName = form.getAppearanceName(); 082 083 if(appearanceName == null) { 084 var sequenceControl = Session.getModelController(SequenceControl.class); 085 var sequence = sequenceControl.getDefaultSequenceUsingNames(SequenceTypes.APPEARANCE.name()); 086 087 appearanceName = SequenceGeneratorLogic.getInstance().getNextSequenceValue(sequence); 088 } 089 090 var appearance = appearanceControl.getAppearanceByName(appearanceName); 091 092 if(appearance == null) { 093 var colorControl = Session.getModelController(ColorControl.class); 094 var textColorName = form.getTextColorName(); 095 var textColor = textColorName == null ? null : colorControl.getColorByName(textColorName); 096 097 if(textColorName == null || textColor != null) { 098 var backgroundColorName = form.getBackgroundColorName(); 099 var backgroundColor = backgroundColorName == null ? null : colorControl.getColorByName(backgroundColorName); 100 101 if(backgroundColorName == null || backgroundColor != null) { 102 var fontStyleName = form.getFontStyleName(); 103 var fontStyle = fontStyleName == null ? null : FontLogic.getInstance().getFontStyleByName(this, fontStyleName); 104 105 if(!hasExecutionErrors()) { 106 var fontWeightName = form.getFontWeightName(); 107 var fontWeight = fontWeightName == null ? null : FontLogic.getInstance().getFontWeightByName(this, fontWeightName); 108 109 if(!hasExecutionErrors()) { 110 var partyPK = getPartyPK(); 111 var isDefault = Boolean.valueOf(form.getIsDefault()); 112 var sortOrder = Integer.valueOf(form.getSortOrder()); 113 var description = form.getDescription(); 114 115 appearance = appearanceControl.createAppearance(appearanceName, textColor, backgroundColor, fontStyle, fontWeight, isDefault, sortOrder, 116 partyPK); 117 118 if(description != null) { 119 appearanceControl.createAppearanceDescription(appearance, getPreferredLanguage(), description, partyPK); 120 } 121 } 122 } 123 } else { 124 addExecutionError(ExecutionErrors.UnknownBackgroundColorName.name(), backgroundColorName); 125 } 126 } else { 127 addExecutionError(ExecutionErrors.UnknownTextColorName.name(), textColorName); 128 } 129 } else { 130 addExecutionError(ExecutionErrors.DuplicateAppearanceName.name(), appearanceName); 131 } 132 133 if(appearance != null) { 134 result.setAppearanceName(appearance.getLastDetail().getAppearanceName()); 135 result.setEntityRef(appearance.getPrimaryKey().getEntityRef()); 136 } 137 138 return result; 139 } 140 141}