Search in sources :

Example 1 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 2 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 3 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)

Example 4 with DBException

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

the class ModelClassGenerator method createColumns.

/**
	 * 	Create Column access methods
	 * 	@param AD_Table_ID table
	 * 	@param mandatory init call for mandatory columns
	 * 	@return set/get method
	 */
private StringBuffer createColumns(int AD_Table_ID, StringBuffer mandatory) {
    StringBuffer sb = new StringBuffer();
    String sql = //	1..3
    "SELECT c.ColumnName, c.IsUpdateable, c.IsMandatory," + //	4..7
    " c.AD_Reference_ID, c.AD_Reference_Value_ID, DefaultValue, SeqNo, " + //	8..12
    " c.FieldLength, c.ValueMin, c.ValueMax, c.VFormat, c.Callout, " + // 13..18
    " c.Name, c.Description, c.ColumnSQL, c.IsEncrypted, c.IsKey, c.IsIdentifier " + "FROM AD_Column c " + "WHERE c.AD_Table_ID=?" + " AND c.ColumnName NOT IN ('AD_Client_ID', 'AD_Org_ID', 'IsActive', 'Created', 'CreatedBy', 'Updated', 'UpdatedBy')" + " AND c.IsActive='Y'" + " ORDER BY c.ColumnName";
    // true if the method "getKeyNamePair" is already generated
    boolean isKeyNamePairCreated = false;
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
        pstmt = DB.prepareStatement(sql, null);
        pstmt.setInt(1, AD_Table_ID);
        rs = pstmt.executeQuery();
        while (rs.next()) {
            String columnName = rs.getString(1);
            boolean isUpdateable = "Y".equals(rs.getString(2));
            boolean isMandatory = "Y".equals(rs.getString(3));
            int displayType = rs.getInt(4);
            int AD_Reference_Value_ID = rs.getInt(5);
            String defaultValue = rs.getString(6);
            int seqNo = rs.getInt(7);
            int fieldLength = rs.getInt(8);
            String ValueMin = rs.getString(9);
            String ValueMax = rs.getString(10);
            String VFormat = rs.getString(11);
            String Callout = rs.getString(12);
            String Name = rs.getString(13);
            String Description = rs.getString(14);
            String ColumnSQL = rs.getString(15);
            boolean virtualColumn = ColumnSQL != null && ColumnSQL.length() > 0;
            boolean IsEncrypted = "Y".equals(rs.getString(16));
            boolean IsKey = "Y".equals(rs.getString(17));
            boolean IsIdentifier = "Y".equals(rs.getString(18));
            //
            sb.append(createColumnMethods(mandatory, columnName, isUpdateable, isMandatory, displayType, AD_Reference_Value_ID, fieldLength, defaultValue, ValueMin, ValueMax, VFormat, Callout, Name, Description, virtualColumn, IsEncrypted, IsKey, AD_Table_ID));
            //
            if (seqNo == 1 && IsIdentifier) {
                if (!isKeyNamePairCreated) {
                    sb.append(createKeyNamePair(columnName, displayType));
                    isKeyNamePairCreated = true;
                } else {
                    throw new RuntimeException("More than one primary identifier found " + " (AD_Table_ID=" + AD_Table_ID + ", ColumnName=" + columnName + ")");
                }
            }
        }
    } catch (SQLException e) {
        throw new DBException(e, sql);
    } finally {
        DB.close(rs, pstmt);
        rs = null;
        pstmt = null;
    }
    return sb;
}
Also used : DBException(org.adempiere.exceptions.DBException) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 5 with DBException

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

the class ModelInterfaceGenerator method createColumns.

/**
	 * Create Column access methods
	 * 
	 * @param AD_Table_ID table
	 * @param mandatory   init call for mandatory columns
	 * @return set/get method
	 */
private StringBuffer createColumns(int AD_Table_ID, StringBuffer mandatory) {
    StringBuffer sb = new StringBuffer();
    String sql = // 1..3
    "SELECT c.ColumnName, c.IsUpdateable, c.IsMandatory," + // 4..7
    " c.AD_Reference_ID, c.AD_Reference_Value_ID, DefaultValue, SeqNo, " + // 8..12
    " c.FieldLength, c.ValueMin, c.ValueMax, c.VFormat, c.Callout, " + // 13..17
    " c.Name, c.Description, c.ColumnSQL, c.IsEncrypted, c.IsKey " + "FROM AD_Column c " + "WHERE c.AD_Table_ID=?" + //				+ " AND c.ColumnName NOT LIKE 'Updated%' "
    " AND c.IsActive='Y'" + " ORDER BY c.ColumnName";
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
        pstmt = DB.prepareStatement(sql, null);
        pstmt.setInt(1, AD_Table_ID);
        rs = pstmt.executeQuery();
        while (rs.next()) {
            String columnName = rs.getString(1);
            boolean isUpdateable = "Y".equals(rs.getString(2));
            boolean isMandatory = "Y".equals(rs.getString(3));
            int displayType = rs.getInt(4);
            int AD_Reference_Value_ID = rs.getInt(5);
            String defaultValue = rs.getString(6);
            //int seqNo = rs.getInt(7);
            int fieldLength = rs.getInt(8);
            String ValueMin = rs.getString(9);
            String ValueMax = rs.getString(10);
            String VFormat = rs.getString(11);
            String Callout = rs.getString(12);
            String Name = rs.getString(13);
            String Description = rs.getString(14);
            String ColumnSQL = rs.getString(15);
            boolean virtualColumn = ColumnSQL != null && ColumnSQL.length() > 0;
            boolean IsEncrypted = "Y".equals(rs.getString(16));
            boolean IsKey = "Y".equals(rs.getString(17));
            // Create COLUMNNAME_ property (teo_sarca, [ 1662447 ])
            sb.append("\n").append("    /** Column name ").append(columnName).append(" */\n").append("    public static final String COLUMNNAME_").append(columnName).append(" = \"").append(columnName).append("\";");
            //
            sb.append(createColumnMethods(mandatory, columnName, isUpdateable, isMandatory, displayType, AD_Reference_Value_ID, fieldLength, defaultValue, ValueMin, ValueMax, VFormat, Callout, Name, Description, virtualColumn, IsEncrypted, IsKey, AD_Table_ID));
        }
    } catch (SQLException e) {
        throw new DBException(e, sql);
    } finally {
        DB.close(rs, pstmt);
        rs = null;
        pstmt = null;
    }
    return sb;
}
Also used : DBException(org.adempiere.exceptions.DBException) SQLException(java.sql.SQLException) 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