From b2fc7de3c189c69738b541f02e130c45a16a70bb Mon Sep 17 00:00:00 2001 From: Heng Sin Low Date: Wed, 6 Jun 2007 01:04:58 +0000 Subject: [PATCH] [ 1731053 ] Refactoring of ProcessCtl and ServerBean --- base/src/org/adempiere/util/ProcessUtil.java | 128 +++++++++++++++++++ base/src/org/compiere/model/MProcess.java | 74 ++++------- 2 files changed, 155 insertions(+), 47 deletions(-) create mode 100644 base/src/org/adempiere/util/ProcessUtil.java diff --git a/base/src/org/adempiere/util/ProcessUtil.java b/base/src/org/adempiere/util/ProcessUtil.java new file mode 100644 index 0000000000..ae1e370d77 --- /dev/null +++ b/base/src/org/adempiere/util/ProcessUtil.java @@ -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; + } +} diff --git a/base/src/org/compiere/model/MProcess.java b/base/src/org/compiere/model/MProcess.java index 19aabce283..fab43a140f 100644 --- a/base/src/org/compiere/model/MProcess.java +++ b/base/src/org/compiere/model/MProcess.java @@ -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