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 com.echothree.util.server.persistence.Session; 045import java.util.List; 046import javax.enterprise.context.Dependent; 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 /** Creates a new instance of EditAppearanceCommand */ 081 public EditAppearanceCommand() { 082 super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 083 } 084 085 @Override 086 public EditAppearanceResult getResult() { 087 return CoreResultFactory.getEditAppearanceResult(); 088 } 089 090 @Override 091 public AppearanceEdit getEdit() { 092 return CoreEditFactory.getAppearanceEdit(); 093 } 094 095 @Override 096 public Appearance getEntity(EditAppearanceResult result) { 097 var appearanceControl = Session.getModelController(AppearanceControl.class); 098 Appearance appearance; 099 var appearanceName = spec.getAppearanceName(); 100 101 if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) { 102 appearance = appearanceControl.getAppearanceByName(appearanceName); 103 } else { // EditMode.UPDATE 104 appearance = appearanceControl.getAppearanceByNameForUpdate(appearanceName); 105 } 106 107 if(appearance == null) { 108 addExecutionError(ExecutionErrors.UnknownAppearanceName.name(), appearanceName); 109 } 110 111 return appearance; 112 } 113 114 @Override 115 public Appearance getLockEntity(Appearance appearance) { 116 return appearance; 117 } 118 119 @Override 120 public void fillInResult(EditAppearanceResult result, Appearance appearance) { 121 var appearanceControl = Session.getModelController(AppearanceControl.class); 122 123 result.setAppearance(appearanceControl.getAppearanceTransfer(getUserVisit(), appearance)); 124 } 125 126 Color textColor; 127 Color backgroundColor; 128 FontStyle fontStyle; 129 FontWeight fontWeight; 130 131 @Override 132 public void doLock(AppearanceEdit edit, Appearance appearance) { 133 var appearanceControl = Session.getModelController(AppearanceControl.class); 134 var appearanceDescription = appearanceControl.getAppearanceDescription(appearance, getPreferredLanguage()); 135 var appearanceDetail = appearance.getLastDetail(); 136 137 textColor = appearanceDetail.getTextColor(); 138 backgroundColor = appearanceDetail.getBackgroundColor(); 139 fontStyle = appearanceDetail.getFontStyle(); 140 fontWeight = appearanceDetail.getFontWeight(); 141 142 edit.setAppearanceName(appearanceDetail.getAppearanceName()); 143 edit.setTextColorName(textColor == null ? null : textColor.getLastDetail().getColorName()); 144 edit.setBackgroundColorName(backgroundColor == null ? null : backgroundColor.getLastDetail().getColorName()); 145 edit.setFontStyleName(fontStyle == null ? null : fontStyle.getLastDetail().getFontStyleName()); 146 edit.setFontWeightName(fontWeight == null ? null : fontWeight.getLastDetail().getFontWeightName()); 147 edit.setIsDefault(appearanceDetail.getIsDefault().toString()); 148 edit.setSortOrder(appearanceDetail.getSortOrder().toString()); 149 150 if(appearanceDescription != null) { 151 edit.setDescription(appearanceDescription.getDescription()); 152 } 153 } 154 155 @Override 156 public void canUpdate(Appearance appearance) { 157 var appearanceControl = Session.getModelController(AppearanceControl.class); 158 var appearanceName = edit.getAppearanceName(); 159 var duplicateAppearance = appearanceControl.getAppearanceByName(appearanceName); 160 161 if(duplicateAppearance != null && !appearance.equals(duplicateAppearance)) { 162 addExecutionError(ExecutionErrors.DuplicateAppearanceName.name(), appearanceName); 163 } else { 164 var colorControl = Session.getModelController(ColorControl.class); 165 var textColorName = edit.getTextColorName(); 166 167 textColor = textColorName == null ? null : colorControl.getColorByName(textColorName); 168 169 if(textColorName == null || textColor != null) { 170 var backgroundColorName = edit.getBackgroundColorName(); 171 172 backgroundColor = backgroundColorName == null ? null : colorControl.getColorByName(backgroundColorName); 173 174 if(backgroundColorName == null || backgroundColor != null) { 175 var fontStyleName = edit.getFontStyleName(); 176 177 fontStyle = fontStyleName == null ? null : FontLogic.getInstance().getFontStyleByName(this, fontStyleName); 178 179 if(!hasExecutionErrors()) { 180 var fontWeightName = edit.getFontWeightName(); 181 182 fontWeight = fontWeightName == null ? null : FontLogic.getInstance().getFontWeightByName(this, fontWeightName); 183 } 184 } else { 185 addExecutionError(ExecutionErrors.UnknownBackgroundColorName.name(), backgroundColorName); 186 } 187 } else { 188 addExecutionError(ExecutionErrors.UnknownTextColorName.name(), textColorName); 189 } 190 } 191 } 192 193 @Override 194 public void doUpdate(Appearance appearance) { 195 var appearanceControl = Session.getModelController(AppearanceControl.class); 196 var partyPK = getPartyPK(); 197 var appearanceDetailValue = appearanceControl.getAppearanceDetailValueForUpdate(appearance); 198 var appearanceDescription = appearanceControl.getAppearanceDescriptionForUpdate(appearance, getPreferredLanguage()); 199 var description = edit.getDescription(); 200 201 appearanceDetailValue.setAppearanceName(edit.getAppearanceName()); 202 appearanceDetailValue.setTextColorPK(textColor == null ? null : textColor.getPrimaryKey()); 203 appearanceDetailValue.setBackgroundColorPK(backgroundColor == null ? null : backgroundColor.getPrimaryKey()); 204 appearanceDetailValue.setFontStylePK(fontStyle == null ? null : fontStyle.getPrimaryKey()); 205 appearanceDetailValue.setFontWeightPK(fontWeight == null ? null : fontWeight.getPrimaryKey()); 206 appearanceDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault())); 207 appearanceDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder())); 208 209 appearanceControl.updateAppearanceFromValue(appearanceDetailValue, partyPK); 210 211 if(appearanceDescription == null && description != null) { 212 appearanceControl.createAppearanceDescription(appearance, getPreferredLanguage(), description, partyPK); 213 } else { 214 if(appearanceDescription != null && description == null) { 215 appearanceControl.deleteAppearanceDescription(appearanceDescription, partyPK); 216 } else { 217 if(appearanceDescription != null && description != null) { 218 var appearanceDescriptionValue = appearanceControl.getAppearanceDescriptionValue(appearanceDescription); 219 220 appearanceDescriptionValue.setDescription(description); 221 appearanceControl.updateAppearanceDescriptionFromValue(appearanceDescriptionValue, partyPK); 222 } 223 } 224 } 225 } 226 227}