Search in sources :

Example 41 with DBException

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

the class ProductLowLevelCalculator method icomponent.

/**
	 * get an implotion the product 
	 * @param ID Product
	 * @param ID BOM
	 * @return DefaultMutableTreeNode Tree with all parent product
	 */
private DefaultMutableTreeNode icomponent(int AD_Client_ID, int PP_Product_BOMLine_ID, int M_Product_ID, DefaultMutableTreeNode bom) {
    final String sql = "SELECT pbom.M_Product_ID , pbom.Value , pbom.PP_Product_BOM_ID FROM  PP_Product_BOMLine pboml" + " INNER JOIN PP_Product_BOM pbom ON (pbom.PP_Product_BOM_ID = pboml.PP_Product_BOM_ID)" + " WHERE pbom.IsActive=? AND pboml.IsActive=? AND pboml.AD_Client_ID=? AND pboml.PP_Product_BOMLine_ID=? ";
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
        pstmt = DB.prepareStatement(sql, m_trxName);
        DB.setParameters(pstmt, new Object[] { true, true, AD_Client_ID, PP_Product_BOMLine_ID });
        rs = pstmt.executeQuery();
        while (rs.next()) {
            if (M_Product_ID != rs.getInt(1)) {
                //BOM Loop Error
                if (!tableproduct(rs.getInt(1), rs.getInt(3))) {
                    bom.add(iparent(AD_Client_ID, rs.getInt(1), rs.getInt(3)));
                } else {
                    throw new AdempiereException("Cycle BOM & Formula:" + rs.getString(2) + "(" + rs.getString(3) + ")");
                }
            } else {
                //Child = Parent error
                MProduct product = MProduct.get(m_ctx, M_Product_ID);
                throw new AdempiereException("Cycle BOM & Formula:" + rs.getString(2) + "(" + rs.getString(3) + ")" + " - Component: " + product.getValue() + "(" + product.getM_Product_ID() + ")");
            }
        }
    } catch (SQLException e) {
        throw new DBException(e, sql);
    } finally {
        DB.close(rs, pstmt);
        rs = null;
        pstmt = null;
    }
    return null;
}
Also used : DBException(org.adempiere.exceptions.DBException) MProduct(org.compiere.model.MProduct) SQLException(java.sql.SQLException) AdempiereException(org.adempiere.exceptions.AdempiereException) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 42 with DBException

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

the class FieldAutoCompleter method updateListData.

/**
	 * Update list model depending on the data in textfield
	 * 
	 * @return
	 */
protected boolean updateListData() {
    final String search = textBox.getText();
    Object userObject = getUserOject();
    if (userObject != null && !isMatching(userObject, search)) {
        setUserObject(null);
    }
    //
    final ArrayList<Object> list = new ArrayList<Object>();
    boolean truncated = false;
    //
    // Load list from database
    final ArrayList<Object> params = new ArrayList<Object>();
    final String sql = getSelectSQL(search, textBox.getCaretPosition(), params);
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
        pstmt = DB.prepareStatement(sql, null);
        DB.setParameters(pstmt, params);
        rs = pstmt.executeQuery();
        int i = 0;
        while (rs.next()) {
            if (i > 0 && i > m_maxItems) {
                list.add(ITEM_More);
                truncated = true;
                break;
            }
            Object o = fetchUserObject(rs);
            list.add(o);
            i++;
        }
    } catch (SQLException e) {
        throw new DBException(e, sql.toString());
    } finally {
        DB.close(rs, pstmt);
        rs = null;
        pstmt = null;
    }
    listBox.setListData(list.toArray());
    // if there is no items on the list return false, to not show the pop-up
    if (list.isEmpty()) {
        return false;
    }
    // If the list has only one item, but that item is not equals with
    // return false to not show any popup
    userObject = getUserOject();
    if (!truncated && list.size() == 1 && userObject != null && list.get(0).equals(userObject)) {
        log.finest("nothing to do 1");
        return false;
    }
    // if first list item matched then select it
    if (isMatching(list.get(0), search)) {
        setUserObject(list.get(0));
        return true;
    }
    // List updated, show we need to show the pop-up
    return true;
}
Also used : DBException(org.adempiere.exceptions.DBException) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 43 with DBException

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

the class CreateAdempiere method copy.

//  dropDatabase	
/**
	 * 	Create Tables and copy data
	 * 	@param whereClause optional where clause
	 * 	@param dropFirst drop first
	 *	@return true if executed
	 */
public boolean copy(String whereClause, boolean dropFirst) {
    log.info(whereClause);
    if (getConnection(false, true) == null)
        return false;
    //
    boolean success = true;
    int count = 0;
    ArrayList<String> list = new ArrayList<String>();
    String sql = "SELECT * FROM AD_Table";
    if (whereClause != null && whereClause.length() > 0)
        sql += " WHERE " + whereClause;
    sql += " ORDER BY TableName";
    //
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
        pstmt = DB.prepareStatement(sql, null);
        //jz: pstmt.getConnection() could be null
        Connection conn = pstmt.getConnection();
        DatabaseMetaData md = null;
        if (conn != null)
            md = conn.getMetaData();
        else {
            //jz: globalization issue??
            throw new DBException("No Connection");
        }
        rs = pstmt.executeQuery();
        while (rs.next() && success) {
            MTable table = new MTable(m_ctx, rs, null);
            if (table.isView())
                continue;
            if (dropFirst) {
                executeCommands(new String[] { "DROP TABLE " + table.getTableName() }, m_conn, false, false);
            }
            //
            if (createTable(table, md)) {
                list.add(table.getTableName());
                count++;
            } else
                success = false;
        }
    } catch (Exception e) {
        log.log(Level.SEVERE, sql, e);
        success = false;
    } finally {
        DB.close(rs, pstmt);
        rs = null;
        pstmt = null;
    }
    if (!success)
        return false;
    /**	Enable Contraints */
    enableConstraints(list);
    databaseBuild();
    log.info("#" + count);
    try {
        if (m_conn != null)
            m_conn.close();
    } catch (SQLException e2) {
        log.log(Level.SEVERE, "close connection", e2);
    }
    m_conn = null;
    return success;
}
Also used : DBException(org.adempiere.exceptions.DBException) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) DatabaseMetaData(java.sql.DatabaseMetaData) DBException(org.adempiere.exceptions.DBException) SQLException(java.sql.SQLException) MTable(org.compiere.model.MTable) ResultSet(java.sql.ResultSet)

