1
|
// $Id$
|
2
|
/**
|
3
|
* Copyright (C) 2007 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
|
|
12
|
package eu.etaxonomy.cdm.remote.controller;
|
13
|
|
14
|
import java.io.IOException;
|
15
|
import static java.net.HttpURLConnection.*;
|
16
|
import java.util.List;
|
17
|
import java.util.UUID;
|
18
|
|
19
|
import javax.servlet.http.HttpServletRequest;
|
20
|
import javax.servlet.http.HttpServletResponse;
|
21
|
|
22
|
import org.apache.http.protocol.HTTP;
|
23
|
import org.springframework.web.bind.WebDataBinder;
|
24
|
import org.springframework.web.bind.annotation.InitBinder;
|
25
|
import org.springframework.web.bind.annotation.RequestMapping;
|
26
|
import org.springframework.web.bind.annotation.RequestMethod;
|
27
|
import org.springframework.web.bind.annotation.RequestParam;
|
28
|
|
29
|
import eu.etaxonomy.cdm.api.service.IService;
|
30
|
import eu.etaxonomy.cdm.api.service.pager.Pager;
|
31
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
32
|
import eu.etaxonomy.cdm.remote.editor.CdmTypePropertyEditor;
|
33
|
import eu.etaxonomy.cdm.remote.editor.UUIDPropertyEditor;
|
34
|
|
35
|
|
36
|
/**
|
37
|
* @author a.kohlbecker
|
38
|
* @date 22.07.2009
|
39
|
*
|
40
|
* @param <T>
|
41
|
* @param <SERVICE>
|
42
|
*
|
43
|
*/
|
44
|
public abstract class BaseListController <T extends CdmBase, SERVICE extends IService<T>> extends AbstractListController<T, SERVICE> {
|
45
|
|
46
|
public static final Integer DEFAULT_PAGESIZE = 20;
|
47
|
public static final Integer DEFAULT_PAGE_NUMBER = 0;
|
48
|
|
49
|
@InitBinder
|
50
|
public void initBinder(WebDataBinder binder) {
|
51
|
binder.registerCustomEditor(UUID.class, new UUIDPropertyEditor());
|
52
|
binder.registerCustomEditor(Class.class, new CdmTypePropertyEditor());
|
53
|
}
|
54
|
|
55
|
|
56
|
/**
|
57
|
* @param pageNumber
|
58
|
* the number of the page to be returned, the first page has the
|
59
|
* pageNumber = 1 - <i>optional parameter</i>
|
60
|
* @param pageSize
|
61
|
* the maximum number of entities returned per page (can be null
|
62
|
* to return all entities in a single page) - <i>optional
|
63
|
* parameter</i>
|
64
|
* @param type
|
65
|
* Further restricts the type of entities to be returned.
|
66
|
* If null the base type <code><T></code> is being used. - <i>optional parameter</i>
|
67
|
* @return
|
68
|
* @throws IOException
|
69
|
*/
|
70
|
@RequestMapping(method = RequestMethod.GET, params = "pageNumber")
|
71
|
public Pager<T> doPage(
|
72
|
@RequestParam(value = "pageNumber") Integer pageNumber,
|
73
|
@RequestParam(value = "pageSize", required = false) Integer pageSize,
|
74
|
@RequestParam(value = "class", required = false) Class<T> type,
|
75
|
HttpServletRequest request,
|
76
|
HttpServletResponse response) throws IOException
|
77
|
{
|
78
|
|
79
|
normalizeAndValidatePagerParameters(pageNumber, pageSize, response);
|
80
|
|
81
|
return service.page(type, pageSize, pageNumber, null, DEFAULT_INIT_STRATEGY);
|
82
|
}
|
83
|
|
84
|
/**
|
85
|
* @param pageNumber
|
86
|
* @param pageSize
|
87
|
* @param response
|
88
|
* @throws IOException
|
89
|
*/
|
90
|
public static void normalizeAndValidatePagerParameters(Integer pageNumber, Integer pageSize, HttpServletResponse response) throws IOException{
|
91
|
|
92
|
if(pageNumber == null){
|
93
|
pageNumber = DEFAULT_PAGE_NUMBER;
|
94
|
if(pageSize == null){ pageSize = DEFAULT_PAGESIZE;}
|
95
|
}
|
96
|
if(pageNumber < 0){
|
97
|
HttpStatusMessage.fromString("The query parameter 'pageNumber' must not be a negative number").setStatusCode(HTTP_BAD_REQUEST).send(response);
|
98
|
}
|
99
|
if(pageSize < 0){
|
100
|
HttpStatusMessage.fromString("The query parameter 'pageSize' must not be a negative number").setStatusCode(HTTP_BAD_REQUEST).send(response);
|
101
|
}
|
102
|
}
|
103
|
|
104
|
/**
|
105
|
* Parameter less method to be used as default when request without parameter are made. Otherwise
|
106
|
* the nameless methods {@link #doPage(Integer, Integer, Class)} and {@link #doList(Integer, Integer, Class)}
|
107
|
* are ambigous.
|
108
|
* @return
|
109
|
* @throws IOException
|
110
|
*/
|
111
|
@RequestMapping(method = RequestMethod.GET)
|
112
|
public Pager<T> doPage(HttpServletRequest request, HttpServletResponse response) throws IOException{
|
113
|
return doPage(null, null, null, request, response);
|
114
|
}
|
115
|
|
116
|
/**
|
117
|
* @param start
|
118
|
* The offset index from the start of the list. The first entity
|
119
|
* has the index = 0 - <i>required parameter</i>
|
120
|
* @param limit
|
121
|
* The maximum number of entities returned. - <i>optional parameter</i>
|
122
|
* If limit is set to a value < 1 all entities will be returned
|
123
|
* @param type
|
124
|
* Further restricts the type of entities to be returned.
|
125
|
* If null the base type <code><T></code> is being used. - <i>optional parameter</i>
|
126
|
* @return a List of entities
|
127
|
*/
|
128
|
@RequestMapping(method = RequestMethod.GET, params = "start")
|
129
|
public List<T> doList(
|
130
|
@RequestParam(value = "start", required = true) Integer start,
|
131
|
@RequestParam(value = "limit", required = false) Integer limit,
|
132
|
@RequestParam(value = "class", required = false) Class<T> type) {
|
133
|
|
134
|
//if(start == null){ start = 0;}
|
135
|
if(limit == null){ limit = DEFAULT_PAGESIZE;}
|
136
|
if(limit < 1){ limit = null;}
|
137
|
return service.list(type, limit, start, null, DEFAULT_INIT_STRATEGY);
|
138
|
}
|
139
|
|
140
|
/* TODO
|
141
|
@RequestMapping(method = RequestMethod.POST)
|
142
|
public T doPost(@ModelAttribute("object") T object, BindingResult result) {
|
143
|
validator.validate(object, result);
|
144
|
if (result.hasErrors()) {
|
145
|
// set http status code depending upon what happened, possibly return
|
146
|
// the put object and errors so that they can be rendered into a suitable error response
|
147
|
} else {
|
148
|
// should set the status to 201 created and "Location" header to "/resource/uuid"
|
149
|
service.save(object);
|
150
|
}
|
151
|
}
|
152
|
*/
|
153
|
}
|