From df96dd8e68c135da0e8a11144cfcf24b77e525b7 Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Wed, 12 Oct 2011 11:36:59 +0000 Subject: [PATCH] ALF-10429 Refactor the common new webscript code for building a PagingRequest object git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@31159 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261 --- .../alfresco/util/ScriptPagingDetails.java | 87 +++++++++++++++++++ 1 file changed, 87 insertions(+) diff --git a/source/java/org/alfresco/util/ScriptPagingDetails.java b/source/java/org/alfresco/util/ScriptPagingDetails.java index 031c12cbcb..051f6210fc 100644 --- a/source/java/org/alfresco/util/ScriptPagingDetails.java +++ b/source/java/org/alfresco/util/ScriptPagingDetails.java @@ -2,6 +2,9 @@ package org.alfresco.util; import org.alfresco.query.PagingRequest; import org.alfresco.query.PagingResults; +import org.springframework.extensions.webscripts.Status; +import org.springframework.extensions.webscripts.WebScriptException; +import org.springframework.extensions.webscripts.WebScriptRequest; /** * A simple paging details wrapper, to hold things like the @@ -41,6 +44,90 @@ public class ScriptPagingDetails extends PagingRequest super(skipCount, maxItems, queryExecutionId); } + public ScriptPagingDetails(PagingRequest paging) + { + super(paging.getSkipCount(), paging.getMaxItems(), paging.getQueryExecutionId()); + setRequestTotalCountMax(paging.getRequestTotalCountMax()); + } + + /** + * Creates a new {@link PagingRequest} object (in the form of + * {@link ScriptPagingDetails}), based on the standard URL + * parameters for webscript paging. + * + * @param req The request object to extract parameters from + * @param maxResultCount The maximum results count if none is specified + */ + public ScriptPagingDetails(WebScriptRequest req, int maxResultCount) throws WebScriptException + { + this(buildPagingRequest(req, maxResultCount)); + } + /** + * Creates a new {@link PagingRequest} object, based on the standard URL + * parameters for webscript paging. + * + * @param req The request object to extract parameters from + * @param maxResultCount The maximum results count if none is specified + */ + public static PagingRequest buildPagingRequest(WebScriptRequest req, int maxResultCount) throws WebScriptException + { + int pageSize = maxResultCount; + int startIndex = 0; + + String queryId = req.getParameter("queryId"); + + String pageSizeS = req.getParameter("pageSize"); + if (pageSizeS != null) + { + try + { + pageSize = Integer.parseInt(pageSizeS); + } + catch (NumberFormatException e) + { + throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Paging size parameters invalid"); + } + } + + String startIndexS = req.getParameter("startIndex"); + if (startIndexS != null) + { + try + { + startIndex = Integer.parseInt(startIndexS); + } + catch (NumberFormatException e) + { + throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Paging size parameters invalid"); + } + } + else + { + // No Start Index given, did they supply a Page Number? + String pageNumberS = req.getParameter("page"); + if(pageNumberS != null) + { + try + { + int pageNumber = Integer.parseInt(pageNumberS); + startIndex = (pageNumber-1) * pageSize; + } + catch (NumberFormatException e) + { + throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Paging size parameters invalid"); + } + } + } + + PagingRequest paging = new PagingRequest( startIndex, pageSize, queryId ); + + // The default total count is the higher of page 10, or 2 pages further + paging.setRequestTotalCountMax( Math.max(10*pageSize,startIndex+2*pageSize) ); + + // All done + return paging; + } + public ItemsSizeConfidence getConfidence() { return confidence;