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