001// --------------------------------------------------------------------------------
002// Copyright 2002-2024 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.GeoConstants;
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.GeoCodeAlias;
027import com.echothree.model.data.geo.server.entity.GeoCodeAliasType;
028import com.echothree.model.data.geo.server.entity.GeoCodeRelationship;
029import com.echothree.model.data.geo.server.entity.GeoCodeScope;
030import com.echothree.model.data.geo.server.entity.GeoCodeType;
031import com.echothree.model.data.user.common.pk.UserVisitPK;
032import com.echothree.util.common.message.ExecutionErrors;
033import com.echothree.util.common.validation.FieldDefinition;
034import com.echothree.util.common.validation.FieldType;
035import com.echothree.util.common.command.BaseResult;
036import com.echothree.util.server.control.BaseSimpleCommand;
037import com.echothree.util.server.control.CommandSecurityDefinition;
038import com.echothree.util.server.control.PartyTypeDefinition;
039import com.echothree.util.server.control.SecurityRoleDefinition;
040import com.echothree.util.server.persistence.Session;
041import java.util.Arrays;
042import java.util.Collection;
043import java.util.Collections;
044import java.util.Iterator;
045import java.util.List;
046
047public class AddCityToCountyCommand
048        extends BaseSimpleCommand<AddCityToCountyForm> {
049    
050    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
051    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
052    
053    static {
054        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList(
055                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
056                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList(
057                    new SecurityRoleDefinition(SecurityRoleGroups.County.name(), SecurityRoles.Edit.name())
058                    )))
059                )));
060
061        FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
062                new FieldDefinition("CityGeoCodeName", FieldType.ENTITY_NAME, true, null, null),
063                new FieldDefinition("CountyGeoCodeName", FieldType.ENTITY_NAME, false, null, null),
064                new FieldDefinition("CountyName", FieldType.ENTITY_NAME, false, null, null)
065                ));
066    }
067    
068    /** Creates a new instance of AddCityToCountyCommand */
069    public AddCityToCountyCommand(UserVisitPK userVisitPK, AddCityToCountyForm form) {
070        super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false);
071    }
072    
073    @Override
074    protected BaseResult execute() {
075        String countyGeoCodeName = form.getCountyGeoCodeName();
076        String countyName = form.getCountyName();
077        var parameterCount = (countyGeoCodeName == null ? 0 : 1) + (countyName == null ? 0 : 1);
078        
079        if(parameterCount == 1) {
080            var geoControl = Session.getModelController(GeoControl.class);
081            GeoCodeType geoCodeType = geoControl.getGeoCodeTypeByName(GeoConstants.GeoCodeType_COUNTY);
082            String cityGeoCodeName = form.getCityGeoCodeName();
083            GeoCode cityGeoCode = geoControl.getGeoCodeByName(cityGeoCodeName);
084            GeoCode countyGeoCode = null;
085            
086            if(countyGeoCodeName != null) {
087                countyGeoCode = geoControl.getGeoCodeByName(countyGeoCodeName);
088                
089                if(!countyGeoCode.getLastDetail().getGeoCodeType().equals(geoCodeType)) {
090                    countyGeoCode = null;
091                }
092                
093                if(countyGeoCode == null) {
094                    addExecutionError(ExecutionErrors.UnknownCountyGeoCodeName.name(), countyGeoCodeName);
095                }
096            } else if(countyName != null) {
097                GeoCodeType stateGeoCodeType = geoControl.getGeoCodeTypeByName(GeoConstants.GeoCodeType_STATE);
098                GeoCode stateGeoCode = null;
099                Collection cityRelationships = geoControl.getGeoCodeRelationshipsByFromGeoCode(cityGeoCode);
100                for(Iterator iter = cityRelationships.iterator(); iter.hasNext();) {
101                    GeoCodeRelationship geoCodeRelationship = (GeoCodeRelationship)iter.next();
102                    GeoCode toGeoCode = geoCodeRelationship.getToGeoCode();
103                    if(toGeoCode.getLastDetail().getGeoCodeType().equals(stateGeoCodeType)) {
104                        stateGeoCode = toGeoCode;
105                        break;
106                    }
107                }
108                
109                GeoCodeAliasType stateGeoCodeAliasType = geoControl.getGeoCodeAliasTypeByName(stateGeoCode.getLastDetail().getGeoCodeType(), GeoConstants.GeoCodeAliasType_POSTAL_2_LETTER);
110                GeoCodeAlias stateGeoCodeAlias = geoControl.getGeoCodeAlias(stateGeoCode, stateGeoCodeAliasType);
111                String statePostal2Letter = stateGeoCodeAlias.getAlias();
112                
113                GeoCodeType countryGeoCodeType = geoControl.getGeoCodeTypeByName(GeoConstants.GeoCodeType_COUNTRY);
114                GeoCode countryGeoCode = null;
115                Collection stateRelationships = geoControl.getGeoCodeRelationshipsByFromGeoCode(stateGeoCode);
116                for(Iterator iter = stateRelationships.iterator(); iter.hasNext();) {
117                    GeoCodeRelationship geoCodeRelationship = (GeoCodeRelationship)iter.next();
118                    GeoCode toGeoCode = geoCodeRelationship.getToGeoCode();
119                    if(toGeoCode.getLastDetail().getGeoCodeType().equals(countryGeoCodeType)) {
120                        countryGeoCode = toGeoCode;
121                        break;
122                    }
123                }
124                
125                GeoCodeAliasType countryGeoCodeAliasType = geoControl.getGeoCodeAliasTypeByName(countryGeoCode.getLastDetail().getGeoCodeType(), GeoConstants.GeoCodeAliasType_ISO_2_LETTER);
126                GeoCodeAlias countryGeoCodeAlias = geoControl.getGeoCodeAlias(countryGeoCode, countryGeoCodeAliasType);
127                String countryIso2Letter = countryGeoCodeAlias.getAlias();
128                
129                String geoCodeScopeName = new StringBuilder().append(countryIso2Letter).append("_").append(statePostal2Letter).append("_COUNTIES").toString();
130                GeoCodeScope geoCodeScope = geoControl.getGeoCodeScopeByName(geoCodeScopeName);
131                
132                GeoCodeAliasType geoCodeAliasType = geoControl.getGeoCodeAliasTypeByName(geoCodeType, GeoConstants.GeoCodeAliasType_COUNTY_NAME);
133                GeoCodeAlias geoCodeAlias = geoControl.getGeoCodeAliasByAliasWithinScope(geoCodeScope, geoCodeAliasType, countyName);
134                countyGeoCode = geoCodeAlias.getGeoCode();
135                
136                if(countyGeoCode == null) {
137                    addExecutionError(ExecutionErrors.UnknownCountyName.name(), countyName);
138                }
139            }
140            
141            if(countyGeoCode != null) {
142                GeoCodeRelationship geoCodeRelationship = geoControl.getGeoCodeRelationship(cityGeoCode, countyGeoCode);
143                
144                if(geoCodeRelationship == null) {
145                    geoControl.createGeoCodeRelationship(cityGeoCode, countyGeoCode, getPartyPK());
146                } else {
147                    addExecutionError(ExecutionErrors.DuplicateGeoCodeRelationship.name());
148                }
149            }
150        } else {
151            addExecutionError(ExecutionErrors.InvalidParameterCount.name());
152        }
153        
154        return null;
155    }
156    
157}