From 10b7484533785ab7273da6ca561f75c1a3a5ed5e Mon Sep 17 00:00:00 2001 From: yamelsenih Date: Thu, 28 Sep 2023 11:02:16 -0400 Subject: [PATCH] refactory --- .../AuthorizationServerInterceptor.java | 13 +- .../org/spin/authentication/Constants.java | 26 -- .../spin/authentication/ContextManager.java | 237 ------------------ .../spin/authentication/SessionManager.java | 73 +++++- .../{BearerToken.java => TokenManager.java} | 18 +- .../spin/grpc/client/MiddlewareClient.java | 4 +- .../org/spin/grpc/server/ContextManager.java | 191 -------------- 7 files changed, 82 insertions(+), 480 deletions(-) delete mode 100644 src/main/java/org/spin/authentication/Constants.java delete mode 100644 src/main/java/org/spin/authentication/ContextManager.java rename src/main/java/org/spin/authentication/{BearerToken.java => TokenManager.java} (76%) delete mode 100644 src/main/java/org/spin/grpc/server/ContextManager.java diff --git a/src/main/java/org/spin/authentication/AuthorizationServerInterceptor.java b/src/main/java/org/spin/authentication/AuthorizationServerInterceptor.java index 4f6a3a9..c17e9ca 100644 --- a/src/main/java/org/spin/authentication/AuthorizationServerInterceptor.java +++ b/src/main/java/org/spin/authentication/AuthorizationServerInterceptor.java @@ -34,11 +34,7 @@ public class AuthorizationServerInterceptor implements ServerInterceptor { private static List ALLOW_REQUESTS_WITHOUT_TOKEN = Arrays.asList( "" ); - - /** Revoke session */ -// private static List REVOKE_TOKEN_SERVICES = Arrays.asList( -// "" -// ); + @Override public ServerCall.Listener interceptCall(ServerCall serverCall, Metadata metadata, ServerCallHandler serverCallHandler) { String callingMethod = serverCall.getMethodDescriptor().getFullMethodName(); @@ -48,17 +44,14 @@ public ServerCall.Listener interceptCall(ServerCall. * - ************************************************************************************/ -package org.spin.authentication; -import io.grpc.Context; -import io.grpc.Metadata; - -import static io.grpc.Metadata.ASCII_STRING_MARSHALLER; - -public class Constants { - public static final String BEARER_TYPE = "Bearer"; - - public static final Metadata.Key AUTHORIZATION_METADATA_KEY = Metadata.Key.of("Authorization", ASCII_STRING_MARSHALLER); - public static final Context.Key CLIENT_ID_CONTEXT_KEY = Context.key("clientId"); -} diff --git a/src/main/java/org/spin/authentication/ContextManager.java b/src/main/java/org/spin/authentication/ContextManager.java deleted file mode 100644 index 6480bd7..0000000 --- a/src/main/java/org/spin/authentication/ContextManager.java +++ /dev/null @@ -1,237 +0,0 @@ -/************************************************************************************* - * Product: Adempiere ERP & CRM Smart Business Solution * - * This program is free software; you can redistribute it and/or modify it * - * under the terms version 2 or later of the GNU General Public License as published * - * by the Free Software Foundation. This program is distributed in the hope * - * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied * - * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * - * See the GNU General Public License for more details. * - * You should have received a copy of the GNU General Public License along * - * with this program; if not, write to the Free Software Foundation, Inc., * - * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. * - * For the text or an alternative of this public license, you may reach us * - * Copyright (C) 2012-2018 E.R.P. Consultores y Asociados, S.A. All Rights Reserved. * - * Contributor(s): Yamel Senih www.erpya.com * - *************************************************************************************/ -package org.spin.authentication; - -import java.math.BigDecimal; -import java.sql.Timestamp; -import java.util.Map; -import java.util.Properties; - -import org.adempiere.exceptions.AdempiereException; -import org.adempiere.core.domains.models.I_AD_Org; -import org.adempiere.core.domains.models.I_AD_Session; -import org.adempiere.core.domains.models.I_M_Warehouse; -import org.compiere.model.MClient; -import org.compiere.model.MCountry; -import org.compiere.model.MLanguage; -import org.compiere.model.MOrg; -import org.compiere.model.MSession; -import org.compiere.model.MWarehouse; -import org.compiere.model.Query; -import org.compiere.util.CCache; -import org.compiere.util.DB; -import org.compiere.util.Env; -import org.compiere.util.Language; -import org.compiere.util.Util; - -/** - * Class for handle Context - * @author Yamel Senih, ysenih@erpya.com , http://www.erpya.com - */ -public class ContextManager { - - /** Session Context */ - private static CCache sessionsContext = new CCache("Session-gRPC-Service", 30, 0); // no time-out - /** Language */ - private static CCache languageCache = new CCache("Language-gRPC-Service", 30, 0); // no time-out - /** Organization Cache */ - private static CCache organizationCache = new CCache(I_AD_Org.Table_Name + "-gRPC-Service", 30, 0); // no time-out - /** Warehouse Cache */ - private static CCache warehouseCache = new CCache(I_M_Warehouse.Table_Name + "-gRPC-Service", 30, 0); // no time-out - - /** - * Get Context without organization and warehouse - * @param sessionUuid - * @param language - * @return - */ - public static Properties getContext(String sessionUuid, String language) { - return getContext(sessionUuid, language, null, null); - } - - /** - * Get context from session - * @param sessionUuid - * @param language - * @param organizationUuid - * @param warehouseUuid - * @return - */ - public static Properties getContext(String sessionUuid, String language, String organizationUuid, String warehouseUuid) { - Properties context = sessionsContext.get(sessionUuid); - if(context != null - && context.size() > 0) { - Env.setContext(context, Env.LANGUAGE, getDefaultLanguage(language)); - setDefault(context, Env.getAD_Org_ID(context), organizationUuid, warehouseUuid); - Env.setCtx((Properties) context.clone()); - return context; - } - context = (Properties) Env.getCtx().clone(); - DB.validateSupportedUUIDFromDB(); - - final String whereClause = I_AD_Session.COLUMNNAME_UUID + " = ? AND " - + I_AD_Session.COLUMNNAME_Processed + " = ? "; - MSession session = new Query(context, I_AD_Session.Table_Name, whereClause, null) - .setParameters(sessionUuid, false) - .first(); - if(session == null - || session.getAD_Session_ID() <= 0) { - throw new AdempiereException("@AD_Session_ID@ @NotFound@"); - } - Env.setContext (context, "#AD_Session_ID", session.getAD_Session_ID()); - Env.setContext(context, "#AD_User_ID", session.getCreatedBy()); - Env.setContext(context, "#AD_Role_ID", session.getAD_Role_ID()); - Env.setContext(context, "#AD_Client_ID", session.getAD_Client_ID()); - setDefault(context, session.getAD_Org_ID(), organizationUuid, warehouseUuid); - Env.setContext(context, "#Date", new Timestamp(System.currentTimeMillis())); - Env.setContext(context, Env.LANGUAGE, getDefaultLanguage(language)); - // Save to Cache - sessionsContext.put(sessionUuid, context); - Env.setCtx((Properties) context.clone()); - return context; - } - - public static void removeSession(String sessionUuid) { - sessionsContext.remove(sessionUuid); - } - - /** - * Set context with attributes - * @param windowNo - * @param context - * @param attributes - * @return {Properties} context with new values - */ - public static Properties setContextWithAttributes(int windowNo, Properties context, Map attributes) { - Env.clearWinContext(windowNo); - // Fill context - attributes.entrySet().forEach(attribute -> { - if(attribute.getValue() instanceof Integer) { - Env.setContext(context, windowNo, attribute.getKey(), (Integer) attribute.getValue()); - } else if(attribute.getValue() instanceof BigDecimal) { - String value = null; - if (attribute.getValue() != null) { - value = attribute.getValue().toString(); - } - Env.setContext(context, windowNo, attribute.getKey(), value); - } else if(attribute.getValue() instanceof Timestamp) { - Env.setContext(context, windowNo, attribute.getKey(), (Timestamp) attribute.getValue()); - } else if(attribute.getValue() instanceof Boolean) { - Env.setContext(context, windowNo, attribute.getKey(), (Boolean) attribute.getValue()); - } else if(attribute.getValue() instanceof String) { - Env.setContext(context, windowNo, attribute.getKey(), (String) attribute.getValue()); - } - }); - - return context; - } - -// public static Properties setContextWithAttributes(int windowNo, Properties context, java.util.List values) { -// Map attributes = ValueUtil.convertValuesToObjects(values); -// return setContextWithAttributes(windowNo, context, attributes); -// } - - /** - * Set Default warehouse and organization - * @param context - * @param defaultOrganizationId - * @param organizationUuid - * @param warehouseUuid - */ - private static void setDefault(Properties context, int defaultOrganizationId, String organizationUuid, String warehouseUuid) { - int organizationId = defaultOrganizationId; - if(!Util.isEmpty(organizationUuid)) { - MOrg organization = organizationCache.get(organizationUuid); - if(organization == null) { - organization = new Query(context, I_AD_Org.Table_Name, I_AD_Org.COLUMNNAME_UUID + " = ?", null) - .setParameters(organizationUuid) - .first(); - } - // - if(organization != null) { - organizationId = organization.getAD_Org_ID(); - organizationCache.put(organizationUuid, organization); - } - } - if(!Util.isEmpty(warehouseUuid)) { - MWarehouse warehouse = warehouseCache.get(warehouseUuid); - if(warehouse == null) { - warehouse = new Query(context, I_M_Warehouse.Table_Name, I_M_Warehouse.COLUMNNAME_UUID + " = ?", null) - .setParameters(warehouseUuid) - .first(); - } - // - if(warehouse != null) { - warehouseCache.put(warehouseUuid, warehouse); - Env.setContext(context, "#M_Warehouse_ID", organizationId); - } - } - Env.setContext(context, "#AD_Org_ID", organizationId); - } - - /** - * Get Default Country - * @return - */ - public static MCountry getDefaultCountry() { - MClient client = MClient.get (Env.getCtx()); - MLanguage language = MLanguage.get(Env.getCtx(), client.getAD_Language()); - MCountry country = MCountry.get(Env.getCtx(), language.getCountryCode()); - // Verify - if(country != null) { - return country; - } - // Default - return MCountry.getDefault(Env.getCtx()); - } - - /** - * Get Default from language - * @param language - * @return - */ - public static String getDefaultLanguage(String language) { - MClient client = MClient.get(Env.getCtx()); - String clientLanguage = client.getAD_Language(); - if(!Util.isEmpty(clientLanguage) - && Util.isEmpty(language)) { - return clientLanguage; - } - String defaultLanguage = language; - if(Util.isEmpty(language)) { - language = Language.AD_Language_en_US; - } - // Using es / en instead es_VE / en_US - // get default - if(language.length() == 2) { - defaultLanguage = languageCache.get(language); - if(!Util.isEmpty(defaultLanguage)) { - return defaultLanguage; - } - defaultLanguage = DB.getSQLValueString(null, "SELECT AD_Language " - + "FROM AD_Language " - + "WHERE LanguageISO = ? " - + "AND (IsSystemLanguage = 'Y' OR IsBaseLanguage = 'Y')", language); - // Set language - languageCache.put(language, defaultLanguage); - } - if(Util.isEmpty(defaultLanguage)) { - defaultLanguage = Language.AD_Language_en_US; - } - // Default return - return defaultLanguage; - } -} diff --git a/src/main/java/org/spin/authentication/SessionManager.java b/src/main/java/org/spin/authentication/SessionManager.java index afbe99e..56236d9 100644 --- a/src/main/java/org/spin/authentication/SessionManager.java +++ b/src/main/java/org/spin/authentication/SessionManager.java @@ -25,11 +25,13 @@ import java.util.Properties; import java.util.logging.Level; +import org.adempiere.core.domains.models.I_AD_Language; import org.adempiere.exceptions.AdempiereException; import org.compiere.model.MAcctSchema; import org.compiere.model.MClient; import org.compiere.model.MClientInfo; import org.compiere.model.MCountry; +import org.compiere.model.MLanguage; import org.compiere.model.MOrg; import org.compiere.model.MRole; import org.compiere.model.MSession; @@ -37,10 +39,12 @@ import org.compiere.model.MUser; import org.compiere.model.MWarehouse; import org.compiere.model.ModelValidationEngine; +import org.compiere.util.CCache; import org.compiere.util.CLogger; import org.compiere.util.DB; import org.compiere.util.Env; import org.compiere.util.Ini; +import org.compiere.util.Language; import org.compiere.util.TimeUtil; import org.compiere.util.Util; import org.spin.eca52.util.JWTUtil; @@ -64,6 +68,61 @@ public class SessionManager { /** Session Context */ /** Logger */ private static CLogger log = CLogger.getCLogger(SessionManager.class); + /** Language */ + private static CCache languageCache = new CCache(I_AD_Language.Table_Name, 30, 0); // no time-out + + /** + * Get Default Country + * @return + */ + public static MCountry getDefaultCountry() { + MClient client = MClient.get (Env.getCtx()); + MLanguage language = MLanguage.get(Env.getCtx(), client.getAD_Language()); + MCountry country = MCountry.get(Env.getCtx(), language.getCountryCode()); + // Verify + if(country != null) { + return country; + } + // Default + return MCountry.getDefault(Env.getCtx()); + } + + /** + * Get Default from language + * @param language + * @return + */ + public static String getDefaultLanguage(String language) { + MClient client = MClient.get(Env.getCtx()); + String clientLanguage = client.getAD_Language(); + if(!Util.isEmpty(clientLanguage) + && Util.isEmpty(language)) { + return clientLanguage; + } + String defaultLanguage = language; + if(Util.isEmpty(language)) { + language = Language.AD_Language_en_US; + } + // Using es / en instead es_VE / en_US + // get default + if(language.length() == 2) { + defaultLanguage = languageCache.get(language); + if(!Util.isEmpty(defaultLanguage)) { + return defaultLanguage; + } + defaultLanguage = DB.getSQLValueString(null, "SELECT AD_Language " + + "FROM AD_Language " + + "WHERE LanguageISO = ? " + + "AND (IsSystemLanguage = 'Y' OR IsBaseLanguage = 'Y')", language); + // Set language + languageCache.put(language, defaultLanguage); + } + if(Util.isEmpty(defaultLanguage)) { + defaultLanguage = Language.AD_Language_en_US; + } + // Default return + return defaultLanguage; + } /** @@ -71,9 +130,7 @@ public class SessionManager { * @param tokenValue */ public static Properties getSessionFromToken(String tokenValue) { - if (tokenValue.startsWith(Constants.BEARER_TYPE)) { - tokenValue = BearerToken.getTokenWithoutType(tokenValue); - } + tokenValue = TokenManager.getTokenWithoutType(tokenValue); // Validate if is token based int userId = -1; int roleId = -1; @@ -135,7 +192,7 @@ public static Properties getSessionFromToken(String tokenValue) { Env.setContext(context, "#AD_Client_ID", session.getAD_Client_ID()); Env.setContext(context, "#Date", new Timestamp(System.currentTimeMillis())); setDefault(context, Env.getAD_Org_ID(context), organizationId, warehouseId); - Env.setContext(context, Env.LANGUAGE, ContextManager.getDefaultLanguage(language)); + Env.setContext(context, Env.LANGUAGE, getDefaultLanguage(language)); return context; } @@ -288,9 +345,7 @@ public static MADToken createSessionFromToken(String tokenValue) { if(Util.isEmpty(tokenValue)) { throw new AdempiereException("@AD_Token_ID@ @NotFound@"); } - if (tokenValue.startsWith(Constants.BEARER_TYPE)) { - tokenValue = BearerToken.getTokenWithoutType(tokenValue); - } + tokenValue = TokenManager.getTokenWithoutType(tokenValue); // try { ITokenGenerator generator = TokenGeneratorHandler.getInstance().getTokenGenerator(MADTokenDefinition.TOKENTYPE_ThirdPartyAccess); @@ -324,7 +379,7 @@ public static void loadDefaultSessionValues(Properties context, String language) MClient client = MClient.get(context, Env.getContextAsInt(context, "#AD_Client_ID")); Env.setContext(context, "#AD_Client_Name", client.getName()); Env.setContext(context, "#Date", new Timestamp(System.currentTimeMillis())); - Env.setContext(context, Env.LANGUAGE, ContextManager.getDefaultLanguage(language)); + Env.setContext(context, Env.LANGUAGE, getDefaultLanguage(language)); // Role Info MRole role = MRole.get(context, Env.getContextAsInt(context, "#AD_Role_ID")); Env.setContext(context, "#AD_Role_Name", role.getName()); @@ -541,7 +596,7 @@ private static void loadPreferences(Properties context) { DB.close(rs, pstmt); } // Country - MCountry country = ContextManager.getDefaultCountry(); + MCountry country = getDefaultCountry(); if(country != null) { Env.setContext(context, "#C_Country_ID", country.getC_Country_ID()); } diff --git a/src/main/java/org/spin/authentication/BearerToken.java b/src/main/java/org/spin/authentication/TokenManager.java similarity index 76% rename from src/main/java/org/spin/authentication/BearerToken.java rename to src/main/java/org/spin/authentication/TokenManager.java index 4ca57b5..b6f3ad0 100644 --- a/src/main/java/org/spin/authentication/BearerToken.java +++ b/src/main/java/org/spin/authentication/TokenManager.java @@ -14,17 +14,25 @@ ************************************************************************************/ package org.spin.authentication; +import static io.grpc.Metadata.ASCII_STRING_MARSHALLER; + import java.util.concurrent.Executor; import io.grpc.CallCredentials; +import io.grpc.Context; import io.grpc.Metadata; import io.grpc.Status; -public class BearerToken extends CallCredentials { +public class TokenManager extends CallCredentials { + + public static final String BEARER_TYPE = "Bearer"; + public static final Metadata.Key AUTHORIZATION_METADATA_KEY = Metadata.Key.of("Authorization", ASCII_STRING_MARSHALLER); + public static final Context.Key CLIENT_ID_CONTEXT_KEY = Context.key("clientId"); + private String value; - public BearerToken(String value) { + public TokenManager(String value) { this.value = value; } @@ -33,7 +41,7 @@ public void applyRequestMetadata(RequestInfo requestInfo, Executor executor, Met executor.execute(() -> { try { Metadata headers = new Metadata(); - headers.put(Constants.AUTHORIZATION_METADATA_KEY, String.format("%s %s", Constants.BEARER_TYPE, value)); + headers.put(AUTHORIZATION_METADATA_KEY, String.format("%s %s", BEARER_TYPE, value)); metadataApplier.apply(headers); } catch (Throwable e) { metadataApplier.fail(Status.UNAUTHENTICATED.withCause(e)); @@ -50,8 +58,8 @@ public static String getTokenWithoutType(String token) { if (token == null || token.trim().length() == 0) { return ""; } - if (token.startsWith(Constants.BEARER_TYPE)) { - return token.substring(Constants.BEARER_TYPE.length()).trim(); + if (token.startsWith(BEARER_TYPE)) { + return token.substring(BEARER_TYPE.length()).trim(); } return token; } diff --git a/src/main/java/org/spin/grpc/client/MiddlewareClient.java b/src/main/java/org/spin/grpc/client/MiddlewareClient.java index d32276c..e3371ae 100644 --- a/src/main/java/org/spin/grpc/client/MiddlewareClient.java +++ b/src/main/java/org/spin/grpc/client/MiddlewareClient.java @@ -23,7 +23,7 @@ import java.util.stream.IntStream; import org.compiere.util.CLogger; -import org.spin.authentication.BearerToken; +import org.spin.authentication.TokenManager; import org.spin.grpc.service.ValueManager; import org.spin.proto.service.Entity; import org.spin.proto.service.CreateEntityRequest; @@ -73,7 +73,7 @@ public static void main(String[] args) throws Exception { String testToken = "Bearer "; byte[] keyBytes = Decoders.BASE64.decode(testToken); Key key = Keys.hmacShaKeyFor(keyBytes); - BearerToken token = new BearerToken(Jwts.builder() + TokenManager token = new TokenManager(Jwts.builder() .setId("101") .setAudience("102") .setSubject("11") diff --git a/src/main/java/org/spin/grpc/server/ContextManager.java b/src/main/java/org/spin/grpc/server/ContextManager.java deleted file mode 100644 index 4907fb2..0000000 --- a/src/main/java/org/spin/grpc/server/ContextManager.java +++ /dev/null @@ -1,191 +0,0 @@ -/************************************************************************************* - * Product: Adempiere ERP & CRM Smart Business Solution * - * This program is free software; you can redistribute it and/or modify it * - * under the terms version 2 or later of the GNU General Public License as published * - * by the Free Software Foundation. This program is distributed in the hope * - * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied * - * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * - * See the GNU General Public License for more details. * - * You should have received a copy of the GNU General Public License along * - * with this program; if not, write to the Free Software Foundation, Inc., * - * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. * - * For the text or an alternative of this public license, you may reach us * - * Copyright (C) 2012-2023 E.R.P. Consultores y Asociados, S.A. All Rights Reserved. * - * Contributor(s): Yamel Senih www.erpya.com * - *************************************************************************************/ -package org.spin.grpc.server; - -import java.math.BigDecimal; -import java.sql.Timestamp; -import java.util.Map; -import java.util.Properties; - -import org.compiere.model.MClient; -import org.compiere.model.MCountry; -import org.compiere.model.MLanguage; -import org.compiere.util.CCache; -import org.compiere.util.DB; -import org.compiere.util.Env; -import org.compiere.util.Language; -import org.compiere.util.Util; -import org.spin.grpc.service.ValueManager; - -import com.google.protobuf.Struct; -import com.google.protobuf.Value; - -/** - * Class for handle Context - * @author Yamel Senih, ysenih@erpya.com , http://www.erpya.com - */ -public class ContextManager { - - /** Language */ - private static CCache languageCache = new CCache("Language-gRPC-Service", 30, 0); // no time-out - - public static Properties setContextWithAttributesFromObjectMap(int windowNo, Properties context, Map attributes) { - return setContextWithAttributes(windowNo, context, attributes, true); - } - - public static Properties setContextWithAttributesFromStruct(int windowNo, Properties context, Struct attributes) { - return setContextWithAttributesFromValuesMap(windowNo, context, attributes.getFieldsMap()); - } - - public static Properties setContextWithAttributesFromValuesMap(int windowNo, Properties context, Map attributes) { - return setContextWithAttributes(windowNo, context, ValueManager.convertValuesMapToObjects(attributes), true); - } - - /** - * Set context with attributes - * @param windowNo - * @param context - * @param attributes - * @return {Properties} context with new values - */ - public static Properties setContextWithAttributes(int windowNo, Properties context, Map attributes, boolean isClearWindow) { - if (isClearWindow) { - Env.clearWinContext(windowNo); - } - if (attributes == null || attributes.size() <= 0) { - return context; - } - - // Fill context - attributes.entrySet().forEach(attribute -> { - setWindowContextByObject(context, windowNo, attribute.getKey(), attribute.getValue()); - }); - - return context; - } - - - /** - * Set context on window by object value - * @param windowNo - * @param context - * @param windowNo - * @param key - * @param value - * @return {Properties} context with new values - */ - public static void setWindowContextByObject(Properties context, int windowNo, String key, Object value) { - if (value instanceof Integer) { - Env.setContext(context, windowNo, key, (Integer) value); - } else if (value instanceof BigDecimal) { - String currentValue = null; - if (value != null) { - currentValue = value.toString(); - } - Env.setContext(context, windowNo, key, currentValue); - } else if (value instanceof Timestamp) { - Env.setContext(context, windowNo, key, (Timestamp) value); - } else if (value instanceof Boolean) { - Env.setContext(context, windowNo, key, (Boolean) value); - } else if (value instanceof String) { - Env.setContext(context, windowNo, key, (String) value); - } - } - - /** - * Set context on tab by object value - * @param windowNo - * @param context - * @param windowNo - * @param tabNo - * @param key - * @param value - * @return {Properties} context with new values - */ - public static void setTabContextByObject(Properties context, int windowNo, int tabNo, String key, Object value) { - if (value instanceof Integer) { - Integer currentValue = (Integer) value; - Env.setContext(context, windowNo, tabNo, key, currentValue.toString()); - }else if (value instanceof BigDecimal) { - String currentValue = null; - if (value != null) { - currentValue = value.toString(); - } - Env.setContext(context, windowNo, tabNo, key, currentValue); - } else if (value instanceof Timestamp) { - Timestamp currentValue = (Timestamp) value; - Env.setContext(context, windowNo, tabNo, key, currentValue.toString()); - } else if (value instanceof Boolean) { - Env.setContext(context, windowNo, tabNo, key, (Boolean) value); - } else if (value instanceof String) { - Env.setContext(context, windowNo, tabNo, key, (String) value); - } - } - - - /** - * Get Default Country - * @return - */ - public static MCountry getDefaultCountry() { - MClient client = MClient.get (Env.getCtx()); - MLanguage language = MLanguage.get(Env.getCtx(), client.getAD_Language()); - MCountry country = MCountry.get(Env.getCtx(), language.getCountryCode()); - // Verify - if(country != null) { - return country; - } - // Default - return MCountry.getDefault(Env.getCtx()); - } - - /** - * Get Default from language - * @param language - * @return - */ - public static String getDefaultLanguage(String language) { - MClient client = MClient.get(Env.getCtx()); - String clientLanguage = client.getAD_Language(); - if(!Util.isEmpty(clientLanguage) - && Util.isEmpty(language)) { - return clientLanguage; - } - String defaultLanguage = language; - if(Util.isEmpty(language)) { - language = Language.AD_Language_en_US; - } - // Using es / en instead es_VE / en_US - // get default - if(language.length() == 2) { - defaultLanguage = languageCache.get(language); - if(!Util.isEmpty(defaultLanguage)) { - return defaultLanguage; - } - defaultLanguage = DB.getSQLValueString(null, "SELECT AD_Language " - + "FROM AD_Language " - + "WHERE LanguageISO = ? " - + "AND (IsSystemLanguage = 'Y' OR IsBaseLanguage = 'Y')", language); - // Set language - languageCache.put(language, defaultLanguage); - } - if(Util.isEmpty(defaultLanguage)) { - defaultLanguage = Language.AD_Language_en_US; - } - // Default return - return defaultLanguage; - } -}