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.form.CreateProfileForm; 020import com.echothree.model.control.core.server.control.MimeTypeControl; 021import com.echothree.model.control.icon.common.IconConstants; 022import com.echothree.model.control.icon.server.control.IconControl; 023import com.echothree.model.control.party.common.PartyTypes; 024import com.echothree.model.control.party.server.control.PartyControl; 025import com.echothree.model.data.user.common.pk.UserVisitPK; 026import com.echothree.util.common.command.BaseResult; 027import com.echothree.util.common.message.ExecutionErrors; 028import com.echothree.util.common.validation.FieldDefinition; 029import com.echothree.util.common.validation.FieldType; 030import com.echothree.util.server.control.BaseSimpleCommand; 031import com.echothree.util.server.persistence.Session; 032import java.util.List; 033import javax.enterprise.context.Dependent; 034 035@Dependent 036public class CreateProfileCommand 037 extends BaseSimpleCommand<CreateProfileForm> { 038 039 private final static List<FieldDefinition> customerFormFieldDefinitions; 040 private final static List<FieldDefinition> otherFormFieldDefinitions; 041 042 static { 043 // If a Customer is creating their Profile, then Nickname is a required field. Otherwise, it, along with all the other 044 // fields, are optional. 045 customerFormFieldDefinitions = List.of( 046 new FieldDefinition("Nickname", FieldType.STRING, true, 1L, 40L), 047 new FieldDefinition("IconName", FieldType.ENTITY_NAME, false, null, null), 048 new FieldDefinition("Pronunciation", FieldType.STRING, false, 1L, 200L), 049 new FieldDefinition("GenderName", FieldType.ENTITY_NAME, false, null, null), 050 new FieldDefinition("Pronouns", FieldType.STRING, false, 1L, 50L), 051 new FieldDefinition("Birthday", FieldType.DATE, false, null, null), 052 new FieldDefinition("BirthdayFormatName", FieldType.ENTITY_NAME, true, null, null), 053 new FieldDefinition("Occupation", FieldType.STRING, false, 1L, 200L), 054 new FieldDefinition("Hobbies", FieldType.STRING, false, 1L, 200L), 055 new FieldDefinition("Location", FieldType.STRING, false, 1L, 60L), 056 new FieldDefinition("BioMimeTypeName", FieldType.MIME_TYPE, false, null, null), 057 new FieldDefinition("Bio", FieldType.STRING, false, 1L, 512L), 058 new FieldDefinition("SignatureMimeTypeName", FieldType.MIME_TYPE, false, null, null), 059 new FieldDefinition("Signature", FieldType.STRING, false, 1L, 512L) 060 ); 061 062 otherFormFieldDefinitions = List.of( 063 new FieldDefinition("PartyName", FieldType.ENTITY_NAME, false, null, null), 064 new FieldDefinition("Nickname", FieldType.STRING, false, 1L, 40L), 065 new FieldDefinition("IconName", FieldType.ENTITY_NAME, false, null, null), 066 new FieldDefinition("Pronunciation", FieldType.STRING, false, 1L, 200L), 067 new FieldDefinition("GenderName", FieldType.ENTITY_NAME, false, null, null), 068 new FieldDefinition("Pronouns", FieldType.STRING, false, 1L, 50L), 069 new FieldDefinition("Birthday", FieldType.DATE, false, null, null), 070 new FieldDefinition("BirthdayFormatName", FieldType.ENTITY_NAME, true, null, null), 071 new FieldDefinition("Occupation", FieldType.STRING, false, 1L, 200L), 072 new FieldDefinition("Hobbies", FieldType.STRING, false, 1L, 200L), 073 new FieldDefinition("Location", FieldType.STRING, false, 1L, 60L), 074 new FieldDefinition("BioMimeTypeName", FieldType.MIME_TYPE, false, null, null), 075 new FieldDefinition("Bio", FieldType.STRING, false, 1L, 512L), 076 new FieldDefinition("SignatureMimeTypeName", FieldType.MIME_TYPE, false, null, null), 077 new FieldDefinition("Signature", FieldType.STRING, false, 1L, 512L) 078 ); 079 } 080 081 /** Creates a new instance of CreateProfileCommand */ 082 public CreateProfileCommand() { 083 super(null, null, false); 084 } 085 086 @Override 087 protected List<FieldDefinition> getFormFieldDefinitions() { 088 var partyTypeName = getPartyTypeName(); 089 090 return partyTypeName == null || partyTypeName.equals(PartyTypes.CUSTOMER.name()) ? customerFormFieldDefinitions : otherFormFieldDefinitions; 091 } 092 093 @Override 094 protected BaseResult execute() { 095 var bioMimeTypeName = form.getBioMimeTypeName(); 096 var bio = form.getBio(); 097 var bioParameterCount = (bioMimeTypeName == null ? 0 : 1) + (bio == null ? 0 : 1); 098 var signatureMimeTypeName = form.getSignatureMimeTypeName(); 099 var signature = form.getSignature(); 100 var signatureParameterCount = (signatureMimeTypeName == null ? 0 : 1) + (signature == null ? 0 : 1); 101 102 if((bioParameterCount == 0 || bioParameterCount == 2) && (signatureParameterCount == 0 || signatureParameterCount == 2)) { 103 var partyControl = Session.getModelController(PartyControl.class); 104 var partyTypeName = getPartyTypeName(); 105 var partyName = partyTypeName.equals(PartyTypes.CUSTOMER.name())? null: form.getPartyName(); 106 var party = partyName == null? null: partyControl.getPartyByName(partyName); 107 108 if(partyName == null || party != null) { 109 var nickname = form.getNickname(); 110 var profile = nickname == null? null: partyControl.getProfileByNickname(nickname); 111 112 if(party == null) { 113 party = getParty(); 114 } 115 116 if(profile == null) { 117 var iconControl = Session.getModelController(IconControl.class); 118 var iconName = form.getIconName(); 119 var icon = iconName == null? null: iconControl.getIconByName(iconName); 120 121 if(iconName == null || icon != null) { 122 if(icon != null) { 123 var iconUsageType = iconControl.getIconUsageTypeByName(IconConstants.IconUsageType_PROFILE); 124 var iconUsage = iconControl.getIconUsage(iconUsageType, icon); 125 126 if(iconUsage == null) { 127 addExecutionError(ExecutionErrors.UnknownIconUsage.name()); 128 } 129 } 130 131 if(!hasExecutionErrors()) { 132 var genderName = form.getGenderName(); 133 var gender = genderName == null? null: partyControl.getGenderByName(genderName); 134 135 if(genderName == null || gender != null) { 136 var birthdayFormatName = form.getBirthdayFormatName(); 137 var birthdayFormat = birthdayFormatName == null? null: partyControl.getBirthdayFormatByName(birthdayFormatName); 138 139 if(birthdayFormat != null) { 140 var mimeTypeControl = Session.getModelController(MimeTypeControl.class); 141 var bioMimeType = bioMimeTypeName == null? null: mimeTypeControl.getMimeTypeByName(bioMimeTypeName); 142 143 if(bioMimeTypeName == null || bioMimeType != null) { 144 var signatureMimeType = signatureMimeTypeName == null? null: mimeTypeControl.getMimeTypeByName(signatureMimeTypeName); 145 146 if(signatureMimeTypeName == null || signatureMimeType != null) { 147 var pronunciation = form.getPronunciation(); 148 var pronouns = form.getPronouns(); 149 var occupation = form.getOccupation(); 150 var hobbies = form.getHobbies(); 151 var location = form.getLocation(); 152 var rawBirthday = form.getBirthday(); 153 var birthday = rawBirthday == null? null: Integer.valueOf(rawBirthday); 154 155 partyControl.createProfile(party, nickname, icon, pronunciation, gender, pronouns, 156 birthday, birthdayFormat, occupation, hobbies, location, bioMimeType, bio, 157 signatureMimeType, signature, getPartyPK()); 158 } else { 159 addExecutionError(ExecutionErrors.UnknownSignatureMimeTypeName.name(), signatureMimeTypeName); 160 } 161 } else { 162 addExecutionError(ExecutionErrors.UnknownBioMimeTypeName.name(), bioMimeTypeName); 163 } 164 } else { 165 addExecutionError(ExecutionErrors.UnknownBirthdayFormatName.name(), birthdayFormatName); 166 } 167 } else { 168 addExecutionError(ExecutionErrors.UnknownGenderName.name(), genderName); 169 } 170 } 171 } else { 172 addExecutionError(ExecutionErrors.UnknownIconName.name(), iconName); 173 } 174 } else { 175 addExecutionError(ExecutionErrors.DuplicateNickname.name(), nickname); 176 } 177 } else { 178 addExecutionError(ExecutionErrors.UnknownPartyName.name(), partyName); 179 } 180 } else { 181 addExecutionError(ExecutionErrors.InvalidParameterCount.name()); 182 } 183 184 return null; 185 } 186 187}