📄 svrprocess.java
字号:
/******************************************************************************
* The contents of this file are subject to the Compiere License Version 1.1
* ("License"); You may not use this file except in compliance with the License
* You may obtain a copy of the License at http://www.compiere.org/license.html
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
* The Original Code is Compiere ERP & CRM Business Solution
* The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
* Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke, parts
* created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
* Contributor(s): ______________________________________.
*****************************************************************************/
package org.compiere.process;
import java.sql.*;
import java.util.*;
import java.math.*;
import org.apache.log4j.Logger;
import org.compiere.util.*;
/**
* Server Process Template
*
* @author Jorg Janke
* @version $Id: SvrProcess.java,v 1.5 2003/04/24 06:14:17 jjanke Exp $
*/
public abstract class SvrProcess implements ProcessCall
{
/**
* Server Process
*/
public SvrProcess()
{
} // SvrProcess
private Properties m_ctx;
private ProcessInfo m_pi;
/** Logger */
protected Logger log = Logger.getLogger (getClass());
/** Optional User_ID */
private Integer m_AD_User_ID;
/** Message */
private String m_msg = "";
/** Array of PLog */
private ArrayList m_log = new ArrayList();
/** Common Error Message */
protected static String MSG_SaveErrorRowNotFound = "@SaveErrorRowNotFound@";
protected static String MSG_InvalidArguments = "@InvalidArguments@";
/**
* Start the process.
* Calls the abstract methods <code>process</code>.
* It should only return false, if the function could not be performed
* as this causes the process to abort.
*
* @param ctx Context
* @param pi Process Info
* @return true if the next process should be performed
*/
public final boolean startProcess (Properties ctx, ProcessInfo pi)
{
// Preparation
m_ctx = ctx == null ? Env.getCtx() : ctx;
m_pi = pi;
lock();
//
return unlock (process());
} // startProcess
/*************************************************************************/
/**
* Process - called when pressing the button ... in ...
* @return true if success an the next process should be started
*/
protected boolean process()
{
prepare();
try
{
m_msg = doIt();
}
catch (Exception e)
{
m_msg = e.getMessage();
if (m_msg == null)
m_msg = e.toString();
if (e.getCause() != null)
log.error("process - " + m_msg, e.getCause());
else
log.error("process - " + m_msg);
setLog();
return false;
}
setLog();
return true;
} // process
/**
* Prepare - e.g., get Parameters.
*/
abstract protected void prepare();
/**
* Perrform process.
* @return Message
* @throws Exception if not successful
*/
abstract protected String doIt() throws Exception;
/**
* Get Return Message.
* @return Message to be returned
*/
protected String getReturnMessage()
{
return m_msg;
} // getReturnMessage
/*************************************************************************/
/**
* Get Process Info
* @return Process Info
*/
protected ProcessInfo getProcessInfo()
{
return m_pi;
} // getProcessInfo
/**
* Get Properties
* @return Properties
*/
protected Properties getCtx()
{
return m_ctx;
} // getCtx
/**
* Get Name
* @return Name
*/
protected String getName()
{
return m_pi.Title;
} // getName
/**
* Get Process Instance
* @return Process Instance
*/
protected int getAD_PInstance_ID()
{
return m_pi.AD_PInstance_ID;
} // getAD_PInstance_ID
/**
* Get Record_ID
* @return Record_ID
*/
protected int getRecord_ID()
{
return m_pi.Record_ID;
} // getRecord_ID
/**
* Get AD_User_ID
* @return AD_User_ID of Process owner
*/
protected int getAD_User_ID()
{
if (m_AD_User_ID == null)
{
String sql = "SELECT AD_User_ID FROM AD_PInstance WHERE AD_PInstance_ID=?";
try
{
PreparedStatement pstmt = DB.prepareStatement(sql);
pstmt.setInt(1, m_pi.AD_PInstance_ID);
ResultSet rs = pstmt.executeQuery();
if (rs.next())
m_AD_User_ID = new Integer(rs.getInt(1));
rs.close();
pstmt.close();
}
catch (SQLException e)
{
log.error("getAD_User_ID", e);
}
}
if (m_AD_User_ID == null)
return 0;
return m_AD_User_ID.intValue();
} // getAD_User_ID
/*************************************************************************/
/**
* Get Parameter of Process
* @return Array of Parameters
*/
protected Parameter[] getParameter()
{
ArrayList list = new ArrayList();
String sql = "SELECT ParameterName," // 1
+ " P_String,P_String_To, P_Number,P_Number_To," // 2/3 4/5
+ " P_Date,P_Date_To, Info, Info_To " // 6/7 8/9
+ "FROM AD_PInstance_Para WHERE AD_PInstance_ID=? "
+ "ORDER BY SeqNo";
try
{
PreparedStatement pstmt = DB.prepareStatement(sql);
pstmt.setInt(1, m_pi.AD_PInstance_ID);
ResultSet rs = pstmt.executeQuery();
while (rs.next())
{
String ParameterName = rs.getString(1);
Object Parameter = rs.getString(2);
Object Parameter_To = rs.getString(3);
if (Parameter == null)
{
Parameter = rs.getBigDecimal(4);
Parameter_To = rs.getBigDecimal(5);
}
if (Parameter == null)
{
Parameter = rs.getTimestamp(6);
Parameter_To = rs.getTimestamp(7);
}
String Info = rs.getString(8);
String Info_To = rs.getString(9);
//
list.add (new Parameter(ParameterName, Parameter, Parameter_To, Info, Info_To));
}
rs.close();
pstmt.close();
}
catch (SQLException e)
{
log.error("getParameter", e);
}
//
Parameter[] retValue = new Parameter[list.size()];
list.toArray(retValue);
return retValue;
} // getParameter
/**
* Process Parameter (ValueObject)
*/
public class Parameter
{
/**
* Construct Parameter
* @param parameterName parameter name
* @param parameter parameter
* @param parameter_To to parameter
* @param info info
* @param info_To to info
*/
public Parameter (String parameterName, Object parameter, Object parameter_To, String info, String info_To)
{
ParameterName = parameterName;
Parameter = parameter;
Parameter_To = parameter_To;
Info = info;
if (Info == null)
Info = "";
Info_To = info_To;
if (Info_To == null)
Info_To = "";
log.debug(ParameterName + " = "
+ Parameter + "(" + Info + ") - " + Parameter_To + "(" + Info_To + ")");
} // Parameter
public String ParameterName;
public Object Parameter;
public Object Parameter_To;
public String Info;
public String Info_To;
} // Parameter
/*************************************************************************/
/**
* Add Log Entry
* @param date date or null
* @param id record id or 0
* @param number number or null
* @param msg message or null
*/
public void addLog (Timestamp date, int id, BigDecimal number, String msg)
{
addLog (new PLog (date, id, number, msg));
} // addLog
/**
* Add Log Entry
* @param log Process Log entry
*/
public void addLog (PLog log)
{
if (log != null)
m_log.add(log);
} // addLog
/**
* Write Process Log
*/
private void setLog ()
{
m_pi.Summary = m_msg;
for (int i = 0; i < m_log.size(); i++)
{
PLog log = (PLog)m_log.get(i);
StringBuffer sql = new StringBuffer ("INSERT INTO AD_PInstance_Log "
+ "(AD_PInstance_ID, Log_ID, P_Date, P_ID, P_Number, P_Msg)"
+ " VALUES (");
sql.append(m_pi.AD_PInstance_ID).append(",")
.append(log.Log_ID).append(",");
if (log.Date == null)
sql.append("NULL,");
else
sql.append(DB.TO_DATE(log.Date, false)).append(",");
if (log.ID == 0)
sql.append("NULL,");
else
sql.append(log.ID).append(",");
if (log.Number == null)
sql.append("NULL,");
else
sql.append(log.Number).append(",");
if (log.Msg == null)
sql.append("NULL)");
else
sql.append(DB.TO_STRING(log.Msg,2000)).append(")");
//
DB.executeUpdate(sql.toString());
}
} // setLog
/** Used for Log Entries */
private static int s_logID = 0;
/**
* Process Log (Value Class)
*/
public class PLog
{
/**
* Log Entry
* @param date date or null
* @param id record id or 0
* @param number number or null
* @param msg message or null
*/
public PLog (Timestamp date, int id, BigDecimal number, String msg)
{
this (s_logID++, date, id, number, msg);
} // Log
/**
* Log Entry
* @param log_ID sequential log id
* @param date date or null
* @param id record id or 0
* @param number number or null
* @param msg message or null
*/
private PLog (int log_ID, Timestamp date, int id, BigDecimal number, String msg)
{
Log_ID = log_ID;
Date = date;
ID = id;
Number = number;
Msg = msg;
} // Log
public int Log_ID;
public Timestamp Date;
public int ID;
public BigDecimal Number;
public String Msg;
} // PLog
/*************************************************************************/
/**
* Lock Process Instance
*/
private void lock()
{
log.info("lock");
DB.executeUpdate("UPDATE AD_PInstance SET IsProcessing='Y' WHERE AD_PInstance_ID=" + m_pi.AD_PInstance_ID);
} // lock
/**
* Unlock Process Instance.
* Update Process Instance and write option return message
* @param success true if success
* @return success
*/
private boolean unlock (boolean success)
{
StringBuffer sql = new StringBuffer("UPDATE AD_PInstance SET IsProcessing='N',Updated=SysDate,Result=");
sql.append(success ? "1" : "0");
String msg = getReturnMessage();
if (msg != null && msg.length() > 0)
sql.append(", ErrorMsg=").append(DB.TO_STRING(msg, 2000));
sql.append(" WHERE AD_PInstance_ID=").append(m_pi.AD_PInstance_ID);
DB.executeUpdate(sql.toString());
log.info("unlock - Success=" + success + " - " + msg);
return success;
} // unlock
} // SvrProcess
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -