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.geo.server.command;
018
019import com.echothree.control.user.geo.common.form.GetCountiesForm;
020import com.echothree.control.user.geo.common.result.GeoResultFactory;
021import com.echothree.model.control.geo.server.control.GeoControl;
022import com.echothree.model.control.party.common.PartyTypes;
023import com.echothree.model.control.security.common.SecurityRoleGroups;
024import com.echothree.model.control.security.common.SecurityRoles;
025import com.echothree.model.data.geo.server.entity.GeoCode;
026import com.echothree.model.data.geo.server.entity.GeoCodeScope;
027import com.echothree.model.data.geo.server.factory.GeoCodeFactory;
028import com.echothree.util.common.command.BaseResult;
029import com.echothree.util.common.message.ExecutionErrors;
030import com.echothree.util.common.validation.FieldDefinition;
031import com.echothree.util.common.validation.FieldType;
032import com.echothree.util.server.control.BasePaginatedMultipleEntitiesCommand;
033import com.echothree.util.server.control.CommandSecurityDefinition;
034import com.echothree.util.server.control.PartyTypeDefinition;
035import com.echothree.util.server.control.SecurityRoleDefinition;
036import java.util.Collection;
037import java.util.List;
038import javax.enterprise.context.Dependent;
039import javax.inject.Inject;
040
041@Dependent
042public class GetCountiesCommand
043        extends BasePaginatedMultipleEntitiesCommand<GeoCode, GetCountiesForm> {
044    
045    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
046    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
047    
048    static {
049        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
050                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
051                new PartyTypeDefinition(PartyTypes.CUSTOMER.name(), null),
052                new PartyTypeDefinition(PartyTypes.VENDOR.name(), null),
053                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
054                        new SecurityRoleDefinition(SecurityRoleGroups.County.name(), SecurityRoles.List.name())
055                        ))
056                ));
057
058        FORM_FIELD_DEFINITIONS = List.of(
059                new FieldDefinition("CountryName", FieldType.ENTITY_NAME, true, null, null),
060                new FieldDefinition("StateName", FieldType.ENTITY_NAME, true, null, null)
061                );
062    }
063    
064    /** Creates a new instance of GetCountiesCommand */
065    public GetCountiesCommand() {
066        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, true);
067    }
068
069    @Inject
070    GeoControl geoControl;
071
072    GeoCode stateGeoCode;
073    GeoCodeScope countiesGeoCodeScope;
074
075    @Override
076    protected void handleForm() {
077        var countryName = form.getCountryName();
078        var countryGeoCode = geoControl.getCountryByAlias(countryName);
079
080        if(countryGeoCode != null) {
081            var stateName = form.getStateName();
082
083            stateGeoCode = geoControl.getStateByAlias(countryGeoCode, stateName);
084
085            if(stateGeoCode != null) {
086                countiesGeoCodeScope = geoControl.getCountiesGeoCodeScope(stateGeoCode);
087            } else {
088                addExecutionError(ExecutionErrors.UnknownStateName.name(), stateName);
089            }
090        } else {
091            addExecutionError(ExecutionErrors.UnknownCountryName.name(), countryName);
092        }
093    }
094
095    @Override
096    protected Long getTotalEntities() {
097        return countiesGeoCodeScope == null ? null : geoControl.countGeoCodesByGeoCodeScope(countiesGeoCodeScope);
098    }
099
100    @Override
101    protected Collection<GeoCode> getEntities() {
102        return countiesGeoCodeScope == null ? null : geoControl.getCountiesByState(countiesGeoCodeScope);
103    }
104
105    @Override
106    protected BaseResult getResult(Collection<GeoCode> entities) {
107        var result = GeoResultFactory.getGetCountiesResult();
108
109        if(entities != null) {
110            var userVisit = getUserVisit();
111
112            result.setState(geoControl.getStateTransfer(userVisit, stateGeoCode));
113
114            if(session.hasLimit(GeoCodeFactory.class)) {
115                result.setCountyCount(getTotalEntities());
116            }
117
118            result.setCounties(geoControl.getCountyTransfers(userVisit, entities));
119        }
120
121        return result;
122    }
123
124}