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.GetCountryForm; 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.GeoCodeScopes; 025import com.echothree.model.control.geo.common.GeoCodeTypes; 026import com.echothree.model.control.geo.server.control.GeoControl; 027import com.echothree.model.control.party.common.PartyTypes; 028import com.echothree.model.control.security.common.SecurityRoleGroups; 029import com.echothree.model.control.security.common.SecurityRoles; 030import com.echothree.model.data.geo.server.entity.GeoCode; 031import com.echothree.model.data.geo.server.entity.GeoCodeAlias; 032import com.echothree.util.common.command.BaseResult; 033import com.echothree.util.common.message.ExecutionErrors; 034import com.echothree.util.common.validation.FieldDefinition; 035import com.echothree.util.common.validation.FieldType; 036import com.echothree.util.server.control.BaseSingleEntityCommand; 037import com.echothree.util.server.control.CommandSecurityDefinition; 038import com.echothree.util.server.control.PartyTypeDefinition; 039import com.echothree.util.server.control.SecurityRoleDefinition; 040import com.echothree.util.server.persistence.Session; 041import java.util.Arrays; 042import java.util.Collections; 043import java.util.List; 044import org.apache.commons.logging.Log; 045import javax.enterprise.context.RequestScoped; 046 047@RequestScoped 048public class GetCountryCommand 049 extends BaseSingleEntityCommand<GeoCode, GetCountryForm> { 050 051 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 052 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 053 054 static { 055 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList( 056 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 057 new PartyTypeDefinition(PartyTypes.CUSTOMER.name(), null), 058 new PartyTypeDefinition(PartyTypes.VENDOR.name(), null), 059 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList( 060 new SecurityRoleDefinition(SecurityRoleGroups.Country.name(), SecurityRoles.Review.name()) 061 ))) 062 ))); 063 064 FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 065 new FieldDefinition("GeoCodeName", FieldType.ENTITY_NAME, false, null, null), 066 new FieldDefinition("CountryName", FieldType.ENTITY_NAME, false, null, null), 067 new FieldDefinition("Iso3Number", FieldType.NUMBER_3, false, null, null), 068 new FieldDefinition("Iso3Letter", FieldType.UPPER_LETTER_3, false, null, null), 069 new FieldDefinition("Iso2Letter", FieldType.UPPER_LETTER_2, false, null, null), 070 new FieldDefinition("Alias", FieldType.ENTITY_NAME, false, null, null) 071 )); 072 } 073 074 Log log = null; 075 076 /** Creates a new instance of GetCountryCommand */ 077 public GetCountryCommand() { 078 super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, true); 079 080 if(GeoDebugFlags.GetCountryCommand) { 081 log = getLog(); 082 } 083 } 084 085 @Override 086 protected GeoCode getEntity() { 087 GeoCode geoCode = null; 088 var geoCodeName = form.getGeoCodeName(); 089 var countryName = form.getCountryName(); 090 var iso3Number = form.getIso3Number(); 091 var iso3Letter = form.getIso3Letter(); 092 var iso2Letter = form.getIso2Letter(); 093 var alias = form.getAlias(); 094 var parameterCount = (geoCodeName == null ? 0 : 1) + (countryName == null ? 0 : 1) + (iso3Number == null ? 0 : 1) + (iso3Letter == null ? 0 : 1) 095 + (iso2Letter == null ? 0 : 1) + (alias == null ? 0 : 1); 096 097 if(GeoDebugFlags.GetCountryCommand) { 098 log.info("parameterCount = " + parameterCount); 099 } 100 101 if(parameterCount < 2) { 102 var geoControl = Session.getModelController(GeoControl.class); 103 var geoCodeScope = geoControl.getGeoCodeScopeByName(GeoCodeScopes.COUNTRIES.name()); 104 105 if(parameterCount == 0) { 106 geoCode = geoControl.getDefaultGeoCode(geoCodeScope); 107 } else { 108 var geoCodeType = geoControl.getGeoCodeTypeByName(GeoCodeTypes.COUNTRY.name()); 109 110 if(geoCodeName != null) { 111 if(GeoDebugFlags.GetCountryCommand) { 112 log.info("lookup will be by geoCodeName"); 113 } 114 115 geoCode = geoControl.getGeoCodeByName(geoCodeName); 116 117 if(geoCode != null) { 118 var geoCodeDetail = geoCode.getLastDetail(); 119 120 if(!geoCodeDetail.getGeoCodeType().equals(geoCodeType)) { 121 addExecutionError(ExecutionErrors.InvalidGeoCodeType.name(), geoCodeDetail.getGeoCodeType().getLastDetail().getGeoCodeTypeName()); 122 } else if(!geoCodeDetail.getGeoCodeScope().equals(geoCodeScope)) { 123 addExecutionError(ExecutionErrors.InvalidGeoCodeScope.name(), geoCodeDetail.getGeoCodeScope().getLastDetail().getGeoCodeScopeName()); 124 } 125 126 if(hasExecutionErrors()) { 127 geoCode = null; 128 } 129 } else { 130 addExecutionError(ExecutionErrors.UnknownGeoCodeName.name(), geoCodeName); 131 } 132 } else if(alias != null) { 133 if(GeoDebugFlags.GetCountryCommand) { 134 log.info("lookup will be by alias"); 135 } 136 137 geoCode = geoControl.getCountryByAlias(alias); 138 139 if(geoCode == null) { 140 addExecutionError(ExecutionErrors.UnknownGeoCodeAlias.name(), alias); 141 } 142 } else { 143 GeoCodeAlias geoCodeAlias = null; 144 145 if(countryName != null) { 146 if(GeoDebugFlags.GetCountryCommand) { 147 log.info("lookup will be by countryName"); 148 } 149 150 var geoCodeAliasType = geoControl.getGeoCodeAliasTypeByName(geoCodeType, GeoCodeAliasTypes.COUNTRY_NAME.name()); 151 geoCodeAlias = geoControl.getGeoCodeAliasByAliasWithinScope(geoCodeScope, geoCodeAliasType, countryName); 152 153 if(geoCodeAlias == null) { 154 addExecutionError(ExecutionErrors.UnknownCountryName.name(), countryName); 155 } 156 } else if(iso3Number != null) { 157 if(GeoDebugFlags.GetCountryCommand) { 158 log.info("lookup will be by iso3Number"); 159 } 160 161 var geoCodeAliasType = geoControl.getGeoCodeAliasTypeByName(geoCodeType, GeoCodeAliasTypes.ISO_3_NUMBER.name()); 162 geoCodeAlias = geoControl.getGeoCodeAliasByAliasWithinScope(geoCodeScope, geoCodeAliasType, iso3Number); 163 164 if(geoCodeAlias == null) { 165 addExecutionError(ExecutionErrors.UnknownCountryIso3Number.name(), iso3Number); 166 } 167 } else if(iso3Letter != null) { 168 if(GeoDebugFlags.GetCountryCommand) { 169 log.info("lookup will be by iso3Letter"); 170 } 171 172 var geoCodeAliasType = geoControl.getGeoCodeAliasTypeByName(geoCodeType, GeoCodeAliasTypes.ISO_3_LETTER.name()); 173 geoCodeAlias = geoControl.getGeoCodeAliasByAliasWithinScope(geoCodeScope, geoCodeAliasType, iso3Letter); 174 175 if(geoCodeAlias == null) { 176 addExecutionError(ExecutionErrors.UnknownCountryIso3Letter.name(), iso3Letter); 177 } 178 } else if(iso2Letter != null) { 179 if(GeoDebugFlags.GetCountryCommand) { 180 log.info("lookup will be by iso2Letter"); 181 } 182 183 var geoCodeAliasType = geoControl.getGeoCodeAliasTypeByName(geoCodeType, GeoCodeAliasTypes.ISO_2_LETTER.name()); 184 geoCodeAlias = geoControl.getGeoCodeAliasByAliasWithinScope(geoCodeScope, geoCodeAliasType, iso2Letter); 185 186 if(geoCodeAlias == null) { 187 addExecutionError(ExecutionErrors.UnknownCountryIso2Letter.name(), iso2Letter); 188 } 189 } 190 191 if(geoCodeAlias != null) { 192 geoCode = geoCodeAlias.getGeoCode(); 193 } 194 } 195 } 196 197 if(geoCode != null) { 198 sendEvent(geoCode.getPrimaryKey(), EventTypes.READ, null, null, getPartyPK()); 199 } 200 } else { 201 addExecutionError(ExecutionErrors.InvalidParameterCount.name()); 202 } 203 204 return geoCode; 205 } 206 207 @Override 208 protected BaseResult getResult(GeoCode entity) { 209 var result = GeoResultFactory.getGetCountryResult(); 210 211 if(entity != null) { 212 var geoControl = Session.getModelController(GeoControl.class); 213 214 result.setCountry(geoControl.getCountryTransfer(getUserVisit(), entity)); 215 } 216 217 return result; 218 } 219 220}