From 491f468015f3568bec7e9b46f0d92ecbcd8e5548 Mon Sep 17 00:00:00 2001 From: teo_sarca Date: Fri, 10 Jul 2009 14:42:39 +0000 Subject: [PATCH] Implement Table and Document ModelValidator script JSR 223 https://sourceforge.net/tracker/?func=detail&atid=879335&aid=1879470&group_id=176962 Apply ADempiere Best Practices (ABP) --- .../compiere/model/MTableScriptValidator.java | 70 ++++++------------- .../compiere/model/ModelValidationEngine.java | 4 +- 2 files changed, 24 insertions(+), 50 deletions(-) diff --git a/base/src/org/compiere/model/MTableScriptValidator.java b/base/src/org/compiere/model/MTableScriptValidator.java index f71776e98d..6e9cc1f524 100644 --- a/base/src/org/compiere/model/MTableScriptValidator.java +++ b/base/src/org/compiere/model/MTableScriptValidator.java @@ -17,15 +17,12 @@ *****************************************************************************/ package org.compiere.model; -import java.sql.PreparedStatement; import java.sql.ResultSet; -import java.util.ArrayList; +import java.util.List; import java.util.Properties; -import java.util.logging.Level; +import org.apache.commons.collections.keyvalue.MultiKey; import org.compiere.util.CCache; -import org.compiere.util.CLogger; -import org.compiere.util.DB; /** * Table Validator Scripts @@ -37,11 +34,10 @@ import org.compiere.util.DB; */ public class MTableScriptValidator extends X_AD_Table_ScriptValidator { - /** * */ - private static final long serialVersionUID = 3640498344452542348L; + private static final long serialVersionUID = 6272423660330749776L; /** * Get table script validator from cache @@ -51,7 +47,7 @@ public class MTableScriptValidator extends X_AD_Table_ScriptValidator */ public static MTableScriptValidator get (Properties ctx, int AD_Table_ScriptValidator_ID) { - Integer key = new Integer (AD_Table_ScriptValidator_ID); + final Integer key = AD_Table_ScriptValidator_ID; MTableScriptValidator retValue = (MTableScriptValidator) s_cache.get (key); if (retValue != null) return retValue; @@ -68,12 +64,13 @@ public class MTableScriptValidator extends X_AD_Table_ScriptValidator * @param event Event * @return array of MTableScriptValidator or null if error or no validators found */ - public static ArrayList getModelValidatorRules (Properties ctx, int ad_table_id, String event) + public static List getModelValidatorRules (Properties ctx, int ad_table_id, String event) { // Try cache - String key = ""+ad_table_id+"_"+event; - ArrayList mvrs = s_cacheTableEvent.get(key); - if (mvrs != null) { + final MultiKey key = new MultiKey(ad_table_id, event); + List mvrs = s_cacheTableEvent.get(key); + if (mvrs != null) + { if (mvrs.size() > 0) return mvrs; else @@ -81,32 +78,16 @@ public class MTableScriptValidator extends X_AD_Table_ScriptValidator } // // Fetch now - mvrs = new ArrayList(); - MTableScriptValidator rule = null; - String sql = "SELECT * FROM AD_Table_ScriptValidator WHERE AD_Table_ID=? AND EventModelValidator=? AND IsActive='Y' ORDER BY SeqNo"; - PreparedStatement pstmt = null; - ResultSet rs = null; - try + final String whereClause = "AD_Table_ID=? AND EventModelValidator=?"; + mvrs = new Query(ctx, Table_Name, whereClause, null) + .setParameters(new Object[]{ad_table_id, event}) + .setOnlyActiveRecords(true) + .setOrderBy(COLUMNNAME_SeqNo) + .list(); + // Store to cache + for (MTableScriptValidator rule : mvrs) { - pstmt = DB.prepareStatement (sql, null); - pstmt.setInt(1, ad_table_id); - pstmt.setString(2, event); - rs = pstmt.executeQuery (); - while (rs.next ()) { - rule = new MTableScriptValidator (ctx, rs, null); - mvrs.add(rule); - // Cache - s_cache.put(rule.get_ID(), rule); - } - } - catch (Exception e) - { - s_log.log(Level.SEVERE, sql, e); - mvrs = null; - } - finally { - DB.close(rs, pstmt); - rs = null; pstmt = null; + s_cache.put(rule.get_ID(), rule); } // Store to cache @@ -117,17 +98,14 @@ public class MTableScriptValidator extends X_AD_Table_ScriptValidator return mvrs; else return null; - } // getModelValidatorLoginRules + } // getModelValidatorRules /** Cache */ private static CCache s_cache = new CCache(Table_Name, 20); /** Cache / Table Event */ - private static CCache> s_cacheTableEvent - = new CCache>(Table_Name+"_TableEvent", 20); - - /** Static Logger */ - private static CLogger s_log = CLogger.getCLogger (MTableScriptValidator.class); + private static CCache> s_cacheTableEvent + = new CCache>(Table_Name+"_TableEvent", 20); /************************************************************************** * Standard Constructor @@ -151,10 +129,7 @@ public class MTableScriptValidator extends X_AD_Table_ScriptValidator super(ctx, rs, trxName); } // MTableScriptValidator - /** - * String Representation - * @return info - */ + @Override public String toString() { StringBuffer sb = new StringBuffer ("MTableScriptValidator["); @@ -162,5 +137,4 @@ public class MTableScriptValidator extends X_AD_Table_ScriptValidator .append(getEventModelValidator()).append("]"); return sb.toString (); } // toString - } // MTableScriptValidator \ No newline at end of file diff --git a/base/src/org/compiere/model/ModelValidationEngine.java b/base/src/org/compiere/model/ModelValidationEngine.java index c412833545..17f96a008c 100644 --- a/base/src/org/compiere/model/ModelValidationEngine.java +++ b/base/src/org/compiere/model/ModelValidationEngine.java @@ -334,7 +334,7 @@ public class ModelValidationEngine } // now process the script model validator for this event - ArrayList scriptValidators = + List scriptValidators = MTableScriptValidator.getModelValidatorRules( po.getCtx(), po.get_Table_ID(), @@ -480,7 +480,7 @@ public class ModelValidationEngine } // now process the script model validator for this event - ArrayList scriptValidators = + List scriptValidators = MTableScriptValidator.getModelValidatorRules( po.getCtx(), po.get_Table_ID(),