[ 1731053 ] Refactoring of ProcessCtl and ServerBean

This commit is contained in:
Heng Sin Low 2007-06-06 01:04:58 +00:00
parent 95d8e5ed95
commit b2fc7de3c1
2 changed files with 155 additions and 47 deletions

View File

@ -0,0 +1,128 @@
package org.adempiere.util;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.Properties;
import java.util.logging.Level;
import org.compiere.process.ProcessCall;
import org.compiere.process.ProcessInfo;
import org.compiere.process.SvrProcess;
import org.compiere.util.CLogger;
import org.compiere.util.DB;
import org.compiere.util.Env;
import org.compiere.util.Msg;
import org.compiere.util.Trx;
import org.compiere.wf.MWFProcess;
import org.compiere.wf.MWorkflow;
/**
*
* @author Low Heng Sin
*
*/
public final class ProcessUtil {
/** Logger */
private static CLogger log = CLogger.getCLogger(ProcessUtil.class);
private ProcessUtil() {}
public static boolean startDatabaseProcedure (ProcessInfo processInfo, String ProcedureName, Trx trx) {
String sql = "{call " + ProcedureName + "(?)}";
String trxName = trx != null ? trx.getTrxName() : null;
try
{
//hengsin, add trx support, updateable support.
CallableStatement cstmt = DB.prepareCall(sql, ResultSet.CONCUR_UPDATABLE, trxName);
cstmt.setInt(1, processInfo.getAD_PInstance_ID());
cstmt.executeUpdate();
cstmt.close();
if (trx != null && trx.isActive())
{
trx.commit(true);
trx.close();
}
}
catch (Exception e)
{
log.log(Level.SEVERE, sql, e);
if (trx != null && trx.isActive())
{
trx.rollback();
trx.close();
}
processInfo.setSummary (Msg.getMsg(Env.getCtx(), "ProcessRunError") + " " + e.getLocalizedMessage());
processInfo.setError (true);
return false;
}
return true;
}
public static boolean startJavaProcess(ProcessInfo pi, Trx trx) {
String className = pi.getClassName();
//Get Class
Class processClass = null;
try
{
processClass = Class.forName (className);
}
catch (ClassNotFoundException ex)
{
log.log(Level.WARNING, className, ex);
pi.setSummary ("ClassNotFound", true);
return false;
}
//Get Process
ProcessCall process = null;
try
{
process = (SvrProcess)processClass.newInstance ();
}
catch (Exception ex)
{
log.log(Level.WARNING, "Instance for " + className, ex);
pi.setSummary ("InstanceError", true);
return false;
}
if (processClass == null) {
pi.setSummary("No Instance for " + pi.getClassName(), true);
return false;
}
try
{
process.startProcess(Env.getCtx(), pi, trx);
if (trx != null)
{
trx.commit(true);
trx.close();
}
}
catch (Exception e)
{
if (trx != null)
{
trx.rollback();
trx.close();
}
pi.setSummary("ProcessError", true);
log.log(Level.SEVERE, pi.getClassName(), e);
return false;
}
return true;
}
public static MWFProcess startWorkFlow(Properties ctx, ProcessInfo pi, int AD_Workflow_ID) {
MWorkflow wf = MWorkflow.get (ctx, AD_Workflow_ID);
MWFProcess wfProcess = null;
if (pi.isBatch())
wfProcess = wf.start(pi); // may return null
else
wfProcess = wf.startWait(pi); // may return null
log.fine(pi.toString());
return wfProcess;
}
}

View File

