From: <fg...@us...> - 2011-06-04 07:59:35
|
Revision: 3498 http://openutils.svn.sourceforge.net/openutils/?rev=3498&view=rev Author: fgiust Date: 2011-06-04 07:59:28 +0000 (Sat, 04 Jun 2011) Log Message: ----------- BSHD-9 pagination Added Paths: ----------- trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/paging/ trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/paging/PaginatedResult.java trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/paging/PaginatedResultImpl.java trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/paging/PaginatedResultUtils.java Added: trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/paging/PaginatedResult.java =================================================================== --- trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/paging/PaginatedResult.java (rev 0) +++ trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/paging/PaginatedResult.java 2011-06-04 07:59:28 UTC (rev 3498) @@ -0,0 +1,75 @@ +/** + * + * openutils base Spring-Hibernate DAO for java 5.0 (http://www.openmindlab.com/lab/products/bshd5.html) + * + * Copyright(C) ${project.inceptionYear}-2011, Openmind S.r.l. http://www.openmindonline.it + * + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * You may obtain a copy of the License at + * + * http://www.gnu.org/licenses/lgpl-2.1.html + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +package it.openutils.hibernate.paging; + +import java.io.Serializable; +import java.util.Collection; + + +/** + * @author fgiust + * @version $Id$ + */ +public interface PaginatedResult<T> extends Serializable +{ + + /** + * Gets the maximum number of results per page + * @return the maximum number of results per page + */ + int getItemsPerPage(); + + /** + * Gets the page number (1, 2, 3...) + * @return the page number (1, 2, 3...) + */ + int getPage(); + + /** + * Gets the total number of results that would be retrieved without pagination. + * @return the total number of results that would be retrieved without pagination. + */ + int getTotalSize(); + + /** + * Gets the total number of pages + * @return total number of pages + */ + int getNumberOfPages(); + + /** + * Gets an iterator over the results + * @return an iterator over the results + */ + Collection<T> getItems(); + + /** + * Returns the fist result if available, null otherwise. + * @return the fist result if available, null otherwise. + */ + T getFirstResult(); + +} Property changes on: trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/paging/PaginatedResult.java ___________________________________________________________________ Added: svn:mime-type + text/plain Added: svn:keywords + Author Date Id Revision Added: svn:eol-style + native Added: trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/paging/PaginatedResultImpl.java =================================================================== --- trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/paging/PaginatedResultImpl.java (rev 0) +++ trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/paging/PaginatedResultImpl.java 2011-06-04 07:59:28 UTC (rev 3498) @@ -0,0 +1,120 @@ +/** + * + * openutils base Spring-Hibernate DAO for java 5.0 (http://www.openmindlab.com/lab/products/bshd5.html) + * + * Copyright(C) ${project.inceptionYear}-2011, Openmind S.r.l. http://www.openmindonline.it + * + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * You may obtain a copy of the License at + * + * http://www.gnu.org/licenses/lgpl-2.1.html + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +package it.openutils.hibernate.paging; + +import java.util.Collection; + + +/** + * @author fgiust + * @version $Id$ + */ +public class PaginatedResultImpl<T> implements PaginatedResult<T> +{ + + private final int itemsPerPage; + + private final int pageNumberStartingFromOne; + + private final int totalSize; + + private Collection<T> results; + + /** + * Stable serialVersionUID. + */ + private static final long serialVersionUID = 42L; + + /** + * @param itemsPerPage + * @param pageNumberStartingFromOne + * @param totalSize + */ + public PaginatedResultImpl(int itemsPerPage, int pageNumberStartingFromOne, int totalSize) + { + this.itemsPerPage = itemsPerPage; + this.pageNumberStartingFromOne = pageNumberStartingFromOne; + this.totalSize = totalSize; + } + + /** + * {@inheritDoc} + */ + public int getItemsPerPage() + { + return itemsPerPage; + } + + /** + * {@inheritDoc} + */ + public int getPage() + { + return pageNumberStartingFromOne; + } + + /** + * {@inheritDoc} + */ + public int getTotalSize() + { + return totalSize; + } + + /** + * {@inheritDoc} + */ + public int getNumberOfPages() + { + return itemsPerPage > 0 ? (int) Math.round(Math.ceil(((float) totalSize / (float) itemsPerPage))) : 1; + } + + /** + * {@inheritDoc} + */ + public Collection<T> getItems() + { + return this.results; + } + + /** + * {@inheritDoc} + */ + public T getFirstResult() + { + if (this.results != null && this.results.size() > 0) + { + return this.results.iterator().next(); + } + return null; + } + + public void setResults(Collection<T> results) + { + this.results = results; + } + +} Property changes on: trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/paging/PaginatedResultImpl.java ___________________________________________________________________ Added: svn:mime-type + text/plain Added: svn:keywords + Author Date Id Revision Added: svn:eol-style + native Added: trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/paging/PaginatedResultUtils.java =================================================================== --- trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/paging/PaginatedResultUtils.java (rev 0) +++ trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/paging/PaginatedResultUtils.java 2011-06-04 07:59:28 UTC (rev 3498) @@ -0,0 +1,91 @@ +/** + * + * openutils base Spring-Hibernate DAO for java 5.0 (http://www.openmindlab.com/lab/products/bshd5.html) + * + * Copyright(C) ${project.inceptionYear}-2011, Openmind S.r.l. http://www.openmindonline.it + * + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * You may obtain a copy of the License at + * + * http://www.gnu.org/licenses/lgpl-2.1.html + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +package it.openutils.hibernate.paging; + +import java.util.Collection; + +import org.hibernate.Criteria; +import org.hibernate.criterion.Projections; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +/** + * @author fgiust + * @version $Id$ + */ +public final class PaginatedResultUtils +{ + + /** + * Logger. + */ + static Logger log = LoggerFactory.getLogger(PaginatedResultUtils.class); + + /** + * Executes a criteria query and returns a paginated result. + * @param <T> element type + * @param criteria criteria query + * @param pagesize number of elements per page + * @param pageNumberStartingFromOne page number, starting from 1 + * @return an instance of Paginated result + */ + @SuppressWarnings("unchecked") + public static <T> PaginatedResult<T> search(Criteria criteria, int pagesize, int pageNumberStartingFromOne) + { + if (pagesize > 0) + { + criteria.setMaxResults(pagesize); + } + + if (pageNumberStartingFromOne > 1 && pagesize > 0) + { + int firstresult = (pageNumberStartingFromOne - 1) * pagesize; + log.debug("Setting first result {}", firstresult); + criteria.setFirstResult(firstresult); + } + + Collection<T> results = criteria.list(); + + // reset firstresult for count + criteria.setFirstResult(0); + Number count = (Number) criteria.setProjection(Projections.count("id")).uniqueResult(); + + if (count == null) + { + count = 0; + } + + PaginatedResultImpl<T> result = new PaginatedResultImpl<T>( + pagesize, + pageNumberStartingFromOne, + count.intValue()); + + result.setResults(results); + + return result; + } +} Property changes on: trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/paging/PaginatedResultUtils.java ___________________________________________________________________ Added: svn:mime-type + text/plain Added: svn:keywords + Author Date Id Revision Added: svn:eol-style + native This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |