Example usage for com.liferay.portal.kernel.dao.search SearchPaginationUtil calculateStartAndEnd

List of usage examples for com.liferay.portal.kernel.dao.search SearchPaginationUtil calculateStartAndEnd

Introduction

In this page you can find the example usage for com.liferay.portal.kernel.dao.search SearchPaginationUtil calculateStartAndEnd.

Prototype

public static int[] calculateStartAndEnd(int start, int end, int total) 

Source Link

Usage

From source file:com.liferay.trash.service.impl.TrashEntryServiceImpl.java

License:Open Source License

/**
 * Returns a range of all the trash entries matching the group ID.
 *
 * @param  groupId the primary key of the group
 * @param  className the class name of the entity
 * @param  start the lower bound of the range of trash entries to return
 * @param  end the upper bound of the range of trash entries to return (not
 *         inclusive)//from ww w .  j a v a2s  .c o  m
 * @param  obc the comparator to order the trash entries (optionally
 *         <code>null</code>)
 * @return the range of matching trash entries ordered by comparator
 *         <code>obc</code>
 */
@Override
public TrashEntryList getEntries(long groupId, String className, int start, int end,
        OrderByComparator<TrashEntry> obc) throws PrincipalException {

    TrashEntryList trashEntriesList = new TrashEntryList();

    int entriesCount = trashEntryPersistence.countByGroupId(groupId);

    boolean approximate = false;

    if (entriesCount > PropsValues.TRASH_SEARCH_LIMIT) {
        approximate = true;
    }

    trashEntriesList.setApproximate(approximate);

    List<TrashEntry> entries = null;

    if (Validator.isNotNull(className)) {
        long classNameId = classNameLocalService.getClassNameId(className);

        entries = trashEntryPersistence.findByG_C(groupId, classNameId, 0, end + PropsValues.TRASH_SEARCH_LIMIT,
                obc);
    } else {
        entries = trashEntryPersistence.findByGroupId(groupId, 0, end + PropsValues.TRASH_SEARCH_LIMIT, obc);
    }

    List<TrashEntry> filteredEntries = filterEntries(entries);

    int total = filteredEntries.size();

    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS)) {
        start = 0;
        end = total;
    }

    int[] startAndEnd = SearchPaginationUtil.calculateStartAndEnd(start, end, total);

    start = startAndEnd[0];
    end = startAndEnd[1];

    filteredEntries = filteredEntries.subList(start, end);

    trashEntriesList.setArray(TrashEntrySoap.toSoapModels(filteredEntries));

    trashEntriesList.setCount(total);
    trashEntriesList.setOriginalTrashEntries(filteredEntries);

    return trashEntriesList;
}