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