fixing ambious controller methods
[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.util.List;
15
16 import org.springframework.web.bind.WebDataBinder;
17 import org.springframework.web.bind.annotation.InitBinder;
18 import org.springframework.web.bind.annotation.RequestMapping;
19 import org.springframework.web.bind.annotation.RequestMethod;
20 import org.springframework.web.bind.annotation.RequestParam;
21
22 import eu.etaxonomy.cdm.api.service.IService;
23 import eu.etaxonomy.cdm.api.service.pager.Pager;
24 import eu.etaxonomy.cdm.model.common.CdmBase;
25 import eu.etaxonomy.cdm.remote.editor.ClassPropertyEditor;
26
27
28 /**
29 * @author a.kohlbecker
30 * @date 22.07.2009
31 *
32 * @param <T>
33 * @param <SERVICE>
34 *
35 */
36 public abstract class BaseListController <T extends CdmBase, SERVICE extends IService<T>> extends AbstractListController<T, SERVICE> {
37
38 public static final Integer DEFAULT_PAGESIZE = 20;
39 public static final Integer DEFAULT_PAGE_NUMBER = 0;
40
41 @InitBinder
42 public void initBinder(WebDataBinder binder) {
43 binder.registerCustomEditor(Class.class, new ClassPropertyEditor());
44 }
45
46
47 /**
48 * @param pageNumber
49 * the number of the page to be returned, the first page has the
50 * pageNumber = 1 - <i>optional parameter</i>
51 * @param pageSize
52 * the maximum number of entities returned per page (can be null
53 * to return all entities in a single page) - <i>optional
54 * parameter</i>
55 * @param type
56 * Further restricts the type of entities to be returned.
57 * If null the base type <code>&lt;T&gt;</code> is being used. - <i>optional parameter</i>
58 * @return
59 */
60 @RequestMapping(method = RequestMethod.GET)
61 public Pager<T> doPage(
62 @RequestParam(value = "pageNumber", required = false) Integer pageNumber,
63 @RequestParam(value = "pageSize", required = false) Integer pageSize,
64 @RequestParam(value = "class", required = false) Class<T> type) {
65
66 if(pageNumber == null){ pageNumber = DEFAULT_PAGE_NUMBER;}
67 if(pageSize == null){ pageSize = DEFAULT_PAGESIZE;}
68
69 return service.page(type, pageSize, pageNumber, null, DEFAULT_INIT_STRATEGY);
70 }
71
72 /**
73 * Parameter less method to be used as default when request without parameter are made. Otherwise
74 * the nameless methods {@link #doPage(Integer, Integer, Class)} and {@link #doList(Integer, Integer, Class)}
75 * are ambigous.
76 * @return
77 */
78 @RequestMapping(method = RequestMethod.GET)
79 public Pager<T> doPage(){
80 return doPage(null, null, null);
81 }
82
83 /**
84 * @param start
85 * The offset index from the start of the list. The first entity
86 * has the index = 0 - <i>required parameter</i>
87 * @param limit
88 * The maximum number of entities returned. - <i>optional parameter</i>
89 * @param type
90 * Further restricts the type of entities to be returned.
91 * If null the base type <code>&lt;T&gt;</code> is being used. - <i>optional parameter</i>
92 * @return a List of entities
93 */
94 @RequestMapping(method = RequestMethod.GET)
95 public List<T> doList(
96 @RequestParam(value = "start", required = true) Integer start,
97 @RequestParam(value = "limit", required = false) Integer limit,
98 @RequestParam(value = "class", required = false) Class<T> type) {
99
100 //if(start == null){ start = 0;}
101 if(limit == null){ limit = DEFAULT_PAGESIZE;}
102 //TODO implement initialization
103 return service.list(type, limit, start, null, DEFAULT_INIT_STRATEGY);
104 }
105
106 /* TODO
107 @RequestMapping(method = RequestMethod.POST)
108 public T doPost(@ModelAttribute("object") T object, BindingResult result) {
109 validator.validate(object, result);
110 if (result.hasErrors()) {
111 // set http status code depending upon what happened, possibly return
112 // the put object and errors so that they can be rendered into a suitable error response
113 } else {
114 // should set the status to 201 created and "Location" header to "/resource/uuid"
115 service.save(object);
116 }
117 }
118 */
119 }