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.item.server.command;
018
019import com.echothree.control.user.item.common.form.GetItemHarmonizedTariffScheduleCodesForm;
020import com.echothree.control.user.item.common.result.ItemResultFactory;
021import com.echothree.model.control.geo.server.control.GeoControl;
022import com.echothree.model.control.geo.server.logic.GeoCodeLogic;
023import com.echothree.model.control.item.server.control.ItemControl;
024import com.echothree.model.control.item.server.logic.HarmonizedTariffScheduleCodeLogic;
025import com.echothree.model.control.item.server.logic.ItemLogic;
026import com.echothree.model.control.party.common.PartyTypes;
027import com.echothree.model.control.security.common.SecurityRoleGroups;
028import com.echothree.model.control.security.common.SecurityRoles;
029import com.echothree.model.data.geo.server.entity.GeoCode;
030import com.echothree.model.data.item.server.entity.HarmonizedTariffScheduleCodeUseType;
031import com.echothree.model.data.item.server.entity.Item;
032import com.echothree.model.data.item.server.entity.ItemHarmonizedTariffScheduleCode;
033import com.echothree.model.data.item.server.factory.ItemHarmonizedTariffScheduleCodeFactory;
034import com.echothree.util.common.command.BaseResult;
035import com.echothree.util.common.message.ExecutionErrors;
036import com.echothree.util.common.validation.FieldDefinition;
037import com.echothree.util.common.validation.FieldType;
038import com.echothree.util.server.control.BasePaginatedMultipleEntitiesCommand;
039import com.echothree.util.server.control.CommandSecurityDefinition;
040import com.echothree.util.server.control.PartyTypeDefinition;
041import com.echothree.util.server.control.SecurityRoleDefinition;
042import java.util.Collection;
043import java.util.List;
044import javax.enterprise.context.Dependent;
045import javax.inject.Inject;
046
047@Dependent
048public class GetItemHarmonizedTariffScheduleCodesCommand
049        extends BasePaginatedMultipleEntitiesCommand<ItemHarmonizedTariffScheduleCode, GetItemHarmonizedTariffScheduleCodesForm> {
050
051    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
052    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
053
054    static {
055        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
056                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
057                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
058                        new SecurityRoleDefinition(SecurityRoleGroups.ItemHarmonizedTariffScheduleCode.name(), SecurityRoles.List.name())
059                ))
060        ));
061
062        FORM_FIELD_DEFINITIONS = List.of(
063                new FieldDefinition("ItemName", FieldType.ENTITY_NAME, false, null, null),
064                new FieldDefinition("CountryName", FieldType.ENTITY_NAME, false, null, null),
065                new FieldDefinition("HarmonizedTariffScheduleCodeUseTypeName", FieldType.ENTITY_NAME, false, null, null)
066        );
067    }
068
069    @Inject
070    GeoControl geoControl;
071
072    @Inject
073    ItemControl itemControl;
074
075    @Inject
076    GeoCodeLogic geoCodeLogic;
077
078    @Inject
079    HarmonizedTariffScheduleCodeLogic harmonizedTariffScheduleCodeLogic;
080
081    @Inject
082    ItemLogic itemLogic;
083
084    /** Creates a new instance of GetItemHarmonizedTariffScheduleCodesCommand */
085    public GetItemHarmonizedTariffScheduleCodesCommand() {
086        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, true);
087    }
088
089    private Item item;
090    private GeoCode countryGeoCode;
091    private HarmonizedTariffScheduleCodeUseType harmonizedTariffScheduleCodeUseType;
092
093    @Override
094    protected void handleForm() {
095        var itemName = form.getItemName();
096        var countryName = form.getCountryName();
097        var harmonizedTariffScheduleCodeUseTypeName = form.getHarmonizedTariffScheduleCodeUseTypeName();
098        var parameterCount = (itemName == null ? 0 : 1) + (countryName == null ? 0 : 1) + (harmonizedTariffScheduleCodeUseTypeName == null ? 0 : 1);
099
100        if(parameterCount == 1) {
101            if(itemName != null) {
102                item = itemLogic.getItemByName(this, itemName);
103            } else if(countryName != null) {
104                countryGeoCode = geoCodeLogic.getGeoCodeByAlias(this, geoControl.getGeoCodeTypeByName("COUNTRY"), geoControl.getGeoCodeScopeByName("UTILITY"), "ISO_2_LETTER", countryName);
105            } else {
106                harmonizedTariffScheduleCodeUseType = harmonizedTariffScheduleCodeLogic.getHarmonizedTariffScheduleCodeUseTypeByName(this, harmonizedTariffScheduleCodeUseTypeName);
107            }
108        } else {
109            addExecutionError(ExecutionErrors.InvalidParameterCount.name());
110        }
111    }
112
113    @Override
114    protected Long getTotalEntities() {
115        Long total = null;
116
117        if(!hasExecutionErrors()) {
118            if(item != null) {
119                total = itemControl.countItemHarmonizedTariffScheduleCodesByItem(item);
120            } else if(countryGeoCode != null) {
121                total = itemControl.countItemHarmonizedTariffScheduleCodesByCountryGeoCode(countryGeoCode);
122            } else if(harmonizedTariffScheduleCodeUseType != null) {
123                total = itemControl.countItemHarmonizedTariffScheduleCodesByHarmonizedTariffScheduleCodeUseType(harmonizedTariffScheduleCodeUseType);
124            }
125        }
126
127        return total;
128    }
129
130    @Override
131    protected Collection<ItemHarmonizedTariffScheduleCode> getEntities() {
132        Collection<ItemHarmonizedTariffScheduleCode> entities = null;
133
134        if(!hasExecutionErrors()) {
135            if(item != null) {
136                entities = itemControl.getItemHarmonizedTariffScheduleCodesByItem(item);
137            } else if(countryGeoCode != null) {
138                entities = itemControl.getItemHarmonizedTariffScheduleCodesByCountryGeoCode(countryGeoCode);
139            } else if(harmonizedTariffScheduleCodeUseType != null) {
140                entities = itemControl.getItemHarmonizedTariffScheduleCodesByHarmonizedTariffScheduleCodeUse(harmonizedTariffScheduleCodeUseType);
141            }
142        }
143
144        return entities;
145    }
146
147    @Override
148    protected BaseResult getResult(Collection<ItemHarmonizedTariffScheduleCode> entities) {
149        var result = ItemResultFactory.getGetItemHarmonizedTariffScheduleCodesResult();
150
151        if(entities != null) {
152            if(item != null) {
153                result.setItem(itemControl.getItemTransfer(getUserVisit(), item));
154            } else if(countryGeoCode != null) {
155                result.setCountry(geoControl.getCountryTransfer(getUserVisit(), countryGeoCode));
156            } else if(harmonizedTariffScheduleCodeUseType != null) {
157                result.setHarmonizedTariffScheduleCodeUseType(itemControl.getHarmonizedTariffScheduleCodeUseTypeTransfer(getUserVisit(), harmonizedTariffScheduleCodeUseType));
158            }
159
160            if(session.hasLimit(ItemHarmonizedTariffScheduleCodeFactory.class)) {
161                result.setItemHarmonizedTariffScheduleCodeCount(getTotalEntities());
162            }
163
164            result.setItemHarmonizedTariffScheduleCodes(itemControl.getItemHarmonizedTariffScheduleCodeTransfers(getUserVisit(), entities));
165        }
166
167        return result;
168    }
169
170}