MScheduler - apply best practices:

BF [ 2791635 ] Use saveEx whenever is possible
https://sourceforge.net/tracker/?func=detail&atid=879335&aid=2791635&group_id=176962

BF [ 1874419 ] JDBC Statement  not close in a finally block
https://sourceforge.net/tracker/?func=detail&atid=879332&aid=1874419&group_id=176962
This commit is contained in:
teo_sarca 2009-07-09 15:45:21 +00:00
parent 29832d9081
commit f7067df3ab
1 changed files with 28 additions and 135 deletions

View File

@ -16,14 +16,12 @@
*****************************************************************************/ *****************************************************************************/
package org.compiere.model; package org.compiere.model;
import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.Timestamp; import java.sql.Timestamp;
import java.util.ArrayList; import java.util.List;
import java.util.Properties; import java.util.Properties;
import java.util.logging.Level; import java.util.TreeSet;
import org.compiere.util.CLogger;
import org.compiere.util.DB; import org.compiere.util.DB;
@ -49,42 +47,14 @@ public class MScheduler extends X_AD_Scheduler
*/ */
public static MScheduler[] getActive (Properties ctx) public static MScheduler[] getActive (Properties ctx)
{ {
ArrayList<MScheduler> list = new ArrayList<MScheduler>(); List<MScheduler> list = new Query(ctx, Table_Name, null, null)
String sql = "SELECT * FROM AD_Scheduler WHERE IsActive='Y'"; .setOnlyActiveRecords(true)
PreparedStatement pstmt = null; .list();
try
{
pstmt = DB.prepareStatement (sql, null);
ResultSet rs = pstmt.executeQuery ();
while (rs.next ())
list.add (new MScheduler (ctx, rs, null));
rs.close ();
pstmt.close ();
pstmt = null;
}
catch (Exception e)
{
s_log.log(Level.SEVERE, sql, e);
}
try
{
if (pstmt != null)
pstmt.close ();
pstmt = null;
}
catch (Exception e)
{
pstmt = null;
}
MScheduler[] retValue = new MScheduler[list.size ()]; MScheduler[] retValue = new MScheduler[list.size ()];
list.toArray (retValue); list.toArray (retValue);
return retValue; return retValue;
} // getActive } // getActive
/** Static Logger */
private static CLogger s_log = CLogger.getCLogger (MScheduler.class);
/** /**
* Standard Constructor * Standard Constructor
* @param ctx context * @param ctx context
@ -152,37 +122,11 @@ public class MScheduler extends X_AD_Scheduler
*/ */
public AdempiereProcessorLog[] getLogs () public AdempiereProcessorLog[] getLogs ()
{ {
ArrayList<MSchedulerLog> list = new ArrayList<MSchedulerLog>(); final String whereClause = MSchedulerLog.COLUMNNAME_AD_Scheduler_ID+"=?";
String sql = "SELECT * " List<MSchedulerLog> list = new Query(getCtx(), MSchedulerLog.Table_Name, whereClause, get_TrxName())
+ "FROM AD_SchedulerLog " .setParameters(new Object[]{getAD_Scheduler_ID()})
+ "WHERE AD_Scheduler_ID=? " .setOrderBy("Created DESC")
+ "ORDER BY Created DESC"; .list();
PreparedStatement pstmt = null;
try
{
pstmt = DB.prepareStatement (sql, get_TrxName());
pstmt.setInt (1, getAD_Scheduler_ID());
ResultSet rs = pstmt.executeQuery ();
while (rs.next ())
list.add (new MSchedulerLog (getCtx(), rs, get_TrxName()));
rs.close ();
pstmt.close ();
pstmt = null;
}
catch (Exception e)
{
log.log(Level.SEVERE, sql, e);
}
try
{
if (pstmt != null)
pstmt.close ();
pstmt = null;
}
catch (Exception e)
{
pstmt = null;
}
MSchedulerLog[] retValue = new MSchedulerLog[list.size ()]; MSchedulerLog[] retValue = new MSchedulerLog[list.size ()];
list.toArray (retValue); list.toArray (retValue);
return retValue; return retValue;
@ -199,7 +143,7 @@ public class MScheduler extends X_AD_Scheduler
String sql = "DELETE AD_SchedulerLog " String sql = "DELETE AD_SchedulerLog "
+ "WHERE AD_Scheduler_ID=" + getAD_Scheduler_ID() + "WHERE AD_Scheduler_ID=" + getAD_Scheduler_ID()
+ " AND (Created+" + getKeepLogDays() + ") < SysDate"; + " AND (Created+" + getKeepLogDays() + ") < SysDate";
int no = DB.executeUpdate(sql, get_TrxName()); int no = DB.executeUpdateEx(sql, get_TrxName());
return no; return no;
} // deleteLog } // deleteLog
@ -221,34 +165,12 @@ public class MScheduler extends X_AD_Scheduler
{ {
if (!reload && m_parameter != null) if (!reload && m_parameter != null)
return m_parameter; return m_parameter;
ArrayList<MSchedulerPara> list = new ArrayList<MSchedulerPara>(); //
String sql = "SELECT * FROM AD_Scheduler_Para WHERE AD_Scheduler_ID=? AND IsActive='Y'"; final String whereClause = MSchedulerPara.COLUMNNAME_AD_Scheduler_ID+"=?";
PreparedStatement pstmt = null; List<MSchedulerPara> list = new Query(getCtx(), MSchedulerPara.Table_Name, whereClause, get_TrxName())
try .setParameters(new Object[]{getAD_Scheduler_ID()})
{ .setOnlyActiveRecords(true)
pstmt = DB.prepareStatement (sql, null); .list();
pstmt.setInt (1, getAD_Scheduler_ID());
ResultSet rs = pstmt.executeQuery ();
while (rs.next ())
list.add (new MSchedulerPara (getCtx(), rs, null));
rs.close ();
pstmt.close ();
pstmt = null;
}
catch (Exception e)
{
log.log (Level.SEVERE, sql, e);
}
try
{
if (pstmt != null)
pstmt.close ();
pstmt = null;
}
catch (Exception e)
{
pstmt = null;
}
m_parameter = new MSchedulerPara[list.size()]; m_parameter = new MSchedulerPara[list.size()];
list.toArray(m_parameter); list.toArray(m_parameter);
return m_parameter; return m_parameter;
@ -263,34 +185,12 @@ public class MScheduler extends X_AD_Scheduler
{ {
if (!reload && m_recipients != null) if (!reload && m_recipients != null)
return m_recipients; return m_recipients;
ArrayList<MSchedulerRecipient> list = new ArrayList<MSchedulerRecipient>(); //
String sql = "SELECT * FROM AD_SchedulerRecipient WHERE AD_Scheduler_ID=? AND IsActive='Y'"; String whereClause = MSchedulerRecipient.COLUMNNAME_AD_Scheduler_ID+"=?";
PreparedStatement pstmt = null; final List<MSchedulerRecipient> list = new Query(getCtx(), MSchedulerRecipient.Table_Name, whereClause, get_TrxName())
try .setParameters(new Object[]{getAD_Scheduler_ID()})
{ .setOnlyActiveRecords(true)
pstmt = DB.prepareStatement (sql, null); .list();
pstmt.setInt (1, getAD_Scheduler_ID());
ResultSet rs = pstmt.executeQuery ();
while (rs.next ())
list.add (new MSchedulerRecipient (getCtx(), rs, null));
rs.close ();
pstmt.close ();
pstmt = null;
}
catch (Exception e)
{
log.log (Level.SEVERE, sql, e);
}
try
{
if (pstmt != null)
pstmt.close ();
pstmt = null;
}
catch (Exception e)
{
pstmt = null;
}
m_recipients = new MSchedulerRecipient[list.size()]; m_recipients = new MSchedulerRecipient[list.size()];
list.toArray(m_recipients); list.toArray(m_recipients);
return m_recipients; return m_recipients;
@ -302,7 +202,7 @@ public class MScheduler extends X_AD_Scheduler
*/ */
public Integer[] getRecipientAD_User_IDs() public Integer[] getRecipientAD_User_IDs()
{ {
ArrayList<Integer> list = new ArrayList<Integer>(); TreeSet<Integer> list = new TreeSet<Integer>();
MSchedulerRecipient[] recipients = getRecipients(false); MSchedulerRecipient[] recipients = getRecipients(false);
for (int i = 0; i < recipients.length; i++) for (int i = 0; i < recipients.length; i++)
{ {
@ -311,9 +211,7 @@ public class MScheduler extends X_AD_Scheduler
continue; continue;
if (recipient.getAD_User_ID() != 0) if (recipient.getAD_User_ID() != 0)
{ {
Integer ii = new Integer(recipient.getAD_User_ID()); list.add(recipient.getAD_User_ID());
if (!list.contains(ii))
list.add(ii);
} }
if (recipient.getAD_Role_ID() != 0) if (recipient.getAD_Role_ID() != 0)
{ {
@ -323,22 +221,17 @@ public class MScheduler extends X_AD_Scheduler
MUserRoles ur = urs[j]; MUserRoles ur = urs[j];
if (!ur.isActive()) if (!ur.isActive())
continue; continue;
Integer ii = new Integer(ur.getAD_User_ID()); list.add(ur.getAD_User_ID());
if (!list.contains(ii))
list.add(ii);
} }
} }
} }
// Add Updater // Add Updater
if (list.size() == 0) if (list.size() == 0)
{ {
Integer ii = new Integer(getUpdatedBy()); list.add(getUpdatedBy());
list.add(ii);
} }
// //
Integer[] recipientIDs = new Integer[list.size()]; return list.toArray(new Integer[list.size()]);
list.toArray(recipientIDs);
return recipientIDs;
} // getRecipientAD_User_IDs } // getRecipientAD_User_IDs
/** /**