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.PostalAddressLineEdit; 021import com.echothree.control.user.contact.common.result.ContactResultFactory; 022import com.echothree.control.user.contact.common.result.EditPostalAddressLineResult; 023import com.echothree.control.user.contact.common.spec.PostalAddressLineUniversalSpec; 024import com.echothree.model.control.contact.server.control.ContactControl; 025import com.echothree.model.control.contact.server.logic.PostalAddressLineLogic; 026import com.echothree.model.data.contact.server.entity.PostalAddressLine; 027import com.echothree.util.common.message.ExecutionErrors; 028import com.echothree.util.common.validation.FieldDefinition; 029import com.echothree.util.common.validation.FieldType; 030import com.echothree.util.server.control.BaseEditCommand; 031import java.util.List; 032import javax.enterprise.context.Dependent; 033import javax.inject.Inject; 034 035@Dependent 036public class EditPostalAddressLineCommand 037 extends BaseEditCommand<PostalAddressLineUniversalSpec, PostalAddressLineEdit, EditPostalAddressLineResult, PostalAddressLine, PostalAddressLine> { 038 039 private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS; 040 private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS; 041 042 static { 043 SPEC_FIELD_DEFINITIONS = List.of( 044 new FieldDefinition("PostalAddressFormatName", FieldType.ENTITY_NAME, false, null, null), 045 new FieldDefinition("PostalAddressLineSortOrder", FieldType.SIGNED_INTEGER, false, null, null), 046 new FieldDefinition("EntityRef", FieldType.ENTITY_REF, false, null, null), 047 new FieldDefinition("Uuid", FieldType.UUID, false, null, null) 048 ); 049 050 EDIT_FIELD_DEFINITIONS = List.of( 051 new FieldDefinition("PostalAddressLineSortOrder", FieldType.SIGNED_INTEGER, true, null, null), 052 new FieldDefinition("Prefix", FieldType.STRING, false, 1L, 10L), 053 new FieldDefinition("AlwaysIncludePrefix", FieldType.BOOLEAN, true, null, null), 054 new FieldDefinition("Suffix", FieldType.STRING, false, 1L, 10L), 055 new FieldDefinition("AlwaysIncludeSuffix", FieldType.BOOLEAN, true, null, null), 056 new FieldDefinition("CollapseIfEmpty", FieldType.BOOLEAN, true, null, null) 057 ); 058 } 059 060 @Inject 061 ContactControl contactControl; 062 063 @Inject 064 PostalAddressLineLogic postalAddressLineLogic; 065 066 /** Creates a new instance of EditPostalAddressLineCommand */ 067 public EditPostalAddressLineCommand() { 068 super(null, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 069 } 070 071 @Override 072 public EditPostalAddressLineResult getResult() { 073 return ContactResultFactory.getEditPostalAddressLineResult(); 074 } 075 076 @Override 077 public PostalAddressLineEdit getEdit() { 078 return ContactEditFactory.getPostalAddressLineEdit(); 079 } 080 081 @Override 082 public PostalAddressLine getEntity(EditPostalAddressLineResult result) { 083 return postalAddressLineLogic.getPostalAddressLineByUniversalSpec(this, spec, editModeToEntityPermission(editMode)); 084 } 085 086 @Override 087 public PostalAddressLine getLockEntity(PostalAddressLine postalAddressLine) { 088 return postalAddressLine; 089 } 090 091 @Override 092 public void fillInResult(EditPostalAddressLineResult result, PostalAddressLine postalAddressLine) { 093 result.setPostalAddressLine(contactControl.getPostalAddressLineTransfer(getUserVisit(), postalAddressLine)); 094 } 095 096 @Override 097 public void doLock(PostalAddressLineEdit edit, PostalAddressLine postalAddressLine) { 098 var postalAddressLineDetail = postalAddressLine.getLastDetail(); 099 100 edit.setPostalAddressLineSortOrder(postalAddressLineDetail.getPostalAddressLineSortOrder().toString()); 101 edit.setPrefix(postalAddressLineDetail.getPrefix()); 102 edit.setAlwaysIncludePrefix(postalAddressLineDetail.getAlwaysIncludePrefix().toString()); 103 edit.setSuffix(postalAddressLineDetail.getSuffix()); 104 edit.setAlwaysIncludeSuffix(postalAddressLineDetail.getAlwaysIncludeSuffix().toString()); 105 edit.setCollapseIfEmpty(postalAddressLineDetail.getCollapseIfEmpty().toString()); 106 } 107 108 @Override 109 public void canUpdate(PostalAddressLine postalAddressLine) { 110 var postalAddressFormat = postalAddressLine.getLastDetail().getPostalAddressFormat(); 111 var postalAddressLineSortOrder = Integer.valueOf(edit.getPostalAddressLineSortOrder()); 112 var duplicatePostalAddressLine = contactControl.getPostalAddressLine(postalAddressFormat, postalAddressLineSortOrder); 113 114 if(duplicatePostalAddressLine != null && !postalAddressLine.equals(duplicatePostalAddressLine)) { 115 addExecutionError(ExecutionErrors.DuplicatePostalAddressLine.name(), 116 postalAddressFormat.getLastDetail().getPostalAddressFormatName(), postalAddressLineSortOrder.toString()); 117 } 118 } 119 120 @Override 121 public void doUpdate(PostalAddressLine postalAddressLine) { 122 var postalAddressLineDetailValue = contactControl.getPostalAddressLineDetailValueForUpdate(postalAddressLine); 123 124 postalAddressLineDetailValue.setPostalAddressLineSortOrder(Integer.valueOf(edit.getPostalAddressLineSortOrder())); 125 postalAddressLineDetailValue.setPrefix(edit.getPrefix()); 126 postalAddressLineDetailValue.setAlwaysIncludePrefix(Boolean.valueOf(edit.getAlwaysIncludePrefix())); 127 postalAddressLineDetailValue.setSuffix(edit.getSuffix()); 128 postalAddressLineDetailValue.setAlwaysIncludeSuffix(Boolean.valueOf(edit.getAlwaysIncludeSuffix())); 129 postalAddressLineDetailValue.setCollapseIfEmpty(Boolean.valueOf(edit.getCollapseIfEmpty())); 130 131 contactControl.updatePostalAddressLineFromValue(postalAddressLineDetailValue, getPartyPK()); 132 } 133 134}