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