Remove SQL code and Replace for Query

https://sourceforge.net/tracker/?func=detail&atid=879335&aid=2214883&group_id=176962

Introduce DB.getSQLValue*Ex methods
https://sourceforge.net/tracker/?func=detail&atid=879335&aid=2448461&group_id=176962

* better exception handling - use saveEx
* introduced MInvoiceTest test case for changed methods
This commit is contained in:
teo_sarca 2009-04-24 13:08:34 +00:00
parent 21c6b985f0
commit 6f51e52b7a
2 changed files with 104 additions and 175 deletions

View File

@ -20,11 +20,14 @@ import java.io.File;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp; import java.sql.Timestamp;
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;
import org.adempiere.exceptions.DBException;
import org.compiere.print.ReportEngine; import org.compiere.print.ReportEngine;
import org.compiere.process.DocAction; import org.compiere.process.DocAction;
import org.compiere.process.DocumentEngine; import org.compiere.process.DocumentEngine;
@ -65,39 +68,10 @@ public class MInvoice extends X_C_Invoice implements DocAction
*/ */
public static MInvoice[] getOfBPartner (Properties ctx, int C_BPartner_ID, String trxName) public static MInvoice[] getOfBPartner (Properties ctx, int C_BPartner_ID, String trxName)
{ {
ArrayList<MInvoice> list = new ArrayList<MInvoice>(); List<MInvoice> list = new Query(ctx, Table_Name, COLUMNNAME_C_BPartner_ID+"=?", trxName)
String sql = "SELECT * FROM C_Invoice WHERE C_BPartner_ID=?"; .setParameters(new Object[]{C_BPartner_ID})
PreparedStatement pstmt = null; .list();
try return list.toArray(new MInvoice[list.size()]);
{
pstmt = DB.prepareStatement(sql, trxName);
pstmt.setInt(1, C_BPartner_ID);
ResultSet rs = pstmt.executeQuery();
while (rs.next())
list.add(new MInvoice(ctx,rs, trxName));
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;
}
//
MInvoice[] retValue = new MInvoice[list.size()];
list.toArray(retValue);
return retValue;
} // getOfBPartner } // getOfBPartner
/** /**
@ -166,8 +140,7 @@ public class MInvoice extends X_C_Invoice implements DocAction
else else
to.setRef_Invoice_ID(0); to.setRef_Invoice_ID(0);
if (!to.save(trxName)) to.saveEx(trxName);
throw new IllegalStateException("Could not create Invoice");
if (counter) if (counter)
from.setRef_Invoice_ID(to.getC_Invoice_ID()); from.setRef_Invoice_ID(to.getC_Invoice_ID());
@ -610,47 +583,14 @@ public class MInvoice extends X_C_Invoice implements DocAction
*/ */
private MInvoiceLine[] getLines (String whereClause) private MInvoiceLine[] getLines (String whereClause)
{ {
ArrayList<MInvoiceLine> list = new ArrayList<MInvoiceLine>(); String whereClauseFinal = "C_Invoice_ID=? ";
String sql = "SELECT * FROM C_InvoiceLine WHERE C_Invoice_ID=? ";
if (whereClause != null) if (whereClause != null)
sql += whereClause; whereClauseFinal += whereClause;
sql += " ORDER BY Line"; List<MInvoiceLine> list = new Query(getCtx(), MInvoiceLine.Table_Name, whereClauseFinal, get_TrxName())
PreparedStatement pstmt = null; .setParameters(new Object[]{getC_Invoice_ID()})
try .setOrderBy(MInvoiceLine.COLUMNNAME_Line)
{ .list();
pstmt = DB.prepareStatement(sql, get_TrxName()); return list.toArray(new MInvoiceLine[list.size()]);
pstmt.setInt(1, getC_Invoice_ID());
ResultSet rs = pstmt.executeQuery();
while (rs.next())
{
MInvoiceLine il = new MInvoiceLine(getCtx(), rs, get_TrxName());
il.setInvoice(this);
list.add(il);
}
rs.close();
pstmt.close();
pstmt = null;
}
catch (Exception e)
{
log.log(Level.SEVERE, "getLines", e);
}
finally
{
try
{
if (pstmt != null)
pstmt.close ();
}
catch (Exception e)
{}
pstmt = null;
}
//
MInvoiceLine[] lines = new MInvoiceLine[list.size()];
list.toArray(lines);
return lines;
} // getLines } // getLines
/** /**
@ -688,7 +628,7 @@ public class MInvoice extends X_C_Invoice implements DocAction
{ {
MInvoiceLine line = lines[i]; MInvoiceLine line = lines[i];
line.setLine(number); line.setLine(number);
line.save(); line.saveEx();
number += step; number += step;
} }
m_lines = null; m_lines = null;
@ -798,37 +738,12 @@ public class MInvoice extends X_C_Invoice implements DocAction
{ {
if (m_taxes != null && !requery) if (m_taxes != null && !requery)
return m_taxes; return m_taxes;
String sql = "SELECT * FROM C_InvoiceTax WHERE C_Invoice_ID=?";
ArrayList<MInvoiceTax> list = new ArrayList<MInvoiceTax>();
PreparedStatement pstmt = null;
try
{
pstmt = DB.prepareStatement (sql, get_TrxName());
pstmt.setInt (1, getC_Invoice_ID());
ResultSet rs = pstmt.executeQuery ();
while (rs.next ())
list.add(new MInvoiceTax(getCtx(), rs, get_TrxName()));
rs.close ();
pstmt.close ();
pstmt = null;
}
catch (Exception e)
{
log.log(Level.SEVERE, "getTaxes", e);
}
try
{
if (pstmt != null)
pstmt.close ();
pstmt = null;
}
catch (Exception e)
{
pstmt = null;
}
m_taxes = new MInvoiceTax[list.size ()]; final String whereClause = MInvoiceTax.COLUMNNAME_C_Invoice_ID+"=?";
list.toArray (m_taxes); List<MInvoiceTax> list = new Query(getCtx(), MInvoiceTax.Table_Name, whereClause, get_TrxName())
.setParameters(new Object[]{get_ID()})
.list();
m_taxes = list.toArray(new MInvoiceTax[list.size()]);
return m_taxes; return m_taxes;
} // getTaxes } // getTaxes
@ -909,7 +824,7 @@ public class MInvoice extends X_C_Invoice implements DocAction
if (schedule[i].isValid() != valid) if (schedule[i].isValid() != valid)
{ {
schedule[i].setIsValid(valid); schedule[i].setIsValid(valid);
schedule[i].save(get_TrxName()); schedule[i].saveEx(get_TrxName());
} }
} }
return valid; return valid;
@ -1056,39 +971,13 @@ public class MInvoice extends X_C_Invoice implements DocAction
* Set Price List (and Currency) when valid * Set Price List (and Currency) when valid
* @param M_PriceList_ID price list * @param M_PriceList_ID price list
*/ */
@Override
public void setM_PriceList_ID (int M_PriceList_ID) public void setM_PriceList_ID (int M_PriceList_ID)
{ {
String sql = "SELECT M_PriceList_ID, C_Currency_ID " MPriceList pl = MPriceList.get(getCtx(), M_PriceList_ID, null);
+ "FROM M_PriceList WHERE M_PriceList_ID=?"; if (pl != null) {
PreparedStatement pstmt = null; setC_Currency_ID(pl.getC_Currency_ID());
try super.setM_PriceList_ID(M_PriceList_ID);
{
pstmt = DB.prepareStatement(sql, null);
pstmt.setInt(1, M_PriceList_ID);
ResultSet rs = pstmt.executeQuery();
if (rs.next())
{
super.setM_PriceList_ID (rs.getInt(1));
setC_Currency_ID (rs.getInt(2));
}
rs.close();
pstmt.close();
pstmt = null;
}
catch (Exception e)
{
log.log(Level.SEVERE, "setM_PriceList_ID", e);
}
finally
{
try
{
if (pstmt != null)
pstmt.close ();
}
catch (Exception e)
{}
pstmt = null;
} }
} // setM_PriceList_ID } // setM_PriceList_ID
@ -1115,14 +1004,16 @@ public class MInvoice extends X_C_Invoice implements DocAction
pstmt.setInt(1, getC_Invoice_ID()); pstmt.setInt(1, getC_Invoice_ID());
rs = pstmt.executeQuery(); rs = pstmt.executeQuery();
if (rs.next()) if (rs.next())
{
retValue = rs.getBigDecimal(1); retValue = rs.getBigDecimal(1);
}
rs.close(); rs.close();
pstmt.close(); pstmt.close();
pstmt = null; pstmt = null;
} }
catch (Exception e) catch (SQLException e)
{ {
log.log(Level.SEVERE, sql, e); throw new DBException(e, sql);
} }
finally finally
{ {
@ -1170,44 +1061,33 @@ public class MInvoice extends X_C_Invoice implements DocAction
*/ */
public static void setIsPaid (Properties ctx, int C_BPartner_ID, String trxName) public static void setIsPaid (Properties ctx, int C_BPartner_ID, String trxName)
{ {
int counter = 0; List<Object> params = new ArrayList<Object>();
String sql = "SELECT * FROM C_Invoice " StringBuffer whereClause = new StringBuffer("IsPaid='N' AND DocStatus IN ('CO','CL')");
+ "WHERE IsPaid='N' AND DocStatus IN ('CO','CL')";
if (C_BPartner_ID > 1) if (C_BPartner_ID > 1)
sql += " AND C_BPartner_ID=?"; {
whereClause.append(" AND C_BPartner_ID=?");
params.add(C_BPartner_ID);
}
else else
sql += " AND AD_Client_ID=" + Env.getAD_Client_ID(ctx);
PreparedStatement pstmt = null;
try
{ {
pstmt = DB.prepareStatement (sql, trxName); whereClause.append(" AND AD_Client_ID=?");
if (C_BPartner_ID > 1) params.add(Env.getAD_Client_ID(ctx));
pstmt.setInt (1, C_BPartner_ID); }
ResultSet rs = pstmt.executeQuery ();
while (rs.next ()) POResultSet<MInvoice> rs = new Query(ctx, MInvoice.Table_Name, whereClause.toString(), trxName)
{ .setParameters(params)
MInvoice invoice = new MInvoice(ctx, rs, trxName); .scroll();
int counter = 0;
try {
while(rs.hasNext()) {
MInvoice invoice = rs.next();
if (invoice.testAllocation()) if (invoice.testAllocation())
if (invoice.save()) if (invoice.save())
counter++; counter++;
} }
rs.close ();
pstmt.close ();
pstmt = null;
} }
catch (Exception e) finally {
{ DB.close(rs);
s_log.log(Level.SEVERE, sql, e);
}
try
{
if (pstmt != null)
pstmt.close ();
pstmt = null;
}
catch (Exception e)
{
pstmt = null;
} }
s_log.config("#" + counter); s_log.config("#" + counter);
/**/ /**/
@ -1463,7 +1343,7 @@ public class MInvoice extends X_C_Invoice implements DocAction
// //
String sql = "SELECT COUNT(*) FROM C_InvoiceLine " String sql = "SELECT COUNT(*) FROM C_InvoiceLine "
+ "WHERE C_Invoice_ID=? " + where; + "WHERE C_Invoice_ID=? " + where;
int count = DB.getSQLValue(get_TrxName(), sql, getC_Invoice_ID()); int count = DB.getSQLValueEx(get_TrxName(), sql, getC_Invoice_ID());
while (count != 0) while (count != 0)
{ {
renumberLines (100); renumberLines (100);
@ -1496,7 +1376,7 @@ public class MInvoice extends X_C_Invoice implements DocAction
newLine.setDescription (bomline.getDescription ()); newLine.setDescription (bomline.getDescription ());
// //
newLine.setPrice (); newLine.setPrice ();
newLine.save (get_TrxName()); newLine.saveEx (get_TrxName());
} }
} }
@ -1532,7 +1412,7 @@ public class MInvoice extends X_C_Invoice implements DocAction
if (line.getDescription () != null) if (line.getDescription () != null)
description += " " + line.getDescription (); description += " " + line.getDescription ();
line.setDescription (description); line.setDescription (description);
line.save (get_TrxName()); line.saveEx (get_TrxName());
} // for all lines with BOM } // for all lines with BOM
m_lines = null; m_lines = null;

View File

@ -0,0 +1,49 @@
/******************************************************************************
* Product: Adempiere ERP & CRM Smart Business Solution *
* Copyright (C) 2008 SC ARHIPAC SERVICE SRL. All Rights Reserved. *
* This program is free software; you can redistribute it and/or modify it *
* under the terms version 2 of the GNU General Public License as published *
* by the Free Software Foundation. This program is distributed in the hope *
* that it will be useful, but WITHOUT ANY WARRANTY; without even the implied *
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
* See the GNU General Public License for more details. *
* You should have received a copy of the GNU General Public License along *
* with this program; if not, write to the Free Software Foundation, Inc., *
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. *
*****************************************************************************/
package test.functional;
import org.compiere.model.MInvoice;
import org.compiere.util.Env;
import test.AdempiereTestCase;
/**
* @author Teo Sarca, www.arhipac.ro
*/
public class MInvoiceTest extends AdempiereTestCase
{
public static final int BPARTNER_TreeFarm = 114;
@Override
protected void setUp() throws Exception
{
super.setUp();
assertEquals("Client is not GardenWorld", 11, Env.getAD_Client_ID(getCtx()));
}
public void testQuery() throws Exception
{
MInvoice.setIsPaid(getCtx(), BPARTNER_TreeFarm, getTrxName());
MInvoice[] invoices = MInvoice.getOfBPartner(getCtx(), BPARTNER_TreeFarm, getTrxName());
assertTrue("Partner "+BPARTNER_TreeFarm+" should have invoices", invoices.length > 0);
for (MInvoice invoice : invoices)
{
invoice.getLines(true); // test query
invoice.getTaxes(true); // test query
}
}
}