1
|
// $Id: TaxonController.java 5473 2009-03-25 13:42:07Z a.kohlbecker $
|
2
|
/**
|
3
|
* Copyright (C) 2011 EDIT
|
4
|
* European Distributed Institute of Taxonomy
|
5
|
* http://www.e-taxonomy.eu
|
6
|
*
|
7
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
8
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
9
|
*/
|
10
|
|
11
|
package eu.etaxonomy.cdm.remote.controller.util;
|
12
|
|
13
|
import static java.net.HttpURLConnection.HTTP_BAD_REQUEST;
|
14
|
|
15
|
import java.io.IOException;
|
16
|
|
17
|
import javax.servlet.http.HttpServletResponse;
|
18
|
|
19
|
import eu.etaxonomy.cdm.api.service.pager.Pager;
|
20
|
import eu.etaxonomy.cdm.api.service.pager.PagerUtils;
|
21
|
import eu.etaxonomy.cdm.remote.controller.HttpStatusMessage;
|
22
|
|
23
|
/**
|
24
|
*
|
25
|
* NOTE: As the indices for objects and pages are 0-based in {@link Pager} the
|
26
|
* <code>pageNumber</code> property of this class also follows this principle.
|
27
|
*
|
28
|
* TODO consider merging this class with {@link PagerUtils}
|
29
|
*
|
30
|
* @author a.kohlbecker
|
31
|
* @date 22.08.2011
|
32
|
*
|
33
|
*/
|
34
|
public class PagerParameters {
|
35
|
|
36
|
private Integer pageSize;
|
37
|
|
38
|
private Integer pageIndex;
|
39
|
|
40
|
public static final Integer DEFAULT_PAGESIZE = 30;
|
41
|
|
42
|
public void setPageSize(Integer pageSize) {
|
43
|
this.pageSize = pageSize;
|
44
|
}
|
45
|
|
46
|
public Integer getPageSize() {
|
47
|
return pageSize;
|
48
|
}
|
49
|
|
50
|
/**
|
51
|
* NOTE: As the indices for objects and pages are 0-based
|
52
|
* @param pageIndex
|
53
|
*/
|
54
|
public void setPageIndex(Integer pageIndex) {
|
55
|
this.pageIndex = pageIndex;
|
56
|
}
|
57
|
|
58
|
/**
|
59
|
* NOTE: As the indices for objects and pages are 0-based
|
60
|
* @return
|
61
|
*/
|
62
|
public Integer getPageIndex() {
|
63
|
return pageIndex;
|
64
|
}
|
65
|
|
66
|
public PagerParameters(Integer pageSize, Integer pageIndex) {
|
67
|
this.pageSize = pageSize;
|
68
|
this.pageIndex = pageIndex;
|
69
|
}
|
70
|
|
71
|
/**
|
72
|
* Normalizes this <code>PagerParameters</code> according to the following
|
73
|
* rules and responds with an HTTP error in case of invalid parameters.
|
74
|
* <ul>
|
75
|
* <li>pageIndex defaults to 0 if set to <code>NULL</code>.</li>
|
76
|
* <li>pageSize defaults to {@link #DEFAULT_PAGESIZE} if set to <code>NULL</code>.</li>
|
77
|
* <li>Sends {@link HTTP_BAD_REQUEST} if <code>pageIndex</code> or <code>pageSize</code> are smaller than 0.
|
78
|
* </ul>
|
79
|
* @param response
|
80
|
* @throws IOException
|
81
|
*/
|
82
|
public void normalizeAndValidate(HttpServletResponse response) throws IOException{
|
83
|
|
84
|
if(pageIndex == null){
|
85
|
pageIndex = 0;
|
86
|
}
|
87
|
if(pageSize == null){
|
88
|
pageSize = DEFAULT_PAGESIZE;
|
89
|
}
|
90
|
if(pageIndex < 0){
|
91
|
HttpStatusMessage.fromString("The query parameter 'pageIndex' must not be a negative number").setStatusCode(HTTP_BAD_REQUEST).send(response);
|
92
|
}
|
93
|
if(pageSize < 0){
|
94
|
HttpStatusMessage.fromString("The query parameter 'pageSize' must not be a negative number").setStatusCode(HTTP_BAD_REQUEST).send(response);
|
95
|
}
|
96
|
}
|
97
|
|
98
|
|
99
|
}
|