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.edit.AppearanceEdit; 020import com.echothree.control.user.core.common.edit.CoreEditFactory; 021import com.echothree.control.user.core.common.form.EditAppearanceForm; 022import com.echothree.control.user.core.common.result.CoreResultFactory; 023import com.echothree.control.user.core.common.result.EditAppearanceResult; 024import com.echothree.control.user.core.common.spec.AppearanceSpec; 025import com.echothree.model.control.core.server.control.AppearanceControl; 026import com.echothree.model.control.core.server.control.ColorControl; 027import com.echothree.model.control.core.server.logic.FontLogic; 028import com.echothree.model.control.party.common.PartyTypes; 029import com.echothree.model.control.security.common.SecurityRoleGroups; 030import com.echothree.model.control.security.common.SecurityRoles; 031import com.echothree.model.data.core.server.entity.Appearance; 032import com.echothree.model.data.core.server.entity.Color; 033import com.echothree.model.data.core.server.entity.FontStyle; 034import com.echothree.model.data.core.server.entity.FontWeight; 035import com.echothree.model.data.user.common.pk.UserVisitPK; 036import com.echothree.util.common.command.EditMode; 037import com.echothree.util.common.message.ExecutionErrors; 038import com.echothree.util.common.validation.FieldDefinition; 039import com.echothree.util.common.validation.FieldType; 040import com.echothree.util.server.control.BaseAbstractEditCommand; 041import com.echothree.util.server.control.CommandSecurityDefinition; 042import com.echothree.util.server.control.PartyTypeDefinition; 043import com.echothree.util.server.control.SecurityRoleDefinition; 044import java.util.List; 045import javax.enterprise.context.Dependent; 046import javax.inject.Inject; 047 048@Dependent 049public class EditAppearanceCommand 050 extends BaseAbstractEditCommand<AppearanceSpec, AppearanceEdit, EditAppearanceResult, Appearance, Appearance> { 051 052 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 053 private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS; 054 private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS; 055 056 static { 057 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of( 058 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 059 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of( 060 new SecurityRoleDefinition(SecurityRoleGroups.Appearance.name(), SecurityRoles.Edit.name()) 061 )) 062 )); 063 064 SPEC_FIELD_DEFINITIONS = List.of( 065 new FieldDefinition("AppearanceName", FieldType.ENTITY_NAME, true, null, null) 066 ); 067 068 EDIT_FIELD_DEFINITIONS = List.of( 069 new FieldDefinition("AppearanceName", FieldType.ENTITY_NAME, true, null, null), 070 new FieldDefinition("TextColorName", FieldType.ENTITY_NAME, false, null, null), 071 new FieldDefinition("BackgroundColorName", FieldType.ENTITY_NAME, false, null, null), 072 new FieldDefinition("FontStyleName", FieldType.ENTITY_NAME, false, null, null), 073 new FieldDefinition("FontWeightName", FieldType.ENTITY_NAME, false, null, null), 074 new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null), 075 new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null), 076 new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L) 077 ); 078 } 079 080 @Inject 081 AppearanceControl appearanceControl; 082 083 @Inject 084 ColorControl colorControl; 085 086 @Inject 087 FontLogic fontLogic; 088 089 090 /** Creates a new instance of EditAppearanceCommand */ 091 public EditAppearanceCommand() { 092 super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 093 } 094 095 @Override 096 public EditAppearanceResult getResult() { 097 return CoreResultFactory.getEditAppearanceResult(); 098 } 099 100 @Override 101 public AppearanceEdit getEdit() { 102 return CoreEditFactory.getAppearanceEdit(); 103 } 104 105 @Override 106 public Appearance getEntity(EditAppearanceResult result) { 107 Appearance appearance; 108 var appearanceName = spec.getAppearanceName(); 109 110 if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) { 111 appearance = appearanceControl.getAppearanceByName(appearanceName); 112 } else { // EditMode.UPDATE 113 appearance = appearanceControl.getAppearanceByNameForUpdate(appearanceName); 114 } 115 116 if(appearance == null) { 117 addExecutionError(ExecutionErrors.UnknownAppearanceName.name(), appearanceName); 118 } 119 120 return appearance; 121 } 122 123 @Override 124 public Appearance getLockEntity(Appearance appearance) { 125 return appearance; 126 } 127 128 @Override 129 public void fillInResult(EditAppearanceResult result, Appearance appearance) { 130 result.setAppearance(appearanceControl.getAppearanceTransfer(getUserVisit(), appearance)); 131 } 132 133 Color textColor; 134 Color backgroundColor; 135 FontStyle fontStyle; 136 FontWeight fontWeight; 137 138 @Override 139 public void doLock(AppearanceEdit edit, Appearance appearance) { 140 var appearanceDescription = appearanceControl.getAppearanceDescription(appearance, getPreferredLanguage()); 141 var appearanceDetail = appearance.getLastDetail(); 142 143 textColor = appearanceDetail.getTextColor(); 144 backgroundColor = appearanceDetail.getBackgroundColor(); 145 fontStyle = appearanceDetail.getFontStyle(); 146 fontWeight = appearanceDetail.getFontWeight(); 147 148 edit.setAppearanceName(appearanceDetail.getAppearanceName()); 149 edit.setTextColorName(textColor == null ? null : textColor.getLastDetail().getColorName()); 150 edit.setBackgroundColorName(backgroundColor == null ? null : backgroundColor.getLastDetail().getColorName()); 151 edit.setFontStyleName(fontStyle == null ? null : fontStyle.getLastDetail().getFontStyleName()); 152 edit.setFontWeightName(fontWeight == null ? null : fontWeight.getLastDetail().getFontWeightName()); 153 edit.setIsDefault(appearanceDetail.getIsDefault().toString()); 154 edit.setSortOrder(appearanceDetail.getSortOrder().toString()); 155 156 if(appearanceDescription != null) { 157 edit.setDescription(appearanceDescription.getDescription()); 158 } 159 } 160 161 @Override 162 public void canUpdate(Appearance appearance) { 163 var appearanceName = edit.getAppearanceName(); 164 var duplicateAppearance = appearanceControl.getAppearanceByName(appearanceName); 165 166 if(duplicateAppearance != null && !appearance.equals(duplicateAppearance)) { 167 addExecutionError(ExecutionErrors.DuplicateAppearanceName.name(), appearanceName); 168 } else { 169 var textColorName = edit.getTextColorName(); 170 171 textColor = textColorName == null ? null : colorControl.getColorByName(textColorName); 172 173 if(textColorName == null || textColor != null) { 174 var backgroundColorName = edit.getBackgroundColorName(); 175 176 backgroundColor = backgroundColorName == null ? null : colorControl.getColorByName(backgroundColorName); 177 178 if(backgroundColorName == null || backgroundColor != null) { 179 var fontStyleName = edit.getFontStyleName(); 180 181 fontStyle = fontStyleName == null ? null : fontLogic.getFontStyleByName(this, fontStyleName); 182 183 if(!hasExecutionErrors()) { 184 var fontWeightName = edit.getFontWeightName(); 185 186 fontWeight = fontWeightName == null ? null : fontLogic.getFontWeightByName(this, fontWeightName); 187 } 188 } else { 189 addExecutionError(ExecutionErrors.UnknownBackgroundColorName.name(), backgroundColorName); 190 } 191 } else { 192 addExecutionError(ExecutionErrors.UnknownTextColorName.name(), textColorName); 193 } 194 } 195 } 196 197 @Override 198 public void doUpdate(Appearance appearance) { 199 var partyPK = getPartyPK(); 200 var appearanceDetailValue = appearanceControl.getAppearanceDetailValueForUpdate(appearance); 201 var appearanceDescription = appearanceControl.getAppearanceDescriptionForUpdate(appearance, getPreferredLanguage()); 202 var description = edit.getDescription(); 203 204 appearanceDetailValue.setAppearanceName(edit.getAppearanceName()); 205 appearanceDetailValue.setTextColorPK(textColor == null ? null : textColor.getPrimaryKey()); 206 appearanceDetailValue.setBackgroundColorPK(backgroundColor == null ? null : backgroundColor.getPrimaryKey()); 207 appearanceDetailValue.setFontStylePK(fontStyle == null ? null : fontStyle.getPrimaryKey()); 208 appearanceDetailValue.setFontWeightPK(fontWeight == null ? null : fontWeight.getPrimaryKey()); 209 appearanceDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault())); 210 appearanceDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder())); 211 212 appearanceControl.updateAppearanceFromValue(appearanceDetailValue, partyPK); 213 214 if(appearanceDescription == null && description != null) { 215 appearanceControl.createAppearanceDescription(appearance, getPreferredLanguage(), description, partyPK); 216 } else { 217 if(appearanceDescription != null && description == null) { 218 appearanceControl.deleteAppearanceDescription(appearanceDescription, partyPK); 219 } else { 220 if(appearanceDescription != null && description != null) { 221 var appearanceDescriptionValue = appearanceControl.getAppearanceDescriptionValue(appearanceDescription); 222 223 appearanceDescriptionValue.setDescription(description); 224 appearanceControl.updateAppearanceDescriptionFromValue(appearanceDescriptionValue, partyPK); 225 } 226 } 227 } 228 } 229 230}