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 java
.util
.List
;
16 import java
.util
.UUID
;
18 import javax
.servlet
.http
.HttpServletRequest
;
19 import javax
.servlet
.http
.HttpServletResponse
;
21 import org
.apache
.log4j
.Logger
;
22 import org
.springframework
.web
.bind
.WebDataBinder
;
23 import org
.springframework
.web
.bind
.annotation
.InitBinder
;
24 import org
.springframework
.web
.bind
.annotation
.RequestMapping
;
25 import org
.springframework
.web
.bind
.annotation
.RequestMethod
;
26 import org
.springframework
.web
.bind
.annotation
.RequestParam
;
28 import eu
.etaxonomy
.cdm
.api
.service
.IService
;
29 import eu
.etaxonomy
.cdm
.api
.service
.pager
.Pager
;
30 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
31 import eu
.etaxonomy
.cdm
.remote
.controller
.util
.PagerParameters
;
32 import eu
.etaxonomy
.cdm
.remote
.editor
.CdmTypePropertyEditor
;
33 import eu
.etaxonomy
.cdm
.remote
.editor
.UUIDPropertyEditor
;
37 * @author a.kohlbecker
44 public abstract class BaseListController
<T
extends CdmBase
, SERVICE
extends IService
<T
>> extends AbstractListController
<T
, SERVICE
> {
46 public static final Logger logger
= Logger
.getLogger(BaseListController
.class);
49 public void initBinder(WebDataBinder binder
) {
50 binder
.registerCustomEditor(UUID
.class, new UUIDPropertyEditor());
51 binder
.registerCustomEditor(Class
.class, new CdmTypePropertyEditor());
56 * NOTE: The indices for pages are 0-based see {@link Pager}
59 * the index of the page to be returned, the first page has the
60 * pageIndex = 0 - <i>optional parameter</i>. Defaults to 0 if
61 * set to <code>NULL</code>.
63 * the maximum number of entities returned per page.
64 * The {@link #DEFAULT_PAGE_SIZE} will be used if pageSize is set to
65 * <code>null</code> - <i>optional parameter</i>
67 * Further restricts the type of entities to be returned.
68 * If null the base type <code><T></code> is being used. - <i>optional parameter</i>
72 @RequestMapping(method
= RequestMethod
.GET
)
73 public Pager
<T
> doPage(
74 @RequestParam(value
= "pageNumber", required
= false) Integer pageIndex
,
75 @RequestParam(value
= "pageSize", required
= false) Integer pageSize
,
76 @RequestParam(value
= "class", required
= false) Class type
,
77 HttpServletRequest request
,
78 HttpServletResponse response
) throws IOException
81 logger
.info("doPage() " + requestPathAndQuery(request
));
82 PagerParameters pagerParameters
= new PagerParameters(pageSize
, pageIndex
);
83 pagerParameters
.normalizeAndValidate(response
);
85 return service
.page(type
, pagerParameters
.getPageSize(), pagerParameters
.getPageIndex(), null, getInitializationStrategy());
89 // * Parameter less method to be used as default when request without parameter are made. Otherwise
90 // * the nameless methods {@link #doPage(Integer, Integer, Class)} and {@link #doList(Integer, Integer, Class)}
93 // * @throws IOException
95 // @RequestMapping(method = RequestMethod.GET)
96 // public Pager<T> doPage(HttpServletRequest request, HttpServletResponse response) throws IOException{
97 // return doPage(null, null, null, request, response);
102 * The offset index from the start of the list. The first entity
103 * has the index = 0 - <i>required parameter</i>
105 * The maximum number of entities returned. - <i>optional parameter</i>
106 * If limit is set to a value < 1 all entities will be returned
108 * Further restricts the type of entities to be returned.
109 * If null the base type <code><T></code> is being used. - <i>optional parameter</i>
110 * @return a List of entities
112 @RequestMapping(method
= RequestMethod
.GET
, params
= "start")
113 public List
<T
> doList(
114 @RequestParam(value
= "start", required
= true) Integer start
,
115 @RequestParam(value
= "limit", required
= false) Integer limit
,
116 @RequestParam(value
= "class", required
= false) Class
<T
> type
,
117 HttpServletRequest request
,
118 HttpServletResponse response
) {
122 logger
.info("doList() " + requestPathAndQuery(request
));
125 //if(start == null){ start = 0;}
126 if(limit
== null){ limit
= PagerParameters
.DEFAULT_PAGESIZE
;}
127 if(limit
< 1){ limit
= null;}
128 return service
.list(type
, limit
, start
, null, getInitializationStrategy());
132 @RequestMapping(method = RequestMethod.POST)
133 public T doPost(@ModelAttribute("object") T object, BindingResult result) {
134 validator.validate(object, result);
135 if (result.hasErrors()) {
136 // set http status code depending upon what happened, possibly return
137 // the put object and errors so that they can be rendered into a suitable error response
139 // should set the status to 201 created and "Location" header to "/resource/uuid"
140 service.save(object);