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.GetItemTaxClassificationsForm;
020import com.echothree.control.user.tax.common.result.TaxResultFactory;
021import com.echothree.model.control.geo.server.control.GeoControl;
022import com.echothree.model.control.item.server.control.ItemControl;
023import com.echothree.model.control.item.server.logic.ItemLogic;
024import com.echothree.model.control.party.common.PartyTypes;
025import com.echothree.model.control.security.common.SecurityRoleGroups;
026import com.echothree.model.control.security.common.SecurityRoles;
027import com.echothree.model.control.tax.server.control.TaxControl;
028import com.echothree.model.data.geo.server.entity.GeoCode;
029import com.echothree.model.data.item.server.entity.Item;
030import com.echothree.model.data.tax.server.entity.ItemTaxClassification;
031import com.echothree.model.data.tax.server.factory.ItemTaxClassificationFactory;
032import com.echothree.util.common.command.BaseResult;
033import com.echothree.util.common.message.ExecutionErrors;
034import com.echothree.util.common.validation.FieldDefinition;
035import com.echothree.util.common.validation.FieldType;
036import com.echothree.util.server.control.BasePaginatedMultipleEntitiesCommand;
037import com.echothree.util.server.control.CommandSecurityDefinition;
038import com.echothree.util.server.control.PartyTypeDefinition;
039import com.echothree.util.server.control.SecurityRoleDefinition;
040import java.util.Collection;
041import java.util.List;
042import javax.enterprise.context.Dependent;
043import javax.inject.Inject;
044
045@Dependent
046public class GetItemTaxClassificationsCommand
047        extends BasePaginatedMultipleEntitiesCommand<ItemTaxClassification, GetItemTaxClassificationsForm> {
048    
049    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
050    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
051    
052    static {
053        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
054                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
055                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
056                        new SecurityRoleDefinition(SecurityRoleGroups.ItemTaxClassification.name(), SecurityRoles.List.name())
057                ))
058        ));
059
060        FORM_FIELD_DEFINITIONS = List.of(
061                new FieldDefinition("ItemName", FieldType.ENTITY_NAME, false, null, null),
062                new FieldDefinition("CountryName", FieldType.ENTITY_NAME, false, null, null)
063        );
064    }
065
066    @Inject
067    GeoControl geoControl;
068
069    @Inject
070    ItemControl itemControl;
071
072    @Inject
073    TaxControl taxControl;
074
075    @Inject
076    ItemLogic itemLogic;
077
078    
079    /** Creates a new instance of GetItemTaxClassificationsCommand */
080    public GetItemTaxClassificationsCommand() {
081        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, true);
082    }
083    
084    private Item item;
085    private GeoCode countryGeoCode;
086
087    @Override
088    protected void handleForm() {
089        var itemName = form.getItemName();
090        var countryName = form.getCountryName();
091        var parameterCount = (itemName == null ? 0 : 1) + (countryName == null ? 0 : 1);
092
093        if(parameterCount == 1) {
094            if(itemName != null) {
095                item = itemLogic.getItemByName(this, itemName);
096            } else {
097                countryGeoCode = geoControl.getCountryByAlias(countryName);
098
099                if(countryGeoCode == null) {
100                    addExecutionError(ExecutionErrors.UnknownCountryName.name(), countryName);
101                }
102            }
103        } else {
104            addExecutionError(ExecutionErrors.InvalidParameterCount.name());
105        }
106    }
107
108    @Override
109    protected Long getTotalEntities() {
110        return hasExecutionErrors() ? null : item == null
111                ? taxControl.countItemTaxClassificationByCountryGeoCode(countryGeoCode)
112                : taxControl.countItemTaxClassificationByItem(item);
113    }
114
115    @Override
116    protected Collection<ItemTaxClassification> getEntities() {
117        return hasExecutionErrors() ? null : item == null
118                ? taxControl.getItemTaxClassificationsByCountryGeoCode(countryGeoCode)
119                : taxControl.getItemTaxClassificationsByItem(item);
120    }
121
122    @Override
123    protected BaseResult getResult(Collection<ItemTaxClassification> entities) {
124        var result = TaxResultFactory.getGetItemTaxClassificationsResult();
125
126        if(entities != null) {
127            var userVisit = getUserVisit();
128
129            if(item == null) {
130                result.setCountry(geoControl.getCountryTransfer(userVisit, countryGeoCode));
131            } else {
132                result.setItem(itemControl.getItemTransfer(userVisit, item));
133            }
134
135            if(session.hasLimit(ItemTaxClassificationFactory.class)) {
136                result.setItemTaxClassificationCount(getTotalEntities());
137            }
138
139            result.setItemTaxClassifications(taxControl.getItemTaxClassificationTransfers(userVisit, entities));
140        }
141
142        return result;
143    }
144    
145}