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