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.GetPostalAddressLinesForm; 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.PostalAddressFormat; 023import com.echothree.model.data.contact.server.entity.PostalAddressLine; 024import com.echothree.model.data.contact.server.factory.PostalAddressLineFactory; 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 GetPostalAddressLinesCommand 037 extends BasePaginatedMultipleEntitiesCommand<PostalAddressLine, GetPostalAddressLinesForm> { 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 ); 045 } 046 047 @Inject 048 ContactControl contactControl; 049 050 051 /** Creates a new instance of GetPostalAddressLinesCommand */ 052 public GetPostalAddressLinesCommand() { 053 super(null, FORM_FIELD_DEFINITIONS, true); 054 } 055 056 private PostalAddressFormat postalAddressFormat; 057 058 @Override 059 protected void handleForm() { 060 var postalAddressFormatName = form.getPostalAddressFormatName(); 061 062 postalAddressFormat = contactControl.getPostalAddressFormatByName(postalAddressFormatName); 063 064 if(postalAddressFormat == null) { 065 addExecutionError(ExecutionErrors.UnknownPostalAddressFormatName.name(), postalAddressFormatName); 066 } 067 } 068 069 @Override 070 protected Long getTotalEntities() { 071 return hasExecutionErrors() ? null : contactControl.countPostalAddressLinesByPostalAddressFormat(postalAddressFormat); 072 } 073 074 @Override 075 protected Collection<PostalAddressLine> getEntities() { 076 return hasExecutionErrors() ? null : contactControl.getPostalAddressLinesByPostalAddressFormat(postalAddressFormat); 077 } 078 079 @Override 080 protected BaseResult getResult(Collection<PostalAddressLine> entities) { 081 var result = ContactResultFactory.getGetPostalAddressLinesResult(); 082 083 if(entities != null) { 084 var userVisit = getUserVisit(); 085 086 result.setPostalAddressFormat(contactControl.getPostalAddressFormatTransfer(userVisit, postalAddressFormat)); 087 088 if(session.hasLimit(PostalAddressLineFactory.class)) { 089 result.setPostalAddressLineCount(getTotalEntities()); 090 } 091 092 result.setPostalAddressLines(contactControl.getPostalAddressLineTransfers(userVisit, entities)); 093 } 094 095 return result; 096 } 097 098}