@ -19,6 +19,8 @@ package org.compiere.model;
import java.sql.*; import java.sql.*;
import java.util.*; import java.util.*;
import java.util.logging.*; import java.util.logging.*;
import org.adempiere.util.ProcessUtil;
import org.compiere.process.*; import org.compiere.process.*;
import org.compiere.util.*; import org.compiere.util.*;
@ -229,13 +231,13 @@ public class MProcess extends X_AD_Process
boolean ok = true; boolean ok = true;
// PL/SQL Procedure ProcessInfo processInfo = new ProcessInfo("", this.getAD_Process_ID());
String ProcedureName = getProcedureName(); processInfo.setAD_PInstance_ID(pInstance.getAD_PInstance_ID());
if (ProcedureName != null && ProcedureName.length() > 0) ok = processIt(processInfo, trx);
ok = startProcess (ProcedureName, pInstance);
// Unlock // Unlock
pInstance.setResult(ok ? MPInstance.RESULT_OK : MPInstance.RESULT_ERROR); pInstance.setResult(ok ? MPInstance.RESULT_OK : MPInstance.RESULT_ERROR);
pInstance.setErrorMsg(processInfo.getSummary());
pInstance.setIsProcessing(false); pInstance.setIsProcessing(false);
pInstance.save(); pInstance.save();
// //
@ -264,13 +266,22 @@ public class MProcess extends X_AD_Process
// Java Class // Java Class
String Classname = getClassname(); String Classname = getClassname();
if (Classname != null && Classname.length() > 0) if (Classname != null && Classname.length() > 0)
ok = startClass(Classname, pi, trx); ok = startClass(pi, trx);
else else
{ {
String msg = "No Classname for " + getName(); // PL/SQL Procedure
String ProcedureName = getProcedureName();
if (ProcedureName != null && ProcedureName.length() > 0)
{
ok = startProcess (ProcedureName, pi);
}
else
{
String msg = "No Classname or ProcedureName for " + getName();
pi.setSummary(msg, ok); pi.setSummary(msg, ok);
log.warning(msg); log.warning(msg);
} }
}
return ok; return ok;
} // process } // process
@ -292,28 +303,13 @@ public class MProcess extends X_AD_Process
* see ProcessCtl.startProcess * see ProcessCtl.startProcess
* @return true if success * @return true if success
*/ */
private boolean startProcess (String ProcedureName, MPInstance pInstance) private boolean startProcess (String ProcedureName, ProcessInfo processInfo)
{ {
int AD_PInstance_ID = pInstance.getAD_PInstance_ID(); int AD_PInstance_ID = processInfo.getAD_PInstance_ID();
// execute on this thread/connection // execute on this thread/connection
log.info(ProcedureName + "(" + AD_PInstance_ID + ")"); log.info(ProcedureName + "(" + AD_PInstance_ID + ")");
String sql = "{call " + ProcedureName + "(?)}";
try return ProcessUtil.startDatabaseProcedure(processInfo, ProcedureName, null);
{
CallableStatement cstmt = DB.prepareCall(sql); // ro??
cstmt.setInt(1, AD_PInstance_ID);
cstmt.executeUpdate();
cstmt.close();
}
catch (Exception e)
{
log.log(Level.SEVERE, sql, e);
pInstance.setResult(MPInstance.RESULT_ERROR);
pInstance.setErrorMsg(e.getLocalizedMessage());
return false;
}
pInstance.setResult(MPInstance.RESULT_OK);
return true;
} // startProcess } // startProcess
@ -330,27 +326,11 @@ public class MProcess extends X_AD_Process
* @return true if success * @return true if success
* see ProcessCtl.startClass * see ProcessCtl.startClass
*/ */
private boolean startClass (String Classname, ProcessInfo pi, Trx trx) private boolean startClass (ProcessInfo pi, Trx trx)
{ {
log.info(Classname + "(" + pi + ")"); log.info(pi.getClassName());
boolean retValue = false;
ProcessCall myObject = null; return ProcessUtil.startJavaProcess(pi, trx);
try
{
Class myClass = Class.forName(Classname);
myObject = (ProcessCall)myClass.newInstance();
if (myObject == null)
retValue = false;
else
retValue = myObject.startProcess(getCtx(), pi, trx);
}
catch (Exception e)
{
pi.setSummary("Error Start Class " + Classname, true);
log.log(Level.SEVERE, Classname, e);
throw new RuntimeException(e);
}
return retValue;
} // startClass } // startClass