[ 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.util.*;
import java.util.logging.*;
import org.adempiere.util.ProcessUtil;
import org.compiere.process.*;
import org.compiere.util.*;
@ -229,13 +231,13 @@ public class MProcess extends X_AD_Process
boolean ok = true;
// PL/SQL Procedure
String ProcedureName = getProcedureName();
if (ProcedureName != null && ProcedureName.length() > 0)
ok = startProcess (ProcedureName, pInstance);
ProcessInfo processInfo = new ProcessInfo("", this.getAD_Process_ID());
processInfo.setAD_PInstance_ID(pInstance.getAD_PInstance_ID());
ok = processIt(processInfo, trx);
// Unlock
pInstance.setResult(ok ? MPInstance.RESULT_OK : MPInstance.RESULT_ERROR);
pInstance.setErrorMsg(processInfo.getSummary());
pInstance.setIsProcessing(false);
pInstance.save();
//
@ -264,12 +266,21 @@ public class MProcess extends X_AD_Process
// Java Class
String Classname = getClassname();
if (Classname != null && Classname.length() > 0)
ok = startClass(Classname, pi, trx);
ok = startClass(pi, trx);
else
{
String msg = "No Classname for " + getName();
pi.setSummary(msg, ok);
log.warning(msg);
// 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);
log.warning(msg);
}
}
return ok;
@ -292,28 +303,13 @@ public class MProcess extends X_AD_Process
* see ProcessCtl.startProcess
* @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
log.info(ProcedureName + "(" + AD_PInstance_ID + ")");
String sql = "{call " + ProcedureName + "(?)}";
try
{
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;
return ProcessUtil.startDatabaseProcedure(processInfo, ProcedureName, null);
} // startProcess
@ -330,27 +326,11 @@ public class MProcess extends X_AD_Process
* @return true if success
* see ProcessCtl.startClass
*/
private boolean startClass (String Classname, ProcessInfo pi, Trx trx)
private boolean startClass (ProcessInfo pi, Trx trx)
{
log.info(Classname + "(" + pi + ")");
boolean retValue = false;
ProcessCall myObject = null;
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;
log.info(pi.getClassName());
return ProcessUtil.startJavaProcess(pi, trx);
} // startClass