// $Id$\r
/**\r
* Copyright (C) 2007 EDIT\r
- * European Distributed Institute of Taxonomy \r
+ * European Distributed Institute of Taxonomy\r
* http://www.e-taxonomy.eu\r
- * \r
+ *\r
* The contents of this file are subject to the Mozilla Public License Version 1.1\r
* See LICENSE.TXT at the top of this package for the full license terms.\r
*/\r
\r
package eu.etaxonomy.cdm.remote.controller;\r
\r
+import java.io.IOException;\r
+import static java.net.HttpURLConnection.*;\r
+import java.util.List;\r
+import java.util.UUID;\r
+\r
+import javax.servlet.http.HttpServletRequest;\r
+import javax.servlet.http.HttpServletResponse;\r
+\r
+import org.apache.http.protocol.HTTP;\r
+import org.springframework.web.bind.WebDataBinder;\r
+import org.springframework.web.bind.annotation.InitBinder;\r
import org.springframework.web.bind.annotation.RequestMapping;\r
import org.springframework.web.bind.annotation.RequestMethod;\r
import org.springframework.web.bind.annotation.RequestParam;\r
import eu.etaxonomy.cdm.api.service.IService;\r
import eu.etaxonomy.cdm.api.service.pager.Pager;\r
import eu.etaxonomy.cdm.model.common.CdmBase;\r
+import eu.etaxonomy.cdm.remote.controller.util.PagerParameters;\r
+import eu.etaxonomy.cdm.remote.editor.CdmTypePropertyEditor;\r
+import eu.etaxonomy.cdm.remote.editor.UUIDPropertyEditor;\r
\r
\r
+/**\r
+ * @author a.kohlbecker\r
+ * @date 22.07.2009\r
+ *\r
+ * @param <T>\r
+ * @param <SERVICE>\r
+ *\r
+ */\r
public abstract class BaseListController <T extends CdmBase, SERVICE extends IService<T>> extends AbstractListController<T, SERVICE> {\r
\r
- public static final Integer DEFAULT_PAGESIZE = 20;\r
- public static final Integer DEFAULT_PAGE = 1;\r
- \r
- @RequestMapping(method = RequestMethod.GET)\r
- public Pager<T> doGet(\r
- @RequestParam(value = "page", required = false) Integer page,\r
- @RequestParam(value = "pageSize", required = false) Integer pageSize) {\r
- \r
- if(page == null){ page = DEFAULT_PAGE;}\r
- if(pageSize == null){ pageSize = DEFAULT_PAGESIZE;}\r
- \r
- return (Pager<T>) service.list(pageSize, page);\r
- }\r
-\r
- /* TODO \r
+ @InitBinder\r
+ public void initBinder(WebDataBinder binder) {\r
+ binder.registerCustomEditor(UUID.class, new UUIDPropertyEditor());\r
+ binder.registerCustomEditor(Class.class, new CdmTypePropertyEditor());\r
+ }\r
+\r
+\r
+ /**\r
+ * NOTE: The indices for pages are 0-based see {@link Pager}\r
+ *\r
+ * @param pageIndex\r
+ * the index of the page to be returned, the first page has the\r
+ * pageIndex = 0 - <i>optional parameter</i>\r
+ * @param pageSize\r
+ * the maximum number of entities returned per page (can be null\r
+ * to return all entities in a single page) - <i>optional\r
+ * parameter</i>\r
+ * @param type\r
+ * Further restricts the type of entities to be returned.\r
+ * If null the base type <code><T></code> is being used. - <i>optional parameter</i>\r
+ * @return\r
+ * @throws IOException\r
+ */\r
+ @RequestMapping(method = RequestMethod.GET, params = "pageNumber")\r
+ public Pager<T> doPage(\r
+ @RequestParam(value = "pageNumber") Integer pageIndex,\r
+ @RequestParam(value = "pageSize", required = false) Integer pageSize,\r
+ @RequestParam(value = "class", required = false) Class<T> type,\r
+ HttpServletRequest request,\r
+ HttpServletResponse response) throws IOException\r
+ {\r
+\r
+ logger.info("doGet() " + request.getServletPath());\r
+ PagerParameters pagerParameters = new PagerParameters(pageSize, pageIndex);\r
+ pagerParameters.normalizeAndValidate(response);\r
+\r
+ return service.page(type, pagerParameters.getPageSize(), pagerParameters.getPageIndex(), null, DEFAULT_INIT_STRATEGY);\r
+ }\r
+\r
+ /**\r
+ * Parameter less method to be used as default when request without parameter are made. Otherwise\r
+ * the nameless methods {@link #doPage(Integer, Integer, Class)} and {@link #doList(Integer, Integer, Class)}\r
+ * are ambigous.\r
+ * @return\r
+ * @throws IOException\r
+ */\r
+ @RequestMapping(method = RequestMethod.GET)\r
+ public Pager<T> doPage(HttpServletRequest request, HttpServletResponse response) throws IOException{\r
+ return doPage(null, null, null, request, response);\r
+ }\r
+\r
+ /**\r
+ * @param start\r
+ * The offset index from the start of the list. The first entity\r
+ * has the index = 0 - <i>required parameter</i>\r
+ * @param limit\r
+ * The maximum number of entities returned. - <i>optional parameter</i>\r
+ * If limit is set to a value < 1 all entities will be returned\r
+ * @param type\r
+ * Further restricts the type of entities to be returned.\r
+ * If null the base type <code><T></code> is being used. - <i>optional parameter</i>\r
+ * @return a List of entities\r
+ */\r
+ @RequestMapping(method = RequestMethod.GET, params = "start")\r
+ public List<T> doList(\r
+ @RequestParam(value = "start", required = true) Integer start,\r
+ @RequestParam(value = "limit", required = false) Integer limit,\r
+ @RequestParam(value = "class", required = false) Class<T> type,\r
+ HttpServletRequest request,\r
+ HttpServletResponse response) {\r
+\r
+ logger.info("doGet() " + request.getServletPath());\r
+ //if(start == null){ start = 0;}\r
+ if(limit == null){ limit = PagerParameters.DEFAULT_PAGESIZE;}\r
+ if(limit < 1){ limit = null;}\r
+ return service.list(type, limit, start, null, DEFAULT_INIT_STRATEGY);\r
+ }\r
+\r
+ /* TODO\r
@RequestMapping(method = RequestMethod.POST)\r
public T doPost(@ModelAttribute("object") T object, BindingResult result) {\r
validator.validate(object, result);\r