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