Project

General

Profile

Download (2.82 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2011 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9

    
10
package eu.etaxonomy.cdm.remote.controller.util;
11

    
12
import static java.net.HttpURLConnection.HTTP_BAD_REQUEST;
13

    
14
import java.io.IOException;
15

    
16
import javax.servlet.http.HttpServletResponse;
17

    
18
import eu.etaxonomy.cdm.api.service.pager.Pager;
19
import eu.etaxonomy.cdm.api.service.pager.PagerUtils;
20
import eu.etaxonomy.cdm.remote.controller.HttpStatusMessage;
21

    
22
/**
23
 *
24
 * NOTE: As the indices for objects and pages are 0-based in {@link Pager} the
25
 * <code>pageNumber</code> property of this class also follows this principle.
26
 *
27
 * TODO consider merging this class with {@link PagerUtils}
28
 *
29
 * @author a.kohlbecker
30
 * @since 22.08.2011
31
 *
32
 */
33
public class PagerParameters {
34

    
35
    private Integer pageSize;
36

    
37
    private Integer pageIndex;
38

    
39
    public static final Integer DEFAULT_PAGESIZE = 30;
40

    
41
    public void setPageSize(Integer pageSize) {
42
        this.pageSize = pageSize;
43
    }
44

    
45
    public Integer getPageSize() {
46
        return pageSize;
47
    }
48

    
49
    /**
50
     * NOTE: As the indices for objects and pages are 0-based
51
     * @param pageIndex
52
     */
53
    public void setPageIndex(Integer pageIndex) {
54
        this.pageIndex = pageIndex;
55
    }
56

    
57
    /**
58
     * NOTE: As the indices for objects and pages are 0-based
59
     * @return
60
     */
61
    public Integer getPageIndex() {
62
        return pageIndex;
63
    }
64

    
65
    public PagerParameters(Integer pageSize, Integer pageIndex) {
66
        this.pageSize = pageSize;
67
        this.pageIndex = pageIndex;
68
    }
69

    
70
    /**
71
     * Normalizes this <code>PagerParameters</code> according to the following
72
     * rules and responds with an HTTP error in case of invalid parameters.
73
     * <ul>
74
     * <li>pageIndex defaults to 0 if set to <code>NULL</code>.</li>
75
     * <li>pageSize defaults to {@link #DEFAULT_PAGESIZE} if set to <code>NULL</code>.</li>
76
     * <li>Sends {@link HTTP_BAD_REQUEST} if <code>pageIndex</code> or <code>pageSize</code> are smaller than 0.
77
     * </ul>
78
     * @param response
79
     * @throws IOException
80
     */
81
    public PagerParameters normalizeAndValidate(HttpServletResponse response) throws IOException{
82

    
83
        if(pageIndex == null){
84
            pageIndex = 0;
85
        }
86
        if(pageSize == null){
87
            pageSize = DEFAULT_PAGESIZE;
88
        }
89
        if(pageIndex < 0){
90
            HttpStatusMessage.create("The query parameter 'pageIndex' must not be a negative number", 400).setStatusCode(HTTP_BAD_REQUEST).send(response);
91
        }
92
        if(pageSize < 0){
93
            HttpStatusMessage.create("The query parameter 'pageSize' must not be a negative number", 400).setStatusCode(HTTP_BAD_REQUEST).send(response);
94
        }
95
        return this;
96
    }
97

    
98

    
99
}
(4-4/5)