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.party.server.command; 018 019import com.echothree.control.user.party.common.edit.PartyEditFactory; 020import com.echothree.control.user.party.common.edit.ProfileEdit; 021import com.echothree.control.user.party.common.form.EditProfileForm; 022import com.echothree.control.user.party.common.result.EditProfileResult; 023import com.echothree.control.user.party.common.result.PartyResultFactory; 024import com.echothree.control.user.party.common.spec.PartySpec; 025import com.echothree.model.control.core.common.MimeTypeUsageTypes; 026import com.echothree.model.control.core.server.logic.MimeTypeLogic; 027import com.echothree.model.control.icon.common.IconConstants; 028import com.echothree.model.control.icon.server.control.IconControl; 029import com.echothree.model.control.party.common.PartyTypes; 030import com.echothree.model.control.party.server.control.PartyControl; 031import com.echothree.model.data.core.server.entity.MimeType; 032import com.echothree.model.data.icon.server.entity.Icon; 033import com.echothree.model.data.party.server.entity.BirthdayFormat; 034import com.echothree.model.data.party.server.entity.Gender; 035import com.echothree.model.data.party.server.entity.Party; 036import com.echothree.model.data.party.server.entity.Profile; 037import com.echothree.model.data.user.common.pk.UserVisitPK; 038import com.echothree.util.common.command.EditMode; 039import com.echothree.util.common.message.ExecutionErrors; 040import com.echothree.util.common.validation.FieldDefinition; 041import com.echothree.util.common.validation.FieldType; 042import com.echothree.util.server.control.BaseAbstractEditCommand; 043import com.echothree.util.server.string.DateUtils; 044import java.util.List; 045import javax.enterprise.context.Dependent; 046import javax.inject.Inject; 047 048@Dependent 049public class EditProfileCommand 050 extends BaseAbstractEditCommand<PartySpec, ProfileEdit, EditProfileResult, Profile, Party> { 051 052 private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS; 053 private final static List<FieldDefinition> customerEditFieldDefinitions; 054 private final static List<FieldDefinition> otherEditFieldDefinitions; 055 056 static { 057 SPEC_FIELD_DEFINITIONS = List.of( 058 new FieldDefinition("PartyName", FieldType.ENTITY_NAME, false, null, null) 059 ); 060 061 customerEditFieldDefinitions = List.of( 062 new FieldDefinition("Nickname", FieldType.STRING, true, 1L, 40L), 063 new FieldDefinition("IconName", FieldType.ENTITY_NAME, false, null, null), 064 new FieldDefinition("Pronunciation", FieldType.STRING, false, 1L, 200L), 065 new FieldDefinition("GenderName", FieldType.ENTITY_NAME, false, null, null), 066 new FieldDefinition("Pronouns", FieldType.STRING, false, 1L, 50L), 067 new FieldDefinition("Birthday", FieldType.DATE, false, null, null), 068 new FieldDefinition("BirthdayFormatName", FieldType.ENTITY_NAME, true, null, null), 069 new FieldDefinition("Occupation", FieldType.STRING, false, 1L, 200L), 070 new FieldDefinition("Hobbies", FieldType.STRING, false, 1L, 200L), 071 new FieldDefinition("Location", FieldType.STRING, false, 1L, 60L), 072 new FieldDefinition("BioMimeTypeName", FieldType.MIME_TYPE, false, null, null), 073 new FieldDefinition("Bio", FieldType.STRING, false, 1L, 512L), 074 new FieldDefinition("SignatureMimeTypeName", FieldType.MIME_TYPE, false, null, null), 075 new FieldDefinition("Signature", FieldType.STRING, false, 1L, 512L) 076 ); 077 078 otherEditFieldDefinitions = List.of( 079 new FieldDefinition("Nickname", FieldType.STRING, false, 1L, 40L), 080 new FieldDefinition("IconName", FieldType.ENTITY_NAME, false, null, null), 081 new FieldDefinition("Pronunciation", FieldType.STRING, false, 1L, 200L), 082 new FieldDefinition("GenderName", FieldType.ENTITY_NAME, false, null, null), 083 new FieldDefinition("Pronouns", FieldType.STRING, false, 1L, 50L), 084 new FieldDefinition("Birthday", FieldType.DATE, false, null, null), 085 new FieldDefinition("BirthdayFormatName", FieldType.ENTITY_NAME, true, null, null), 086 new FieldDefinition("Occupation", FieldType.STRING, false, 1L, 200L), 087 new FieldDefinition("Hobbies", FieldType.STRING, false, 1L, 200L), 088 new FieldDefinition("Location", FieldType.STRING, false, 1L, 60L), 089 new FieldDefinition("BioMimeTypeName", FieldType.MIME_TYPE, false, null, null), 090 new FieldDefinition("Bio", FieldType.STRING, false, 1L, 512L), 091 new FieldDefinition("SignatureMimeTypeName", FieldType.MIME_TYPE, false, null, null), 092 new FieldDefinition("Signature", FieldType.STRING, false, 1L, 512L) 093 ); 094 } 095 096 @Inject 097 IconControl iconControl; 098 099 @Inject 100 PartyControl partyControl; 101 102 @Inject 103 MimeTypeLogic mimeTypeLogic; 104 105 106 /** Creates a new instance of EditProfileCommand */ 107 public EditProfileCommand() { 108 super(null, SPEC_FIELD_DEFINITIONS, null); 109 } 110 111 @Override 112 protected List<FieldDefinition> getEditFieldDefinitions() { 113 var partyTypeName = getPartyTypeName(); 114 115 return partyTypeName == null || partyTypeName.equals(PartyTypes.CUSTOMER.name()) ? customerEditFieldDefinitions : otherEditFieldDefinitions; 116 } 117 118 @Override 119 public EditProfileResult getResult() { 120 return PartyResultFactory.getEditProfileResult(); 121 } 122 123 @Override 124 public ProfileEdit getEdit() { 125 return PartyEditFactory.getProfileEdit(); 126 } 127 128 @Override 129 public Profile getEntity(EditProfileResult result) { 130 Profile profile = null; 131 var partyTypeName = getPartyTypeName(); 132 var partyName = partyTypeName.equals(PartyTypes.CUSTOMER.name()) ? null : spec.getPartyName(); 133 var party = partyName == null ? null : partyControl.getPartyByName(partyName); 134 135 if(partyName == null || party != null) { 136 if(party == null) { 137 party = getParty(); 138 } 139 140 if(party == null) { 141 addExecutionError(ExecutionErrors.PartyRequired.name()); 142 } else { 143 if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) { 144 profile = partyControl.getProfile(party); 145 } else { // EditMode.UPDATE 146 profile = partyControl.getProfileForUpdate(party); 147 } 148 149 if(profile == null) { 150 addExecutionError(ExecutionErrors.UnknownProfile.name(), partyName); 151 } 152 } 153 } else { 154 addExecutionError(ExecutionErrors.UnknownPartyName.name(), partyName); 155 } 156 157 return profile; 158 } 159 160 @Override 161 public Party getLockEntity(Profile profile) { 162 return profile.getParty(); 163 } 164 165 @Override 166 public void fillInResult(EditProfileResult result, Profile profile) { 167 result.setParty(partyControl.getPartyTransfer(getUserVisit(), profile.getParty())); 168 } 169 170 Icon icon; 171 Gender gender; 172 MimeType bioMimeType; 173 MimeType signatureMimeType; 174 175 @Override 176 public void doLock(ProfileEdit edit, Profile profile) { 177 icon = profile.getIcon(); 178 gender = profile.getGender(); 179 180 bioMimeType = profile.getBioMimeType(); 181 signatureMimeType = profile.getSignatureMimeType(); 182 183 edit.setNickname(profile.getNickname()); 184 edit.setIconName(icon == null? null: icon.getLastDetail().getIconName()); 185 edit.setPronunciation(profile.getPronunciation()); 186 edit.setGenderName(gender == null? null: gender.getLastDetail().getGenderName()); 187 edit.setPronouns(profile.getPronouns()); 188 edit.setBirthday(DateUtils.getInstance().formatDate(getUserVisit(), profile.getBirthday())); 189 edit.setBirthdayFormatName(profile.getBirthdayFormat().getLastDetail().getBirthdayFormatName()); 190 edit.setOccupation(profile.getOccupation()); 191 edit.setHobbies(profile.getHobbies()); 192 edit.setLocation(profile.getLocation()); 193 edit.setBioMimeTypeName(bioMimeType == null ? null : bioMimeType.getLastDetail().getMimeTypeName()); 194 edit.setBio(profile.getBio()); 195 edit.setSignatureMimeTypeName(signatureMimeType == null ? null : signatureMimeType.getLastDetail().getMimeTypeName()); 196 edit.setSignature(profile.getSignature()); 197 } 198 199 BirthdayFormat birthdayFormat; 200 201 @Override 202 protected void canUpdate(Profile profile) { 203 var bioMimeTypeName = edit.getBioMimeTypeName(); 204 var bio = edit.getBio(); 205 var signatureMimeTypeName = edit.getSignatureMimeTypeName(); 206 var signature = edit.getSignature(); 207 208 bioMimeType = mimeTypeLogic.checkMimeType(this, bioMimeTypeName, bio, MimeTypeUsageTypes.TEXT.name(), 209 ExecutionErrors.MissingRequiredBioMimeTypeName.name(), ExecutionErrors.MissingRequiredBio.name(), 210 ExecutionErrors.UnknownBioMimeTypeName.name(), ExecutionErrors.UnknownBioMimeTypeUsage.name()); 211 212 signatureMimeType = mimeTypeLogic.checkMimeType(this, signatureMimeTypeName, signature, MimeTypeUsageTypes.TEXT.name(), 213 ExecutionErrors.MissingRequiredSignatureMimeTypeName.name(), ExecutionErrors.MissingRequiredSignature.name(), 214 ExecutionErrors.UnknownSignatureMimeTypeName.name(), ExecutionErrors.UnknownSignatureMimeTypeUsage.name()); 215 216 var nickname = edit.getNickname(); 217 var duplicateProfile = nickname == null ? null : partyControl.getProfileByNickname(nickname); 218 219 if(duplicateProfile == null || duplicateProfile.getPrimaryKey().equals(profile.getPrimaryKey())) { 220 var iconName = edit.getIconName(); 221 222 icon = iconName == null? null: iconControl.getIconByName(iconName); 223 224 if(iconName == null || icon != null) { 225 if(icon != null) { 226 var iconUsageType = iconControl.getIconUsageTypeByName(IconConstants.IconUsageType_PROFILE); 227 var iconUsage = iconControl.getIconUsage(iconUsageType, icon); 228 229 if(iconUsage == null) { 230 addExecutionError(ExecutionErrors.UnknownIconUsage.name()); 231 } 232 } 233 234 if(!hasExecutionErrors()) { 235 var genderName = edit.getGenderName(); 236 237 gender = genderName == null? null: partyControl.getGenderByName(genderName); 238 239 if(genderName == null || gender != null) { 240 var birthdayFormatName = edit.getBirthdayFormatName(); 241 242 birthdayFormat = birthdayFormatName == null ? null : partyControl.getBirthdayFormatByName(birthdayFormatName); 243 244 if(birthdayFormat == null) { 245 addExecutionError(ExecutionErrors.UnknownBirthdayFormatName.name(), birthdayFormatName); 246 } 247 } else { 248 addExecutionError(ExecutionErrors.UnknownGenderName.name(), genderName); 249 } 250 } 251 } else { 252 addExecutionError(ExecutionErrors.UnknownIconName.name(), iconName); 253 } 254 } else { 255 addExecutionError(ExecutionErrors.DuplicateNickname.name(), nickname); 256 } 257 } 258 259 @Override 260 public void doUpdate(Profile profile) { 261 var profileValue = partyControl.getProfileValue(profile); 262 var nickname = edit.getNickname(); 263 var pronunciation = edit.getPronunciation(); 264 var pronouns = edit.getPronouns(); 265 var birthday = edit.getBirthday(); 266 267 profileValue.setNickname(nickname); 268 profileValue.setIconPK(icon == null ? null : icon.getPrimaryKey()); 269 profileValue.setPronunciation(pronunciation); 270 profileValue.setGenderPK(gender == null ? null : gender.getPrimaryKey()); 271 profileValue.setPronouns(pronouns); 272 profileValue.setBirthday(birthday == null ? null : Integer.valueOf(birthday)); 273 profileValue.setBirthdayFormatPK(birthdayFormat.getPrimaryKey()); 274 profileValue.setOccupation(edit.getOccupation()); 275 profileValue.setHobbies(edit.getHobbies()); 276 profileValue.setLocation(edit.getLocation()); 277 profileValue.setBioMimeTypePK(bioMimeType == null ? null : bioMimeType.getPrimaryKey()); 278 profileValue.setBio(edit.getBio()); 279 profileValue.setSignatureMimeTypePK(signatureMimeType == null ? null : signatureMimeType.getPrimaryKey()); 280 profileValue.setSignature(edit.getSignature()); 281 282 partyControl.updateProfileFromValue(profileValue, getPartyPK()); 283 } 284 285}