Search in sources :

Example 66 with DBException

use of org.adempiere.exceptions.DBException in project adempiere by adempiere.

the class InventoryUtil method getLineCosts.

private static BigDecimal getLineCosts(PO line, BigDecimal lineQty, boolean isSOTrx) {
    ArrayList<Object> params = new ArrayList<Object>();
    String sql = "SELECT" + " SUM(" + MCostDetail.COLUMNNAME_Amt + ")" + ",SUM(" + MCostDetail.COLUMNNAME_Qty + ")" + " FROM " + MCostDetail.Table_Name + " WHERE " + line.get_TableName() + "_ID=?";
    params.add(line.get_ID());
    if (line instanceof I_M_MovementLine) {
        sql += " AND " + MCostDetail.COLUMNNAME_IsSOTrx + "=?";
        params.add(isSOTrx);
    }
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    BigDecimal costs = null;
    BigDecimal qty = null;
    try {
        pstmt = DB.prepareStatement(sql, line.get_TrxName());
        DB.setParameters(pstmt, params);
        rs = pstmt.executeQuery();
        if (rs.next()) {
            costs = rs.getBigDecimal(1);
            qty = rs.getBigDecimal(2);
        }
    } catch (SQLException e) {
        throw new DBException(e, sql);
    } finally {
        DB.close(rs, pstmt);
        rs = null;
        pstmt = null;
    }
    if (costs == null)
        costs = Env.ZERO;
    if (qty == null)
        qty = Env.ZERO;
    // Convert Qty sign from Absolute to Relative (for comparation purposes)
    if (isSOTrx)
        qty = qty.negate();
    // Check Line Qty (if specified)
    if (lineQty != null && qty.compareTo(lineQty) != 0) {
        throw new RuntimeException("CostDetail Qty Not Match - TargetQty=" + lineQty + ", ActualQty=" + qty + ", Costs=" + costs);
    }
    return costs;
}
Also used : DBException(org.adempiere.exceptions.DBException) SQLException(java.sql.SQLException) I_M_MovementLine(org.compiere.model.I_M_MovementLine) ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) BigDecimal(java.math.BigDecimal)

Example 67 with DBException

use of org.adempiere.exceptions.DBException in project adempiere by adempiere.

the class DBTest method test_getSQLValueBDEx.

public void test_getSQLValueBDEx() throws Exception {
    BigDecimal result = DB.getSQLValueBDEx(null, "SELECT 10 FROM DUAL");
    assertEquals(BigDecimal.TEN, result);
    //
    result = DB.getSQLValueBD(null, "SELECT 10 FROM AD_SYSTEM WHERE 1=2");
    assertNull("No value should be returned", result);
    //
    DBException ex = null;
    try {
        result = DB.getSQLValueBDEx(null, "SELECT 10 FROM INEXISTENT_TABLE");
    } catch (DBException e) {
        ex = e;
    }
    assertNotNull("No DBException Was Throwed", ex);
}
Also used : DBException(org.adempiere.exceptions.DBException) BigDecimal(java.math.BigDecimal)

Example 68 with DBException

use of org.adempiere.exceptions.DBException in project adempiere by adempiere.

the class DBTest method test_getSQLValueStringEx.

public void test_getSQLValueStringEx() throws Exception {
    String result = DB.getSQLValueStringEx(null, "SELECT 'string' FROM DUAL");
    assertEquals("string", result);
    //
    result = DB.getSQLValueStringEx(null, "SELECT 10 FROM AD_SYSTEM WHERE 1=2");
    assertNull("No value should be returned", result);
    //
    DBException ex = null;
    try {
        result = DB.getSQLValueStringEx(null, "SELECT 'string' FROM INEXISTENT_TABLE");
    } catch (DBException e) {
        ex = e;
    }
    assertNotNull("No DBException Was Throwed", ex);
}
Also used : DBException(org.adempiere.exceptions.DBException)

Example 69 with DBException

use of org.adempiere.exceptions.DBException in project adempiere by adempiere.

the class DBTest method test_getSQLValueEx.

public void test_getSQLValueEx() throws Exception {
    int result = DB.getSQLValueEx(null, "SELECT 10 FROM DUAL");
    assertEquals(10, result);
    //
    result = DB.getSQLValue(null, "SELECT 10 FROM AD_SYSTEM WHERE 1=2");
    assertEquals("No value should be returned", -1, result);
    //
    DBException ex = null;
    try {
        result = DB.getSQLValueEx(null, "SELECT 10 FROM INEXISTENT_TABLE");
    } catch (DBException e) {
        ex = e;
    }
    assertNotNull("No DBException Was Throwed", ex);
}
Also used : DBException(org.adempiere.exceptions.DBException)

Example 70 with DBException

use of org.adempiere.exceptions.DBException in project adempiere by adempiere.

the class DBTest method test_getSQLValueTSEx.

public void test_getSQLValueTSEx() throws Exception {
    final Timestamp target = TimeUtil.getDay(2008, 01, 01);
    //
    Timestamp result = DB.getSQLValueTSEx(null, "SELECT TO_DATE('2008-01-01','YYYY-MM-DD') FROM AD_SYSTEM");
    assertEquals(target, result);
    //
    result = DB.getSQLValueTSEx(null, "SELECT TO_DATE('2008-01-01','YYYY-MM-DD') FROM AD_SYSTEM WHERE 1=2");
    assertNull("No value should be returned", result);
    //
    DBException ex = null;
    try {
        result = DB.getSQLValueTSEx(null, "SELECT TO_DATE('2008-01-01','YYYY-MM-DD') FROM INEXISTENT_TABLE");
    } catch (DBException e) {
        ex = e;
    }
    assertNotNull("No DBException Was Throwed", ex);
}
Also used : DBException(org.adempiere.exceptions.DBException) Timestamp(java.sql.Timestamp)

Aggregations

DBException (org.adempiere.exceptions.DBException)89 SQLException (java.sql.SQLException)82 PreparedStatement (java.sql.PreparedStatement)75 ResultSet (java.sql.ResultSet)75 BigDecimal (java.math.BigDecimal)27 ArrayList (java.util.ArrayList)23 Timestamp (java.sql.Timestamp)15 POResultSet (org.compiere.model.POResultSet)8 AdempiereException (org.adempiere.exceptions.AdempiereException)6 KeyNamePair (org.compiere.util.KeyNamePair)5 Connection (java.sql.Connection)4 Savepoint (java.sql.Savepoint)4 MProduct (org.compiere.model.MProduct)4 Trx (org.compiere.util.Trx)4 Date (java.util.Date)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 IDColumn (org.compiere.minigrid.IDColumn)2 MLocator (org.compiere.model.MLocator)2 MTable (org.compiere.model.MTable)2 MUOM (org.compiere.model.MUOM)2