FR [ 2571917 ] Introduce MAttributeSetInstance.create helper method

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

* proper close PreparedStatement & ResultSet
* minor javadoc
This commit is contained in:
teo_sarca 2009-02-06 10:31:32 +00:00
parent 4de3321f32
commit 2d7e2c5119
1 changed files with 23 additions and 13 deletions

View File

@ -43,7 +43,7 @@ public class MAttributeSetInstance extends X_M_AttributeSetInstance
* @param ctx context * @param ctx context
* @param M_AttributeSetInstance_ID id or 0 * @param M_AttributeSetInstance_ID id or 0
* @param M_Product_ID required if id is 0 * @param M_Product_ID required if id is 0
* @return Attribute Set Instance or null * @return Attribute Set Instance or null if error
*/ */
public static MAttributeSetInstance get (Properties ctx, public static MAttributeSetInstance get (Properties ctx,
int M_AttributeSetInstance_ID, int M_Product_ID) int M_AttributeSetInstance_ID, int M_Product_ID)
@ -63,11 +63,12 @@ public class MAttributeSetInstance extends X_M_AttributeSetInstance
+ "FROM M_Product " + "FROM M_Product "
+ "WHERE M_Product_ID=?"; + "WHERE M_Product_ID=?";
PreparedStatement pstmt = null; PreparedStatement pstmt = null;
ResultSet rs = null;
try try
{ {
pstmt = DB.prepareStatement(sql, null); pstmt = DB.prepareStatement(sql, null);
pstmt.setInt(1, M_Product_ID); pstmt.setInt(1, M_Product_ID);
ResultSet rs = pstmt.executeQuery(); rs = pstmt.executeQuery();
if (rs.next()) if (rs.next())
{ {
int M_AttributeSet_ID = rs.getInt(1); int M_AttributeSet_ID = rs.getInt(1);
@ -75,23 +76,17 @@ public class MAttributeSetInstance extends X_M_AttributeSetInstance
// //
retValue = new MAttributeSetInstance (ctx, 0, M_AttributeSet_ID, null); retValue = new MAttributeSetInstance (ctx, 0, M_AttributeSet_ID, null);
} }
rs.close();
pstmt.close();
pstmt = null;
} }
catch (SQLException ex) catch (SQLException ex)
{ {
s_log.log(Level.SEVERE, sql, ex); s_log.log(Level.SEVERE, sql, ex);
retValue = null;
} }
try finally
{ {
if (pstmt != null) DB.close(rs, pstmt);
pstmt.close(); rs = null; pstmt = null;
} }
catch (SQLException ex1)
{
}
pstmt = null;
// //
return retValue; return retValue;
} // get } // get
@ -375,5 +370,20 @@ public class MAttributeSetInstance extends X_M_AttributeSetInstance
return false; return false;
} }
/**
* Create & save a new ASI for given product
* @param ctx
* @param product
* @param trxName
* @return newly created ASI
*/
public static MAttributeSetInstance create(Properties ctx, MProduct product, String trxName)
{
MAttributeSetInstance asi = new MAttributeSetInstance(ctx, 0, trxName);
asi.setClientOrg(product.getAD_Client_ID(), 0);
asi.setM_AttributeSet_ID(product.getM_AttributeSet_ID());
asi.saveEx();
return asi;
}
} // MAttributeSetInstance } // MAttributeSetInstance