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.form.GetPostalAddressLineElementsForm; 020import com.echothree.control.user.contact.common.result.ContactResultFactory; 021import com.echothree.model.control.contact.server.control.ContactControl; 022import com.echothree.model.data.contact.server.entity.PostalAddressLine; 023import com.echothree.model.data.contact.server.entity.PostalAddressLineElement; 024import com.echothree.model.data.contact.server.factory.PostalAddressLineElementFactory; 025import com.echothree.util.common.command.BaseResult; 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.server.control.BasePaginatedMultipleEntitiesCommand; 030import java.util.Collection; 031import java.util.List; 032import javax.enterprise.context.Dependent; 033import javax.inject.Inject; 034 035@Dependent 036public class GetPostalAddressLineElementsCommand 037 extends BasePaginatedMultipleEntitiesCommand<PostalAddressLineElement, GetPostalAddressLineElementsForm> { 038 039 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 040 041 static { 042 FORM_FIELD_DEFINITIONS = List.of( 043 new FieldDefinition("PostalAddressFormatName", FieldType.ENTITY_NAME, true, null, null), 044 new FieldDefinition("PostalAddressLineSortOrder", FieldType.SIGNED_INTEGER, true, null, null) 045 ); 046 } 047 048 @Inject 049 ContactControl contactControl; 050 051 052 /** Creates a new instance of GetPostalAddressLineElementsCommand */ 053 public GetPostalAddressLineElementsCommand() { 054 super(null, FORM_FIELD_DEFINITIONS, true); 055 } 056 057 private PostalAddressLine postalAddressLine; 058 059 @Override 060 protected void handleForm() { 061 var postalAddressFormatName = form.getPostalAddressFormatName(); 062 var postalAddressFormat = contactControl.getPostalAddressFormatByName(postalAddressFormatName); 063 064 if(postalAddressFormat != null) { 065 var postalAddressLineSortOrder = Integer.valueOf(form.getPostalAddressLineSortOrder()); 066 067 postalAddressLine = contactControl.getPostalAddressLine(postalAddressFormat, postalAddressLineSortOrder); 068 069 if(postalAddressLine == null) { 070 addExecutionError(ExecutionErrors.UnknownPostalAddressLine.name(), 071 postalAddressFormat.getLastDetail().getPostalAddressFormatName(), 072 postalAddressLineSortOrder); 073 } 074 } else { 075 addExecutionError(ExecutionErrors.UnknownPostalAddressFormatName.name(), postalAddressFormatName); 076 } 077 } 078 079 @Override 080 protected Long getTotalEntities() { 081 return hasExecutionErrors() ? null : contactControl.countPostalAddressLineElementsByPostalAddressLine(postalAddressLine); 082 } 083 084 @Override 085 protected Collection<PostalAddressLineElement> getEntities() { 086 return hasExecutionErrors() ? null : contactControl.getPostalAddressLineElementsByPostalAddressLine(postalAddressLine); 087 } 088 089 @Override 090 protected BaseResult getResult(Collection<PostalAddressLineElement> entities) { 091 var result = ContactResultFactory.getGetPostalAddressLineElementsResult(); 092 093 if(entities != null) { 094 var userVisit = getUserVisit(); 095 096 result.setPostalAddressLine(contactControl.getPostalAddressLineTransfer(userVisit, postalAddressLine)); 097 098 if(session.hasLimit(PostalAddressLineElementFactory.class)) { 099 result.setPostalAddressLineElementCount(getTotalEntities()); 100 } 101 102 result.setPostalAddressLineElements(contactControl.getPostalAddressLineElementTransfersByPostalAddressLine(userVisit, postalAddressLine)); 103 } 104 105 return result; 106 } 107 108}