fix clearCache handling and more progressMonitor subtasks
[cdmlib.git] / cdmlib-remote / src / main / java / eu / etaxonomy / cdm / remote / controller / BaseListController.java
1 /**
2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
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.
8 */
9
10
11 package eu.etaxonomy.cdm.remote.controller;
12
13 import java.io.IOException;
14 import java.util.List;
15 import java.util.UUID;
16
17 import javax.servlet.http.HttpServletRequest;
18 import javax.servlet.http.HttpServletResponse;
19
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;
26
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;
33
34
35 /**
36 * @author a.kohlbecker
37 * @date 22.07.2009
38 *
39 * @param <T>
40 * @param <SERVICE>
41 */
42 public abstract class BaseListController <T extends CdmBase, SERVICE extends IService<T>> extends AbstractListController<T, SERVICE> {
43
44 public static final Logger logger = Logger.getLogger(BaseListController.class);
45
46 @InitBinder
47 public void initBinder(WebDataBinder binder) {
48 binder.registerCustomEditor(UUID.class, new UUIDPropertyEditor());
49 binder.registerCustomEditor(Class.class, new CdmTypePropertyEditor());
50 }
51
52
53 /**
54 * NOTE: The indices for pages are 0-based see {@link Pager}
55 *
56 * @param pageIndex
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>.
60 * @param pageSize
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>
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 @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
79 {
80
81 logger.info("doPage() " + requestPathAndQuery(request));
82 PagerParameters pagerParameters = new PagerParameters(pageSize, pageIndex);
83 pagerParameters.normalizeAndValidate(response);
84
85 if(type != null) {
86 orderBy = orderBy.checkSuitableFor(type);
87 // TODO how can we check in case type == null?
88 }
89 return service.page(type, pagerParameters.getPageSize(), pagerParameters.getPageIndex(), orderBy.orderHints(), getInitializationStrategy());
90 }
91
92 // /**
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)}
95 // * are ambigous.
96 // * @return
97 // * @throws IOException
98 // */
99 // @RequestMapping(method = RequestMethod.GET)
100 // public Pager<T> doPage(HttpServletRequest request, HttpServletResponse response) throws IOException{
101 // return doPage(null, null, null, request, response);
102 // }
103
104 /**
105 * @param start
106 * The offset index from the start of the list. The first entity
107 * has the index = 0 - <i>required parameter</i>
108 * @param limit
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
111 * @param type
112 * Further restricts the type of entities to be returned.
113 * If null the base type <code>&lt;T&gt;</code> is being used. - <i>optional parameter</i>
114 * @return a List of entities
115 */
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) {
123
124 if (request != null)
125 {
126 logger.info("doList() " + requestPathAndQuery(request));
127 }
128
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());
133 }
134
135 /* TODO
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
142 } else {
143 // should set the status to 201 created and "Location" header to "/resource/uuid"
144 service.save(object);
145 }
146 }
147 */
148 }