3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
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.
12 package eu
.etaxonomy
.cdm
.remote
.controller
;
14 import java
.io
.IOException
;
15 import static java
.net
.HttpURLConnection
.*;
16 import java
.util
.List
;
17 import java
.util
.UUID
;
19 import javax
.servlet
.http
.HttpServletRequest
;
20 import javax
.servlet
.http
.HttpServletResponse
;
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
;
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
.controller
.util
.PagerParameters
;
33 import eu
.etaxonomy
.cdm
.remote
.editor
.CdmTypePropertyEditor
;
34 import eu
.etaxonomy
.cdm
.remote
.editor
.UUIDPropertyEditor
;
38 * @author a.kohlbecker
45 public abstract class BaseListController
<T
extends CdmBase
, SERVICE
extends IService
<T
>> extends AbstractListController
<T
, SERVICE
> {
48 public void initBinder(WebDataBinder binder
) {
49 binder
.registerCustomEditor(UUID
.class, new UUIDPropertyEditor());
50 binder
.registerCustomEditor(Class
.class, new CdmTypePropertyEditor());
55 * NOTE: The indices for pages are 0-based see {@link Pager}
58 * the index of the page to be returned, the first page has the
59 * pageIndex = 0 - <i>optional parameter</i>
61 * the maximum number of entities returned per page (can be null
62 * to return all entities in a single page) - <i>optional
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>
70 @RequestMapping(method
= RequestMethod
.GET
, params
= "pageNumber")
71 public Pager
<T
> doPage(
72 @RequestParam(value
= "pageNumber") Integer pageIndex
,
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
79 logger
.info("doGet() " + request
.getServletPath());
80 PagerParameters pagerParameters
= new PagerParameters(pageSize
, pageIndex
);
81 pagerParameters
.normalizeAndValidate(response
);
83 return service
.page(type
, pagerParameters
.getPageSize(), pagerParameters
.getPageIndex(), null, DEFAULT_INIT_STRATEGY
);
87 * Parameter less method to be used as default when request without parameter are made. Otherwise
88 * the nameless methods {@link #doPage(Integer, Integer, Class)} and {@link #doList(Integer, Integer, Class)}
93 @RequestMapping(method
= RequestMethod
.GET
)
94 public Pager
<T
> doPage(HttpServletRequest request
, HttpServletResponse response
) throws IOException
{
95 return doPage(null, null, null, request
, response
);
100 * The offset index from the start of the list. The first entity
101 * has the index = 0 - <i>required parameter</i>
103 * The maximum number of entities returned. - <i>optional parameter</i>
104 * If limit is set to a value < 1 all entities will be returned
106 * Further restricts the type of entities to be returned.
107 * If null the base type <code><T></code> is being used. - <i>optional parameter</i>
108 * @return a List of entities
110 @RequestMapping(method
= RequestMethod
.GET
, params
= "start")
111 public List
<T
> doList(
112 @RequestParam(value
= "start", required
= true) Integer start
,
113 @RequestParam(value
= "limit", required
= false) Integer limit
,
114 @RequestParam(value
= "class", required
= false) Class
<T
> type
,
115 HttpServletRequest request
,
116 HttpServletResponse response
) {
118 logger
.info("doGet() " + request
.getServletPath());
119 //if(start == null){ start = 0;}
120 if(limit
== null){ limit
= PagerParameters
.DEFAULT_PAGESIZE
;}
121 if(limit
< 1){ limit
= null;}
122 return service
.list(type
, limit
, start
, null, DEFAULT_INIT_STRATEGY
);
126 @RequestMapping(method = RequestMethod.POST)
127 public T doPost(@ModelAttribute("object") T object, BindingResult result) {
128 validator.validate(object, result);
129 if (result.hasErrors()) {
130 // set http status code depending upon what happened, possibly return
131 // the put object and errors so that they can be rendered into a suitable error response
133 // should set the status to 201 created and "Location" header to "/resource/uuid"
134 service.save(object);