Search in sources :

Example 86 with DBException

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

the class MLanguage method addTable.

//	deleteTable
/**
	 * 	Add Translation to table
	 *	@param tableName table name
	 *	@return number of records inserted
	 */
private int addTable(String tableName) {
    String baseTable = tableName.substring(0, tableName.length() - 4);
    String sql = "SELECT c.ColumnName " + "FROM AD_Column c" + " INNER JOIN AD_Table t ON (c.AD_Table_ID=t.AD_Table_ID) " + "WHERE t.TableName=?" + "  AND c.IsTranslated='Y' AND c.IsActive='Y' " + "ORDER BY c.ColumnName";
    ArrayList<String> columns = new ArrayList<String>(5);
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
        pstmt = DB.prepareStatement(sql, null);
        pstmt.setString(1, baseTable);
        rs = pstmt.executeQuery();
        while (rs.next()) {
            columns.add(rs.getString(1));
        }
    } catch (SQLException e) {
        throw new DBException(e, sql);
    } finally {
        DB.close(rs, pstmt);
        rs = null;
        pstmt = null;
    }
    //	Columns
    if (columns.size() == 0) {
        log.log(Level.SEVERE, "No Columns found for " + baseTable);
        return 0;
    }
    StringBuffer cols = new StringBuffer();
    for (int i = 0; i < columns.size(); i++) cols.append(",").append(columns.get(i));
    //	Insert Statement
    int AD_User_ID = Env.getAD_User_ID(getCtx());
    String keyColumn = baseTable + "_ID";
    String insert = "INSERT INTO " + tableName + "(AD_Language,IsTranslated, AD_Client_ID,AD_Org_ID, " + "Created,Updated, " + "Createdby,UpdatedBy, " + keyColumn + cols + ") " + "SELECT '" + getAD_Language() + "','N', AD_Client_ID,AD_Org_ID, SYSDATE, SYSDATE ," + AD_User_ID + "," + AD_User_ID + ", " + keyColumn + cols + " FROM " + baseTable + " WHERE " + keyColumn + " NOT IN (SELECT " + keyColumn + " FROM " + tableName + " WHERE AD_Language='" + getAD_Language() + "')";
    //	+ " WHERE (" + keyColumn + ",'" + getAD_Language()+ "') NOT IN (SELECT " 
    //		+ keyColumn + ",AD_Language FROM " + tableName + ")";
    int no = DB.executeUpdateEx(insert, null, get_TrxName());
    log.fine(tableName + " #" + no);
    return no;
}
Also used : DBException(org.adempiere.exceptions.DBException) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 87 with DBException

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

the class MRoleIncluded method hasLoop.

/**
	 * Check if there is a loop in the tree defined in given table
	 * @param tableName
	 * @param idColumnName Node_ID column name
	 * @param parentIdColumnName Parent_ID column name
	 * @param nodeId current Node_ID
	 * @param trace current tree path (optional)
	 * @param trxName transaction name
	 * @return true if loop detected. If you specified not null trace, you will have in that list the IDs from the loop
	 */
// TODO: refactor this method and move into org.compiere.util.DB class because it's general and usefull of others too
private static boolean hasLoop(String tableName, String idColumnName, String parentIdColumnName, int nodeId, List<Integer> trace, String trxName) {
    final List<Integer> trace2;
    if (trace == null) {
        trace2 = new ArrayList<Integer>(10);
    } else {
        trace2 = new ArrayList<Integer>(trace);
    }
    trace2.add(nodeId);
    //
    final String sql = "SELECT " + idColumnName + "," + parentIdColumnName + " FROM " + tableName + " WHERE " + parentIdColumnName + "=?";
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
        pstmt = DB.prepareStatement(sql, trxName);
        pstmt.setInt(1, nodeId);
        rs = pstmt.executeQuery();
        while (rs.next()) {
            final int childId = rs.getInt(1);
            if (trace2.contains(childId)) {
                trace.clear();
                trace.addAll(trace2);
                trace.add(childId);
                return true;
            }
            if (hasLoop(tableName, idColumnName, parentIdColumnName, childId, trace2, trxName)) {
                trace.clear();
                trace.addAll(trace2);
                return true;
            }
        }
    } catch (SQLException e) {
        throw new DBException(e, sql);
    } finally {
        DB.close(rs, pstmt);
        rs = null;
        pstmt = null;
    }
    //
    return false;
}
Also used : DBException(org.adempiere.exceptions.DBException) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 88 with DBException

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

the class InventoryTestException method assertStorage.

