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