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.CreateCustomerWithLoginForm;
020import com.echothree.control.user.party.common.result.PartyResultFactory;
021import com.echothree.model.control.accounting.common.AccountingConstants;
022import com.echothree.model.control.accounting.server.control.AccountingControl;
023import com.echothree.model.control.cancellationpolicy.common.CancellationKinds;
024import com.echothree.model.control.cancellationpolicy.server.control.CancellationPolicyControl;
025import com.echothree.model.control.contact.common.ContactMechanismPurposes;
026import com.echothree.model.control.contact.server.logic.ContactEmailAddressLogic;
027import com.echothree.model.control.contactlist.server.logic.ContactListLogic;
028import com.echothree.model.control.core.server.control.EntityInstanceControl;
029import com.echothree.model.control.customer.common.workflow.CustomerCreditStatusConstants;
030import com.echothree.model.control.customer.common.workflow.CustomerStatusConstants;
031import com.echothree.model.control.customer.server.control.CustomerControl;
032import com.echothree.model.control.offer.server.control.OfferControl;
033import com.echothree.model.control.offer.server.control.OfferUseControl;
034import com.echothree.model.control.offer.server.control.SourceControl;
035import com.echothree.model.control.offer.server.control.UseControl;
036import com.echothree.model.control.party.common.PartyTypes;
037import com.echothree.model.control.party.server.control.PartyControl;
038import com.echothree.model.control.party.server.logic.PartyChainLogic;
039import com.echothree.model.control.party.server.logic.PasswordStringPolicyLogic;
040import com.echothree.model.control.returnpolicy.common.ReturnKinds;
041import com.echothree.model.control.returnpolicy.server.control.ReturnPolicyControl;
042import com.echothree.model.control.term.server.control.TermControl;
043import com.echothree.model.control.user.common.UserConstants;
044import com.echothree.model.control.workflow.server.control.WorkflowControl;
045import com.echothree.model.data.accounting.server.entity.Currency;
046import com.echothree.model.data.cancellationpolicy.server.entity.CancellationPolicy;
047import com.echothree.model.data.customer.server.entity.Customer;
048import com.echothree.model.data.offer.server.entity.OfferUse;
049import com.echothree.model.data.returnpolicy.server.entity.ReturnPolicy;
050import com.echothree.model.data.user.common.pk.UserVisitPK;
051import com.echothree.util.common.command.BaseResult;
052import com.echothree.util.common.form.ValidationResult;
053import com.echothree.util.common.message.ExecutionErrors;
054import com.echothree.util.common.persistence.BasePK;
055import com.echothree.util.common.validation.FieldDefinition;
056import com.echothree.util.common.validation.FieldType;
057import com.echothree.util.server.control.BaseSimpleCommand;
058import com.echothree.util.server.persistence.EntityPermission;
059import com.echothree.util.server.persistence.Session;
060import com.echothree.util.server.validation.Validator;
061import java.util.Arrays;
062import java.util.Collections;
063import java.util.List;
064import org.apache.commons.codec.language.Soundex;
065import javax.enterprise.context.RequestScoped;
066
067@RequestScoped
068public class CreateCustomerWithLoginCommand
069        extends BaseSimpleCommand<CreateCustomerWithLoginForm> {
070    
071    // No COMMAND_SECURITY_DEFINITION, anyone may execute this command.
072    private final static List<FieldDefinition> customerFormFieldDefinitions;
073    private final static List<FieldDefinition> otherFormFieldDefinitions;
074    
075    static {
076        // customerFormFieldDefinitions differs from otherFormFieldDefinitions in that when the PartyType
077        // executing this command is null, or equal to CUSTOMER, FirstName and LastName are required fields.
078        // For all other PartyTypes, that requirement is relaxed.
079        customerFormFieldDefinitions = Collections.unmodifiableList(Arrays.asList(
080                new FieldDefinition("CustomerTypeName", FieldType.ENTITY_NAME, false, null, null),
081                new FieldDefinition("CancellationPolicyName", FieldType.ENTITY_NAME, false, null, null),
082                new FieldDefinition("ReturnPolicyName", FieldType.ENTITY_NAME, false, null, null),
083                new FieldDefinition("ArGlAccountName", FieldType.ENTITY_NAME, false, null, null),
084                new FieldDefinition("InitialOfferName", FieldType.ENTITY_NAME, false, null, null),
085                new FieldDefinition("InitialUseName", FieldType.ENTITY_NAME, false, null, null),
086                new FieldDefinition("InitialSourceName", FieldType.ENTITY_NAME, false, null, null),
087                new FieldDefinition("PersonalTitleId", FieldType.ID, false, null, null),
088                new FieldDefinition("FirstName", FieldType.STRING, true, 1L, 20L),
089                new FieldDefinition("MiddleName", FieldType.STRING, false, 1L, 20L),
090                new FieldDefinition("LastName", FieldType.STRING, true, 1L, 20L),
091                new FieldDefinition("NameSuffixId", FieldType.ID, false, null, null),
092                new FieldDefinition("Name", FieldType.STRING, false, 1L, 60L),
093                new FieldDefinition("PreferredLanguageIsoName", FieldType.ENTITY_NAME, false, null, null),
094                new FieldDefinition("PreferredCurrencyIsoName", FieldType.ENTITY_NAME, false, null, null),
095                new FieldDefinition("PreferredJavaTimeZoneName", FieldType.TIME_ZONE_NAME, false, null, null),
096                new FieldDefinition("PreferredDateTimeFormatName", FieldType.ENTITY_NAME, false, null, null),
097                new FieldDefinition("EmailAddress", FieldType.EMAIL_ADDRESS, true, null, null),
098                new FieldDefinition("AllowSolicitation", FieldType.BOOLEAN, true, null, null),
099                new FieldDefinition("Username", FieldType.STRING, true, 1L, 80L),
100                new FieldDefinition("Password1", FieldType.STRING, true, 1L, 40L),
101                new FieldDefinition("Password2", FieldType.STRING, true, 1L, 40L),
102                new FieldDefinition("RecoveryQuestionName", FieldType.ENTITY_NAME, true, null, null),
103                new FieldDefinition("Answer", FieldType.STRING, true, 1L, 40L),
104                new FieldDefinition("CustomerStatusChoice", FieldType.ENTITY_NAME, false, null, null),
105                new FieldDefinition("CustomerCreditStatusChoice", FieldType.ENTITY_NAME, false, null, null)
106                ));
107        
108        otherFormFieldDefinitions = Collections.unmodifiableList(Arrays.asList(
109                new FieldDefinition("CustomerTypeName", FieldType.ENTITY_NAME, false, null, null),
110                new FieldDefinition("CancellationPolicyName", FieldType.ENTITY_NAME, false, null, null),
111                new FieldDefinition("ReturnPolicyName", FieldType.ENTITY_NAME, false, null, null),
112                new FieldDefinition("ArGlAccountName", FieldType.ENTITY_NAME, false, null, null),
113                new FieldDefinition("InitialOfferName", FieldType.ENTITY_NAME, false, null, null),
114                new FieldDefinition("InitialUseName", FieldType.ENTITY_NAME, false, null, null),
115                new FieldDefinition("InitialSourceName", FieldType.ENTITY_NAME, false, null, null),
116                new FieldDefinition("PersonalTitleId", FieldType.ID, false, null, null),
117                new FieldDefinition("FirstName", FieldType.STRING, false, 1L, 20L),
118                new FieldDefinition("MiddleName", FieldType.STRING, false, 1L, 20L),
119                new FieldDefinition("LastName", FieldType.STRING, false, 1L, 20L),
120                new FieldDefinition("NameSuffixId", FieldType.ID, false, null, null),
121                new FieldDefinition("Name", FieldType.STRING, false, 1L, 60L),
122                new FieldDefinition("PreferredLanguageIsoName", FieldType.ENTITY_NAME, false, null, null),
123                new FieldDefinition("PreferredCurrencyIsoName", FieldType.ENTITY_NAME, false, null, null),
124                new FieldDefinition("PreferredJavaTimeZoneName", FieldType.TIME_ZONE_NAME, false, null, null),
125                new FieldDefinition("PreferredDateTimeFormatName", FieldType.ENTITY_NAME, false, null, null),
126                new FieldDefinition("EmailAddress", FieldType.EMAIL_ADDRESS, true, null, null),
127                new FieldDefinition("AllowSolicitation", FieldType.BOOLEAN, true, null, null),
128                new FieldDefinition("Username", FieldType.STRING, true, 1L, 80L),
129                new FieldDefinition("Password1", FieldType.STRING, true, 1L, 40L),
130                new FieldDefinition("Password2", FieldType.STRING, true, 1L, 40L),
131                new FieldDefinition("RecoveryQuestionName", FieldType.ENTITY_NAME, true, null, null),
132                new FieldDefinition("Answer", FieldType.STRING, true, 1L, 40L),
133                new FieldDefinition("CustomerStatusChoice", FieldType.ENTITY_NAME, false, null, null),
134                new FieldDefinition("CustomerCreditStatusChoice", FieldType.ENTITY_NAME, false, null, null)
135                ));
136    }
137    
138    /** Creates a new instance of CreateCustomerWithLoginCommand */
139    public CreateCustomerWithLoginCommand() {
140        super(null, null, false);
141    }
142    
143    @Override
144    protected ValidationResult validate() {
145        var partyTypeName = getPartyTypeName();
146        var FORM_FIELD_DEFINITIONS = partyTypeName == null || partyTypeName.equals(PartyTypes.CUSTOMER.name())? customerFormFieldDefinitions: otherFormFieldDefinitions;
147        var validator = new Validator(this);
148        var validationResult = validator.validate(form, FORM_FIELD_DEFINITIONS);
149        
150        return validationResult;
151    }
152    
153    @Override
154    protected BaseResult execute() {
155        var userControl = getUserControl();
156        var result = PartyResultFactory.getCreateCustomerWithLoginResult();
157        var customerControl = Session.getModelController(CustomerControl.class);
158        Customer customer = null;
159        var username = form.getUsername();
160        var userLogin = userControl.getUserLoginByUsername(username);
161
162        if(userLogin == null) {
163            var password1 = form.getPassword1();
164            var password2 = form.getPassword2();
165
166            if(password1.equals(password2)) {
167                var partyControl = Session.getModelController(PartyControl.class);
168                var partyType = partyControl.getPartyTypeByName(PartyTypes.CUSTOMER.name());
169                var partyTypePasswordStringPolicy = PasswordStringPolicyLogic.getInstance().checkStringPassword(session,
170                        getUserVisit(), this, partyType, null, null, password1);
171
172                if(!hasExecutionErrors()) {
173                    var customerTypeName = form.getCustomerTypeName();
174                    var customerType = customerTypeName == null ? customerControl.getDefaultCustomerType() : customerControl.getCustomerTypeByName(customerTypeName);
175
176                    if(customerType != null) {
177                        var cancellationPolicyName = form.getCancellationPolicyName();
178                        CancellationPolicy cancellationPolicy = null;
179
180                        if(cancellationPolicyName != null) {
181                            var cancellationPolicyControl = Session.getModelController(CancellationPolicyControl.class);
182                            var returnKind = cancellationPolicyControl.getCancellationKindByName(CancellationKinds.CUSTOMER_CANCELLATION.name());
183
184                            cancellationPolicy = cancellationPolicyControl.getCancellationPolicyByName(returnKind, cancellationPolicyName);
185                        }
186
187                        if(cancellationPolicyName == null || cancellationPolicy != null) {
188                            var returnPolicyName = form.getReturnPolicyName();
189                            ReturnPolicy returnPolicy = null;
190
191                            if(returnPolicyName != null) {
192                                var returnPolicyControl = Session.getModelController(ReturnPolicyControl.class);
193                                var returnKind = returnPolicyControl.getReturnKindByName(ReturnKinds.CUSTOMER_RETURN.name());
194
195                                returnPolicy = returnPolicyControl.getReturnPolicyByName(returnKind, returnPolicyName);
196                            }
197
198                            if(returnPolicyName == null || returnPolicy != null) {
199                                var accountingControl = Session.getModelController(AccountingControl.class);
200                                var arGlAccountName = form.getArGlAccountName();
201                                var arGlAccount = arGlAccountName == null ? null : accountingControl.getGlAccountByName(arGlAccountName);
202
203                                if(arGlAccountName == null || arGlAccount != null) {
204                                    var glAccountCategoryName = arGlAccount == null ? null
205                                            : arGlAccount.getLastDetail().getGlAccountCategory().getLastDetail().getGlAccountCategoryName();
206
207                                    if(glAccountCategoryName == null || glAccountCategoryName.equals(AccountingConstants.GlAccountCategory_ACCOUNTS_RECEIVABLE)) {
208                                        var termControl = Session.getModelController(TermControl.class);
209                                        var customerTypeDetail = customerType.getLastDetail();
210                                        var term = customerTypeDetail.getDefaultTerm();
211
212                                        if(term == null) {
213                                            term = termControl.getDefaultTerm();
214                                        }
215
216                                        if(term != null) {
217                                            var initialOfferName = form.getInitialOfferName();
218                                            var initialUseName = form.getInitialUseName();
219                                            var initialSourceName = form.getInitialSourceName();
220                                            OfferUse initialOfferUse = null;
221                                            var invalidInitialOfferOrSourceSpecification = false;
222
223                                            if(initialOfferName != null && initialUseName != null && initialSourceName == null) {
224                                                var offerControl = Session.getModelController(OfferControl.class);
225                                                var initialOffer = offerControl.getOfferByName(initialOfferName);
226
227                                                if(initialOffer != null) {
228                                                    var useControl = Session.getModelController(UseControl.class);
229                                                    var initialUse = useControl.getUseByName(initialUseName);
230
231                                                    if(initialUse != null) {
232                                                        var offerUseControl = Session.getModelController(OfferUseControl.class);
233                                                        initialOfferUse = offerUseControl.getOfferUse(initialOffer, initialUse);
234
235                                                        if(initialOfferUse == null) {
236                                                            addExecutionError(ExecutionErrors.UnknownInitialOfferUse.name());
237                                                        }
238                                                    } else {
239                                                        addExecutionError(ExecutionErrors.UnknownInitialUseName.name(), initialUseName);
240                                                    }
241                                                } else {
242                                                    addExecutionError(ExecutionErrors.UnknownInitialOfferName.name(), initialOfferName);
243                                                }
244                                            } else {
245                                                var sourceControl = Session.getModelController(SourceControl.class);
246
247                                                if(initialOfferName == null && initialUseName == null && initialSourceName != null) {
248                                                    var source = sourceControl.getSourceByName(initialSourceName);
249
250                                                    if(source != null) {
251                                                        initialOfferUse = source.getLastDetail().getOfferUse();
252                                                    } else {
253                                                        addExecutionError(ExecutionErrors.UnknownInitialSourceName.name(), initialSourceName);
254                                                    }
255                                                } else {
256                                                    initialOfferUse = getUserVisit().getOfferUse();
257
258                                                    if(initialOfferUse == null) {
259                                                        // If all three parameters are null, then try to get the default Source and use its OfferUse.
260                                                        var source = sourceControl.getDefaultSource();
261
262                                                        if(source != null) {
263                                                            initialOfferUse = source.getLastDetail().getOfferUse();
264                                                        } else {
265                                                            addExecutionError(ExecutionErrors.InvalidInitialOfferOrSourceSpecification.name());
266                                                            invalidInitialOfferOrSourceSpecification = true;
267                                                        }
268                                                    }
269                                                }
270                                            }
271
272                                            if(initialOfferUse != null) {
273                                                var preferredLanguageIsoName = form.getPreferredLanguageIsoName();
274                                                var preferredLanguage = preferredLanguageIsoName == null ? null : partyControl.getLanguageByIsoName(preferredLanguageIsoName);
275
276                                                if(preferredLanguageIsoName == null || (preferredLanguage != null)) {
277                                                    var preferredJavaTimeZoneName = form.getPreferredJavaTimeZoneName();
278                                                    var preferredTimeZone = preferredJavaTimeZoneName == null ? null : partyControl.getTimeZoneByJavaName(preferredJavaTimeZoneName);
279
280                                                    if(preferredJavaTimeZoneName == null || (preferredTimeZone != null)) {
281                                                        var preferredDateTimeFormatName = form.getPreferredDateTimeFormatName();
282                                                        var preferredDateTimeFormat = preferredDateTimeFormatName == null ? null
283                                                                : partyControl.getDateTimeFormatByName(preferredDateTimeFormatName);
284
285                                                        if(preferredDateTimeFormatName == null || (preferredDateTimeFormat != null)) {
286                                                            var preferredCurrencyIsoName = form.getPreferredCurrencyIsoName();
287                                                            Currency preferredCurrency;
288
289                                                            if(preferredCurrencyIsoName == null) {
290                                                                preferredCurrency = null;
291                                                            } else {
292                                                                preferredCurrency = accountingControl.getCurrencyByIsoName(preferredCurrencyIsoName);
293                                                            }
294
295                                                            if(preferredCurrencyIsoName == null || (preferredCurrency != null)) {
296                                                                var recoveryQuestionName = form.getRecoveryQuestionName();
297                                                                var recoveryQuestion = userControl.getRecoveryQuestionByName(recoveryQuestionName);
298
299                                                                if(recoveryQuestion != null) {
300                                                                    var workflowControl = Session.getModelController(WorkflowControl.class);
301                                                                    var soundex = new Soundex();
302                                                                    BasePK createdBy = getPartyPK();
303                                                                    var personalTitleId = form.getPersonalTitleId();
304                                                                    var personalTitle = personalTitleId == null ? null
305                                                                            : partyControl.convertPersonalTitleIdToEntity(personalTitleId, EntityPermission.READ_ONLY);
306                                                                    var firstName = form.getFirstName();
307                                                                    var middleName = form.getMiddleName();
308                                                                    var lastName = form.getLastName();
309                                                                    var nameSuffixId = form.getNameSuffixId();
310                                                                    var nameSuffix = nameSuffixId == null ? null
311                                                                            : partyControl.convertNameSuffixIdToEntity(nameSuffixId, EntityPermission.READ_ONLY);
312                                                                    var name = form.getName();
313                                                                    var emailAddress = form.getEmailAddress();
314                                                                    var allowSolicitation = Boolean.valueOf(form.getAllowSolicitation());
315
316                                                                    String firstNameSdx;
317                                                                    try {
318                                                                        firstNameSdx = firstName == null ? null : soundex.encode(firstName);
319                                                                    } catch(IllegalArgumentException iae) {
320                                                                        firstNameSdx = null;
321                                                                    }
322
323                                                                    String middleNameSdx;
324                                                                    try {
325                                                                        middleNameSdx = middleName == null ? null : soundex.encode(middleName);
326                                                                    } catch(IllegalArgumentException iae) {
327                                                                        middleNameSdx = null;
328                                                                    }
329
330                                                                    String lastNameSdx;
331                                                                    try {
332                                                                        lastNameSdx = lastName == null ? null : soundex.encode(lastName);
333                                                                    } catch(IllegalArgumentException iae) {
334                                                                        lastNameSdx = null;
335                                                                    }
336
337                                                                    var party = partyControl.createParty(null, partyType, preferredLanguage, preferredCurrency, preferredTimeZone, preferredDateTimeFormat,
338                                                                            createdBy);
339
340                                                                    if(createdBy == null) {
341                                                                        createdBy = party.getPrimaryKey();
342                                                                    }
343                                                                    if(personalTitle != null || firstName != null || middleName != null || lastName != null || nameSuffix != null) {
344                                                                        partyControl.createPerson(party, personalTitle, firstName, firstNameSdx, middleName,
345                                                                                middleNameSdx, lastName, lastNameSdx, nameSuffix, createdBy);
346                                                                    }
347
348                                                                    if(name != null) {
349                                                                        partyControl.createPartyGroup(party, name, createdBy);
350                                                                    }
351
352                                                                    customer = customerControl.createCustomer(party, customerType, initialOfferUse, cancellationPolicy, returnPolicy, arGlAccount,
353                                                                            customerTypeDetail.getDefaultHoldUntilComplete(), customerTypeDetail.getDefaultAllowBackorders(),
354                                                                            customerTypeDetail.getDefaultAllowSubstitutions(), customerTypeDetail.getDefaultAllowCombiningShipments(),
355                                                                            customerTypeDetail.getDefaultRequireReference(), customerTypeDetail.getDefaultAllowReferenceDuplicates(),
356                                                                            customerTypeDetail.getDefaultReferenceValidationPattern(), createdBy);
357                                                                    userControl.createUserLogin(party, username, createdBy);
358
359                                                                    ContactEmailAddressLogic.getInstance().createContactEmailAddress(party,
360                                                                            emailAddress, allowSolicitation, null,
361                                                                            ContactMechanismPurposes.PRIMARY_EMAIL.name(),
362                                                                            createdBy);
363
364                                                                    termControl.createPartyTerm(party, term, customerTypeDetail.getDefaultTaxable(), createdBy);
365
366                                                                    for(var customerTypeCreditLimit : termControl.getCustomerTypeCreditLimitsByCustomerType(customerType)) {
367                                                                        var currency = customerTypeCreditLimit.getCurrency();
368                                                                        var creditLimit = customerTypeCreditLimit.getCreditLimit();
369                                                                        var potentialCreditLimit = customerTypeCreditLimit.getPotentialCreditLimit();
370
371                                                                        termControl.createPartyCreditLimit(party, currency, creditLimit, potentialCreditLimit, createdBy);
372                                                                    }
373
374                                                                    var userLoginPasswordType = userControl.getUserLoginPasswordTypeByName(UserConstants.UserLoginPasswordType_STRING);
375                                                                    var userLoginPassword = userControl.createUserLoginPassword(party, userLoginPasswordType, createdBy);
376                                                                    userControl.createUserLoginPasswordString(userLoginPassword, password1, session.START_TIME_LONG, false, createdBy);
377
378                                                                    if(partyTypePasswordStringPolicy != null && partyTypePasswordStringPolicy.getLastDetail().getForceChangeAfterCreate()) {
379                                                                        var userLoginStatus = userControl.getUserLoginStatusForUpdate(party);
380
381                                                                        userLoginStatus.setForceChange(true);
382                                                                    }
383
384                                                                    var answer = form.getAnswer();
385                                                                    userControl.createRecoveryAnswer(party, recoveryQuestion, answer, createdBy);
386
387                                                                    // TODO: error checking for unknown customerStatusChoice
388                                                                    var customerStatusChoice = form.getCustomerStatusChoice();
389                                                                    var customerStatusWorkflow = workflowControl.getWorkflowByName(CustomerStatusConstants.Workflow_CUSTOMER_STATUS);
390                                                                    var customerStatusWorkflowEntrance = customerStatusChoice == null ? customerTypeDetail.getDefaultCustomerStatus()
391                                                                            : workflowControl.getWorkflowEntranceByName(customerStatusWorkflow, customerStatusChoice);
392
393                                                                    if(customerStatusWorkflowEntrance == null) {
394                                                                        customerStatusWorkflowEntrance = workflowControl.getDefaultWorkflowEntrance(customerStatusWorkflow);
395                                                                    }
396
397                                                                    // TODO: error checking for unknown customerCreditStatusChoice
398                                                                    var customerCreditStatusChoice = form.getCustomerCreditStatusChoice();
399                                                                    var customerCreditStatusWorkflow = workflowControl.getWorkflowByName(CustomerCreditStatusConstants.Workflow_CUSTOMER_CREDIT_STATUS);
400                                                                    var customerCreditStatusWorkflowEntrance = customerCreditStatusChoice == null
401                                                                            ? customerTypeDetail.getDefaultCustomerCreditStatus()
402                                                                            : workflowControl.getWorkflowEntranceByName(customerCreditStatusWorkflow, customerCreditStatusChoice);
403
404                                                                    if(customerCreditStatusWorkflowEntrance == null) {
405                                                                        customerCreditStatusWorkflowEntrance = workflowControl.getDefaultWorkflowEntrance(customerCreditStatusWorkflow);
406                                                                    }
407
408                                                                    var entityInstanceControl = Session.getModelController(EntityInstanceControl.class);
409                                                                    var entityInstance = entityInstanceControl.getEntityInstanceByBasePK(party.getPrimaryKey());
410                                                                    workflowControl.addEntityToWorkflow(customerStatusWorkflowEntrance, entityInstance, null, null, createdBy);
411                                                                    workflowControl.addEntityToWorkflow(customerCreditStatusWorkflowEntrance, entityInstance, null, null, createdBy);
412
413                                                                    ContactListLogic.getInstance().setupInitialContactLists(this, party, createdBy);
414                                                                    
415                                                                    // ExecutionErrorAccumulator is passed in as null so that an Exception will be thrown if there is an error.
416                                                                    PartyChainLogic.getInstance().createPartyWelcomeChainInstance(null, party, createdBy);
417                                                                } else {
418                                                                    addExecutionError(ExecutionErrors.UnknownRecoveryQuestionName.name(), recoveryQuestionName);
419                                                                }
420                                                            } else {
421                                                                addExecutionError(ExecutionErrors.UnknownCurrencyIsoName.name(), preferredCurrencyIsoName);
422                                                            }
423                                                        } else {
424                                                            addExecutionError(ExecutionErrors.UnknownDateTimeFormatName.name(), preferredDateTimeFormatName);
425                                                        }
426                                                    } else {
427                                                        addExecutionError(ExecutionErrors.UnknownJavaTimeZoneName.name(), preferredJavaTimeZoneName);
428                                                    }
429                                                } else {
430                                                    addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), preferredLanguageIsoName);
431                                                }
432                                            }
433                                        } else {
434                                            addExecutionError(ExecutionErrors.UnknownDefaultTerm.name());
435                                        }
436                                    } else {
437                                        addExecutionError(ExecutionErrors.InvalidGlAccountCategory.name(), glAccountCategoryName);
438                                    }
439                                } else {
440                                    addExecutionError(ExecutionErrors.UnknownArGlAccountName.name(), arGlAccountName);
441                                }
442                            } else {
443                                addExecutionError(ExecutionErrors.UnknownReturnPolicyName.name(), returnPolicyName);
444                            }
445                        } else {
446                            addExecutionError(ExecutionErrors.UnknownCancellationPolicyName.name(), cancellationPolicyName);
447                        }
448                    } else {
449                        if(customerTypeName != null) {
450                            addExecutionError(ExecutionErrors.UnknownCustomerTypeName.name(), customerTypeName);
451                        } else {
452                            addExecutionError(ExecutionErrors.UnknownDefaultCustomerType.name());
453                        }
454                    }
455                }
456            } else {
457                addExecutionError(ExecutionErrors.MismatchedPasswords.name());
458            }
459        } else {
460            addExecutionError(ExecutionErrors.DuplicateUsername.name());
461
462            var party = userLogin.getParty();
463            customer = customerControl.getCustomer(party);
464        }
465
466        if(customer != null) {
467            var party = customer.getParty();
468
469            result.setEntityRef(party.getPrimaryKey().getEntityRef());
470            result.setCustomerName(customer.getCustomerName());
471            result.setPartyName(party.getLastDetail().getPartyName());
472        }
473
474        return result;
475    }
476    
477}