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