IDEMPIERE-4678 Save log informing Notice created when background job (#559)

* IDEMPIERE-4678 Save log informing Notice created when background job

* Added also a refactor for the INSERT INTO AD_PInstance_Log to use safer binding variables and add method saveEx

* - Implement suggestions from @hengsin
- Add some additional improvements
This commit is contained in:
Carlos Ruiz 2021-01-29 01:57:15 +01:00 committed by GitHub
parent 9c1c4a54a8
commit fd3fef9e7b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 107 additions and 76 deletions

View File

@ -50,10 +50,10 @@ import org.osgi.service.event.Event;
*/
public class MPInstance extends X_AD_PInstance
{
/**
/**
*
*/
private static final long serialVersionUID = 558778359873793799L;
private static final long serialVersionUID = 3756494717528301224L;
public static final String ON_RUNNING_JOB_CHANGED_TOPIC = "onRunningJobChanged";
@ -229,16 +229,30 @@ public class MPInstance extends X_AD_PInstance
* @param P_Number number
* @param P_Msg msg
*/
public void addLog (Timestamp P_Date, int P_ID, BigDecimal P_Number, String P_Msg)
public MPInstanceLog addLog (Timestamp P_Date, int P_ID, BigDecimal P_Number, String P_Msg)
{
return addLog(P_Date, P_ID, P_Number, P_Msg, 0, 0);
} // addLog
/**
* @param P_Date date
* @param P_ID id
* @param P_Number number
* @param P_Msg msg
* @param AD_Table_ID tableID
* @param Record_ID recordID
* @return
*/
public MPInstanceLog addLog (Timestamp P_Date, int P_ID, BigDecimal P_Number, String P_Msg, int AD_Table_ID, int Record_ID)
{
MPInstanceLog logEntry = new MPInstanceLog (getAD_PInstance_ID(), m_log.size()+1,
P_Date, P_ID, P_Number, P_Msg);
P_Date, P_ID, P_Number, P_Msg, AD_Table_ID, Record_ID);
m_log.add(logEntry);
// save it to DB ?
// log.saveEx();
return logEntry;
} // addLog
/**
* Set AD_Process_ID.
* Check Role if process can be performed

View File

@ -22,6 +22,7 @@ import java.sql.SQLException;
import java.sql.Timestamp;
import org.compiere.util.DB;
import org.compiere.util.Env;
/**
* Process Instance Log Model.
@ -33,7 +34,7 @@ import org.compiere.util.DB;
public class MPInstanceLog
{
/**
* Full Constructor
* Constructor without Table/Record
* @param AD_PInstance_ID instance
* @param Log_ID log sequence
* @param P_Date date
@ -43,6 +44,23 @@ public class MPInstanceLog
*/
public MPInstanceLog (int AD_PInstance_ID, int Log_ID, Timestamp P_Date,
int P_ID, BigDecimal P_Number, String P_Msg)
{
this(AD_PInstance_ID, Log_ID, P_Date, P_ID, P_Number, P_Msg, 0, 0);
} // MPInstance_Log
/**
* Full Constructor
* @param AD_PInstance_ID
* @param Log_ID
* @param P_Date
* @param P_ID
* @param P_Number
* @param P_Msg
* @param AD_Table_ID
* @param Record_ID
*/
public MPInstanceLog (int AD_PInstance_ID, int Log_ID, Timestamp P_Date,
int P_ID, BigDecimal P_Number, String P_Msg, int AD_Table_ID, int Record_ID)
{
setAD_PInstance_ID(AD_PInstance_ID);
setLog_ID(Log_ID);
@ -50,6 +68,8 @@ public class MPInstanceLog
setP_ID(P_ID);
setP_Number(P_Number);
setP_Msg(P_Msg);
setAD_Table_ID(AD_Table_ID);
setRecord_ID(Record_ID);
} // MPInstance_Log
/**
@ -74,6 +94,8 @@ public class MPInstanceLog
private int m_P_ID;
private BigDecimal m_P_Number;
private String m_P_Msg;
private int m_AD_Table_ID;
private int m_Record_ID;
/**
@ -97,54 +119,53 @@ public class MPInstanceLog
} // toString
private final static String insertSql = "INSERT INTO AD_PInstance_Log "
+ "(AD_PInstance_ID, Log_ID, P_Date, P_ID, P_Number, P_Msg, AD_Table_ID, Record_ID)"
+ " VALUES (?,?,?,?,?,?,?,?)";
/**
* Save to Database
* @return true if saved
*/
public boolean save ()
{
StringBuilder sql = new StringBuilder("INSERT INTO AD_PInstance_Log "
+ "(AD_PInstance_ID, Log_ID, P_Date, P_ID, P_Number, P_Msg)"
+ " VALUES (");
sql.append(m_AD_PInstance_ID).append(",")
.append(m_Log_ID).append(",");
if (m_P_Date == null)
{
sql.append("NULL,");
}
else
{
sql.append(DB.TO_DATE(m_P_Date, false)).append(",");
}
if (m_P_ID == 0)
{
sql.append("NULL,");
}
else
{
sql.append(m_P_ID).append(",");
}
if (m_P_Number == null)
{
sql.append("NULL,");
}
else
{
sql.append(m_P_Number).append(",");
}
if (m_P_Msg == null)
{
sql.append("NULL)");
}
else
{
sql.append(DB.TO_STRING(m_P_Msg, 2000)).append(")");
//
}
int no = DB.executeUpdate(sql.toString(), null); // outside of trx
int no = DB.executeUpdate(insertSql, getInsertParams(), false, null); // outside of trx
return no == 1;
} // save
/**
* Save to Database throwing Exception
*/
public void saveEx ()
{
DB.executeUpdateEx(insertSql, getInsertParams(), null); // outside of trx
} // saveEx
private Object[] getInsertParams() {
MColumn colMsg = MColumn.get(Env.getCtx(), I_AD_PInstance_Log.Table_Name, I_AD_PInstance_Log.COLUMNNAME_P_Msg);
int maxMsgLength = colMsg.getFieldLength();
Object[] params = new Object[8];
params[0] = m_AD_PInstance_ID;
params[1] = m_Log_ID;
if (m_P_Date != null)
params[2] = m_P_Date;
if (m_P_ID != 0)
params[3] = m_P_ID;
if (m_P_Number != null)
params[4] = m_P_Number;
if (m_P_Msg != null) {
if (m_P_Msg.length() > maxMsgLength)
params[5] = m_P_Msg.substring(0, maxMsgLength);
else
params[5] = m_P_Msg;
}
if (m_AD_Table_ID != 0)
params[6] = m_AD_Table_ID;
if (m_Record_ID != 0)
params[7] = m_Record_ID;
return params;
}
/**
* Get AD_PInstance_ID
* @return Instance id
@ -253,4 +274,22 @@ public class MPInstanceLog
m_P_Msg = P_Msg;
}
/**
* Set Table ID
* @param tableId
*/
public void setAD_Table_ID(int tableId)
{
m_AD_Table_ID = tableId;
}
/**
* Set Record ID
* @param recordId
*/
public void setRecord_ID(int recordId)
{
m_Record_ID = recordId;
}
} // MPInstance_Log

