001// --------------------------------------------------------------------------------
002// Copyright 2002-2025 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.GetCountyForm;
020import com.echothree.control.user.geo.common.result.GeoResultFactory;
021import com.echothree.model.control.core.common.EventTypes;
022import com.echothree.model.control.geo.common.GeoCodeAliasTypes;
023import com.echothree.model.control.geo.common.GeoCodeTypes;
024import com.echothree.model.control.geo.server.control.GeoControl;
025import com.echothree.model.control.party.common.PartyTypes;
026import com.echothree.model.control.security.common.SecurityRoleGroups;
027import com.echothree.model.control.security.common.SecurityRoles;
028import com.echothree.model.data.geo.server.entity.GeoCode;
029import com.echothree.model.data.geo.server.entity.GeoCodeAlias;
030import com.echothree.util.common.command.BaseResult;
031import com.echothree.util.common.message.ExecutionErrors;
032import com.echothree.util.common.validation.FieldDefinition;
033import com.echothree.util.common.validation.FieldType;
034import com.echothree.util.server.control.BaseSingleEntityCommand;
035import com.echothree.util.server.control.CommandSecurityDefinition;
036import com.echothree.util.server.control.PartyTypeDefinition;
037import com.echothree.util.server.control.SecurityRoleDefinition;
038import com.echothree.util.server.persistence.Session;
039import java.util.Arrays;
040import java.util.Collections;
041import java.util.List;
042import javax.enterprise.context.RequestScoped;
043
044@RequestScoped
045public class GetCountyCommand
046        extends BaseSingleEntityCommand<GeoCode, GetCountyForm> {
047    
048    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
049    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
050    
051    static {
052        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList(
053                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
054                new PartyTypeDefinition(PartyTypes.CUSTOMER.name(), null),
055                new PartyTypeDefinition(PartyTypes.VENDOR.name(), null),
056                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList(
057                        new SecurityRoleDefinition(SecurityRoleGroups.County.name(), SecurityRoles.Review.name())
058                        )))
059                )));
060        
061        FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
062                new FieldDefinition("StateGeoCodeName", FieldType.ENTITY_NAME, true, null, null),
063                new FieldDefinition("CountyName", FieldType.ENTITY_NAME, false, null, null),
064                new FieldDefinition("CountyNumber", FieldType.NUMBER_3, false, null, null)
065                ));
066    }
067    
068    /** Creates a new instance of GetCountyCommand */
069    public GetCountyCommand() {
070        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, true);
071    }
072
073    @Override
074    protected GeoCode getEntity() {
075        GeoCode geoCode = null;
076        var countyName = form.getCountyName();
077        var countyNumber = form.getCountyNumber();
078        var parameterCount = (countyName == null ? 0 : 1) + (countyNumber == null ? 0 : 1);
079
080        if(parameterCount == 1) {
081            var geoControl = Session.getModelController(GeoControl.class);
082            var createdBy = getPartyPK();
083
084            var stateGeoCodeName = form.getStateGeoCodeName();
085            var stateGeoCode = geoControl.getGeoCodeByName(stateGeoCodeName);
086
087            var stateGeoCodeAliasType = geoControl.getGeoCodeAliasTypeByName(stateGeoCode.getLastDetail().getGeoCodeType(), GeoCodeAliasTypes.POSTAL_2_LETTER.name());
088            var stateGeoCodeAlias = geoControl.getGeoCodeAlias(stateGeoCode, stateGeoCodeAliasType);
089            var statePostal2Letter = stateGeoCodeAlias.getAlias();
090
091            var countryGeoCodeType = geoControl.getGeoCodeTypeByName(GeoCodeTypes.COUNTRY.name());
092            GeoCode countryGeoCode = null;
093            var stateRelationships = geoControl.getGeoCodeRelationshipsByFromGeoCode(stateGeoCode);
094            for(var geoCodeRelationship : stateRelationships) {
095                var toGeoCode = geoCodeRelationship.getToGeoCode();
096                if(toGeoCode.getLastDetail().getGeoCodeType().equals(countryGeoCodeType)) {
097                    countryGeoCode = toGeoCode;
098                    break;
099                }
100            }
101
102            var countryGeoCodeAliasType = geoControl.getGeoCodeAliasTypeByName(countryGeoCode.getLastDetail().getGeoCodeType(), GeoCodeAliasTypes.ISO_2_LETTER.name());
103            var countryGeoCodeAlias = geoControl.getGeoCodeAlias(countryGeoCode, countryGeoCodeAliasType);
104            var countryIso2Letter = countryGeoCodeAlias.getAlias();
105
106            var geoCodeScopeName = countryIso2Letter + "_" + statePostal2Letter + "_COUNTIES";
107            var geoCodeScope = geoControl.getGeoCodeScopeByName(geoCodeScopeName);
108            if(geoCodeScope == null) {
109                geoCodeScope = geoControl.createGeoCodeScope(geoCodeScopeName, false, 0, createdBy);
110            }
111
112            if(geoCodeScope != null) {
113                var geoCodeType = geoControl.getGeoCodeTypeByName(GeoCodeTypes.COUNTY.name());
114                GeoCodeAlias geoCodeAlias;
115
116                if(countyName != null) {
117                    var geoCodeAliasType = geoControl.getGeoCodeAliasTypeByName(geoCodeType, GeoCodeAliasTypes.COUNTY_NAME.name());
118                    geoCodeAlias = geoControl.getGeoCodeAliasByAliasWithinScope(geoCodeScope, geoCodeAliasType, countyName);
119
120                    if(geoCodeAlias == null) {
121                        addExecutionError(ExecutionErrors.UnknownCountyName.name(), countyName);
122                    }
123                } else {
124                    var geoCodeAliasType = geoControl.getGeoCodeAliasTypeByName(geoCodeType, GeoCodeAliasTypes.COUNTY_NUMBER.name());
125                    geoCodeAlias = geoControl.getGeoCodeAliasByAliasWithinScope(geoCodeScope, geoCodeAliasType, countyNumber);
126
127                    if(geoCodeAlias == null) {
128                        addExecutionError(ExecutionErrors.UnknownCountyNumber.name());
129                    }
130                }
131
132                if(geoCodeAlias != null) {
133                    geoCode = geoCodeAlias.getGeoCode();
134
135                    sendEvent(geoCode.getPrimaryKey(), EventTypes.READ, null, null, createdBy);
136                }
137            } else {
138                addExecutionError(ExecutionErrors.UnknownGeoCodeScopeName.name(), geoCodeScopeName);
139            }
140        } else {
141            addExecutionError(ExecutionErrors.InvalidParameterCount.name());
142        }
143
144        return geoCode;
145    }
146
147    @Override
148    protected BaseResult getResult(GeoCode entity) {
149        var result = GeoResultFactory.getGetCountyResult();
150
151        if(entity != null) {
152            var geoControl = Session.getModelController(GeoControl.class);
153
154            result.setCounty(geoControl.getCountyTransfer(getUserVisit(), entity));
155        }
156
157        return result;
158    }
159
160}