2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
11 package eu
.etaxonomy
.cdm
.remote
.controller
;
13 import java
.io
.IOException
;
14 import java
.util
.List
;
15 import java
.util
.UUID
;
17 import javax
.servlet
.http
.HttpServletRequest
;
18 import javax
.servlet
.http
.HttpServletResponse
;
20 import org
.apache
.log4j
.Logger
;
21 import org
.springframework
.web
.bind
.WebDataBinder
;
22 import org
.springframework
.web
.bind
.annotation
.InitBinder
;
23 import org
.springframework
.web
.bind
.annotation
.RequestMapping
;
24 import org
.springframework
.web
.bind
.annotation
.RequestMethod
;
25 import org
.springframework
.web
.bind
.annotation
.RequestParam
;
27 import eu
.etaxonomy
.cdm
.api
.service
.IService
;
28 import eu
.etaxonomy
.cdm
.api
.service
.pager
.Pager
;
29 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
30 import eu
.etaxonomy
.cdm
.remote
.controller
.util
.PagerParameters
;
31 import eu
.etaxonomy
.cdm
.remote
.editor
.CdmTypePropertyEditor
;
32 import eu
.etaxonomy
.cdm
.remote
.editor
.UUIDPropertyEditor
;
36 * @author a.kohlbecker
42 public abstract class BaseListController
<T
extends CdmBase
, SERVICE
extends IService
<T
>> extends AbstractListController
<T
, SERVICE
> {
44 public static final Logger logger
= Logger
.getLogger(BaseListController
.class);
47 public void initBinder(WebDataBinder binder
) {
48 binder
.registerCustomEditor(UUID
.class, new UUIDPropertyEditor());
49 binder
.registerCustomEditor(Class
.class, new CdmTypePropertyEditor());
54 * NOTE: The indices for pages are 0-based see {@link Pager}
57 * the index of the page to be returned, the first page has the
58 * pageIndex = 0 - <i>optional parameter</i>. Defaults to 0 if
59 * set to <code>NULL</code>.
61 * the maximum number of entities returned per page.
62 * The {@link #DEFAULT_PAGE_SIZE} will be used if pageSize is set to
63 * <code>null</code> - <i>optional parameter</i>
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 @SuppressWarnings("unchecked")
71 @RequestMapping(method
= RequestMethod
.GET
)
72 public Pager
<T
> doPage(
73 @RequestParam(value
= "pageNumber", required
= false) Integer pageIndex
,
74 @RequestParam(value
= "pageSize", required
= false) Integer pageSize
,
75 @RequestParam(value
= "class", required
= false) Class type
,
76 @RequestParam(name
="orderBy", defaultValue
="BY_TITLE_CACHE_ASC", required
=true) OrderHintPreset orderBy
,
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
);
86 orderBy
= orderBy
.checkSuitableFor(type
);
87 // TODO how can we check in case type == null?
89 return service
.page(type
, pagerParameters
.getPageSize(), pagerParameters
.getPageIndex(), orderBy
.orderHints(), getInitializationStrategy());
93 // * Parameter less method to be used as default when request without parameter are made. Otherwise
94 // * the nameless methods {@link #doPage(Integer, Integer, Class)} and {@link #doList(Integer, Integer, Class)}
97 // * @throws IOException
99 // @RequestMapping(method = RequestMethod.GET)
100 // public Pager<T> doPage(HttpServletRequest request, HttpServletResponse response) throws IOException{
101 // return doPage(null, null, null, request, response);
106 * The offset index from the start of the list. The first entity
107 * has the index = 0 - <i>required parameter</i>
109 * The maximum number of entities returned. - <i>optional parameter</i>
110 * If limit is set to a value < 1 all entities will be returned
112 * Further restricts the type of entities to be returned.
113 * If null the base type <code><T></code> is being used. - <i>optional parameter</i>
114 * @return a List of entities
116 @RequestMapping(method
= RequestMethod
.GET
, params
= "start")
117 public List
<T
> doList(
118 @RequestParam(value
= "start", required
= true) Integer start
,
119 @RequestParam(value
= "limit", required
= false) Integer limit
,
120 @RequestParam(value
= "class", required
= false) Class
<T
> type
,
121 HttpServletRequest request
,
122 HttpServletResponse response
) {
126 logger
.info("doList() " + requestPathAndQuery(request
));
129 //if(start == null){ start = 0;}
130 if(limit
== null){ limit
= PagerParameters
.DEFAULT_PAGESIZE
;}
131 if(limit
< 1){ limit
= null;}
132 return service
.list(type
, limit
, start
, null, getInitializationStrategy());
136 @RequestMapping(method = RequestMethod.POST)
137 public T doPost(@ModelAttribute("object") T object, BindingResult result) {
138 validator.validate(object, result);
139 if (result.hasErrors()) {
140 // set http status code depending upon what happened, possibly return
141 // the put object and errors so that they can be rendered into a suitable error response
143 // should set the status to 201 created and "Location" header to "/resource/uuid"
144 service.save(object);