Example usage for org.hibernate ScrollableResults previous

List of usage examples for org.hibernate ScrollableResults previous

Introduction

In this page you can find the example usage for org.hibernate ScrollableResults previous.

Prototype

boolean previous();

Source Link

Document

Retreat to the previous result.

Usage

From source file:com.liferay.jbpm.util.QueryUtil.java

License:Open Source License

public static Comparable<?>[] getPrevAndNext(Query query, int count, OrderByComparator obc,
        Comparable<?> comparable) {

    int pos = count;
    int boundary = 0;

    Comparable<?>[] array = new Comparable[3];

    ScrollableResults sr = query.scroll();

    if (sr.first()) {
        while (true) {
            Object obj = sr.get(0);

            if (obj == null) {
                if (_log.isWarnEnabled()) {
                    _log.warn("Object is null");
                }//from w  ww  . j  a v  a  2  s .  c o  m

                break;
            }

            Comparable<?> curComparable = (Comparable<?>) obj;

            int value = obc.compare(comparable, curComparable);

            if (_log.isDebugEnabled()) {
                _log.debug("Comparison result is " + value);
            }

            if (value == 0) {
                if (!comparable.equals(curComparable)) {
                    break;
                }

                array[1] = curComparable;

                if (sr.previous()) {
                    array[0] = (Comparable<?>) sr.get(0);
                }

                sr.next();

                if (sr.next()) {
                    array[2] = (Comparable<?>) sr.get(0);
                }

                break;
            }

            if (pos == 1) {
                break;
            }

            pos = (int) Math.ceil(pos / 2.0);

            int scrollPos = pos;

            if (value < 0) {
                scrollPos = scrollPos * -1;
            }

            boundary += scrollPos;

            if (boundary < 0) {
                scrollPos = scrollPos + (boundary * -1) + 1;

                boundary = 0;
            }

            if (boundary > count) {
                scrollPos = scrollPos - (boundary - count);

                boundary = scrollPos;
            }

            if (_log.isDebugEnabled()) {
                _log.debug("Scroll " + scrollPos);
            }

            if (!sr.scroll(scrollPos)) {
                if (value < 0) {
                    if (!sr.next()) {
                        break;
                    }
                } else {
                    if (!sr.previous()) {
                        break;
                    }
                }
            }
        }
    }

    return array;
}

From source file:edu.utah.further.ds.impl.executor.db.hibernate.criteria.HibernateCriteriaScrollableResultsExecutor.java

License:Apache License

/**
 * @param request//  w w  w  .j  a v a2 s  . co  m
 * @return
 * @see edu.utah.further.core.chain.AbstractRequestHandler#process(edu.utah.further.core.api.chain.ChainRequest)
 */
@Override
public boolean process(final ChainRequest request) {
    final QueryExecutionRequest executionRequest = new QueryExecutionRequest(request);
    final GenericCriteria criteria = executionRequest.getResult();
    final ScrollableResults results = getResultListFromHibernate(criteria);
    executionRequest.setResult(results);
    executionRequest.setStatus("Executed query @ " + TimeService.getDate());
    // Hibernate's ScrollableResults interface has no hasNext() or peek(), so go one
    // row forward (if exists) and then back all the way so that we don't miss the
    // first row in subsequent processors
    final boolean hasNext = results.next();
    if (hasNext) {
        results.previous();
    }
    return hasNext;
}