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.form.CreatePostalAddressLineElementForm; 020import com.echothree.model.control.contact.server.control.ContactControl; 021import com.echothree.model.data.contact.server.entity.PostalAddressElementType; 022import com.echothree.model.data.contact.server.entity.PostalAddressFormat; 023import com.echothree.model.data.contact.server.entity.PostalAddressLine; 024import com.echothree.model.data.contact.server.entity.PostalAddressLineElement; 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.server.control.BaseSimpleCommand; 031import com.echothree.util.server.persistence.Session; 032import java.util.Arrays; 033import java.util.Collections; 034import java.util.List; 035 036public class CreatePostalAddressLineElementCommand 037 extends BaseSimpleCommand<CreatePostalAddressLineElementForm> { 038 039 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 040 041 static { 042 FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 043 new FieldDefinition("PostalAddressFormatName", FieldType.ENTITY_NAME, true, null, null), 044 new FieldDefinition("PostalAddressLineSortOrder", FieldType.SIGNED_INTEGER, true, null, null), 045 new FieldDefinition("PostalAddressLineElementSortOrder", FieldType.SIGNED_INTEGER, true, null, null), 046 new FieldDefinition("PostalAddressElementTypeName", FieldType.ENTITY_NAME, true, null, null), 047 new FieldDefinition("Prefix", FieldType.STRING, false, 1L, 10L), 048 new FieldDefinition("AlwaysIncludePrefix", FieldType.BOOLEAN, true, null, null), 049 new FieldDefinition("Suffix", FieldType.STRING, false, 1L, 10L), 050 new FieldDefinition("AlwaysIncludeSuffix", FieldType.BOOLEAN, true, null, null) 051 )); 052 } 053 054 /** Creates a new instance of CreatePostalAddressLineElementCommand */ 055 public CreatePostalAddressLineElementCommand(UserVisitPK userVisitPK, CreatePostalAddressLineElementForm form) { 056 super(userVisitPK, form, null, FORM_FIELD_DEFINITIONS, false); 057 } 058 059 @Override 060 protected BaseResult execute() { 061 var contactControl = Session.getModelController(ContactControl.class); 062 String postalAddressFormatName = form.getPostalAddressFormatName(); 063 PostalAddressFormat postalAddressFormat = contactControl.getPostalAddressFormatByName(postalAddressFormatName); 064 065 if(postalAddressFormat != null) { 066 Integer postalAddressLineSortOrder = Integer.valueOf(form.getPostalAddressLineSortOrder()); 067 PostalAddressLine postalAddressLine = contactControl.getPostalAddressLine(postalAddressFormat, postalAddressLineSortOrder); 068 069 if(postalAddressLine != null) { 070 Integer postalAddressLineElementSortOrder = Integer.valueOf(form.getPostalAddressLineElementSortOrder()); 071 PostalAddressLineElement postalAddressLineElement = contactControl.getPostalAddressLineElement(postalAddressLine, postalAddressLineElementSortOrder); 072 073 if(postalAddressLineElement == null) { 074 String postalAddressElementTypeName = form.getPostalAddressElementTypeName(); 075 PostalAddressElementType postalAddressElementType = contactControl.getPostalAddressElementTypeByName(postalAddressElementTypeName); 076 077 if(postalAddressElementType != null) { 078 String prefix = form.getPrefix(); 079 Boolean alwaysIncludePrefix = Boolean.valueOf(form.getAlwaysIncludePrefix()); 080 081 if(!alwaysIncludePrefix || (alwaysIncludePrefix && prefix != null)) { 082 String suffix = form.getSuffix(); 083 Boolean alwaysIncludeSuffix = Boolean.valueOf(form.getAlwaysIncludeSuffix()); 084 085 if(!alwaysIncludeSuffix || (alwaysIncludeSuffix && suffix != null)) { 086 contactControl.createPostalAddressLineElement(postalAddressLine, postalAddressLineElementSortOrder, postalAddressElementType, prefix, alwaysIncludePrefix, suffix, 087 alwaysIncludeSuffix, getPartyPK()); 088 } else { 089 addExecutionError(ExecutionErrors.CannotAlwaysIncludeEmptySuffix.name()); 090 } 091 } else { 092 addExecutionError(ExecutionErrors.CannotAlwaysIncludeEmptyPrefix.name()); 093 } 094 } else { 095 addExecutionError(ExecutionErrors.UnknownPostalAddressElementTypeName.name(), postalAddressElementTypeName); 096 } 097 } else { 098 addExecutionError(ExecutionErrors.DuplicatePostalAddressLineElement.name(), postalAddressLineElementSortOrder); 099 } 100 } else { 101 addExecutionError(ExecutionErrors.UnknownPostalAddressLine.name(), postalAddressLineSortOrder); 102 } 103 } else { 104 addExecutionError(ExecutionErrors.UnknownPostalAddressFormatName.name(), postalAddressFormatName); 105 } 106 107 return null; 108 } 109 110}