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