View File

@ -22,6 +22,7 @@ import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import org.compiere.model.MPInstanceLog;
import org.compiere.util.CLogger;
import org.compiere.util.DB;
import org.compiere.util.Env;
@ -165,37 +166,10 @@ public class ProcessInfoUtil
}
for (int i = 0; i < logs.length; i++)
{
StringBuilder sql = new StringBuilder ("INSERT INTO AD_PInstance_Log "
+ "(AD_PInstance_ID, Log_ID, P_Date, P_ID, P_Number, P_Msg, AD_Table_ID,Record_ID)"
+ " VALUES (");
sql.append(pi.getAD_PInstance_ID()).append(",")
.append(logs[i].getLog_ID()).append(",");
if (logs[i].getP_Date() == null)
sql.append("NULL,");
else
sql.append(DB.TO_DATE(logs[i].getP_Date(), false)).append(",");
if (logs[i].getP_ID() == 0)
sql.append("NULL,");
else
sql.append(logs[i].getP_ID()).append(",");
if (logs[i].getP_Number() == null)
sql.append("NULL,");
else
sql.append(logs[i].getP_Number()).append(",");
if (logs[i].getP_Msg() == null)
sql.append("NULL,");
else
sql.append(DB.TO_STRING(logs[i].getP_Msg(),2000)).append(",");
if (logs[i].getAD_Table_ID() == 0)
sql.append("NULL,");
else
sql.append(logs[i].getAD_Table_ID()).append(",");
if (logs[i].getRecord_ID() == 0)
sql.append("NULL)");
else
sql.append(logs[i].getRecord_ID()).append(")");
//
DB.executeUpdate(sql.toString(), null);
MPInstanceLog il = new MPInstanceLog(pi.getAD_PInstance_ID(), logs[i].getLog_ID(), logs[i].getP_Date(),
logs[i].getP_ID(), logs[i].getP_Number(), logs[i].getP_Msg(),
logs[i].getAD_Table_ID(), logs[i].getRecord_ID());
il.save();
}
pi.setLogList(null); // otherwise log entries are twice
} // saveLogToDB

View File

@ -66,6 +66,7 @@ import org.compiere.model.MLookupFactory;
import org.compiere.model.MLookupInfo;
import org.compiere.model.MNote;
import org.compiere.model.MPInstance;
import org.compiere.model.MPInstanceLog;
import org.compiere.model.MPInstancePara;
import org.compiere.model.MProcess;
import org.compiere.model.MRole;
@ -1260,6 +1261,9 @@ public abstract class AbstractProcessDialog extends Window implements IProcessUI
}
if (attachment != null)
attachment.saveEx();
MPInstanceLog il = instance.addLog(null, 0, null, Msg.parseTranslation(m_ctx, "@Created@ @AD_Note_ID@ " + note.getAD_Note_ID()),
MNote.Table_ID, note.getAD_Note_ID());
il.saveEx();
}
} catch (Exception e) {
log.log(Level.SEVERE, e.getLocalizedMessage());