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.AddZipCodeToCityForm; 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 AddZipCodeToCityCommand 043 extends BaseSimpleCommand<AddZipCodeToCityForm> { 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.City.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("ZipCodeGeoCodeName", FieldType.ENTITY_NAME, false, null, null), 059 new FieldDefinition("ZipCodeName", FieldType.ENTITY_NAME, false, null, null) 060 ); 061 } 062 063 @Inject 064 GeoControl geoControl; 065 066 067 /** Creates a new instance of AddZipCodeToCityCommand */ 068 public AddZipCodeToCityCommand() { 069 super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false); 070 } 071 072 @Override 073 protected BaseResult execute() { 074 var zipCodeGeoCodeName = form.getZipCodeGeoCodeName(); 075 var zipCodeName = form.getZipCodeName(); 076 var parameterCount = (zipCodeGeoCodeName == null ? 0 : 1) + (zipCodeName == null ? 0 : 1); 077 078 if(parameterCount == 1) { 079 var geoCodeType = geoControl.getGeoCodeTypeByName(GeoCodeTypes.ZIP_CODE.name()); 080 var cityGeoCodeName = form.getCityGeoCodeName(); 081 var cityGeoCode = geoControl.getGeoCodeByName(cityGeoCodeName); 082 GeoCode zipCodeGeoCode = null; 083 084 if(zipCodeGeoCodeName != null) { 085 zipCodeGeoCode = geoControl.getGeoCodeByName(zipCodeGeoCodeName); 086 087 if(!zipCodeGeoCode.getLastDetail().getGeoCodeType().equals(geoCodeType)) { 088 zipCodeGeoCode = null; 089 } 090 091 if(zipCodeGeoCode == null) { 092 addExecutionError(ExecutionErrors.UnknownZipCodeGeoCodeName.name(), zipCodeGeoCodeName); 093 } 094 } else if(zipCodeName != 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 countryGeoCodeType = geoControl.getGeoCodeTypeByName(GeoCodeTypes.COUNTRY.name()); 108 GeoCode countryGeoCode = null; 109 Collection stateRelationships = geoControl.getGeoCodeRelationshipsByFromGeoCode(stateGeoCode); 110 for(var iter = stateRelationships.iterator(); iter.hasNext();) { 111 var geoCodeRelationship = (GeoCodeRelationship)iter.next(); 112 var toGeoCode = geoCodeRelationship.getToGeoCode(); 113 if(toGeoCode.getLastDetail().getGeoCodeType().equals(countryGeoCodeType)) { 114 countryGeoCode = toGeoCode; 115 break; 116 } 117 } 118 119 var countryGeoCodeAliasType = geoControl.getGeoCodeAliasTypeByName(countryGeoCode.getLastDetail().getGeoCodeType(), GeoCodeAliasTypes.ISO_2_LETTER.name()); 120 var countryGeoCodeAlias = geoControl.getGeoCodeAlias(countryGeoCode, countryGeoCodeAliasType); 121 var countryIso2Letter = countryGeoCodeAlias.getAlias(); 122 123 var geoCodeScopeName = countryIso2Letter + "_ZIP_CODES"; 124 var geoCodeScope = geoControl.getGeoCodeScopeByName(geoCodeScopeName); 125 126 var geoCodeAliasType = geoControl.getGeoCodeAliasTypeByName(geoCodeType, GeoCodeAliasTypes.ZIP_CODE.name()); 127 var geoCodeAlias = geoControl.getGeoCodeAliasByAliasWithinScope(geoCodeScope, geoCodeAliasType, zipCodeName); 128 zipCodeGeoCode = geoCodeAlias.getGeoCode(); 129 130 if(zipCodeGeoCode == null) { 131 addExecutionError(ExecutionErrors.UnknownZipCodeName.name(), zipCodeName); 132 } 133 } 134 135 if(zipCodeGeoCode != null) { 136 var geoCodeRelationship = geoControl.getGeoCodeRelationship(zipCodeGeoCode, cityGeoCode); 137 138 if(geoCodeRelationship == null) { 139 geoControl.createGeoCodeRelationship(zipCodeGeoCode, cityGeoCode, getPartyPK()); 140 } else { 141 addExecutionError(ExecutionErrors.DuplicateGeoCodeRelationship.name()); 142 } 143 } 144 } else { 145 addExecutionError(ExecutionErrors.InvalidParameterCount.name()); 146 } 147 148 return null; 149 } 150 151}