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.vendor.server.command;
018
019import com.echothree.control.user.vendor.common.edit.VendorEdit;
020import com.echothree.control.user.vendor.common.edit.VendorEditFactory;
021import com.echothree.control.user.vendor.common.form.EditVendorForm;
022import com.echothree.control.user.vendor.common.result.EditVendorResult;
023import com.echothree.control.user.vendor.common.result.VendorResultFactory;
024import com.echothree.control.user.vendor.common.spec.VendorUniversalSpec;
025import com.echothree.model.control.accounting.common.AccountingConstants;
026import com.echothree.model.control.accounting.server.control.AccountingControl;
027import com.echothree.model.control.cancellationpolicy.common.CancellationKinds;
028import com.echothree.model.control.cancellationpolicy.server.control.CancellationPolicyControl;
029import com.echothree.model.control.item.server.control.ItemControl;
030import com.echothree.model.control.party.common.PartyTypes;
031import com.echothree.model.control.party.server.control.PartyControl;
032import com.echothree.model.control.returnpolicy.common.ReturnKinds;
033import com.echothree.model.control.returnpolicy.server.control.ReturnPolicyControl;
034import com.echothree.model.control.security.common.SecurityRoleGroups;
035import com.echothree.model.control.security.common.SecurityRoles;
036import com.echothree.model.control.vendor.server.control.VendorControl;
037import com.echothree.model.control.vendor.server.logic.VendorLogic;
038import com.echothree.model.data.accounting.server.entity.Currency;
039import com.echothree.model.data.accounting.server.entity.GlAccount;
040import com.echothree.model.data.cancellationpolicy.server.entity.CancellationPolicy;
041import com.echothree.model.data.item.server.entity.ItemAliasType;
042import com.echothree.model.data.party.server.entity.DateTimeFormat;
043import com.echothree.model.data.party.server.entity.Language;
044import com.echothree.model.data.party.server.entity.Party;
045import com.echothree.model.data.party.server.entity.TimeZone;
046import com.echothree.model.data.returnpolicy.server.entity.ReturnPolicy;
047import com.echothree.model.data.user.common.pk.UserVisitPK;
048import com.echothree.model.data.vendor.server.entity.Vendor;
049import com.echothree.util.common.command.EditMode;
050import com.echothree.util.common.form.BaseForm;
051import com.echothree.util.common.message.ExecutionErrors;
052import com.echothree.util.common.validation.FieldDefinition;
053import com.echothree.util.common.validation.FieldType;
054import com.echothree.util.server.control.BaseAbstractEditCommand;
055import com.echothree.util.server.control.CommandSecurityDefinition;
056import com.echothree.util.server.control.PartyTypeDefinition;
057import com.echothree.util.server.control.SecurityRoleDefinition;
058import com.echothree.util.server.persistence.EntityPermission;
059import com.echothree.util.server.persistence.Session;
060import com.echothree.util.server.string.AmountUtils;
061import com.echothree.util.server.validation.Validator;
062import java.util.List;
063import org.apache.commons.codec.language.Soundex;
064import javax.enterprise.context.Dependent;
065
066@Dependent
067public class EditVendorCommand
068        extends BaseAbstractEditCommand<VendorUniversalSpec, VendorEdit, EditVendorResult, Party, Party> {
069
070    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
071    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
072    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
073    
074    static {
075        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
076                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
077                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
078                    new SecurityRoleDefinition(SecurityRoleGroups.Vendor.name(), SecurityRoles.Edit.name())
079                    ))
080                ));
081        
082        SPEC_FIELD_DEFINITIONS = List.of(
083                new FieldDefinition("VendorName", FieldType.ENTITY_NAME, false, null, null),
084                new FieldDefinition("PartyName", FieldType.ENTITY_NAME, false, null, null),
085                new FieldDefinition("EntityRef", FieldType.ENTITY_REF, false, null, null),
086                new FieldDefinition("Uuid", FieldType.UUID, false, null, null)
087        );
088        
089        EDIT_FIELD_DEFINITIONS = List.of(
090                new FieldDefinition("VendorName", FieldType.ENTITY_NAME, true, null, null),
091                new FieldDefinition("VendorTypeName", FieldType.ENTITY_NAME, true, null, null),
092                new FieldDefinition("MinimumPurchaseOrderLines", FieldType.UNSIGNED_INTEGER, false, null, null),
093                new FieldDefinition("MaximumPurchaseOrderLines", FieldType.UNSIGNED_INTEGER, false, null, null),
094                new FieldDefinition("MinimumPurchaseOrderAmount", FieldType.UNSIGNED_COST_LINE, false, null, null),
095                new FieldDefinition("MaximumPurchaseOrderAmount", FieldType.UNSIGNED_COST_LINE, false, null, null),
096                new FieldDefinition("UseItemPurchasingCategories", FieldType.BOOLEAN, true, null, null),
097                new FieldDefinition("DefaultItemAliasTypeName", FieldType.ENTITY_NAME, false, null, null),
098                new FieldDefinition("CancellationPolicyName", FieldType.ENTITY_NAME, false, null, null),
099                new FieldDefinition("ReturnPolicyName", FieldType.ENTITY_NAME, false, null, null),
100                new FieldDefinition("ApGlAccountName", FieldType.ENTITY_NAME, false, null, null),
101                new FieldDefinition("PersonalTitleId", FieldType.ID, false, null, null),
102                new FieldDefinition("FirstName", FieldType.STRING, false, 1L, 20L),
103                new FieldDefinition("MiddleName", FieldType.STRING, false, 1L, 20L),
104                new FieldDefinition("LastName", FieldType.STRING, false, 1L, 20L),
105                new FieldDefinition("NameSuffixId", FieldType.ID, false, null, null),
106                new FieldDefinition("Name", FieldType.STRING, false, 1L, 60L),
107                new FieldDefinition("PreferredLanguageIsoName", FieldType.ENTITY_NAME, false, null, null),
108                new FieldDefinition("PreferredCurrencyIsoName", FieldType.ENTITY_NAME, false, null, null),
109                new FieldDefinition("PreferredJavaTimeZoneName", FieldType.STRING, false, null, null),
110                new FieldDefinition("PreferredDateTimeFormatName", FieldType.ENTITY_NAME, false, null, null),
111                new FieldDefinition("HoldUntilComplete", FieldType.BOOLEAN, true, null, null),
112                new FieldDefinition("AllowBackorders", FieldType.BOOLEAN, true, null, null),
113                new FieldDefinition("AllowSubstitutions", FieldType.BOOLEAN, true, null, null),
114                new FieldDefinition("AllowCombiningShipments", FieldType.BOOLEAN, true, null, null),
115                new FieldDefinition("RequireReference", FieldType.BOOLEAN, true, null, null),
116                new FieldDefinition("AllowReferenceDuplicates", FieldType.BOOLEAN, true, null, null),
117                new FieldDefinition("ReferenceValidationPattern", FieldType.REGULAR_EXPRESSION, false, null, null)
118                );
119    }
120    
121    /** Creates a new instance of EditVendorCommand */
122    public EditVendorCommand() {
123        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
124    }
125    
126    @Override
127    protected void setupValidatorForEdit(Validator validator, BaseForm specForm) {
128        var vendorControl = Session.getModelController(VendorControl.class);
129        var vendorName = spec.getVendorName();
130        
131        if(vendorName != null) {
132            var vendor = vendorControl.getVendorByNameForUpdate(vendorName);
133            
134            if(vendor != null) {
135                var partyControl = Session.getModelController(PartyControl.class);
136                
137                validator.setCurrency(partyControl.getPreferredCurrency(vendor.getParty()));
138            }
139        }
140    }
141
142    @Override
143    public EditVendorResult getResult() {
144        return VendorResultFactory.getEditVendorResult();
145    }
146
147    @Override
148    public VendorEdit getEdit() {
149        return VendorEditFactory.getVendorEdit();
150    }
151
152    @Override
153    public Party getEntity(EditVendorResult result) {
154        var vendorControl = Session.getModelController(VendorControl.class);
155        Vendor vendor;
156
157        if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
158            vendor = VendorLogic.getInstance().getVendorByUniversalSpec(this, spec);
159        } else { // EditMode.UPDATE
160            vendor = VendorLogic.getInstance().getVendorByUniversalSpecForUpdate(this, spec);
161        }
162
163        if(!hasExecutionErrors()) {
164            result.setVendor(vendorControl.getVendorTransfer(getUserVisit(), vendor));
165        }
166
167        return vendor.getParty();
168    }
169
170    @Override
171    public Party getLockEntity(Party party) {
172        return party;
173    }
174
175    @Override
176    public void fillInResult(EditVendorResult result, Party party) {
177        var vendorControl = Session.getModelController(VendorControl.class);
178
179        result.setVendor(vendorControl.getVendorTransfer(getUserVisit(), party));
180    }
181
182    @Override
183    public void doLock(VendorEdit edit, Party party) {
184        var partyControl = Session.getModelController(PartyControl.class);
185        var vendorControl = Session.getModelController(VendorControl.class);
186        var vendor = vendorControl.getVendor(party);
187        var amountUtils = AmountUtils.getInstance();
188        var vendorPreferredCurrency = getPreferredCurrency(party);
189        var partyDetail = party.getLastDetail();
190        var partyGroup = partyControl.getPartyGroup(party);
191        var apGlAccount = vendor.getApGlAccount();
192        var cancellationPolicy = vendor.getCancellationPolicy();
193        var returnPolicy = vendor.getReturnPolicy();
194        var minimumPurchaseOrderLines = vendor.getMinimumPurchaseOrderLines();
195        var maximumPurchaseOrderLines = vendor.getMaximumPurchaseOrderLines();
196        var minimumPurchaseOrderAmount = vendor.getMinimumPurchaseOrderAmount();
197        var maximumPurchaseOrderAmount = vendor.getMaximumPurchaseOrderAmount();
198        var itemAliasType = vendor.getDefaultItemAliasType();
199        var preferredLanguage = partyDetail.getPreferredLanguage();
200        var preferredCurrency = partyDetail.getPreferredCurrency();
201        var preferredTimeZone = partyDetail.getPreferredTimeZone();
202        var dateTimeFormat = partyDetail.getPreferredDateTimeFormat();
203        var person = partyControl.getPerson(party);
204        var personalTitle = person == null ? null : person.getPersonalTitle();
205        var nameSuffix = person == null ? null : person.getNameSuffix();
206
207        edit.setVendorName(vendor.getVendorName());
208        edit.setVendorTypeName(vendor.getVendorType().getLastDetail().getVendorTypeName());
209        edit.setMinimumPurchaseOrderLines(minimumPurchaseOrderLines == null ? null : minimumPurchaseOrderLines.toString());
210        edit.setMaximumPurchaseOrderLines(maximumPurchaseOrderLines == null ? null : maximumPurchaseOrderLines.toString());
211        edit.setMinimumPurchaseOrderAmount(minimumPurchaseOrderAmount == null ? null : amountUtils.formatCostLine(vendorPreferredCurrency, minimumPurchaseOrderAmount));
212        edit.setMaximumPurchaseOrderAmount(maximumPurchaseOrderAmount == null ? null : amountUtils.formatCostLine(vendorPreferredCurrency, maximumPurchaseOrderAmount));
213        edit.setUseItemPurchasingCategories(vendor.getUseItemPurchasingCategories().toString());
214        edit.setCancellationPolicyName(cancellationPolicy == null ? null : cancellationPolicy.getLastDetail().getCancellationPolicyName());
215        edit.setReturnPolicyName(returnPolicy == null ? null : returnPolicy.getLastDetail().getReturnPolicyName());
216        edit.setApGlAccountName(apGlAccount == null ? null : apGlAccount.getLastDetail().getGlAccountName());
217        edit.setHoldUntilComplete(vendor.getHoldUntilComplete().toString());
218        edit.setAllowBackorders(vendor.getAllowBackorders().toString());
219        edit.setAllowSubstitutions(vendor.getAllowSubstitutions().toString());
220        edit.setAllowCombiningShipments(vendor.getAllowCombiningShipments().toString());
221        edit.setRequireReference(vendor.getRequireReference().toString());
222        edit.setAllowReferenceDuplicates(vendor.getAllowReferenceDuplicates().toString());
223        edit.setReferenceValidationPattern(vendor.getReferenceValidationPattern());
224        edit.setDefaultItemAliasTypeName(itemAliasType == null ? null : itemAliasType.getLastDetail().getItemAliasTypeName());
225        edit.setPersonalTitleId(personalTitle == null ? null : personalTitle.getPrimaryKey().getEntityId().toString());
226        edit.setFirstName(person == null ? null : person.getFirstName());
227        edit.setMiddleName(person == null ? null : person.getMiddleName());
228        edit.setLastName(person == null ? null : person.getLastName());
229        edit.setNameSuffixId(nameSuffix == null ? null : nameSuffix.getPrimaryKey().getEntityId().toString());
230        edit.setName(partyGroup == null ? null : partyGroup.getName());
231        edit.setPreferredLanguageIsoName(preferredLanguage == null ? null : preferredLanguage.getLanguageIsoName());
232        edit.setPreferredCurrencyIsoName(preferredCurrency == null ? null : preferredCurrency.getCurrencyIsoName());
233        edit.setPreferredJavaTimeZoneName(preferredTimeZone == null ? null : preferredTimeZone.getLastDetail().getJavaTimeZoneName());
234        edit.setPreferredDateTimeFormatName(dateTimeFormat == null ? null : dateTimeFormat.getLastDetail().getDateTimeFormatName());
235    }
236
237    CancellationPolicy cancellationPolicy;
238    ReturnPolicy returnPolicy;
239    GlAccount apGlAccount;
240    ItemAliasType defaultItemAliasType;
241    Language preferredLanguage;
242    TimeZone preferredTimeZone;
243    DateTimeFormat preferredDateTimeFormat;
244    Currency preferredCurrency;
245
246    @Override
247    public void canUpdate(Party party) {
248        var partyControl = Session.getModelController(PartyControl.class);
249        var vendorControl = Session.getModelController(VendorControl.class);
250        var vendor = vendorControl.getVendorForUpdate(party);
251        var vendorName = edit.getVendorName();
252        var duplicateVendor = vendorControl.getVendorByName(vendorName);
253
254        if(duplicateVendor == null || duplicateVendor.getPrimaryKey().equals(vendor.getPrimaryKey())) {
255            var vendorTypeName = edit.getVendorTypeName();
256            var vendorType = vendorControl.getVendorTypeByName(vendorTypeName);
257
258            if(vendorType != null) {
259                var cancellationPolicyName = edit.getCancellationPolicyName();
260
261                if(cancellationPolicyName != null) {
262                    var cancellationPolicyControl = Session.getModelController(CancellationPolicyControl.class);
263                    var cancellationKind = cancellationPolicyControl.getCancellationKindByName(CancellationKinds.VENDOR_CANCELLATION.name());
264
265                    cancellationPolicy = cancellationPolicyControl.getCancellationPolicyByName(cancellationKind, cancellationPolicyName);
266                }
267
268                if(cancellationPolicyName == null || cancellationPolicy != null) {
269                    var returnPolicyName = edit.getReturnPolicyName();
270
271                    if(returnPolicyName != null) {
272                        var returnPolicyControl = Session.getModelController(ReturnPolicyControl.class);
273                        var returnKind = returnPolicyControl.getReturnKindByName(ReturnKinds.VENDOR_RETURN.name());
274
275                        returnPolicy = returnPolicyControl.getReturnPolicyByName(returnKind, returnPolicyName);
276                    }
277
278                    if(returnPolicyName == null || returnPolicy != null) {
279                        var accountingControl = Session.getModelController(AccountingControl.class);
280                        var apGlAccountName = edit.getApGlAccountName();
281
282                        apGlAccount = apGlAccountName == null ? null : accountingControl.getGlAccountByName(apGlAccountName);
283
284                        if(apGlAccountName == null || apGlAccount != null) {
285                            var glAccountCategoryName = apGlAccount == null ? null : apGlAccount.getLastDetail().getGlAccountCategory().getLastDetail().getGlAccountCategoryName();
286
287                            if(glAccountCategoryName == null || glAccountCategoryName.equals(AccountingConstants.GlAccountCategory_ACCOUNTS_PAYABLE)) {
288                                var itemControl = Session.getModelController(ItemControl.class);
289                                var defaultItemAliasTypeName = edit.getDefaultItemAliasTypeName();
290
291                                defaultItemAliasType = itemControl.getItemAliasTypeByName(defaultItemAliasTypeName);
292
293                                if(defaultItemAliasTypeName == null || defaultItemAliasType != null) {
294                                    if(defaultItemAliasType == null || !defaultItemAliasType.getLastDetail().getAllowMultiple()) {
295                                        var preferredLanguageIsoName = edit.getPreferredLanguageIsoName();
296                                        preferredLanguage = preferredLanguageIsoName == null ? null : partyControl.getLanguageByIsoName(preferredLanguageIsoName);
297
298                                        if(preferredLanguageIsoName == null || (preferredLanguage != null)) {
299                                            var preferredJavaTimeZoneName = edit.getPreferredJavaTimeZoneName();
300                                            preferredTimeZone = preferredJavaTimeZoneName == null ? null : partyControl.getTimeZoneByJavaName(preferredJavaTimeZoneName);
301
302                                            if(preferredJavaTimeZoneName == null || (preferredTimeZone != null)) {
303                                                var preferredDateTimeFormatName = edit.getPreferredDateTimeFormatName();
304                                                preferredDateTimeFormat = preferredDateTimeFormatName == null ? null : partyControl.getDateTimeFormatByName(preferredDateTimeFormatName);
305
306                                                if(preferredDateTimeFormatName == null || (preferredDateTimeFormat != null)) {
307                                                    var preferredCurrencyIsoName = edit.getPreferredCurrencyIsoName();
308
309                                                    if(preferredCurrencyIsoName == null) {
310                                                        preferredCurrency = null;
311                                                    } else {
312                                                        preferredCurrency = accountingControl.getCurrencyByIsoName(preferredCurrencyIsoName);
313                                                    }
314
315                                                    if(preferredCurrencyIsoName != null && (preferredCurrency == null)) {
316                                                        addExecutionError(ExecutionErrors.UnknownCurrencyIsoName.name(), preferredCurrencyIsoName);
317                                                    }
318                                                } else {
319                                                    addExecutionError(ExecutionErrors.UnknownDateTimeFormatName.name(), preferredDateTimeFormatName);
320                                                }
321                                            } else {
322                                                addExecutionError(ExecutionErrors.UnknownJavaTimeZoneName.name(), preferredJavaTimeZoneName);
323                                            }
324                                        } else {
325                                            addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), preferredLanguageIsoName);
326                                        }
327                                    } else {
328                                        addExecutionError(ExecutionErrors.InvalidItemAliasType.name(), defaultItemAliasTypeName);
329                                    }
330                                } else {
331                                    addExecutionError(ExecutionErrors.UnknownDefaultItemAliasTypeName.name(), defaultItemAliasTypeName);
332                                }
333                            } else {
334                                addExecutionError(ExecutionErrors.InvalidGlAccountCategory.name(), glAccountCategoryName);
335                            }
336                        } else {
337                            addExecutionError(ExecutionErrors.UnknownApGlAccountName.name(), apGlAccountName);
338                        }
339                    } else {
340                        addExecutionError(ExecutionErrors.UnknownReturnPolicyName.name(), returnPolicyName);
341                    }
342                } else {
343                    addExecutionError(ExecutionErrors.UnknownCancellationPolicyName.name(), cancellationPolicyName);
344                }
345            } else {
346                addExecutionError(ExecutionErrors.UnknownVendorTypeName.name(), vendorTypeName);
347            }
348        } else {
349            addExecutionError(ExecutionErrors.DuplicateVendorName.name(), vendorName);
350        }
351    }
352
353    @Override
354    public void doUpdate(Party party) {
355        var partyControl = Session.getModelController(PartyControl.class);
356        var vendorControl = Session.getModelController(VendorControl.class);
357        var soundex = new Soundex();
358        var vendor = vendorControl.getVendorForUpdate(party);
359        var vendorValue = vendorControl.getVendorValue(vendor);
360        var partyDetailValue = partyControl.getPartyDetailValueForUpdate(party);
361        var partyGroup = partyControl.getPartyGroupForUpdate(party);
362        var person = partyControl.getPersonForUpdate(party);
363        var minimumPurchaseOrderLines = edit.getMinimumPurchaseOrderLines();
364        var maximumPurchaseOrderLines = edit.getMaximumPurchaseOrderLines();
365        var minimumPurchaseOrderAmount = edit.getMinimumPurchaseOrderAmount();
366        var maximumPurchaseOrderAmount = edit.getMaximumPurchaseOrderAmount();
367        var personalTitleId = edit.getPersonalTitleId();
368        var personalTitle = personalTitleId == null ? null : partyControl.convertPersonalTitleIdToEntity(personalTitleId, EntityPermission.READ_ONLY);
369        var firstName = edit.getFirstName();
370        var firstNameSdx = soundex.encode(firstName);
371        var middleName = edit.getMiddleName();
372        var middleNameSdx = middleName == null ? null : soundex.encode(middleName);
373        var lastName = edit.getLastName();
374        var lastNameSdx = soundex.encode(lastName);
375        var nameSuffixId = edit.getNameSuffixId();
376        var nameSuffix = nameSuffixId == null ? null : partyControl.convertNameSuffixIdToEntity(nameSuffixId, EntityPermission.READ_ONLY);
377        var name = edit.getName();
378        var updatedBy = getPartyPK();
379
380        vendorValue.setVendorName(edit.getVendorName());
381        vendorValue.setMinimumPurchaseOrderLines(minimumPurchaseOrderLines == null ? null : Integer.valueOf(minimumPurchaseOrderLines));
382        vendorValue.setMaximumPurchaseOrderLines(maximumPurchaseOrderLines == null ? null : Integer.valueOf(maximumPurchaseOrderLines));
383        vendorValue.setMinimumPurchaseOrderAmount(minimumPurchaseOrderAmount == null ? null : Long.valueOf(minimumPurchaseOrderAmount));
384        vendorValue.setMaximumPurchaseOrderAmount(maximumPurchaseOrderAmount == null ? null : Long.valueOf(maximumPurchaseOrderAmount));
385        vendorValue.setUseItemPurchasingCategories(Boolean.valueOf(edit.getUseItemPurchasingCategories()));
386        vendorValue.setDefaultItemAliasTypePK(defaultItemAliasType == null ? null : defaultItemAliasType.getPrimaryKey());
387        vendorValue.setCancellationPolicyPK(cancellationPolicy == null ? null : cancellationPolicy.getPrimaryKey());
388        vendorValue.setReturnPolicyPK(returnPolicy == null ? null : returnPolicy.getPrimaryKey());
389        vendorValue.setApGlAccountPK(apGlAccount == null ? null : apGlAccount.getPrimaryKey());
390        vendorValue.setHoldUntilComplete(Boolean.valueOf(edit.getHoldUntilComplete()));
391        vendorValue.setAllowBackorders(Boolean.valueOf(edit.getAllowBackorders()));
392        vendorValue.setAllowSubstitutions(Boolean.valueOf(edit.getAllowSubstitutions()));
393        vendorValue.setAllowCombiningShipments(Boolean.valueOf(edit.getAllowCombiningShipments()));
394        vendorValue.setRequireReference(Boolean.valueOf(edit.getRequireReference()));
395        vendorValue.setAllowReferenceDuplicates(Boolean.valueOf(edit.getAllowReferenceDuplicates()));
396        vendorValue.setReferenceValidationPattern(edit.getReferenceValidationPattern());
397
398        partyDetailValue.setPreferredLanguagePK(preferredLanguage == null ? null : preferredLanguage.getPrimaryKey());
399        partyDetailValue.setPreferredTimeZonePK(preferredTimeZone == null ? null : preferredTimeZone.getPrimaryKey());
400        partyDetailValue.setPreferredDateTimeFormatPK(preferredDateTimeFormat == null ? null : preferredDateTimeFormat.getPrimaryKey());
401        partyDetailValue.setPreferredCurrencyPK(preferredCurrency == null ? null : preferredCurrency.getPrimaryKey());
402
403        if(name != null) {
404            if(partyGroup != null) {
405                var partyGroupValue = partyControl.getPartyGroupValue(partyGroup);
406
407                partyGroupValue.setName(name);
408                partyControl.updatePartyGroupFromValue(partyGroupValue, updatedBy);
409            } else {
410                partyControl.createPartyGroup(party, name, updatedBy);
411            }
412        } else {
413            if(partyGroup != null) {
414                partyControl.deletePartyGroup(partyGroup, updatedBy);
415            }
416        }
417
418        if(personalTitle != null || firstName != null || middleName != null || lastName != null || nameSuffix != null) {
419            if(person != null) {
420                var personValue = partyControl.getPersonValue(person);
421
422                personValue.setPersonalTitlePK(personalTitle == null ? null : personalTitle.getPrimaryKey());
423                personValue.setFirstName(firstName);
424                personValue.setFirstNameSdx(firstNameSdx);
425                personValue.setMiddleName(middleName);
426                personValue.setMiddleNameSdx(middleNameSdx);
427                personValue.setLastName(lastName);
428                personValue.setLastNameSdx(lastNameSdx);
429                personValue.setNameSuffixPK(nameSuffix == null ? null : nameSuffix.getPrimaryKey());
430                partyControl.updatePersonFromValue(personValue, updatedBy);
431            } else {
432                partyControl.createPerson(party, personalTitle, firstName, firstNameSdx, middleName,
433                        middleNameSdx, lastName, lastNameSdx, nameSuffix, updatedBy);
434            }
435        } else {
436            if(person != null) {
437                partyControl.deletePerson(person, updatedBy);
438            }
439        }
440
441        vendorControl.updateVendorFromValue(vendorValue, updatedBy);
442        partyControl.updatePartyFromValue(partyDetailValue, updatedBy);
443    }
444
445}