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