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.tax.server.command;
018
019import com.echothree.control.user.tax.common.form.GetGeoCodeTaxesForm;
020import com.echothree.control.user.tax.common.result.TaxResultFactory;
021import com.echothree.model.control.geo.server.control.GeoControl;
022import com.echothree.model.control.geo.server.logic.GeoCodeLogic;
023import com.echothree.model.control.tax.server.control.TaxControl;
024import com.echothree.model.control.tax.server.logic.TaxLogic;
025import com.echothree.model.data.geo.server.entity.GeoCode;
026import com.echothree.model.data.tax.server.entity.GeoCodeTax;
027import com.echothree.model.data.tax.server.entity.Tax;
028import com.echothree.model.data.tax.server.factory.GeoCodeTaxFactory;
029import com.echothree.util.common.command.BaseResult;
030import com.echothree.util.common.message.ExecutionErrors;
031import com.echothree.util.common.validation.FieldDefinition;
032import com.echothree.util.common.validation.FieldType;
033import com.echothree.util.server.control.BasePaginatedMultipleEntitiesCommand;
034import java.util.Collection;
035import java.util.List;
036import javax.enterprise.context.Dependent;
037import javax.inject.Inject;
038
039@Dependent
040public class GetGeoCodeTaxesCommand
041        extends BasePaginatedMultipleEntitiesCommand<GeoCodeTax, GetGeoCodeTaxesForm> {
042    
043    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
044    
045    static {
046        FORM_FIELD_DEFINITIONS = List.of(
047                new FieldDefinition("GeoCodeName", FieldType.ENTITY_NAME, false, null, null),
048                new FieldDefinition("TaxName", FieldType.ENTITY_NAME, false, null, null)
049        );
050    }
051
052    @Inject
053    GeoControl geoControl;
054
055    @Inject
056    TaxControl taxControl;
057
058    @Inject
059    GeoCodeLogic geoCodeLogic;
060
061    @Inject
062    TaxLogic taxLogic;
063
064    /** Creates a new instance of GetGeoCodeTaxesCommand */
065    public GetGeoCodeTaxesCommand() {
066        super(null, FORM_FIELD_DEFINITIONS, true);
067    }
068
069    GeoCode geoCode;
070    Tax tax;
071
072    @Override
073    protected void handleForm() {
074        var geoCodeName = form.getGeoCodeName();
075        var taxName = form.getTaxName();
076        var parameterCount = (geoCodeName == null ? 0 : 1) + (taxName == null ? 0 : 1);
077
078        if(parameterCount == 1) {
079            if(geoCodeName == null) {
080                tax = taxLogic.getTaxByName(this, taxName);
081            } else {
082                geoCode = geoCodeLogic.getGeoCodeByName(this, geoCodeName);
083            }
084        } else {
085            addExecutionError(ExecutionErrors.InvalidParameterCount.name());
086        }
087    }
088
089    @Override
090    protected Long getTotalEntities() {
091        return hasExecutionErrors() ? null : geoCode == null
092                ? taxControl.countGeoCodeTaxesByTax(tax)
093                : taxControl.countGeoCodeTaxesByGeoCode(geoCode);
094    }
095
096    @Override
097    protected Collection<GeoCodeTax> getEntities() {
098        return hasExecutionErrors() ? null : geoCode == null
099                ? taxControl.getGeoCodeTaxesByTax(tax)
100                : taxControl.getGeoCodeTaxesByGeoCode(geoCode);
101    }
102
103    @Override
104    protected BaseResult getResult(Collection<GeoCodeTax> entities) {
105        var result = TaxResultFactory.getGetGeoCodeTaxesResult();
106
107        if(entities != null) {
108            var userVisit = getUserVisit();
109
110            if(geoCode == null) {
111                result.setTax(taxControl.getTaxTransfer(userVisit, tax));
112            } else {
113                result.setGeoCode(geoControl.getGeoCodeTransfer(userVisit, geoCode));
114            }
115
116            if(session.hasLimit(GeoCodeTaxFactory.class)) {
117                result.setGeoCodeTaxCount(getTotalEntities());
118            }
119
120            result.setGeoCodeTaxes(taxControl.getGeoCodeTaxTransfers(userVisit, entities));
121        }
122
123        return result;
124    }
125    
126}