a little bit documentation
[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 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.controller.util.PagerParameters;
33 import eu.etaxonomy.cdm.remote.editor.CdmTypePropertyEditor;
34 import eu.etaxonomy.cdm.remote.editor.UUIDPropertyEditor;
35
36
37 /**
38 * @author a.kohlbecker
39 * @date 22.07.2009
40 *
41 * @param <T>
42 * @param <SERVICE>
43 *
44 */
45 public abstract class BaseListController <T extends CdmBase, SERVICE extends IService<T>> extends AbstractListController<T, SERVICE> {
46
47 @InitBinder
48 public void initBinder(WebDataBinder binder) {
49 binder.registerCustomEditor(UUID.class, new UUIDPropertyEditor());
50 binder.registerCustomEditor(Class.class, new CdmTypePropertyEditor());
51 }
52
53
54 /**
55 * NOTE: The indices for pages are 0-based see {@link Pager}
56 *
57 * @param pageIndex
58 * the index of the page to be returned, the first page has the
59 * pageIndex = 0 - <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>&lt;T&gt;</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 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
77 {
78
79 logger.info("doGet() " + request.getServletPath());
80 PagerParameters pagerParameters = new PagerParameters(pageSize, pageIndex);
81 pagerParameters.normalizeAndValidate(response);
82
83 return service.page(type, pagerParameters.getPageSize(), pagerParameters.getPageIndex(), null, DEFAULT_INIT_STRATEGY);
84 }
85
86 /**
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)}
89 * are ambigous.
90 * @return
91 * @throws IOException
92 */
93 @RequestMapping(method = RequestMethod.GET)
94 public Pager<T> doPage(HttpServletRequest request, HttpServletResponse response) throws IOException{
95 return doPage(null, null, null, request, response);
96 }
97
98 /**
99 * @param start
100 * The offset index from the start of the list. The first entity
101 * has the index = 0 - <i>required parameter</i>
102 * @param limit
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
105 * @param type
106 * Further restricts the type of entities to be returned.
107 * If null the base type <code>&lt;T&gt;</code> is being used. - <i>optional parameter</i>
108 * @return a List of entities
109 */
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) {
117
118 if (request != null)
119 {
120 logger.info("doGet() " + request.getServletPath());
121 }
122
123 //if(start == null){ start = 0;}
124 if(limit == null){ limit = PagerParameters.DEFAULT_PAGESIZE;}
125 if(limit < 1){ limit = null;}
126 return service.list(type, limit, start, null, DEFAULT_INIT_STRATEGY);
127 }
128
129 /* TODO
130 @RequestMapping(method = RequestMethod.POST)
131 public T doPost(@ModelAttribute("object") T object, BindingResult result) {
132 validator.validate(object, result);
133 if (result.hasErrors()) {
134 // set http status code depending upon what happened, possibly return
135 // the put object and errors so that they can be rendered into a suitable error response
136 } else {
137 // should set the status to 201 created and "Location" header to "/resource/uuid"
138 service.save(object);
139 }
140 }
141 */
142 }