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)
This commit is contained in:
teo_sarca 2009-07-10 14:42:39 +00:00
parent 43c266a3ec
commit 491f468015
2 changed files with 24 additions and 50 deletions

View File

@ -17,15 +17,12 @@
*****************************************************************************/ *****************************************************************************/
package org.compiere.model; package org.compiere.model;
import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.util.ArrayList; import java.util.List;
import java.util.Properties; 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.CCache;
import org.compiere.util.CLogger;
import org.compiere.util.DB;
/** /**
* Table Validator Scripts * Table Validator Scripts
@ -37,11 +34,10 @@ import org.compiere.util.DB;
*/ */
public class MTableScriptValidator extends X_AD_Table_ScriptValidator 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 * 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) 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); MTableScriptValidator retValue = (MTableScriptValidator) s_cache.get (key);
if (retValue != null) if (retValue != null)
return retValue; return retValue;
@ -68,12 +64,13 @@ public class MTableScriptValidator extends X_AD_Table_ScriptValidator
* @param event Event * @param event Event
* @return array of MTableScriptValidator or null if error or no validators found * @return array of MTableScriptValidator or null if error or no validators found
*/ */
public static ArrayList<MTableScriptValidator> getModelValidatorRules (Properties ctx, int ad_table_id, String event) public static List<MTableScriptValidator> getModelValidatorRules (Properties ctx, int ad_table_id, String event)
{ {
// Try cache // Try cache
String key = ""+ad_table_id+"_"+event; final MultiKey key = new MultiKey(ad_table_id, event);
ArrayList<MTableScriptValidator> mvrs = s_cacheTableEvent.get(key); List<MTableScriptValidator> mvrs = s_cacheTableEvent.get(key);
if (mvrs != null) { if (mvrs != null)
{
if (mvrs.size() > 0) if (mvrs.size() > 0)
return mvrs; return mvrs;
else else
@ -81,33 +78,17 @@ public class MTableScriptValidator extends X_AD_Table_ScriptValidator
} }
// //
// Fetch now // Fetch now
mvrs = new ArrayList<MTableScriptValidator>(); final String whereClause = "AD_Table_ID=? AND EventModelValidator=?";
MTableScriptValidator rule = null; mvrs = new Query(ctx, Table_Name, whereClause, null)
String sql = "SELECT * FROM AD_Table_ScriptValidator WHERE AD_Table_ID=? AND EventModelValidator=? AND IsActive='Y' ORDER BY SeqNo"; .setParameters(new Object[]{ad_table_id, event})
PreparedStatement pstmt = null; .setOnlyActiveRecords(true)
ResultSet rs = null; .setOrderBy(COLUMNNAME_SeqNo)
try .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); 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;
}
// Store to cache // Store to cache
if (mvrs != null) if (mvrs != null)
@ -117,17 +98,14 @@ public class MTableScriptValidator extends X_AD_Table_ScriptValidator
return mvrs; return mvrs;
else else
return null; return null;
} // getModelValidatorLoginRules } // getModelValidatorRules
/** Cache */ /** Cache */
private static CCache<Integer,MTableScriptValidator> s_cache private static CCache<Integer,MTableScriptValidator> s_cache
= new CCache<Integer,MTableScriptValidator>(Table_Name, 20); = new CCache<Integer,MTableScriptValidator>(Table_Name, 20);
/** Cache / Table Event */ /** Cache / Table Event */
private static CCache<String,ArrayList<MTableScriptValidator>> s_cacheTableEvent private static CCache<MultiKey,List<MTableScriptValidator>> s_cacheTableEvent
= new CCache<String,ArrayList<MTableScriptValidator>>(Table_Name+"_TableEvent", 20); = new CCache<MultiKey,List<MTableScriptValidator>>(Table_Name+"_TableEvent", 20);
/** Static Logger */
private static CLogger s_log = CLogger.getCLogger (MTableScriptValidator.class);
/************************************************************************** /**************************************************************************
* Standard Constructor * Standard Constructor
@ -151,10 +129,7 @@ public class MTableScriptValidator extends X_AD_Table_ScriptValidator
super(ctx, rs, trxName); super(ctx, rs, trxName);
} // MTableScriptValidator } // MTableScriptValidator
/** @Override
* String Representation
* @return info
*/
public String toString() public String toString()
{ {
StringBuffer sb = new StringBuffer ("MTableScriptValidator["); StringBuffer sb = new StringBuffer ("MTableScriptValidator[");
@ -162,5 +137,4 @@ public class MTableScriptValidator extends X_AD_Table_ScriptValidator
.append(getEventModelValidator()).append("]"); .append(getEventModelValidator()).append("]");
return sb.toString (); return sb.toString ();
} // toString } // toString
} // MTableScriptValidator } // MTableScriptValidator

View File

@ -334,7 +334,7 @@ public class ModelValidationEngine
} }
// now process the script model validator for this event // now process the script model validator for this event
ArrayList<MTableScriptValidator> scriptValidators = List<MTableScriptValidator> scriptValidators =
MTableScriptValidator.getModelValidatorRules( MTableScriptValidator.getModelValidatorRules(
po.getCtx(), po.getCtx(),
po.get_Table_ID(), po.get_Table_ID(),
@ -480,7 +480,7 @@ public class ModelValidationEngine
} }
// now process the script model validator for this event // now process the script model validator for this event
ArrayList<MTableScriptValidator> scriptValidators = List<MTableScriptValidator> scriptValidators =
MTableScriptValidator.getModelValidatorRules( MTableScriptValidator.getModelValidatorRules(
po.getCtx(), po.getCtx(),
po.get_Table_ID(), po.get_Table_ID(),