private void assertStorage(MMDocument doc, String trxName) {
    MLocator locator = InventoryUtil.getCreateLocator(-1, doc.LocatorValue, doc.LocatorValue);
    MProduct product = InventoryUtil.getCreateProduct(doc.ProductValue, null);
    int M_ASI_ID = -1;
    if (!Util.isEmpty(doc.ASI, true)) {
        M_ASI_ID = doc.scenario.getM_ASI_ID(doc.ASI);
    }
    ArrayList<Object> params = new ArrayList<Object>();
    String sql = "SELECT" + " COALESCE(SUM(QtyOnHand),0)" + ",COALESCE(SUM(QtyReserved),0)" + ",COALESCE(SUM(QtyOrdered),0)" + " FROM M_Storage" + " WHERE M_Locator_ID=? AND M_Product_ID=?";
    params.add(locator.get_ID());
    params.add(product.get_ID());
    if (M_ASI_ID >= 0) {
        sql += " AND " + MStorage.COLUMNNAME_M_AttributeSetInstance_ID + "=?";
        params.add(M_ASI_ID);
    }
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    BigDecimal qtyOnHand = Env.ZERO;
    BigDecimal qtyOrdered = Env.ZERO;
    BigDecimal qtyReserved = Env.ZERO;
    try {
        pstmt = DB.prepareStatement(sql, trxName);
        DB.setParameters(pstmt, params);
        rs = pstmt.executeQuery();
        if (rs.next()) {
            qtyOnHand = rs.getBigDecimal(1);
            qtyReserved = rs.getBigDecimal(2);
            qtyOrdered = rs.getBigDecimal(3);
        }
    } catch (SQLException e) {
        throw new DBException(e, sql);
    } finally {
        DB.close(rs, pstmt);
        rs = null;
        pstmt = null;
    }
    //
    //
    assertEquals("QtyOnHand not match " + doc, doc.Qty, qtyOnHand);
    assertEquals("QtyReserved not match " + doc, doc.QtyReserved, qtyReserved);
    assertEquals("QtyOrdered not match " + doc, doc.QtyOrdered, qtyOrdered);
}
Also used : DBException(org.adempiere.exceptions.DBException) MProduct(org.compiere.model.MProduct) SQLException(java.sql.SQLException) MLocator(org.compiere.model.MLocator) ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) BigDecimal(java.math.BigDecimal)

Example 89 with DBException

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

the class MDepreciationMethod method invoke.

/**
	 * Calculate adjustment
	 * @return adjustment to be applied in the specified period
	 */
public BigDecimal invoke(int A_Asset_ID, BigDecimal A_Asset_Adjustment, int A_PeriodNo, String PostingType, int A_Asset_Acct_ID) {
    String depreciationType = getDepreciationType();
    BigDecimal retValue = null;
    if (CLogMgt.isLevelFine())
        log.fine("Entering: DepreciationMethodType=" + depreciationType + ", A_Asset_ID=" + A_Asset_ID + ", A_Asset_Adjustment=" + A_Asset_Adjustment + ", A_PeriodNo=" + A_PeriodNo + ", PostingType=" + PostingType + ", A_Asset_Acct_ID=" + A_Asset_Acct_ID);
    if (depreciationType.equalsIgnoreCase("MDI")) {
        retValue = apply_MDI(A_Asset_ID, A_Asset_Adjustment, A_PeriodNo, PostingType, A_Asset_Acct_ID);
    } else if (depreciationType.equalsIgnoreCase("YDI")) {
        retValue = apply_YDI(A_Asset_ID, A_Asset_Adjustment, A_PeriodNo, PostingType, A_Asset_Acct_ID);
    } else if (depreciationType.equalsIgnoreCase("LDI")) {
        retValue = apply_LDI(A_Asset_ID, A_Asset_Adjustment, A_PeriodNo, PostingType, A_Asset_Acct_ID);
    } else {
        String sql = "{ ? = call " + depreciationType + "(?, ?, ?, ?, ?) }";
        CallableStatement cs = null;
        try {
            cs = DB.prepareCall(sql);
            cs.registerOutParameter(1, java.sql.Types.DECIMAL);
            cs.setInt(2, A_Asset_ID);
            cs.setBigDecimal(3, A_Asset_Adjustment);
            cs.setInt(4, A_PeriodNo);
            cs.setString(5, PostingType);
            cs.setInt(6, A_Asset_Acct_ID);
            cs.execute();
            retValue = cs.getBigDecimal(1);
            cs.close();
        } catch (SQLException e) {
            throw new DBException(e);
        } finally {
            DB.close(cs);
            cs = null;
        }
    }
    //
    if (retValue == null) {
        retValue = BigDecimal.ZERO;
    }
    //
    if (CLogMgt.isLevelFine())
        log.fine("Leaving: retValue=" + retValue);
    return retValue;
}
Also used : DBException(org.adempiere.exceptions.DBException) SQLException(java.sql.SQLException) CallableStatement(java.sql.CallableStatement) BigDecimal(java.math.BigDecimal)

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