use of org.compiere.model.MRule in project adempiere by adempiere.
the class BrowserRow method processCallOut.
/**
* ***********************************************************************
* Adapted for Browse CallOuts
* Process Callout(s) Adapted.
* <p/>
* The Callout is in the string of
* "class.method;class.method;"
* If there is no class name, i.e. only a method name, the class is regarded
* as CalloutSystem.
* The class needs to comply with the Interface Callout.
* <p/>
* For a limited time, the old notation of Sx_matheod / Ux_menthod is maintained.
*
* @param field field
* @return error message or ""
* @see org.compiere.model.Callout
*/
public String processCallOut(Properties ctx, int WindowNo, GridField field, Object value, Object oldValue, int currentRow, int currentColumn) {
// Set current Row
setCurrentRow(currentRow);
// Valid Callout
String callout = field.getCallout();
if (callout.length() == 0)
return "";
//Object value = field.getValue();
//Object oldValue = field.getOldValue();
log.fine(field.getColumnName() + "=" + value + " (" + callout + ") - old=" + oldValue);
StringTokenizer st = new StringTokenizer(callout, ";,", false);
while (// for each callout
st.hasMoreTokens()) {
String cmd = st.nextToken().trim();
//detect infinite loop
if (activeCallOuts.contains(cmd))
continue;
String retValue = "";
// Victor Perez - vpj-cd implement JSR 223 Scripting
if (cmd.toLowerCase().startsWith(MRule.SCRIPT_PREFIX)) {
MRule rule = MRule.get(ctx, cmd.substring(MRule.SCRIPT_PREFIX.length()));
if (rule == null) {
retValue = "Callout " + cmd + " not found";
log.log(Level.SEVERE, retValue);
return retValue;
}
if (!(rule.getEventType().equals(MRule.EVENTTYPE_Callout) && rule.getRuleType().equals(MRule.RULETYPE_JSR223ScriptingAPIs))) {
retValue = "Callout " + cmd + " must be of type JSR 223 and event Callout";
log.log(Level.SEVERE, retValue);
return retValue;
}
ScriptEngine engine = rule.getScriptEngine();
// Window context are W_
// Login context are G_
MRule.setContext(engine, ctx, WindowNo);
// now add the callout parameters windowNo, tab, field, value, oldValue to the engine
// Method arguments context are A_
engine.put(MRule.ARGUMENTS_PREFIX + "WindowNo", WindowNo);
engine.put(MRule.ARGUMENTS_PREFIX + "Tab", this);
engine.put(MRule.ARGUMENTS_PREFIX + "Field", field);
engine.put(MRule.ARGUMENTS_PREFIX + "Value", value);
engine.put(MRule.ARGUMENTS_PREFIX + "OldValue", oldValue);
engine.put(MRule.ARGUMENTS_PREFIX + "currentRow", currentRow);
engine.put(MRule.ARGUMENTS_PREFIX + "currentColumn", currentColumn);
engine.put(MRule.ARGUMENTS_PREFIX + "Ctx", ctx);
try {
activeCallOuts.add(cmd);
retValue = engine.eval(rule.getScript()).toString();
} catch (Exception e) {
log.log(Level.SEVERE, "", e);
retValue = "Callout Invalid: " + e.toString();
return retValue;
} finally {
activeCallOuts.remove(cmd);
}
} else {
BrowserCallOut call = null;
String method = null;
int methodStart = cmd.lastIndexOf('.');
try {
if (// no class
methodStart != -1) {
Class<?> cClass = Class.forName(cmd.substring(0, methodStart));
call = (BrowserCallOut) cClass.newInstance();
method = cmd.substring(methodStart + 1);
}
} catch (Exception e) {
log.log(Level.SEVERE, "class", e);
return "Callout Invalid: " + cmd + " (" + e.toString() + ")";
}
if (call == null || method == null || method.length() == 0)
return "Callout Invalid: " + method;
try {
activeCallOuts.add(cmd);
activeCallOutInstance.add(call);
retValue = call.start(ctx, method, WindowNo, this, field, value, oldValue, currentRow, currentColumn);
} catch (Exception e) {
log.log(Level.SEVERE, "start", e);
retValue = "Callout Invalid: " + e.toString();
return retValue;
} finally {
activeCallOuts.remove(cmd);
activeCallOutInstance.remove(call);
}
}
if (// interrupt on first error
!Util.isEmpty(retValue)) {
log.severe(retValue);
return retValue;
}
}
// for each callout
return "";
}
use of org.compiere.model.MRule in project adempiere by adempiere.
the class MRuleTest method testQuery.
public void testQuery() throws Exception {
MRule getrule = MRule.get(getCtx(), "beanshell:getAvailable");
assertTrue("BeanRule must be exact", getrule.getAD_Rule_ID() == 1000000);
List<MRule> rules = MRule.getModelValidatorLoginRules(getCtx());
//red1 set in DB Rule.EventType = L before testing
assertTrue("Rules has array", rules.size() > 0);
}
use of org.compiere.model.MRule in project adempiere by adempiere.
the class MHRProcess method executeScript.
/**
* Execute the script
* @param ruleId
* @param columnType Column Type
* @return Object
*/
private Object executeScript(MHRConcept concept, int ruleId, String columnType) {
MRule rule = MRule.get(getCtx(), ruleId);
Object result = null;
description = null;
try {
if (rule == null) {
logger.log(Level.WARNING, " @AD_Rule_ID@ @NotFound@");
}
if (!(rule.getEventType().equals(MRule.EVENTTYPE_HumanResourcePayroll) && rule.getRuleType().equals(MRule.RULETYPE_JSR223ScriptingAPIs))) {
logger.log(Level.WARNING, " must be of type JSR 223 and event human resource");
}
if (rule.getEngineName() != null)
return executeScriptEngine(concept, rule, columnType);
String text = "";
if (rule.getScript() != null) {
text = rule.getScript().trim().replaceAll("\\bget", "process.get").replace(".process.get", ".get");
}
String resultType = "double";
// Yamel Senih Add DefValue to another Types
String defValue = "0";
if (MHRAttribute.COLUMNTYPE_Date.equals(columnType)) {
resultType = "Timestamp";
defValue = "null";
} else if (MHRAttribute.COLUMNTYPE_Text.equals(columnType)) {
resultType = "String";
defValue = "null";
}
final String script = s_scriptImport.toString() + " " + resultType + " result = " + defValue + ";" + " String description = null;" + text;
Scriptlet engine = new Scriptlet(Scriptlet.VARIABLE, script, scriptCtx);
Exception ex = engine.execute();
if (ex != null) {
throw ex;
}
result = engine.getResult(false);
description = engine.getDescription();
} catch (Exception e) {
throw new AdempiereException("@HR_Employee_ID@ : " + employee.getName() + " " + employee.getName2() + " @HR_Concept_ID@ " + concept.getValue() + " -> " + concept.getName() + " @AD_Rule_ID@=" + rule.getValue() + " Execution error " + e.getLocalizedMessage());
}
return result;
}
use of org.compiere.model.MRule in project adempiere by adempiere.
the class ProcessUtil method startScriptProcess.
public static boolean startScriptProcess(Properties ctx, ProcessInfo pi, Trx trx) {
String msg = null;
boolean success = true;
try {
String cmd = pi.getClassName();
MRule rule = MRule.get(ctx, cmd.substring(MRule.SCRIPT_PREFIX.length()));
if (rule == null) {
log.log(Level.WARNING, cmd + " not found");
pi.setSummary("ScriptNotFound", true);
return false;
}
if (!(rule.getEventType().equals(MRule.EVENTTYPE_Process) && rule.getRuleType().equals(MRule.RULETYPE_JSR223ScriptingAPIs))) {
log.log(Level.WARNING, cmd + " must be of type JSR 223 and event Process");
pi.setSummary("ScriptNotFound", true);
return false;
}
ScriptEngine engine = rule.getScriptEngine();
// Window context are W_
// Login context are G_
// Method arguments context are A_
// Parameter context are P_
// no window
MRule.setContext(engine, ctx, 0);
// now add the method arguments to the engine
engine.put(MRule.ARGUMENTS_PREFIX + "Ctx", ctx);
if (trx == null)
trx = Trx.get(pi.getTitle() + "_" + pi.getAD_PInstance_ID(), true);
engine.put(MRule.ARGUMENTS_PREFIX + "Trx", trx);
engine.put(MRule.ARGUMENTS_PREFIX + "TrxName", trx.getTrxName());
engine.put(MRule.ARGUMENTS_PREFIX + "Record_ID", pi.getRecord_ID());
engine.put(MRule.ARGUMENTS_PREFIX + "AD_Client_ID", pi.getAD_Client_ID());
engine.put(MRule.ARGUMENTS_PREFIX + "AD_User_ID", pi.getAD_User_ID());
engine.put(MRule.ARGUMENTS_PREFIX + "AD_PInstance_ID", pi.getAD_PInstance_ID());
engine.put(MRule.ARGUMENTS_PREFIX + "Table_ID", pi.getTable_ID());
// Add process parameters
ProcessInfoParameter[] para = pi.getParameter();
if (para == null) {
ProcessInfoUtil.setParameterFromDB(pi);
para = pi.getParameter();
}
if (para != null) {
engine.put(MRule.ARGUMENTS_PREFIX + "Parameter", pi.getParameter());
for (int i = 0; i < para.length; i++) {
String name = para[i].getParameterName();
if (para[i].getParameter_To() == null) {
Object value = para[i].getParameter();
if (name.endsWith("_ID") && (value instanceof BigDecimal))
engine.put(MRule.PARAMETERS_PREFIX + name, ((BigDecimal) value).intValue());
else
engine.put(MRule.PARAMETERS_PREFIX + name, value);
} else {
Object value1 = para[i].getParameter();
Object value2 = para[i].getParameter_To();
if (name.endsWith("_ID") && (value1 instanceof BigDecimal))
engine.put(MRule.PARAMETERS_PREFIX + name + "1", ((BigDecimal) value1).intValue());
else
engine.put(MRule.PARAMETERS_PREFIX + name + "1", value1);
if (name.endsWith("_ID") && (value2 instanceof BigDecimal))
engine.put(MRule.PARAMETERS_PREFIX + name + "2", ((BigDecimal) value2).intValue());
else
engine.put(MRule.PARAMETERS_PREFIX + name + "2", value2);
}
}
}
engine.put(MRule.ARGUMENTS_PREFIX + "ProcessInfo", pi);
msg = engine.eval(rule.getScript()).toString();
//transaction should rollback if there are error in process
if ("@Error@".equals(msg))
success = false;
// Parse Variables
msg = Msg.parseTranslation(ctx, msg);
pi.setSummary(msg, !success);
} catch (Exception e) {
pi.setSummary("ScriptError", true);
log.log(Level.SEVERE, pi.getClassName(), e);
success = false;
}
if (success) {
if (trx != null) {
try {
trx.commit(true);
} catch (Exception e) {
log.log(Level.SEVERE, "Commit failed.", e);
pi.addSummary("Commit Failed.");
pi.setError(true);
success = false;
}
trx.close();
}
} else {
if (trx != null) {
trx.rollback();
trx.close();
}
}
return success;
}
Aggregations