MUOMConversion:

* BF [ 1874419 ] JDBC Statement  not close in a finally block
  https://sourceforge.net/tracker/?func=detail&aid=1874419&group_id=176962&atid=879332
* use new Query API when possible
* minor error reporting problem fix
This commit is contained in:
teo_sarca 2009-06-11 07:18:15 +00:00
parent 98832daff2
commit d50f0c102c
1 changed files with 31 additions and 55 deletions

View File

@ -25,9 +25,11 @@ import java.sql.Timestamp;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Calendar; import java.util.Calendar;
import java.util.GregorianCalendar; import java.util.GregorianCalendar;
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.util.CCache; import org.compiere.util.CCache;
import org.compiere.util.CLogger; import org.compiere.util.CLogger;
import org.compiere.util.DB; import org.compiere.util.DB;
@ -174,10 +176,12 @@ public class MUOMConversion extends X_C_UOM_Conversion
+ "FROM C_UOM_Conversion " + "FROM C_UOM_Conversion "
+ "WHERE IsActive='Y' AND M_Product_ID IS NULL", + "WHERE IsActive='Y' AND M_Product_ID IS NULL",
"C_UOM_Conversion", MRole.SQL_NOTQUALIFIED, MRole.SQL_RO); "C_UOM_Conversion", MRole.SQL_NOTQUALIFIED, MRole.SQL_RO);
PreparedStatement pstmt = null;
ResultSet rs = null;
try try
{ {
PreparedStatement pstmt = DB.prepareStatement(sql, null); pstmt = DB.prepareStatement(sql, null);
ResultSet rs = pstmt.executeQuery(); rs = pstmt.executeQuery();
while (rs.next()) while (rs.next())
{ {
Point p = new Point (rs.getInt(1), rs.getInt(2)); Point p = new Point (rs.getInt(1), rs.getInt(2));
@ -191,13 +195,16 @@ public class MUOMConversion extends X_C_UOM_Conversion
if (dr != null) if (dr != null)
s_conversions.put(new Point(p.y,p.x), dr); s_conversions.put(new Point(p.y,p.x), dr);
} }
rs.close();
pstmt.close();
} }
catch (SQLException e) catch (SQLException e)
{ {
s_log.log(Level.SEVERE, sql, e); s_log.log(Level.SEVERE, sql, e);
} }
finally
{
DB.close(rs, pstmt);
rs = null; pstmt = null;
}
} // createRatess } // createRatess
/** /**
@ -403,39 +410,31 @@ public class MUOMConversion extends X_C_UOM_Conversion
+ " AND c.M_Product_ID IS NULL" + " AND c.M_Product_ID IS NULL"
+ " ORDER BY c.AD_Client_ID DESC, c.AD_Org_ID DESC"; + " ORDER BY c.AD_Client_ID DESC, c.AD_Org_ID DESC";
PreparedStatement pstmt = null; PreparedStatement pstmt = null;
ResultSet rs = null;
try try
{ {
pstmt = DB.prepareStatement(sql, null); pstmt = DB.prepareStatement(sql, null);
pstmt.setInt(1, C_UOM_From_ID); pstmt.setInt(1, C_UOM_From_ID);
pstmt.setInt(2, C_UOM_To_ID); pstmt.setInt(2, C_UOM_To_ID);
ResultSet rs = pstmt.executeQuery(); rs = pstmt.executeQuery();
if (rs.next()) if (rs.next())
{ {
retValue = rs.getBigDecimal(1); retValue = rs.getBigDecimal(1);
precision = rs.getInt(StdPrecision ? 2 : 3); precision = rs.getInt(StdPrecision ? 2 : 3);
} }
rs.close();
pstmt.close();
pstmt = null;
} }
catch (Exception e) catch (SQLException e)
{ {
s_log.log(Level.SEVERE, sql, e); throw new DBException(e, sql);
} }
try finally
{ {
if (pstmt != null) DB.close(rs, pstmt);
pstmt.close(); rs = null; pstmt = null;
pstmt = null;
}
catch (Exception e)
{
pstmt = null;
} }
if (retValue == null) if (retValue == null)
{ {
s_log.info ("NOT found - FromUOM=" + C_UOM_From_ID s_log.info ("NOT found - FromUOM=" + C_UOM_From_ID + ", ToUOM=" + C_UOM_To_ID);
+ ", ToUOM=" + C_UOM_To_ID);
return null; return null;
} }
@ -601,37 +600,14 @@ public class MUOMConversion extends X_C_UOM_Conversion
MUOMConversion defRate = new MUOMConversion (MProduct.get(ctx, M_Product_ID)); MUOMConversion defRate = new MUOMConversion (MProduct.get(ctx, M_Product_ID));
list.add(defRate); list.add(defRate);
// //
String sql = "SELECT * FROM C_UOM_Conversion c " final String whereClause = "M_Product_ID=?"
+ "WHERE c.M_Product_ID=?" + " AND EXISTS (SELECT 1 FROM M_Product p "
+ " AND EXISTS (SELECT * FROM M_Product p " + "WHERE C_UOM_Conversion.M_Product_ID=p.M_Product_ID AND C_UOM_Conversion.C_UOM_ID=p.C_UOM_ID)";
+ "WHERE c.M_Product_ID=p.M_Product_ID AND c.C_UOM_ID=p.C_UOM_ID)" List<MUOMConversion> conversions = new Query(ctx, Table_Name, whereClause, null)
+ " AND c.IsActive='Y'"; .setParameters(new Object[]{M_Product_ID})
PreparedStatement pstmt = null; .setOnlyActiveRecords(true)
try .list();
{ list.addAll(conversions);
pstmt = DB.prepareStatement (sql, null);
pstmt.setInt (1, M_Product_ID);
ResultSet rs = pstmt.executeQuery ();
while (rs.next ())
list.add(new MUOMConversion(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;
}
// Convert & save // Convert & save
result = new MUOMConversion[list.size ()]; result = new MUOMConversion[list.size ()];
@ -642,13 +618,13 @@ public class MUOMConversion extends X_C_UOM_Conversion
} // getProductConversions } // getProductConversions
/** Static Logger */ /** Static Logger */
private static CLogger s_log = CLogger.getCLogger(MUOMConversion.class); private static final CLogger s_log = CLogger.getCLogger(MUOMConversion.class);
/** Indicator for Rate */ /** Indicator for Rate */
private static BigDecimal GETRATE = new BigDecimal(123.456); private static final BigDecimal GETRATE = new BigDecimal(123.456);
/** Conversion Map: Key=Point(from,to) Value=BigDecimal */ /** Conversion Map: Key=Point(from,to) Value=BigDecimal */
private static CCache<Point,BigDecimal> s_conversions = null; private static CCache<Point,BigDecimal> s_conversions = null;
/** Product Conversion Map */ /** Product Conversion Map */
private static CCache<Integer,MUOMConversion[]> s_conversionProduct private static final CCache<Integer,MUOMConversion[]> s_conversionProduct
= new CCache<Integer,MUOMConversion[]>("C_UOMConversion", 20); = new CCache<Integer,MUOMConversion[]>("C_UOMConversion", 20);
@ -716,7 +692,7 @@ public class MUOMConversion extends X_C_UOM_Conversion
// From - To is the same // From - To is the same
if (getC_UOM_ID() == getC_UOM_To_ID()) if (getC_UOM_ID() == getC_UOM_To_ID())
{ {
log.saveError("Error", Msg.parseTranslation(getCtx(), "@C_UOM_ID@ = @C_UOM_ID@")); log.saveError("Error", Msg.parseTranslation(getCtx(), "@C_UOM_ID@ = @C_UOM_To_ID@"));
return false; return false;
} }
// Nothing to convert // Nothing to convert