From 322a2413d7cc89111dd3d24e1070d1e9a2400727 Mon Sep 17 00:00:00 2001 From: hieplq Date: Tue, 19 Mar 2019 09:08:33 +0700 Subject: [PATCH] IDEMPIERE-3917:IDEMPIERE-3321:IDEMPIERE-3627: seperate evaluator to common --- .../src/org/compiere/model/GridField.java | 66 +---------------- .../src/org/compiere/util/Evaluator.java | 72 +++++++++++++++++++ 2 files changed, 75 insertions(+), 63 deletions(-) diff --git a/org.adempiere.base/src/org/compiere/model/GridField.java b/org.adempiere.base/src/org/compiere/model/GridField.java index 1f26b575df..2879eda64c 100644 --- a/org.adempiere.base/src/org/compiere/model/GridField.java +++ b/org.adempiere.base/src/org/compiere/model/GridField.java @@ -31,10 +31,8 @@ import java.util.Calendar; import java.util.Collections; import java.util.Date; import java.util.List; -import java.util.Map; import java.util.Properties; import java.util.StringTokenizer; -import java.util.concurrent.ConcurrentHashMap; import java.util.logging.Level; import org.adempiere.base.ILookupFactory; @@ -358,7 +356,7 @@ public class GridField { boolean retValue = false; if (m_vo.MandatoryLogic != null && m_vo.MandatoryLogic.startsWith("@SQL=")) { - retValue = parseSQLLogic(m_vo.MandatoryLogic); + retValue = Evaluator.parseSQLLogic(m_vo.MandatoryLogic, m_vo.ctx, m_vo.WindowNo, m_vo.TabNo, m_vo.ColumnName); } else{ retValue= Evaluator.evaluateLogic(this, m_vo.MandatoryLogic); @@ -390,57 +388,6 @@ public class GridField return isDisplayed (checkContext); } // isMandatory - private boolean parseSQLLogic(String sqlLogic) { - String sql = sqlLogic.substring(5); // remove @SQL= - boolean reverse = false; - if (sql.startsWith("!")) { - reverse = true; - sql = sql.substring(1); //remove ! - } - sql = Env.parseContext(m_vo.ctx, m_vo.WindowNo, m_vo.TabNo, sql, false, false); // replace - - // variables - if (sql.equals("")) { - log.log(Level.WARNING,"(" + m_vo.ColumnName + ") - SQL variable parse failed: " + sqlLogic); - } else { - SQLLogicResult cache = sqlLogicCache.get(sql); - if (cache != null) { - long since = System.currentTimeMillis() - cache.timestamp; - if (since <= 500) { - cache.timestamp = System.currentTimeMillis(); - if (cache.value) - return reverse ? false : true; - else - return reverse ? true : false; - } - } - PreparedStatement stmt = null; - ResultSet rs = null; - try { - stmt = DB.prepareStatement(sql, null); - rs = stmt.executeQuery(); - boolean hasNext = rs.next(); - if (cache == null) { - cache = new SQLLogicResult(); - sqlLogicCache.put(sql, cache); - } - cache.value = hasNext; - cache.timestamp = System.currentTimeMillis(); - if (hasNext) - return reverse ? false : true; - else - return reverse ? true : false; - } catch (SQLException e) { - log.log(Level.WARNING, "(" + m_vo.ColumnName + ") " + sql, e); - } finally { - DB.close(rs, stmt); - rs = null; - stmt = null; - } - } - return false; - } - /** * Is parameter Editable - checks if parameter is Read Only * @param checkContext if true checks Context @@ -451,7 +398,7 @@ public class GridField { if (m_vo.ReadOnlyLogic.startsWith("@SQL=")) { - boolean retValue = !parseSQLLogic(m_vo.ReadOnlyLogic); + boolean retValue = !Evaluator.parseSQLLogic(m_vo.ReadOnlyLogic, m_vo.ctx, m_vo.WindowNo, m_vo.TabNo, m_vo.ColumnName); if (!retValue) return false; } @@ -563,7 +510,7 @@ public class GridField { if (m_vo.ReadOnlyLogic.startsWith("@SQL=")) { - boolean retValue = !parseSQLLogic(m_vo.ReadOnlyLogic); + boolean retValue = !Evaluator.parseSQLLogic(m_vo.ReadOnlyLogic, m_vo.ctx, m_vo.WindowNo, m_vo.TabNo, m_vo.ColumnName); if (!retValue) return false; } @@ -2605,13 +2552,6 @@ public class GridField return m_lookupEditorSettingValue; } - private static final Map sqlLogicCache = new ConcurrentHashMap<>(); - - private class SQLLogicResult { - long timestamp; - boolean value; - } - public void processUIVirtualColumn() { String sql = m_vo.ColumnSQL.substring(5); sql = Env.parseContext(Env.getCtx(), getWindowNo(), sql, false); diff --git a/org.adempiere.base/src/org/compiere/util/Evaluator.java b/org.adempiere.base/src/org/compiere/util/Evaluator.java index 9ca826fba5..3dfcea07fb 100644 --- a/org.adempiere.base/src/org/compiere/util/Evaluator.java +++ b/org.adempiere.base/src/org/compiere/util/Evaluator.java @@ -17,8 +17,14 @@ package org.compiere.util; import java.math.BigDecimal; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; import java.util.ArrayList; +import java.util.Map; +import java.util.Properties; import java.util.StringTokenizer; +import java.util.concurrent.ConcurrentHashMap; import java.util.logging.Level; @@ -33,6 +39,13 @@ public class Evaluator /** Static Logger */ private static CLogger s_log = CLogger.getCLogger (Evaluator.class); + private static final Map sqlLogicCache = new ConcurrentHashMap<>(); + + public static class SQLLogicResult { + long timestamp; + boolean value; + } + /** * Check if All Variables are Defined * @param source source @@ -299,4 +312,63 @@ public class Evaluator } } // parseDepends + /** + * evaluator a expression logic base on sql + * @param sqlLogic + * @param ctx + * @param windowNo + * @param tabNo + * @param targetObjectName expression logic is evaluated for, that target object (purpose for logging) can be field name, toolbar button name,.. + * @return + */ + public static boolean parseSQLLogic(String sqlLogic, Properties ctx, int windowNo, int tabNo, String targetObjectName) { + String sql = sqlLogic.substring(5); // remove @SQL= + boolean reverse = false; + if (sql.startsWith("!")) { + reverse = true; + sql = sql.substring(1); //remove ! + } + sql = Env.parseContext(ctx, windowNo, tabNo, sql, false, false); // replace + + // variables + if (sql.equals("")) { + s_log.log(Level.WARNING,"(" + targetObjectName + ") - SQL variable parse failed: " + sqlLogic); + } else { + SQLLogicResult cache = sqlLogicCache.get(sql); + if (cache != null) { + long since = System.currentTimeMillis() - cache.timestamp; + if (since <= 500) { + cache.timestamp = System.currentTimeMillis(); + if (cache.value) + return reverse ? false : true; + else + return reverse ? true : false; + } + } + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = DB.prepareStatement(sql, null); + rs = stmt.executeQuery(); + boolean hasNext = rs.next(); + if (cache == null) { + cache = new SQLLogicResult(); + sqlLogicCache.put(sql, cache); + } + cache.value = hasNext; + cache.timestamp = System.currentTimeMillis(); + if (hasNext) + return reverse ? false : true; + else + return reverse ? true : false; + } catch (SQLException e) { + s_log.log(Level.WARNING, "(" + targetObjectName + ") " + sql, e); + } finally { + DB.close(rs, stmt); + rs = null; + stmt = null; + } + } + return false; + } } // Evaluator