IDEMPIERE-3917:IDEMPIERE-3321:IDEMPIERE-3627: seperate evaluator to common

This commit is contained in:
hieplq 2019-03-19 09:08:33 +07:00
parent c9d4d19fdd
commit 322a2413d7
2 changed files with 75 additions and 63 deletions

View File

@ -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<String, SQLLogicResult> 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);

View File

@ -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<String, SQLLogicResult> 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