MRefList:
* BF [ 1874419 ] JDBC Statement not close in a finally block * organized imports * added test case
This commit is contained in:
parent
f3cabb1af5
commit
5364036b18
|
@ -16,11 +16,18 @@
|
||||||
*****************************************************************************/
|
*****************************************************************************/
|
||||||
package org.compiere.model;
|
package org.compiere.model;
|
||||||
|
|
||||||
import java.sql.*;
|
import java.sql.PreparedStatement;
|
||||||
import java.util.*;
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Properties;
|
||||||
|
import java.util.logging.Level;
|
||||||
|
|
||||||
import java.util.logging.*;
|
import org.compiere.util.CCache;
|
||||||
import org.compiere.util.*;
|
import org.compiere.util.CLogger;
|
||||||
|
import org.compiere.util.DB;
|
||||||
|
import org.compiere.util.Env;
|
||||||
|
import org.compiere.util.ValueNamePair;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reference List Value
|
* Reference List Value
|
||||||
|
@ -42,37 +49,9 @@ public class MRefList extends X_AD_Ref_List
|
||||||
*/
|
*/
|
||||||
public static MRefList get (Properties ctx, int AD_Reference_ID, String Value, String trxName)
|
public static MRefList get (Properties ctx, int AD_Reference_ID, String Value, String trxName)
|
||||||
{
|
{
|
||||||
MRefList retValue = null;
|
return new Query(ctx, Table_Name, "AD_Reference_ID=? AND Value=?", trxName)
|
||||||
String sql = "SELECT * FROM AD_Ref_List "
|
.setParameters(new Object[]{AD_Reference_ID, Value})
|
||||||
+ "WHERE AD_Reference_ID=? AND Value=?";
|
.first();
|
||||||
PreparedStatement pstmt = null;
|
|
||||||
try
|
|
||||||
{
|
|
||||||
pstmt = DB.prepareStatement (sql, trxName);
|
|
||||||
pstmt.setInt (1, AD_Reference_ID);
|
|
||||||
pstmt.setString (2, Value);
|
|
||||||
ResultSet rs = pstmt.executeQuery ();
|
|
||||||
if (rs.next ())
|
|
||||||
retValue = new MRefList (ctx, rs, trxName);
|
|
||||||
rs.close ();
|
|
||||||
pstmt.close ();
|
|
||||||
pstmt = null;
|
|
||||||
}
|
|
||||||
catch (SQLException ex)
|
|
||||||
{
|
|
||||||
s_log.log(Level.SEVERE, sql, ex);
|
|
||||||
}
|
|
||||||
try
|
|
||||||
{
|
|
||||||
if (pstmt != null)
|
|
||||||
pstmt.close ();
|
|
||||||
}
|
|
||||||
catch (SQLException ex1)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
pstmt = null;
|
|
||||||
|
|
||||||
return retValue;
|
|
||||||
} // get
|
} // get
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -98,6 +77,7 @@ public class MRefList extends X_AD_Ref_List
|
||||||
+ " INNER JOIN AD_Ref_List r ON (r.AD_Ref_List_ID=t.AD_Ref_List_ID) "
|
+ " INNER JOIN AD_Ref_List r ON (r.AD_Ref_List_ID=t.AD_Ref_List_ID) "
|
||||||
+ "WHERE r.AD_Reference_ID=? AND r.Value=? AND t.AD_Language=?";
|
+ "WHERE r.AD_Reference_ID=? AND r.Value=? AND t.AD_Language=?";
|
||||||
PreparedStatement pstmt = null;
|
PreparedStatement pstmt = null;
|
||||||
|
ResultSet rs = null;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
pstmt = DB.prepareStatement (sql, null);
|
pstmt = DB.prepareStatement (sql, null);
|
||||||
|
@ -105,26 +85,18 @@ public class MRefList extends X_AD_Ref_List
|
||||||
pstmt.setString(2, Value);
|
pstmt.setString(2, Value);
|
||||||
if (!isBaseLanguage)
|
if (!isBaseLanguage)
|
||||||
pstmt.setString(3, AD_Language);
|
pstmt.setString(3, AD_Language);
|
||||||
ResultSet rs = pstmt.executeQuery ();
|
rs = pstmt.executeQuery ();
|
||||||
if (rs.next ())
|
if (rs.next ())
|
||||||
retValue = rs.getString(1);
|
retValue = rs.getString(1);
|
||||||
rs.close ();
|
|
||||||
pstmt.close ();
|
|
||||||
pstmt = null;
|
|
||||||
}
|
}
|
||||||
catch (SQLException ex)
|
catch (SQLException ex)
|
||||||
{
|
{
|
||||||
s_log.log(Level.SEVERE, sql + " - " + key, ex);
|
s_log.log(Level.SEVERE, sql + " - " + key, ex);
|
||||||
}
|
}
|
||||||
try
|
finally {
|
||||||
{
|
DB.close(rs, pstmt);
|
||||||
if (pstmt != null)
|
rs = null; pstmt = null;
|
||||||
pstmt.close ();
|
|
||||||
}
|
}
|
||||||
catch (SQLException ex1)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
pstmt = null;
|
|
||||||
|
|
||||||
// Save into Cache
|
// Save into Cache
|
||||||
if (retValue == null)
|
if (retValue == null)
|
||||||
|
@ -158,6 +130,7 @@ public class MRefList extends X_AD_Ref_List
|
||||||
+ " ORDER BY t.Name"
|
+ " ORDER BY t.Name"
|
||||||
;
|
;
|
||||||
PreparedStatement pstmt = null;
|
PreparedStatement pstmt = null;
|
||||||
|
ResultSet rs = null;
|
||||||
ArrayList<ValueNamePair> list = new ArrayList<ValueNamePair>();
|
ArrayList<ValueNamePair> list = new ArrayList<ValueNamePair>();
|
||||||
if (optional)
|
if (optional)
|
||||||
list.add(new ValueNamePair("", ""));
|
list.add(new ValueNamePair("", ""));
|
||||||
|
@ -167,26 +140,21 @@ public class MRefList extends X_AD_Ref_List
|
||||||
pstmt.setInt(1, AD_Reference_ID);
|
pstmt.setInt(1, AD_Reference_ID);
|
||||||
if (!isBaseLanguage)
|
if (!isBaseLanguage)
|
||||||
pstmt.setString(2, ad_language);
|
pstmt.setString(2, ad_language);
|
||||||
ResultSet rs = pstmt.executeQuery();
|
rs = pstmt.executeQuery();
|
||||||
while (rs.next())
|
while (rs.next())
|
||||||
list.add(new ValueNamePair(rs.getString(1), rs.getString(2)));
|
list.add(new ValueNamePair(rs.getString(1), rs.getString(2)));
|
||||||
rs.close();
|
rs.close();
|
||||||
pstmt.close();
|
pstmt.close();
|
||||||
pstmt = null;
|
pstmt = null;
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (SQLException e)
|
||||||
{
|
{
|
||||||
s_log.log(Level.SEVERE, sql, e);
|
s_log.log(Level.SEVERE, sql, e);
|
||||||
}
|
}
|
||||||
try
|
finally
|
||||||
{
|
{
|
||||||
if (pstmt != null)
|
DB.close(rs, pstmt);
|
||||||
pstmt.close();
|
rs = null; pstmt = null;
|
||||||
pstmt = null;
|
|
||||||
}
|
|
||||||
catch (Exception e)
|
|
||||||
{
|
|
||||||
pstmt = null;
|
|
||||||
}
|
}
|
||||||
ValueNamePair[] retValue = new ValueNamePair[list.size()];
|
ValueNamePair[] retValue = new ValueNamePair[list.size()];
|
||||||
list.toArray(retValue);
|
list.toArray(retValue);
|
||||||
|
|
|
@ -18,6 +18,7 @@ public class FunctionalTestSuite {
|
||||||
suite.addTestSuite(MStorageTest.class);
|
suite.addTestSuite(MStorageTest.class);
|
||||||
suite.addTestSuite(MSysConfigTest.class);
|
suite.addTestSuite(MSysConfigTest.class);
|
||||||
suite.addTestSuite(QueryTest.class);
|
suite.addTestSuite(QueryTest.class);
|
||||||
|
suite.addTestSuite(MRefListTest.class);
|
||||||
//$JUnit-END$
|
//$JUnit-END$
|
||||||
return suite;
|
return suite;
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,42 @@
|
||||||
|
/******************************************************************************
|
||||||
|
* Product: XERP Core *
|
||||||
|
* Copyright (C) 2008 SC ARHIPAC SERVICE SRL. All Rights Reserved. *
|
||||||
|
*****************************************************************************/
|
||||||
|
|
||||||
|
package test.functional;
|
||||||
|
|
||||||
|
import org.compiere.model.MRefList;
|
||||||
|
import org.compiere.model.X_AD_Table;
|
||||||
|
import org.compiere.util.Env;
|
||||||
|
|
||||||
|
import test.AdempiereTestCase;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test MRefList class
|
||||||
|
* @author Teo Sarca, SC ARHIPAC SERVICE SRL
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class MRefListTest extends AdempiereTestCase {
|
||||||
|
public void testGet() throws Exception {
|
||||||
|
//
|
||||||
|
// Should be found
|
||||||
|
MRefList rl = MRefList.get(getCtx(),
|
||||||
|
X_AD_Table.ACCESSLEVEL_AD_Reference_ID,
|
||||||
|
X_AD_Table.ACCESSLEVEL_Organization,
|
||||||
|
getTrxName());
|
||||||
|
assertNotNull(rl);
|
||||||
|
assertEquals("Should be found", X_AD_Table.ACCESSLEVEL_Organization, rl.getValue());
|
||||||
|
//
|
||||||
|
// Should not be found
|
||||||
|
rl = MRefList.get(getCtx(), 7654321, "7654321", getTrxName());
|
||||||
|
assertNull("Should not be found", rl);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testGetListName() throws Exception {
|
||||||
|
Env.setContext(getCtx(), Env.LANGUAGE, "en_US");
|
||||||
|
String name = MRefList.getListName(getCtx(),
|
||||||
|
X_AD_Table.ACCESSLEVEL_AD_Reference_ID,
|
||||||
|
X_AD_Table.ACCESSLEVEL_All);
|
||||||
|
assertEquals("All", name);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue