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