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.AddCityToCountyForm;
020import com.echothree.model.control.geo.common.GeoCodeAliasTypes;
021import com.echothree.model.control.geo.common.GeoCodeTypes;
022import com.echothree.model.control.geo.server.control.GeoControl;
023import com.echothree.model.control.party.common.PartyTypes;
024import com.echothree.model.control.security.common.SecurityRoleGroups;
025import com.echothree.model.control.security.common.SecurityRoles;
026import com.echothree.model.data.geo.server.entity.GeoCode;
027import com.echothree.model.data.geo.server.entity.GeoCodeRelationship;
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.BaseSimpleCommand;
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 AddCityToCountyCommand
043        extends BaseSimpleCommand<AddCityToCountyForm> {
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.EMPLOYEE.name(), List.of(
052                        new SecurityRoleDefinition(SecurityRoleGroups.County.name(), SecurityRoles.Edit.name())
053                ))
054        ));
055
056        FORM_FIELD_DEFINITIONS = List.of(
057                new FieldDefinition("CityGeoCodeName", FieldType.ENTITY_NAME, true, null, null),
058                new FieldDefinition("CountyGeoCodeName", FieldType.ENTITY_NAME, false, null, null),
059                new FieldDefinition("CountyName", FieldType.ENTITY_NAME, false, null, null)
060        );
061    }
062
063    @Inject
064    GeoControl geoControl;
065
066    
067    /** Creates a new instance of AddCityToCountyCommand */
068    public AddCityToCountyCommand() {
069        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false);
070    }
071    
072    @Override
073    protected BaseResult execute() {
074        var countyGeoCodeName = form.getCountyGeoCodeName();
075        var countyName = form.getCountyName();
076        var parameterCount = (countyGeoCodeName == null ? 0 : 1) + (countyName == null ? 0 : 1);
077        
078        if(parameterCount == 1) {
079            var geoCodeType = geoControl.getGeoCodeTypeByName(GeoCodeTypes.COUNTY.name());
080            var cityGeoCodeName = form.getCityGeoCodeName();
081            var cityGeoCode = geoControl.getGeoCodeByName(cityGeoCodeName);
082            GeoCode countyGeoCode = null;
083            
084            if(countyGeoCodeName != null) {
085                countyGeoCode = geoControl.getGeoCodeByName(countyGeoCodeName);
086                
087                if(!countyGeoCode.getLastDetail().getGeoCodeType().equals(geoCodeType)) {
088                    countyGeoCode = null;
089                }
090                
091                if(countyGeoCode == null) {
092                    addExecutionError(ExecutionErrors.UnknownCountyGeoCodeName.name(), countyGeoCodeName);
093                }
094            } else if(countyName != null) {
095                var stateGeoCodeType = geoControl.getGeoCodeTypeByName(GeoCodeTypes.STATE.name());
096                GeoCode stateGeoCode = null;
097                Collection cityRelationships = geoControl.getGeoCodeRelationshipsByFromGeoCode(cityGeoCode);
098                for(var iter = cityRelationships.iterator(); iter.hasNext();) {
099                    var geoCodeRelationship = (GeoCodeRelationship)iter.next();
100                    var toGeoCode = geoCodeRelationship.getToGeoCode();
101                    if(toGeoCode.getLastDetail().getGeoCodeType().equals(stateGeoCodeType)) {
102                        stateGeoCode = toGeoCode;
103                        break;
104                    }
105                }
106
107                var stateGeoCodeAliasType = geoControl.getGeoCodeAliasTypeByName(stateGeoCode.getLastDetail().getGeoCodeType(), GeoCodeAliasTypes.POSTAL_2_LETTER.name());
108                var stateGeoCodeAlias = geoControl.getGeoCodeAlias(stateGeoCode, stateGeoCodeAliasType);
109                var statePostal2Letter = stateGeoCodeAlias.getAlias();
110
111                var countryGeoCodeType = geoControl.getGeoCodeTypeByName(GeoCodeTypes.COUNTRY.name());
112                GeoCode countryGeoCode = null;
113                Collection stateRelationships = geoControl.getGeoCodeRelationshipsByFromGeoCode(stateGeoCode);
114                for(var iter = stateRelationships.iterator(); iter.hasNext();) {
115                    var geoCodeRelationship = (GeoCodeRelationship)iter.next();
116                    var toGeoCode = geoCodeRelationship.getToGeoCode();
117                    if(toGeoCode.getLastDetail().getGeoCodeType().equals(countryGeoCodeType)) {
118                        countryGeoCode = toGeoCode;
119                        break;
120                    }
121                }
122
123                var countryGeoCodeAliasType = geoControl.getGeoCodeAliasTypeByName(countryGeoCode.getLastDetail().getGeoCodeType(), GeoCodeAliasTypes.ISO_2_LETTER.name());
124                var countryGeoCodeAlias = geoControl.getGeoCodeAlias(countryGeoCode, countryGeoCodeAliasType);
125                var countryIso2Letter = countryGeoCodeAlias.getAlias();
126
127                var geoCodeScopeName = countryIso2Letter + "_" + statePostal2Letter + "_COUNTIES";
128                var geoCodeScope = geoControl.getGeoCodeScopeByName(geoCodeScopeName);
129
130                var geoCodeAliasType = geoControl.getGeoCodeAliasTypeByName(geoCodeType, GeoCodeAliasTypes.COUNTY_NAME.name());
131                var geoCodeAlias = geoControl.getGeoCodeAliasByAliasWithinScope(geoCodeScope, geoCodeAliasType, countyName);
132                countyGeoCode = geoCodeAlias.getGeoCode();
133                
134                if(countyGeoCode == null) {
135                    addExecutionError(ExecutionErrors.UnknownCountyName.name(), countyName);
136                }
137            }
138            
139            if(countyGeoCode != null) {
140                var geoCodeRelationship = geoControl.getGeoCodeRelationship(cityGeoCode, countyGeoCode);
141                
142                if(geoCodeRelationship == null) {
143                    geoControl.createGeoCodeRelationship(cityGeoCode, countyGeoCode, getPartyPK());
144                } else {
145                    addExecutionError(ExecutionErrors.DuplicateGeoCodeRelationship.name());
146                }
147            }
148        } else {
149            addExecutionError(ExecutionErrors.InvalidParameterCount.name());
150        }
151        
152        return null;
153    }
154    
155}