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    /** Creates a new instance of GetPostalAddressLineElementsCommand */
052    public GetPostalAddressLineElementsCommand() {
053        super(null, FORM_FIELD_DEFINITIONS, true);
054    }
055
056    private PostalAddressLine postalAddressLine;
057
058    @Override
059    protected void handleForm() {
060        var postalAddressFormatName = form.getPostalAddressFormatName();
061        var postalAddressFormat = contactControl.getPostalAddressFormatByName(postalAddressFormatName);
062
063        if(postalAddressFormat != null) {
064            var postalAddressLineSortOrder = Integer.valueOf(form.getPostalAddressLineSortOrder());
065
066            postalAddressLine = contactControl.getPostalAddressLine(postalAddressFormat, postalAddressLineSortOrder);
067
068            if(postalAddressLine == null) {
069                addExecutionError(ExecutionErrors.UnknownPostalAddressLine.name(),
070                        postalAddressFormat.getLastDetail().getPostalAddressFormatName(),
071                        postalAddressLineSortOrder);
072            }
073        } else {
074            addExecutionError(ExecutionErrors.UnknownPostalAddressFormatName.name(), postalAddressFormatName);
075        }
076    }
077
078    @Override
079    protected Long getTotalEntities() {
080        return hasExecutionErrors() ? null : contactControl.countPostalAddressLineElementsByPostalAddressLine(postalAddressLine);
081    }
082
083    @Override
084    protected Collection<PostalAddressLineElement> getEntities() {
085        return hasExecutionErrors() ? null : contactControl.getPostalAddressLineElementsByPostalAddressLine(postalAddressLine);
086    }
087
088    @Override
089    protected BaseResult getResult(Collection<PostalAddressLineElement> entities) {
090        var result = ContactResultFactory.getGetPostalAddressLineElementsResult();
091        
092        if(entities != null) {
093            var userVisit = getUserVisit();
094
095            result.setPostalAddressLine(contactControl.getPostalAddressLineTransfer(userVisit, postalAddressLine));
096
097            if(session.hasLimit(PostalAddressLineElementFactory.class)) {
098                result.setPostalAddressLineElementCount(getTotalEntities());
099            }
100
101            result.setPostalAddressLineElements(contactControl.getPostalAddressLineElementTransfersByPostalAddressLine(userVisit, postalAddressLine));
102        }
103        
104        return result;
105    }
106    
107}