Search in sources :

Example 1 with MSearchDefinition

use of org.compiere.model.MSearchDefinition in project adempiere by adempiere.

the class AbstractDocumentSearch method getID.

/**
	 * search for id's that fit the searchString
	 *
	 * @param transactionCode
	 * @param searchString
	 */
private void getID(String transactionCode, String searchString) {
    ResultSet rsSO = null;
    ResultSet rsPO = null;
    PreparedStatement pstmtSO = null;
    PreparedStatement pstmtPO = null;
    String sqlSO = null;
    String sqlPO = null;
    final Properties ctx = Env.getCtx();
    final MRole role = MRole.get(ctx, Env.getAD_Role_ID(ctx), Env.getAD_User_ID(ctx), true);
    try {
        for (MSearchDefinition msd : MSearchDefinition.getForCode(transactionCode)) {
            MTable table = new MTable(Env.getCtx(), msd.getAD_Table_ID(), null);
            // SearchDefinition with a given table and column
            if (msd.getSearchType().equals(MSearchDefinition.SEARCHTYPE_TABLE)) {
                MColumn column = new MColumn(Env.getCtx(), msd.getAD_Column_ID(), null);
                sqlSO = "SELECT " + table.getTableName() + "_ID FROM " + table.getTableName() + " ";
                // search for an Integer
                if (msd.getDataType().equals(MSearchDefinition.DATATYPE_INTEGER)) {
                    sqlSO += "WHERE " + column.getColumnName() + "=?";
                // search for a String
                } else {
                    sqlSO += "WHERE UPPER(" + column.getColumnName() + ") LIKE UPPER(?)";
                }
                if (msd.getPO_Window_ID() != 0) {
                    sqlPO = sqlSO + " AND IsSOTrx='N'";
                    sqlSO += " AND IsSOTrx='Y'";
                }
                pstmtSO = DB.prepareStatement(sqlSO, null);
                pstmtPO = DB.prepareStatement(sqlPO, null);
                // search for a Integer
                if (msd.getDataType().equals(MSearchDefinition.DATATYPE_INTEGER)) {
                    pstmtSO.setInt(1, Integer.valueOf(searchString.replaceAll("\\D", "")));
                    if (msd.getPO_Window_ID() != 0) {
                        pstmtPO.setInt(1, Integer.valueOf(searchString.replaceAll("\\D", "")));
                    }
                // search for a String
                } else if (msd.getDataType().equals(MSearchDefinition.DATATYPE_STRING)) {
                    pstmtSO.setString(1, searchString);
                    if (msd.getPO_Window_ID() != 0) {
                        pstmtPO.setString(1, searchString);
                    }
                }
            // SearchDefinition with a special query
            } else if (msd.getSearchType().equals(MSearchDefinition.SEARCHTYPE_QUERY)) {
                sqlSO = msd.getQuery();
                pstmtSO = DB.prepareStatement(sqlSO, null);
                // count '?' in statement
                int count = 1;
                for (char c : sqlSO.toCharArray()) {
                    if (c == '?') {
                        count++;
                    }
                }
                for (int i = 1; i < count; i++) {
                    if (msd.getDataType().equals(MSearchDefinition.DATATYPE_INTEGER)) {
                        pstmtSO.setInt(i, Integer.valueOf(searchString.replaceAll("\\D", "")));
                    } else if (msd.getDataType().equals(MSearchDefinition.DATATYPE_STRING)) {
                        pstmtSO.setString(i, searchString);
                    }
                }
            }
            if (pstmtSO != null) {
                log.fine("SQL Sales: " + sqlSO);
                rsSO = pstmtSO.executeQuery();
                Vector<Integer> idSO = new Vector<Integer>();
                while (rsSO.next()) {
                    idSO.add(new Integer(rsSO.getInt(1)));
                }
                if (role.getWindowAccess(msd.getAD_Window_ID()) != null) {
                    log.fine("Open Window: " + msd.getAD_Window_ID() + " / Table: " + table.getTableName() + " / Number of Results: " + idSO.size());
                    if (idSO.size() == 0 && (searchString == null || searchString.trim().length() == 0)) {
                        // No search string - open the window with new record
                        idSO.add(new Integer(0));
                    }
                    openWindow(idSO, table.getTableName(), msd.getAD_Window_ID());
                } else {
                    log.warning("Role is not allowed to view this window");
                }
            }
            if (pstmtPO != null) {
                log.fine("SQL Purchase: " + sqlPO);
                rsPO = pstmtPO.executeQuery();
                Vector<Integer> idPO = new Vector<Integer>();
                while (rsPO.next()) {
                    idPO.add(new Integer(rsPO.getInt(1)));
                }
                if (role.getWindowAccess(msd.getPO_Window_ID()) != null) {
                    log.fine("Open Window: " + msd.getPO_Window_ID() + " / Table: " + table.getTableName() + " / Number of Results: " + idPO.size());
                    openWindow(idPO, table.getTableName(), msd.getPO_Window_ID());
                } else {
                    log.warning("Role is not allowed to view this window");
                }
            }
            DB.close(rsSO, pstmtSO);
            DB.close(rsPO, pstmtPO);
            pstmtSO = null;
            pstmtPO = null;
            rsSO = null;
            rsPO = null;
        }
    } catch (Exception e) {
        log.severe(e.toString());
        e.printStackTrace();
    } finally {
        DB.close(rsSO, pstmtSO);
        DB.close(rsPO, pstmtPO);
        rsSO = null;
        rsPO = null;
        pstmtSO = null;
        pstmtPO = null;
    }
}
Also used : MColumn(org.compiere.model.MColumn) MTable(org.compiere.model.MTable) MSearchDefinition(org.compiere.model.MSearchDefinition) MRole(org.compiere.model.MRole) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) Properties(java.util.Properties) Vector(java.util.Vector) SQLException(java.sql.SQLException)

Aggregations

PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 Properties (java.util.Properties)1 Vector (java.util.Vector)1 MColumn (org.compiere.model.MColumn)1 MRole (org.compiere.model.MRole)1 MSearchDefinition (org.compiere.model.MSearchDefinition)1 MTable (org.compiere.model.MTable)1