From 3de220daec86c45cb7bdbc67f5cf9a69cc2e1dc1 Mon Sep 17 00:00:00 2001 From: Carlos Ruiz Date: Tue, 10 Sep 2024 09:58:16 +0200 Subject: [PATCH] IDEMPIERE-5647 Allow definition of context variables by Role - fix issue not being used in Logic (#2452) --- .../src/org/compiere/model/MQuery.java | 4 +-- .../print/layout/PrintDataEvaluatee.java | 4 +-- .../src/org/compiere/util/Env.java | 36 ++++++++++++++++--- 3 files changed, 35 insertions(+), 9 deletions(-) diff --git a/org.adempiere.base/src/org/compiere/model/MQuery.java b/org.adempiere.base/src/org/compiere/model/MQuery.java index 036f0917f0..5bef7ea887 100644 --- a/org.adempiere.base/src/org/compiere/model/MQuery.java +++ b/org.adempiere.base/src/org/compiere/model/MQuery.java @@ -1845,7 +1845,7 @@ class QueryEvaluatee implements Evaluatee { } String value = null; - if (variableName.startsWith("#") || variableName.startsWith("$")) { + if (Env.isGlobalVariable(variableName)) { value = Env.getContext(ctx, variableName); } else { value = parameterMap.get(variableName); @@ -1857,7 +1857,7 @@ class QueryEvaluatee implements Evaluatee { id = Integer.parseInt(value); } catch (Exception e){} if (id > 0) { - if (variableName.startsWith("#") || variableName.startsWith("$")) { + if (Env.isGlobalVariable(variableName)) { variableName = variableName.substring(1); } else if (variableName.indexOf("|") > 0) { variableName = variableName.substring(variableName.lastIndexOf("|")+1); diff --git a/org.adempiere.base/src/org/compiere/print/layout/PrintDataEvaluatee.java b/org.adempiere.base/src/org/compiere/print/layout/PrintDataEvaluatee.java index cac55ce991..f4093e41a9 100644 --- a/org.adempiere.base/src/org/compiere/print/layout/PrintDataEvaluatee.java +++ b/org.adempiere.base/src/org/compiere/print/layout/PrintDataEvaluatee.java @@ -55,7 +55,7 @@ public class PrintDataEvaluatee implements Evaluatee { } String value = null; - if (variableName.startsWith("#") || variableName.startsWith("$")) { + if (Env.isGlobalVariable(variableName)) { value = Env.getContext(Env.getCtx(), variableName); } else { Object obj = m_data.getNode(variableName); @@ -89,7 +89,7 @@ public class PrintDataEvaluatee implements Evaluatee { "SELECT " + foreignColumn + " FROM " + foreignTable + " WHERE " + foreignTable + "_ID = ?", id); } else { - if (variableName.startsWith("#") || variableName.startsWith("$")) { + if (Env.isGlobalVariable(variableName)) { variableName = variableName.substring(1); } else if (variableName.indexOf("|") > 0) { variableName = variableName.substring(variableName.lastIndexOf("|")+1); diff --git a/org.adempiere.base/src/org/compiere/util/Env.java b/org.adempiere.base/src/org/compiere/util/Env.java index 06cc57f6d4..ddac8a409e 100644 --- a/org.adempiere.base/src/org/compiere/util/Env.java +++ b/org.adempiere.base/src/org/compiere/util/Env.java @@ -629,7 +629,7 @@ public final class Env if (s == null) { // Explicit Base Values - if (context.startsWith("#") || context.startsWith("$") || context.startsWith("P|")) + if (Env.isGlobalVariable(context) || Env.isPreference(context)) return getContext(ctx, context); if (onlyWindow) // no Default values return ""; @@ -1039,6 +1039,8 @@ public final class Env retValue = ctx.getProperty("#"+context); // Login setting if (retValue == null) retValue = ctx.getProperty("$"+context); // Accounting setting + if (retValue == null) + retValue = ctx.getProperty("+"+context); // Injected Role Variable } // return (retValue == null ? "" : retValue); @@ -1527,7 +1529,7 @@ public final class Env } String ctxInfo = getContext(ctx, WindowNo, token, onlyWindow); // get context - if (ctxInfo.length() == 0 && (token.startsWith("#") || token.startsWith("$")) ) + if (ctxInfo.length() == 0 && Env.isGlobalVariable(token)) ctxInfo = getContext(ctx, token); // get global context if (ctxInfo.length() == 0 && defaultV != null) @@ -1613,7 +1615,7 @@ public final class Env ctxInfo = getContext(ctx, WindowNo, tabNo, token, onlyTab); // get context } - if (ctxInfo.length() == 0 && (token.startsWith("#") || token.startsWith("$")) ) + if (ctxInfo.length() == 0 && Env.isGlobalVariable(token)) ctxInfo = getContext(ctx, token); // get global context if (ctxInfo.length() == 0 && defaultV != null) @@ -1729,7 +1731,7 @@ public final class Env } Properties ctx = po != null ? po.getCtx() : Env.getCtx(); - if (token.startsWith("#") || token.startsWith("$")) { + if (Env.isGlobalVariable(token)) { //take from context String v = Env.getContext(ctx, token); if (v != null && v.length() > 0) { @@ -1818,7 +1820,7 @@ public final class Env String token, String format, MColumn colToken, Object value, StringBuilder outStr) { if (format != null && format.length() > 0) { String foreignTable = colToken != null ? colToken.getReferenceTableName() : null; - if (value instanceof String && token.endsWith("_ID") && (token.startsWith("#") || token.startsWith("$"))) { + if (value instanceof String && token.endsWith("_ID") && Env.isGlobalVariable(token)) { try { int id = Integer.parseInt((String)value); value = id; @@ -2343,4 +2345,28 @@ public final class Env return "Y".equals(Env.getContext(Env.getCtx(), "IsReadOnlySession")); } + /** + * Verifies if a context variable name is global, this is, starting with: + * # Login + * $ Accounting + * + Role Injected + * @param variable + * @return + */ + public static boolean isGlobalVariable(String variable) { + return variable.startsWith("#") + || variable.startsWith("$") + || variable.startsWith("+"); + } + + /** + * Verifies if a context variable name is a preference, this is, starting with: + * P| Preference + * @param variable + * @return + */ + public static boolean isPreference(String variable) { + return variable.startsWith("P|"); + } + } // Env \ No newline at end of file