MRMA, MRMALine - 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:28:32 +00:00
parent c01fc5e457
commit 29832d9081
2 changed files with 35 additions and 82 deletions

View File

@ -18,9 +18,9 @@ package org.compiere.model;
import java.io.File; import java.io.File;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List;
import java.util.Properties; import java.util.Properties;
import java.util.logging.Level; import java.util.logging.Level;
@ -90,39 +90,15 @@ public class MRMA extends X_M_RMA implements DocAction
*/ */
public MRMALine[] getLines (boolean requery) public MRMALine[] getLines (boolean requery)
{ {
if (m_lines != null && !requery) { if (m_lines != null && !requery)
{
set_TrxName(m_lines, get_TrxName()); set_TrxName(m_lines, get_TrxName());
return m_lines; return m_lines;
} }
ArrayList<MRMALine> list = new ArrayList<MRMALine>(); List<MRMALine> list = new Query(getCtx(), MRMALine.Table_Name, "M_RMA_ID=?", get_TrxName())
.setParameters(new Object[]{getM_RMA_ID()})
String sql = "SELECT * FROM M_RMALine WHERE M_RMA_ID=?"; .setOrderBy(MRMALine.COLUMNNAME_Line)
PreparedStatement pstmt = null; .list();
try
{
pstmt = DB.prepareStatement (sql, get_TrxName());
pstmt.setInt (1, getM_RMA_ID());
ResultSet rs = pstmt.executeQuery ();
while (rs.next ())
list.add (new MRMALine(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;
}
m_lines = new MRMALine[list.size ()]; m_lines = new MRMALine[list.size ()];
list.toArray (m_lines); list.toArray (m_lines);
@ -175,7 +151,7 @@ public class MRMA extends X_M_RMA implements DocAction
else else
{ {
String sqlStmt = "SELECT C_Invoice_ID FROM C_Invoice WHERE C_Order_ID=?"; String sqlStmt = "SELECT C_Invoice_ID FROM C_Invoice WHERE C_Order_ID=?";
invId = DB.getSQLValue(null, sqlStmt, shipment.getC_Order_ID()); invId = DB.getSQLValueEx(null, sqlStmt, shipment.getC_Order_ID());
} }
if (invId <= 0) if (invId <= 0)
@ -491,7 +467,7 @@ public class MRMA extends X_M_RMA implements DocAction
// //
counter.setAD_Org_ID(counterAD_Org_ID); counter.setAD_Org_ID(counterAD_Org_ID);
counter.setC_BPartner_ID(counterC_BPartner_ID); counter.setC_BPartner_ID(counterC_BPartner_ID);
counter.save(get_TrxName()); counter.saveEx(get_TrxName());
// Update copied lines // Update copied lines
MRMALine[] counterLines = counter.getLines(true); MRMALine[] counterLines = counter.getLines(true);
@ -500,7 +476,7 @@ public class MRMA extends X_M_RMA implements DocAction
MRMALine counterLine = counterLines[i]; MRMALine counterLine = counterLines[i];
counterLine.setClientOrg(counter); counterLine.setClientOrg(counter);
// //
counterLine.save(get_TrxName()); counterLine.saveEx(get_TrxName());
} }
log.fine(counter.toString()); log.fine(counter.toString());
@ -512,7 +488,7 @@ public class MRMA extends X_M_RMA implements DocAction
{ {
counter.setDocAction(counterDT.getDocAction()); counter.setDocAction(counterDT.getDocAction());
counter.processIt(counterDT.getDocAction()); counter.processIt(counterDT.getDocAction());
counter.save(get_TrxName()); counter.saveEx(get_TrxName());
} }
} }
return counter; return counter;
@ -563,12 +539,11 @@ public class MRMA extends X_M_RMA implements DocAction
if (peer.getRef_InOut_ID() != 0) if (peer.getRef_InOut_ID() != 0)
to.setInOut_ID(peer.getRef_InOut_ID()); to.setInOut_ID(peer.getRef_InOut_ID());
} }
to.set_Value("Ref_RMA_ID", from.getM_RMA_ID()); to.setRef_RMA_ID(from.getM_RMA_ID());
if (!to.save(trxName)) to.saveEx(trxName);
throw new IllegalStateException("Could not create RMA");
if (counter) if (counter)
from.set_Value("Ref_RMA_ID", to.getM_RMA_ID()); from.setRef_RMA_ID(to.getM_RMA_ID());
if (to.copyLinesFrom(from, counter) == 0) if (to.copyLinesFrom(from, counter) == 0)
throw new IllegalStateException("Could not create RMA Lines"); throw new IllegalStateException("Could not create RMA Lines");
@ -599,10 +574,10 @@ public class MRMA extends X_M_RMA implements DocAction
else else
PO.copyValues(fromLine, line, fromLine.getAD_Client_ID(), fromLine.getAD_Org_ID()); PO.copyValues(fromLine, line, fromLine.getAD_Client_ID(), fromLine.getAD_Org_ID());
line.setM_RMA_ID(getM_RMA_ID()); line.setM_RMA_ID(getM_RMA_ID());
line.set_ValueNoCheck ("M_RMALine_ID", I_ZERO); // new line.set_ValueNoCheck (MRMALine.COLUMNNAME_M_RMALine_ID, I_ZERO); // new
if (counter) if (counter)
{ {
line.set_Value("Ref_RMALine_ID", fromLine.getM_RMALine_ID()); line.setRef_RMALine_ID(fromLine.getM_RMALine_ID());
if (fromLine.getM_InOutLine_ID() != 0) if (fromLine.getM_InOutLine_ID() != 0)
{ {
MInOutLine peer = new MInOutLine (getCtx(), fromLine.getM_InOutLine_ID(), get_TrxName()); MInOutLine peer = new MInOutLine (getCtx(), fromLine.getM_InOutLine_ID(), get_TrxName());
@ -617,8 +592,8 @@ public class MRMA extends X_M_RMA implements DocAction
// Cross Link // Cross Link
if (counter) if (counter)
{ {
fromLine.set_Value("Ref_RMALine_ID", line.getM_RMALine_ID()); fromLine.setRef_RMALine_ID(line.getM_RMALine_ID());
fromLine.save(get_TrxName()); fromLine.saveEx(get_TrxName());
} }
} }
if (fromLines.length != count) if (fromLines.length != count)
@ -645,11 +620,7 @@ public class MRMA extends X_M_RMA implements DocAction
rmaLine.addDescription(Msg.getMsg(getCtx(), "Voided") + " (" + rmaLine.getQty() + ")"); rmaLine.addDescription(Msg.getMsg(getCtx(), "Voided") + " (" + rmaLine.getQty() + ")");
rmaLine.setQty(Env.ZERO); rmaLine.setQty(Env.ZERO);
rmaLine.setAmt(Env.ZERO); rmaLine.setAmt(Env.ZERO);
rmaLine.saveEx();
if (!rmaLine.save())
{
m_processMsg = "Could not update line";
}
} }
addDescription(Msg.getMsg(getCtx(), "Voided")); addDescription(Msg.getMsg(getCtx(), "Voided"));
@ -753,12 +724,11 @@ public class MRMA extends X_M_RMA implements DocAction
public void setProcessed (boolean processed) public void setProcessed (boolean processed)
{ {
super.setProcessed (processed); super.setProcessed (processed);
if (get_ID() == 0) if (get_ID() <= 0)
return; return;
String set = "SET Processed='" int noLine = DB.executeUpdateEx("UPDATE M_RMALine SET Processed=? WHERE M_RMA_ID=?",
+ (processed ? "Y" : "N") new Object[]{processed, get_ID()},
+ "' WHERE M_RMA_ID=" + getM_RMA_ID(); get_TrxName());
int noLine = DB.executeUpdate("UPDATE M_RMALine " + set, get_TrxName());
m_lines = null; m_lines = null;
log.fine("setProcessed - " + processed + " - Lines=" + noLine); log.fine("setProcessed - " + processed + " - Lines=" + noLine);
} // setProcessed } // setProcessed

View File

@ -90,7 +90,7 @@ public class MRMALine extends X_M_RMALine
+ "AND IsTaxExempt='Y' AND ValidFrom < SYSDATE ORDER BY IsDefault DESC"; + "AND IsTaxExempt='Y' AND ValidFrom < SYSDATE ORDER BY IsDefault DESC";
// Set tax for charge as exempt // Set tax for charge as exempt
taxId = DB.getSQLValue(null, sql, Env.getAD_Client_ID(getCtx())); taxId = DB.getSQLValueEx(null, sql, Env.getAD_Client_ID(getCtx()));
m_ioLine = null; m_ioLine = null;
} }
else else
@ -124,7 +124,7 @@ public class MRMALine extends X_M_RMALine
} }
else if (getC_Charge_ID() != 0) else if (getC_Charge_ID() != 0)
{ {
MCharge charge = new MCharge(this.getCtx(), getC_Charge_ID(), null); MCharge charge = MCharge.get(this.getCtx(), getC_Charge_ID());
unitAmount = charge.getChargeAmt(); unitAmount = charge.getChargeAmt();
} }
} }
@ -167,18 +167,10 @@ public class MRMALine extends X_M_RMALine
*/ */
private int getInvoiceLineId() private int getInvoiceLineId()
{ {
String whereClause = "M_InOutLine_ID=" + getM_InOutLine_ID(); int invoiceLine_ID = new Query(getCtx(), MInvoiceLine.Table_Name, "M_InOutLine_ID=?", get_TrxName())
.setParameters(new Object[]{getM_InOutLine_ID()})
int invoiceLineIds[] = MInvoiceLine.getAllIDs(MInvoiceLine.Table_Name, whereClause, null); .firstIdOnly();
return invoiceLine_ID <= 0 ? 0 : invoiceLine_ID;
if (invoiceLineIds.length == 0)
{
return 0;
}
else
{
return invoiceLineIds[0];
}
} }
/** /**
@ -223,11 +215,7 @@ public class MRMALine extends X_M_RMALine
return (getInvoiceLineId() != 0); return (getInvoiceLineId() != 0);
} }
/** @Override
* Before Save
* @param newRecord new
* @return save
*/
protected boolean beforeSave(boolean newRecord) protected boolean beforeSave(boolean newRecord)
{ {
if (newRecord && getParent().isComplete()) { if (newRecord && getParent().isComplete()) {
@ -272,9 +260,9 @@ public class MRMALine extends X_M_RMALine
// Set default amount for charge and qty // Set default amount for charge and qty
if (this.getC_Charge_ID() != 0 && this.getQty().doubleValue() <= 0) if (this.getC_Charge_ID() != 0 && this.getQty().doubleValue() <= 0)
{ {
if (Env.ZERO.compareTo(getQty()) == 0) if (getQty().signum() == 0)
this.setQty(new BigDecimal(1)); this.setQty(Env.ONE);
if (Env.ZERO.compareTo(getAmt()) == 0) if (getAmt().signum() == 0)
this.setAmt(getUnitAmt()); this.setAmt(getUnitAmt());
} }
@ -283,7 +271,7 @@ public class MRMALine extends X_M_RMALine
{ {
this.setAmt(getUnitAmt()); this.setAmt(getUnitAmt());
if (newRecord && Env.ZERO.compareTo(getQty()) == 0) if (newRecord && getQty().signum() == 0)
{ {
this.setQty(originalQty); this.setQty(originalQty);
} }
@ -294,12 +282,7 @@ public class MRMALine extends X_M_RMALine
return true; return true;
} }
/** @Override
* After Save
* @param newRecord new
* @param success success
* @return true if can be saved
*/
protected boolean afterSave(boolean newRecord, boolean success) protected boolean afterSave(boolean newRecord, boolean success)
{ {
if (!success) if (!success)