Example usage for java.sql ResultSet relative

List of usage examples for java.sql ResultSet relative

Introduction

In this page you can find the example usage for java.sql ResultSet relative.

Prototype

boolean relative(int rows) throws SQLException;

Source Link

Document

Moves the cursor a relative number of rows, either positive or negative.

Usage

From source file:org.wso2.carbon.repository.core.jdbc.dao.JDBCLogsDAO.java

private List<ResourceActivity> internalGetLogs(boolean paginate, String resourcePath, int action,
        String userName, Date from, Date to, boolean descending, Connection conn) throws RepositoryException {
    try {/*  www .j a v  a  2s  .c  o  m*/
        String dbName = conn.getMetaData().getDatabaseProductName();
        if (dbName.contains("Microsoft") || dbName.equals("Oracle")) {
            enableApiPagination = "false";
        }
    } catch (SQLException e) {
        throw new RepositoryDBException("Failed to get Database product name ", e);
    }

    if (conn == null) {
        log.fatal(
                "Failed to get Logs. Communications link failure. The connection to the database could not be acquired.");
        throw new RepositoryDBException(
                "Failed to get Logs. Communications link failure. The connection to the database could not be acquired.");
    }

    PreparedStatement s = null;
    ResultSet results = null;

    boolean paginated = false;
    int start = 0;
    int count = 0;
    //        String sortOrder ="";
    //        String sortBy  ="";
    //        MessageContext messageContext = null;
    /*
    //   enableApiPagination is the value of system property - enable.registry.api.paginating
    if (enableApiPagination == null || enableApiPagination.equals("true")) {
    messageContext = MessageContext.getCurrentMessageContext();
    if (messageContext != null && PaginationUtils.isPaginationHeadersExist(messageContext)) {
            
        PaginationContext paginationContext = PaginationUtils.initPaginationContext(messageContext);
        start = paginationContext.getStart();
        count = paginationContext.getCount();
        if(start == 0){
            start =1;
        }
        sortBy = paginationContext.getSortBy();
        sortOrder = paginationContext.getSortOrder();
        paginated = paginate;
    }
    }*/
    String sql = "SELECT REG_PATH, REG_USER_ID, REG_LOGGED_TIME, REG_ACTION, REG_ACTION_DATA FROM " + "REG_LOG";

    boolean queryStarted = false;
    sql = addWherePart(resourcePath, queryStarted, sql, userName, from, to, action);

    if (descending) {
        sql = sql + " ORDER BY REG_LOGGED_TIME DESC";
    }
    try {
        if (enableApiPagination == null || enableApiPagination.equals("true")) {
            s = conn.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
        } else {
            s = conn.prepareStatement(sql);
        }
        int paramNumber = 1;

        if (resourcePath != null) {
            s.setString(paramNumber, resourcePath);
            paramNumber++;
        }

        if (userName != null) {
            s.setString(paramNumber, userName);
            paramNumber++;
        }

        if (from != null) {
            s.setTimestamp(paramNumber, new Timestamp(from.getTime()));
            paramNumber++;
        }

        if (to != null) {
            s.setTimestamp(paramNumber, new Timestamp(to.getTime()));
            paramNumber++;
        }

        if (action != -1) {
            s.setInt(paramNumber, action);
            paramNumber++;
        }
        s.setInt(paramNumber, CurrentContext.getTenantId());

        results = s.executeQuery();

        List<ResourceActivity> resultList = new ArrayList<ResourceActivity>();
        if (paginated) {
            if (results.relative(start)) {
                //This is to get cursor to correct position to execute results.next().
                results.previous();
                int i = 0;
                while (results.next() && i < count) {
                    i++;
                    resultList.add(getActivity(results));
                }
            } else {
                log.debug("start index doesn't exist in the result set");
            }
            //move the cursor to the last index
            if (results.last()) {
                log.debug("cursor move to the last index of result set");
            } else {
                log.debug("cursor doesn't move to the last index of result set");
            }
            //set row count to the message context.
            //                PaginationUtils.setRowCount(messageContext, Integer.toString(results.getRow()));

        } else {
            while (results.next()) {
                resultList.add(getActivity(results));
            }
            //                Activity[] logEntries = getPaginatedLogs(resultList.toArray(new Activity[resultList.size()]));
            //                resultList = Arrays.asList(logEntries);
        }
        return resultList;

    } catch (SQLException e) {

        String msg = "Failed to get logs. " + e.getMessage();
        log.error(msg, e);
        throw new RepositoryDBException(msg, e);
    } finally {
        try {
            try {
                if (results != null) {
                    results.close();
                }
            } finally {
                if (s != null) {
                    s.close();
                }
            }
        } catch (SQLException ex) {
            String msg = InternalConstants.RESULT_SET_PREPARED_STATEMENT_CLOSE_ERROR;
            log.error(msg, ex);
        }
    }
}

