refactoring taxon controllers, reducing code duplication
[cdmlib.git] / cdmlib-remote / src / main / java / eu / etaxonomy / cdm / remote / controller / BaseListController.java
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 java.util.List;
16 import java.util.UUID;
17
18 import javax.servlet.http.HttpServletRequest;
19 import javax.servlet.http.HttpServletResponse;
20
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;
27
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;
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 Logger logger = Logger.getLogger(BaseListController.class);
47
48 @InitBinder
49 public void initBinder(WebDataBinder binder) {
50 binder.registerCustomEditor(UUID.class, new UUIDPropertyEditor());
51 binder.registerCustomEditor(Class.class, new CdmTypePropertyEditor());
52 }
53
54
55 /**
56 * NOTE: The indices for pages are 0-based see {@link Pager}
57 *
58 * @param pageIndex
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>.
62 * @param pageSize
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>
66 * @param type
67 * Further restricts the type of entities to be returned.
68 * If null the base type <code>&lt;T&gt;</code> is being used. - <i>optional parameter</i>
69 * @return
70 * @throws IOException
71 */
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
79 {
80
81 logger.info("doPage() " + requestPathAndQuery(request));
82 PagerParameters pagerParameters = new PagerParameters(pageSize, pageIndex);
83 pagerParameters.normalizeAndValidate(response);
84
85 return service.page(type, pagerParameters.getPageSize(), pagerParameters.getPageIndex(), null, getInitializationStrategy());
86 }
87
88 // /**
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)}
91 // * are ambigous.
92 // * @return
93 // * @throws IOException
94 // */
95 // @RequestMapping(method = RequestMethod.GET)
96 // public Pager<T> doPage(HttpServletRequest request, HttpServletResponse response) throws IOException{
97 // return doPage(null, null, null, request, response);
98 // }
99
100 /**
101 * @param start
102 * The offset index from the start of the list. The first entity
103 * has the index = 0 - <i>required parameter</i>
104 * @param limit
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
107 * @param type
108 * Further restricts the type of entities to be returned.
109 * If null the base type <code>&lt;T&gt;</code> is being used. - <i>optional parameter</i>
110 * @return a List of entities
111 */
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) {
119
120 if (request != null)
121 {
122 logger.info("doList() " + requestPathAndQuery(request));
123 }
124
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());
129 }
130
131 /* TODO
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
138 } else {
139 // should set the status to 201 created and "Location" header to "/resource/uuid"
140 service.save(object);
141 }
142 }
143 */
144 }