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 java.util.ArrayList; 033import java.util.List; 034import javax.enterprise.context.Dependent; 035import javax.inject.Inject; 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 @Inject 062 ContactControl contactControl; 063 064 065 /** Creates a new instance of EditPostalAddressLineElementCommand */ 066 public EditPostalAddressLineElementCommand() { 067 super(null, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 068 } 069 070 @Override 071 protected BaseResult execute() { 072 var result = ContactResultFactory.getEditPostalAddressLineElementResult(); 073 var postalAddressFormatName = spec.getPostalAddressFormatName(); 074 var postalAddressFormat = contactControl.getPostalAddressFormatByName(postalAddressFormatName); 075 076 if(postalAddressFormat != null) { 077 var postalAddressLineSortOrder = Integer.valueOf(spec.getPostalAddressLineSortOrder()); 078 var postalAddressLine = contactControl.getPostalAddressLine(postalAddressFormat, postalAddressLineSortOrder); 079 080 if(postalAddressLine != null) { 081 if(editMode.equals(EditMode.LOCK)) { 082 var postalAddressLineElementSortOrder = Integer.valueOf(spec.getPostalAddressLineElementSortOrder()); 083 var postalAddressLineElement = contactControl.getPostalAddressLineElement(postalAddressLine, postalAddressLineElementSortOrder); 084 085 if(postalAddressLineElement != null) { 086 result.setPostalAddressLineElement(contactControl.getPostalAddressLineElementTransfer(getUserVisit(), postalAddressLineElement)); 087 088 if(lockEntity(postalAddressLineElement)) { 089 var edit = ContactEditFactory.getPostalAddressLineElementEdit(); 090 091 result.setEdit(edit); 092 edit.setPostalAddressLineElementSortOrder(postalAddressLineElement.getPostalAddressLineElementSortOrder().toString()); 093 edit.setPostalAddressElementTypeName(postalAddressLineElement.getPostalAddressElementType().getPostalAddressElementTypeName()); 094 edit.setPrefix(postalAddressLineElement.getPrefix()); 095 edit.setAlwaysIncludePrefix(postalAddressLineElement.getAlwaysIncludePrefix().toString()); 096 edit.setSuffix(postalAddressLineElement.getSuffix()); 097 edit.setAlwaysIncludeSuffix(postalAddressLineElement.getAlwaysIncludeSuffix().toString()); 098 } else { 099 addExecutionError(ExecutionErrors.EntityLockFailed.name()); 100 } 101 102 result.setEntityLock(getEntityLockTransfer(postalAddressLineElement)); 103 } else { 104 addExecutionError(ExecutionErrors.UnknownPostalAddressLineElement.name(), postalAddressLineElementSortOrder.toString()); 105 } 106 } else if(editMode.equals(EditMode.UPDATE)) { 107 var postalAddressLineElementSortOrder = Integer.valueOf(spec.getPostalAddressLineElementSortOrder()); 108 var postalAddressLineElement = contactControl.getPostalAddressLineElementForUpdate(postalAddressLine, 109 postalAddressLineElementSortOrder); 110 111 if(postalAddressLineElement != null) { 112 postalAddressLineElementSortOrder = Integer.valueOf(edit.getPostalAddressLineElementSortOrder()); 113 var duplicatePostalAddressLineElement = contactControl.getPostalAddressLineElementForUpdate(postalAddressLine, 114 postalAddressLineElementSortOrder); 115 116 if(duplicatePostalAddressLineElement == null || postalAddressLineElement.equals(duplicatePostalAddressLineElement)) { 117 var postalAddressElementTypeName = edit.getPostalAddressElementTypeName(); 118 var postalAddressElementType = contactControl.getPostalAddressElementTypeByName(postalAddressElementTypeName); 119 120 if(postalAddressElementType != null) { 121 if(lockEntityForUpdate(postalAddressLineElement)) { 122 try { 123 var postalAddressLineElementValue = contactControl.getPostalAddressLineElementValueForUpdate(postalAddressLineElement); 124 125 postalAddressLineElementValue.setPostalAddressLineElementSortOrder(Integer.valueOf(edit.getPostalAddressLineElementSortOrder())); 126 postalAddressLineElementValue.setPostalAddressElementTypePK(postalAddressElementType.getPrimaryKey()); 127 postalAddressLineElementValue.setPrefix(edit.getPrefix()); 128 postalAddressLineElementValue.setAlwaysIncludePrefix(Boolean.valueOf(edit.getAlwaysIncludePrefix())); 129 postalAddressLineElementValue.setSuffix(edit.getSuffix()); 130 postalAddressLineElementValue.setAlwaysIncludeSuffix(Boolean.valueOf(edit.getAlwaysIncludeSuffix())); 131 132 contactControl.updatePostalAddressLineElementFromValue(postalAddressLineElementValue, getPartyPK()); 133 } finally { 134 unlockEntity(postalAddressLineElement); 135 } 136 } else { 137 addExecutionError(ExecutionErrors.EntityLockStale.name()); 138 } 139 } else { 140 addExecutionError(ExecutionErrors.UnknownPostalAddressElementTypeName.name(), postalAddressElementTypeName); 141 } 142 } else { 143 addExecutionError(ExecutionErrors.DuplicatePostalAddressLineElement.name(), postalAddressLineElementSortOrder.toString()); 144 } 145 } else { 146 addExecutionError(ExecutionErrors.UnknownPostalAddressLineElement.name(), postalAddressLineElementSortOrder.toString()); 147 } 148 149 if(hasExecutionErrors()) { 150 result.setPostalAddressLineElement(contactControl.getPostalAddressLineElementTransfer(getUserVisit(), postalAddressLineElement)); 151 result.setEntityLock(getEntityLockTransfer(postalAddressLineElement)); 152 } 153 } 154 } else { 155 addExecutionError(ExecutionErrors.UnknownPostalAddressLine.name(), postalAddressLineSortOrder.toString()); 156 } 157 } else { 158 addExecutionError(ExecutionErrors.UnknownPostalAddressFormatName.name(), postalAddressFormatName); 159 } 160 161 return result; 162 } 163 164}