From source file:org.wso2.carbon.repository.core.jdbc.dao.JDBCLogsDAO.java

public List<Activity> getLogs(String resourcePath, int action, String userName, Date from, Date to,
        boolean descending) throws RepositoryException {
    JDBCDatabaseTransaction.ManagedRegistryConnection conn = JDBCDatabaseTransaction.getConnection();

    try {//from  w w  w  . ja  v a 2 s .c om
        String dbName = conn.getMetaData().getDatabaseProductName();
        if (dbName.contains("Microsoft") || dbName.equals("Oracle")) {
            enableApiPagination = "false";
        }
    } catch (SQLException e) {
        throw new RepositoryDBException("Failed to get Database product name ", e);
    }

    if (conn == null) {
        log.fatal(
                "Failed to get Logs. Communications link failure. The connection to the database could not be acquired.");
        throw new RepositoryDBException(
                "Failed to get Logs. Communications link failure. The connection to the database could not be acquired.");
    }

    PreparedStatement s = null;
    ResultSet results = null;

    boolean paginated = false;
    int start = 0;
    int count = 0;
    String sortOrder = "";
    String sortBy = "";

    /*
    MessageContext messageContext = null;
    //   enableApiPagination is the value of system property - enable.registry.api.paginating
    if (enableApiPagination == null || enableApiPagination.equals("true")) {
    messageContext = MessageContext.getCurrentMessageContext();
    if (messageContext != null && PaginationUtils.isPaginationHeadersExist(messageContext)) {
            
        PaginationContext paginationContext = PaginationUtils.initPaginationContext(messageContext);
        start = paginationContext.getStart();
        count = paginationContext.getCount();
        if(start == 0){
            start =1;
        }
        sortBy = paginationContext.getSortBy();
        sortOrder = paginationContext.getSortOrder();
        paginated = true;
    }
    }
    */

    String sql = "SELECT REG_PATH, REG_USER_ID, REG_LOGGED_TIME, REG_ACTION, REG_ACTION_DATA FROM REG_LOG";

    boolean queryStarted = false;
    sql = addWherePart(resourcePath, queryStarted, sql, userName, from, to, action);

    if (descending) {
        sql = sql + " ORDER BY REG_LOGGED_TIME DESC";
    }

    try {
        if (enableApiPagination == null || enableApiPagination.equals("true")) {
            s = conn.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
        } else {
            s = conn.prepareStatement(sql);
        }

        int paramNumber = 1;

        if (resourcePath != null) {
            s.setString(paramNumber, resourcePath);
            paramNumber++;
        }

        if (userName != null) {
            s.setString(paramNumber, userName);
            paramNumber++;
        }

        if (from != null) {
            s.setTimestamp(paramNumber, new Timestamp(from.getTime()));
            paramNumber++;
        }

        if (to != null) {
            s.setTimestamp(paramNumber, new Timestamp(to.getTime()));
            paramNumber++;
        }

        if (action != -1) {
            s.setInt(paramNumber, action);
            paramNumber++;
        }

        s.setInt(paramNumber, CurrentContext.getTenantId());

        results = s.executeQuery();

        List<Activity> resultList = new ArrayList<Activity>();
        if (paginated) {
            //Check start index is a valid one
            if (results.relative(start)) {
                //This is to get cursor to correct position to execute results.next().
                results.previous();
                int i = 0;
                while (results.next() && i < count) {
                    i++;
                    resultList.add(getLogEntry(results));
                }
            } else {
                log.debug("start index doesn't exist in the result set");
            }
            //move the cursor to the last index
            if (results.last()) {
                log.debug("cursor move to the last index of result set");
            } else {
                log.debug("cursor doesn't move to the last index of result set");
            }
            //set row count to the message context.
            //                PaginationUtils.setRowCount(messageContext, Integer.toString(results.getRow()));

        } else {
            while (results.next()) {
                resultList.add(getLogEntry(results));
            }
            //                Activity[] logEntries = getPaginatedLogs(resultList.toArray(new Activity[resultList.size()]));
            //                resultList = Arrays.asList(logEntries);
        }
        return resultList;

    } catch (SQLException e) {

        String msg = "Failed to get logs. " + e.getMessage();
        log.error(msg, e);
        throw new RepositoryDBException(msg, e);
    } finally {
        try {
            try {
                if (results != null) {
                    results.close();
                }
            } finally {
                if (s != null) {
                    s.close();
                }
            }
        } catch (SQLException ex) {
            String msg = InternalConstants.RESULT_SET_PREPARED_STATEMENT_CLOSE_ERROR;
            log.error(msg, ex);
        }
    }
}