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