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.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.GeoConstants; 024import com.echothree.model.control.geo.server.control.GeoControl; 025import com.echothree.model.control.party.common.PartyTypes; 026import com.echothree.model.control.security.common.SecurityRoleGroups; 027import com.echothree.model.control.security.common.SecurityRoles; 028import com.echothree.model.data.geo.server.entity.GeoCode; 029import com.echothree.model.data.geo.server.entity.GeoCodeAlias; 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; 043import org.apache.commons.logging.Log; 044 045public class GetStateCommand 046 extends BaseSingleEntityCommand<GeoCode, GetStateForm> { 047 048 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 049 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 050 051 static { 052 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList( 053 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 054 new PartyTypeDefinition(PartyTypes.CUSTOMER.name(), null), 055 new PartyTypeDefinition(PartyTypes.VENDOR.name(), null), 056 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList( 057 new SecurityRoleDefinition(SecurityRoleGroups.State.name(), SecurityRoles.Review.name()) 058 ))) 059 ))); 060 061 FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 062 new FieldDefinition("CountryGeoCodeName", FieldType.ENTITY_NAME, true, null, null), 063 new FieldDefinition("StateName", FieldType.ENTITY_NAME, false, null, null), 064 new FieldDefinition("Postal2Letter", FieldType.UPPER_LETTER_2, false, null, null) 065 )); 066 } 067 068 Log log = null; 069 070 /** Creates a new instance of GetStateCommand */ 071 public GetStateCommand(UserVisitPK userVisitPK, GetStateForm form) { 072 super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, true); 073 074 if(GeoDebugFlags.GetStateCommand) { 075 log = getLog(); 076 } 077 } 078 079 @Override 080 protected GeoCode getEntity() { 081 GeoCode geoCode = null; 082 var stateName = form.getStateName(); 083 var postal2Letter = form.getPostal2Letter(); 084 var parameterCount = (stateName == null ? 0 : 1) + (postal2Letter == null ? 0 : 1); 085 086 if(GeoDebugFlags.GetStateCommand) { 087 log.info("parameterCount = " + parameterCount); 088 } 089 090 if(parameterCount == 1) { 091 var geoControl = Session.getModelController(GeoControl.class); 092 093 var countryGeoCodeName = form.getCountryGeoCodeName(); 094 var countryGeoCode = geoControl.getGeoCodeByName(countryGeoCodeName); 095 096 var countryGeoCodeAliasType = geoControl.getGeoCodeAliasTypeByName(countryGeoCode.getLastDetail().getGeoCodeType(), GeoConstants.GeoCodeAliasType_ISO_2_LETTER); 097 var countryGeoCodeAlias = geoControl.getGeoCodeAlias(countryGeoCode, countryGeoCodeAliasType); 098 var countryIso2Letter = countryGeoCodeAlias.getAlias(); 099 100 var geoCodeScopeName = countryIso2Letter + "_STATES"; 101 var geoCodeScope = geoControl.getGeoCodeScopeByName(geoCodeScopeName); 102 103 if(geoCodeScope != null) { 104 var geoCodeType = geoControl.getGeoCodeTypeByName(GeoConstants.GeoCodeType_STATE); 105 GeoCodeAlias geoCodeAlias; 106 107 if(stateName != null) { 108 if(GeoDebugFlags.GetStateCommand) { 109 log.info("lookup will be by stateName"); 110 } 111 112 var geoCodeAliasType = geoControl.getGeoCodeAliasTypeByName(geoCodeType, GeoConstants.GeoCodeAliasType_STATE_NAME); 113 geoCodeAlias = geoControl.getGeoCodeAliasByAliasWithinScope(geoCodeScope, geoCodeAliasType, stateName); 114 115 if(geoCodeAlias == null) { 116 addExecutionError(ExecutionErrors.UnknownStateName.name(), stateName); 117 } 118 } else { 119 if(GeoDebugFlags.GetStateCommand) { 120 log.info("lookup will be by postal2Letter"); 121 } 122 123 var geoCodeAliasType = geoControl.getGeoCodeAliasTypeByName(geoCodeType, GeoConstants.GeoCodeAliasType_POSTAL_2_LETTER); 124 geoCodeAlias = geoControl.getGeoCodeAliasByAliasWithinScope(geoCodeScope, geoCodeAliasType, postal2Letter); 125 126 if(geoCodeAlias == null) { 127 addExecutionError(ExecutionErrors.UnknownStatePostal2Letter.name()); 128 } 129 } 130 131 if(geoCodeAlias != null) { 132 geoCode = geoCodeAlias.getGeoCode(); 133 134 sendEvent(geoCode.getPrimaryKey(), EventTypes.READ, null, null, getPartyPK()); 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.getGetStateResult(); 149 150 if(entity != null) { 151 var geoControl = Session.getModelController(GeoControl.class); 152 153 result.setState(geoControl.getStateTransfer(getUserVisit(), entity)); 154 } 155 156 return result; 157 } 158 159}