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