Example 44 with DBException

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

the class WMRPDetailed method setMRP.

private void setMRP() {
    int M_Product_ID = getM_Product_ID();
    int M_AttributeSetInstance_ID = getM_AttributeSetInstance_ID();
    int M_Warehouse_ID = getM_Warehouse_ID();
    // Check Product (mandatory):
    if (M_Product_ID <= 0)
        return;
    //
    // Set Quantities
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
        StringBuffer sql = new StringBuffer("SELECT ").append("BOMQtyOnHandASI(M_Product_ID,?,?,?) as qtyonhand, ").append("BOMQtyReservedASI(M_Product_ID,?,?,?) as qtyreserved, ").append("BOMQtyAvailableASI(M_Product_ID,?,?,?) as qtyavailable, ").append("BOMQtyOrderedASI(M_Product_ID,?,?,?) as qtyordered").append(" FROM M_Product WHERE M_Product_ID=?");
        pstmt = DB.prepareStatement(sql.toString(), null);
        DB.setParameters(pstmt, new Object[] { getM_AttributeSetInstance_ID(), getM_Warehouse_ID(), 0, getM_AttributeSetInstance_ID(), getM_Warehouse_ID(), 0, getM_AttributeSetInstance_ID(), getM_Warehouse_ID(), 0, getM_AttributeSetInstance_ID(), getM_Warehouse_ID(), 0, getM_Product_ID() });
        rs = pstmt.executeQuery();
        while (rs.next()) {
            fOnhand.setValue(rs.getBigDecimal(1));
            fReserved.setValue(rs.getBigDecimal(2));
            fAvailable.setValue(rs.getBigDecimal(3));
            fOrdered.setValue(rs.getBigDecimal(4));
        }
    } catch (SQLException ex) {
        throw new DBException(ex);
    } finally {
        DB.close(rs, pstmt);
        rs = null;
        pstmt = null;
    }
    //
    // Set UOM:
    int uom_id = MProduct.get(getCtx(), M_Product_ID).getC_UOM_ID();
    MUOM um = MUOM.get(getCtx(), uom_id);
    KeyNamePair kum = new KeyNamePair(um.getC_UOM_ID(), um.get_Translation(MUOM.COLUMNNAME_Name));
    fUOM.setText(kum.toString());
    //
    // Set Replenish Min Level:
    BigDecimal replenishLevelMin = Env.ZERO;
    if (getM_Warehouse_ID() > 0) {
        String sql = "SELECT Level_Min FROM M_Replenish" + " WHERE AD_Client_ID=? AND M_Product_ID=? AND M_Warehouse_ID=?";
        replenishLevelMin = DB.getSQLValueBD(null, sql, AD_Client_ID, M_Product_ID, M_Warehouse_ID);
    }
    fReplenishMin.setValue(replenishLevelMin);
}
Also used : DBException(org.adempiere.exceptions.DBException) MUOM(org.compiere.model.MUOM) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) KeyNamePair(org.compiere.util.KeyNamePair) BigDecimal(java.math.BigDecimal)

Example 45 with DBException

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

the class WAutoCompleterCity method fillList.

public void fillList() {
    // Carlos Ruiz - globalqss - improve to avoid going to the database on every keystroke
    m_cities.clear();
    m_citiesShow.clear();
    ArrayList<Object> params = new ArrayList<Object>();
    final StringBuffer sql = new StringBuffer("SELECT cy.C_City_ID, cy.Name, cy.C_Region_ID, r.Name" + " FROM C_City cy" + " LEFT OUTER JOIN C_Region r ON (r.C_Region_ID=cy.C_Region_ID)" + " WHERE cy.AD_Client_ID IN (0,?)");
    params.add(getAD_Client_ID());
    if (getC_Region_ID() > 0) {
        sql.append(" AND cy.C_Region_ID=?");
        params.add(getC_Region_ID());
    }
    if (getC_Country_ID() > 0) {
        sql.append(" AND cy.C_Country_ID=?");
        params.add(getC_Country_ID());
    }
    sql.append(" ORDER BY cy.Name, r.Name");
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
        pstmt = DB.prepareStatement(sql.toString(), null);
        DB.setParameters(pstmt, params);
        rs = pstmt.executeQuery();
        int i = 0;
        while (rs.next()) {
            CityVO vo = new CityVO(rs.getInt(1), rs.getString(2), rs.getInt(3), rs.getString(4));
            m_cities.add(vo);
            if (i <= m_maxRows) {
                m_citiesShow.add(vo);
            } else if (i == m_maxRows + 1 && i > 0) {
                m_citiesShow.add(ITEM_More);
            }
            i++;
        }
    } catch (SQLException e) {
        throw new DBException(e, sql.toString());
    } finally {
        DB.close(rs, pstmt);
        rs = null;
        pstmt = null;
    }
    refreshData("");
}
Also used : DBException(org.adempiere.exceptions.DBException) SQLException(java.sql.SQLException) CityVO(org.compiere.grid.ed.CityVO) ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

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