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.GetStateForm; 020import com.echothree.control.user.geo.common.result.GeoResultFactory; 021import com.echothree.control.user.geo.server.GeoDebugFlags; 022import com.echothree.model.control.core.common.EventTypes; 023import com.echothree.model.control.geo.common.GeoCodeAliasTypes; 024import com.echothree.model.control.geo.common.GeoCodeTypes; 025import com.echothree.model.control.geo.server.control.GeoControl; 026import com.echothree.model.control.party.common.PartyTypes; 027import com.echothree.model.control.security.common.SecurityRoleGroups; 028import com.echothree.model.control.security.common.SecurityRoles; 029import com.echothree.model.data.geo.server.entity.GeoCode; 030import com.echothree.model.data.geo.server.entity.GeoCodeAlias; 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 java.util.List; 040import javax.enterprise.context.Dependent; 041import javax.inject.Inject; 042 043@Dependent 044public class GetStateCommand 045 extends BaseSingleEntityCommand<GeoCode, GetStateForm> { 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(List.of( 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(), List.of( 056 new SecurityRoleDefinition(SecurityRoleGroups.State.name(), SecurityRoles.Review.name()) 057 )) 058 )); 059 060 FORM_FIELD_DEFINITIONS = List.of( 061 new FieldDefinition("CountryGeoCodeName", FieldType.ENTITY_NAME, true, null, null), 062 new FieldDefinition("StateName", FieldType.ENTITY_NAME, false, null, null), 063 new FieldDefinition("Postal2Letter", FieldType.UPPER_LETTER_2, false, null, null) 064 ); 065 } 066 067 @Inject 068 GeoControl geoControl; 069 070 071 /** Creates a new instance of GetStateCommand */ 072 public GetStateCommand() { 073 super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, true); 074 } 075 076 @Override 077 protected GeoCode getEntity() { 078 GeoCode geoCode = null; 079 var stateName = form.getStateName(); 080 var postal2Letter = form.getPostal2Letter(); 081 var parameterCount = (stateName == null ? 0 : 1) + (postal2Letter == null ? 0 : 1); 082 083 if(GeoDebugFlags.GetStateCommand) { 084 log.info("parameterCount = " + parameterCount); 085 } 086 087 if(parameterCount == 1) { 088 var countryGeoCodeName = form.getCountryGeoCodeName(); 089 var countryGeoCode = geoControl.getGeoCodeByName(countryGeoCodeName); 090 091 var countryGeoCodeAliasType = geoControl.getGeoCodeAliasTypeByName(countryGeoCode.getLastDetail().getGeoCodeType(), GeoCodeAliasTypes.ISO_2_LETTER.name()); 092 var countryGeoCodeAlias = geoControl.getGeoCodeAlias(countryGeoCode, countryGeoCodeAliasType); 093 var countryIso2Letter = countryGeoCodeAlias.getAlias(); 094 095 var geoCodeScopeName = countryIso2Letter + "_STATES"; 096 var geoCodeScope = geoControl.getGeoCodeScopeByName(geoCodeScopeName); 097 098 if(geoCodeScope != null) { 099 var geoCodeType = geoControl.getGeoCodeTypeByName(GeoCodeTypes.STATE.name()); 100 GeoCodeAlias geoCodeAlias; 101 102 if(stateName != null) { 103 if(GeoDebugFlags.GetStateCommand) { 104 log.info("lookup will be by stateName"); 105 } 106 107 var geoCodeAliasType = geoControl.getGeoCodeAliasTypeByName(geoCodeType, GeoCodeAliasTypes.STATE_NAME.name()); 108 geoCodeAlias = geoControl.getGeoCodeAliasByAliasWithinScope(geoCodeScope, geoCodeAliasType, stateName); 109 110 if(geoCodeAlias == null) { 111 addExecutionError(ExecutionErrors.UnknownStateName.name(), stateName); 112 } 113 } else { 114 if(GeoDebugFlags.GetStateCommand) { 115 log.info("lookup will be by postal2Letter"); 116 } 117 118 var geoCodeAliasType = geoControl.getGeoCodeAliasTypeByName(geoCodeType, GeoCodeAliasTypes.POSTAL_2_LETTER.name()); 119 geoCodeAlias = geoControl.getGeoCodeAliasByAliasWithinScope(geoCodeScope, geoCodeAliasType, postal2Letter); 120 121 if(geoCodeAlias == null) { 122 addExecutionError(ExecutionErrors.UnknownStatePostal2Letter.name()); 123 } 124 } 125 126 if(geoCodeAlias != null) { 127 geoCode = geoCodeAlias.getGeoCode(); 128 129 sendEvent(geoCode.getPrimaryKey(), EventTypes.READ, null, null, getPartyPK()); 130 } 131 } else { 132 addExecutionError(ExecutionErrors.UnknownGeoCodeScopeName.name(), geoCodeScopeName); 133 } 134 } else { 135 addExecutionError(ExecutionErrors.InvalidParameterCount.name()); 136 } 137 138 return geoCode; 139 } 140 141 @Override 142 protected BaseResult getResult(GeoCode entity) { 143 var result = GeoResultFactory.getGetStateResult(); 144 145 if(entity != null) { 146 result.setState(geoControl.getStateTransfer(getUserVisit(), entity)); 147 } 148 149 return result; 150 } 151 152}