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.contact.server.command; 018 019import com.echothree.control.user.contact.common.edit.ContactEditFactory; 020import com.echothree.control.user.contact.common.edit.PostalAddressLineElementEdit; 021import com.echothree.control.user.contact.common.form.EditPostalAddressLineElementForm; 022import com.echothree.control.user.contact.common.result.ContactResultFactory; 023import com.echothree.control.user.contact.common.spec.PostalAddressLineElementSpec; 024import com.echothree.model.control.contact.server.control.ContactControl; 025import com.echothree.model.data.user.common.pk.UserVisitPK; 026import com.echothree.util.common.message.ExecutionErrors; 027import com.echothree.util.common.validation.FieldDefinition; 028import com.echothree.util.common.validation.FieldType; 029import com.echothree.util.common.command.BaseResult; 030import com.echothree.util.common.command.EditMode; 031import com.echothree.util.server.control.BaseEditCommand; 032import com.echothree.util.server.persistence.Session; 033import java.util.ArrayList; 034import java.util.List; 035import javax.enterprise.context.Dependent; 036 037@Dependent 038public class EditPostalAddressLineElementCommand 039 extends BaseEditCommand<PostalAddressLineElementSpec, PostalAddressLineElementEdit> { 040 041 private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS; 042 private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS; 043 044 static { 045 SPEC_FIELD_DEFINITIONS = List.of( 046 new FieldDefinition("PostalAddressFormatName", FieldType.ENTITY_NAME, true, null, null), 047 new FieldDefinition("PostalAddressLineSortOrder", FieldType.SIGNED_INTEGER, true, null, null), 048 new FieldDefinition("PostalAddressLineElementSortOrder", FieldType.SIGNED_INTEGER, true, null, null) 049 ); 050 051 EDIT_FIELD_DEFINITIONS = List.of( 052 new FieldDefinition("PostalAddressLineElementSortOrder", FieldType.SIGNED_INTEGER, true, null, null), 053 new FieldDefinition("PostalAddressElementTypeName", FieldType.ENTITY_NAME, true, null, null), 054 new FieldDefinition("Prefix", FieldType.STRING, false, 1L, 10L), 055 new FieldDefinition("AlwaysIncludePrefix", FieldType.BOOLEAN, true, null, null), 056 new FieldDefinition("Suffix", FieldType.STRING, false, 1L, 10L), 057 new FieldDefinition("AlwaysIncludeSuffix", FieldType.BOOLEAN, true, null, null) 058 ); 059 } 060 061 /** Creates a new instance of EditPostalAddressLineElementCommand */ 062 public EditPostalAddressLineElementCommand() { 063 super(null, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 064 } 065 066 @Override 067 protected BaseResult execute() { 068 var contactControl = Session.getModelController(ContactControl.class); 069 var result = ContactResultFactory.getEditPostalAddressLineElementResult(); 070 var postalAddressFormatName = spec.getPostalAddressFormatName(); 071 var postalAddressFormat = contactControl.getPostalAddressFormatByName(postalAddressFormatName); 072 073 if(postalAddressFormat != null) { 074 var postalAddressLineSortOrder = Integer.valueOf(spec.getPostalAddressLineSortOrder()); 075 var postalAddressLine = contactControl.getPostalAddressLine(postalAddressFormat, postalAddressLineSortOrder); 076 077 if(postalAddressLine != null) { 078 if(editMode.equals(EditMode.LOCK)) { 079 var postalAddressLineElementSortOrder = Integer.valueOf(spec.getPostalAddressLineElementSortOrder()); 080 var postalAddressLineElement = contactControl.getPostalAddressLineElement(postalAddressLine, postalAddressLineElementSortOrder); 081 082 if(postalAddressLineElement != null) { 083 result.setPostalAddressLineElement(contactControl.getPostalAddressLineElementTransfer(getUserVisit(), postalAddressLineElement)); 084 085 if(lockEntity(postalAddressLineElement)) { 086 var edit = ContactEditFactory.getPostalAddressLineElementEdit(); 087 088 result.setEdit(edit); 089 edit.setPostalAddressLineElementSortOrder(postalAddressLineElement.getPostalAddressLineElementSortOrder().toString()); 090 edit.setPostalAddressElementTypeName(postalAddressLineElement.getPostalAddressElementType().getPostalAddressElementTypeName()); 091 edit.setPrefix(postalAddressLineElement.getPrefix()); 092 edit.setAlwaysIncludePrefix(postalAddressLineElement.getAlwaysIncludePrefix().toString()); 093 edit.setSuffix(postalAddressLineElement.getSuffix()); 094 edit.setAlwaysIncludeSuffix(postalAddressLineElement.getAlwaysIncludeSuffix().toString()); 095 } else { 096 addExecutionError(ExecutionErrors.EntityLockFailed.name()); 097 } 098 099 result.setEntityLock(getEntityLockTransfer(postalAddressLineElement)); 100 } else { 101 addExecutionError(ExecutionErrors.UnknownPostalAddressLineElement.name(), postalAddressLineElementSortOrder.toString()); 102 } 103 } else if(editMode.equals(EditMode.UPDATE)) { 104 var postalAddressLineElementSortOrder = Integer.valueOf(spec.getPostalAddressLineElementSortOrder()); 105 var postalAddressLineElement = contactControl.getPostalAddressLineElementForUpdate(postalAddressLine, 106 postalAddressLineElementSortOrder); 107 108 if(postalAddressLineElement != null) { 109 postalAddressLineElementSortOrder = Integer.valueOf(edit.getPostalAddressLineElementSortOrder()); 110 var duplicatePostalAddressLineElement = contactControl.getPostalAddressLineElementForUpdate(postalAddressLine, 111 postalAddressLineElementSortOrder); 112 113 if(duplicatePostalAddressLineElement == null || postalAddressLineElement.equals(duplicatePostalAddressLineElement)) { 114 var postalAddressElementTypeName = edit.getPostalAddressElementTypeName(); 115 var postalAddressElementType = contactControl.getPostalAddressElementTypeByName(postalAddressElementTypeName); 116 117 if(postalAddressElementType != null) { 118 if(lockEntityForUpdate(postalAddressLineElement)) { 119 try { 120 var postalAddressLineElementValue = contactControl.getPostalAddressLineElementValueForUpdate(postalAddressLineElement); 121 122 postalAddressLineElementValue.setPostalAddressLineElementSortOrder(Integer.valueOf(edit.getPostalAddressLineElementSortOrder())); 123 postalAddressLineElementValue.setPostalAddressElementTypePK(postalAddressElementType.getPrimaryKey()); 124 postalAddressLineElementValue.setPrefix(edit.getPrefix()); 125 postalAddressLineElementValue.setAlwaysIncludePrefix(Boolean.valueOf(edit.getAlwaysIncludePrefix())); 126 postalAddressLineElementValue.setSuffix(edit.getSuffix()); 127 postalAddressLineElementValue.setAlwaysIncludeSuffix(Boolean.valueOf(edit.getAlwaysIncludeSuffix())); 128 129 contactControl.updatePostalAddressLineElementFromValue(postalAddressLineElementValue, getPartyPK()); 130 } finally { 131 unlockEntity(postalAddressLineElement); 132 } 133 } else { 134 addExecutionError(ExecutionErrors.EntityLockStale.name()); 135 } 136 } else { 137 addExecutionError(ExecutionErrors.UnknownPostalAddressElementTypeName.name(), postalAddressElementTypeName); 138 } 139 } else { 140 addExecutionError(ExecutionErrors.DuplicatePostalAddressLineElement.name(), postalAddressLineElementSortOrder.toString()); 141 } 142 } else { 143 addExecutionError(ExecutionErrors.UnknownPostalAddressLineElement.name(), postalAddressLineElementSortOrder.toString()); 144 } 145 146 if(hasExecutionErrors()) { 147 result.setPostalAddressLineElement(contactControl.getPostalAddressLineElementTransfer(getUserVisit(), postalAddressLineElement)); 148 result.setEntityLock(getEntityLockTransfer(postalAddressLineElement)); 149 } 150 } 151 } else { 152 addExecutionError(ExecutionErrors.UnknownPostalAddressLine.name(), postalAddressLineSortOrder.toString()); 153 } 154 } else { 155 addExecutionError(ExecutionErrors.UnknownPostalAddressFormatName.name(), postalAddressFormatName); 156 } 157 158 return result; 159 } 160 161}