X-Git-Url: https://dev.e-taxonomy.eu/gitweb/cdmlib.git/blobdiff_plain/39f75c90dab49a7856a67ab62ef44d27f5047ca8..47260ff304c046fd1f6afc7d781d6468b5bc49d6:/cdmlib-remote/src/main/java/eu/etaxonomy/cdm/remote/controller/BaseListController.java diff --git a/cdmlib-remote/src/main/java/eu/etaxonomy/cdm/remote/controller/BaseListController.java b/cdmlib-remote/src/main/java/eu/etaxonomy/cdm/remote/controller/BaseListController.java index d23a96f44b..78f1d168a4 100644 --- a/cdmlib-remote/src/main/java/eu/etaxonomy/cdm/remote/controller/BaseListController.java +++ b/cdmlib-remote/src/main/java/eu/etaxonomy/cdm/remote/controller/BaseListController.java @@ -1,9 +1,9 @@ // $Id$ /** * Copyright (C) 2007 EDIT - * European Distributed Institute of Taxonomy + * European Distributed Institute of Taxonomy * http://www.e-taxonomy.eu - * + * * The contents of this file are subject to the Mozilla Public License Version 1.1 * See LICENSE.TXT at the top of this package for the full license terms. */ @@ -29,7 +29,8 @@ import org.springframework.web.bind.annotation.RequestParam; import eu.etaxonomy.cdm.api.service.IService; import eu.etaxonomy.cdm.api.service.pager.Pager; import eu.etaxonomy.cdm.model.common.CdmBase; -import eu.etaxonomy.cdm.remote.editor.ClassPropertyEditor; +import eu.etaxonomy.cdm.remote.controller.util.PagerParameters; +import eu.etaxonomy.cdm.remote.editor.CdmTypePropertyEditor; import eu.etaxonomy.cdm.remote.editor.UUIDPropertyEditor; @@ -39,105 +40,85 @@ import eu.etaxonomy.cdm.remote.editor.UUIDPropertyEditor; * * @param * @param - * + * */ public abstract class BaseListController > extends AbstractListController { - public static final Integer DEFAULT_PAGESIZE = 20; - public static final Integer DEFAULT_PAGE_NUMBER = 0; - - @InitBinder + @InitBinder public void initBinder(WebDataBinder binder) { - binder.registerCustomEditor(UUID.class, new UUIDPropertyEditor()); - binder.registerCustomEditor(Class.class, new ClassPropertyEditor()); - } - - - /** - * @param pageNumber - * the number of the page to be returned, the first page has the - * pageNumber = 1 - optional parameter - * @param pageSize - * the maximum number of entities returned per page (can be null - * to return all entities in a single page) - optional - * parameter - * @param type - * Further restricts the type of entities to be returned. - * If null the base type <T> is being used. - optional parameter - * @return - * @throws IOException - */ - @RequestMapping(method = RequestMethod.GET, params = "pageNumber") - public Pager doPage( - @RequestParam(value = "pageNumber") Integer pageNumber, - @RequestParam(value = "pageSize", required = false) Integer pageSize, - @RequestParam(value = "class", required = false) Class type, - HttpServletRequest request, - HttpServletResponse response) throws IOException - { - - normalizeAndValidatePagerParameters(pageNumber, pageSize, response); - - return service.page(type, pageSize, pageNumber, null, DEFAULT_INIT_STRATEGY); - } - - /** - * @param pageNumber - * @param pageSize - * @param response - * @throws IOException - */ - public static void normalizeAndValidatePagerParameters(Integer pageNumber, Integer pageSize, HttpServletResponse response) throws IOException{ - - if(pageNumber == null){ - pageNumber = DEFAULT_PAGE_NUMBER; - if(pageSize == null){ pageSize = DEFAULT_PAGESIZE;} - } - if(pageNumber < 0){ - HttpStatusMessage.fromString("The query parameter 'pageNumber' must not be a negative number").setStatusCode(HTTP_BAD_REQUEST).send(response); - } - if(pageSize < 0){ - HttpStatusMessage.fromString("The query parameter 'pageSize' must not be a negative number").setStatusCode(HTTP_BAD_REQUEST).send(response); - } - } - - /** - * Parameter less method to be used as default when request without parameter are made. Otherwise - * the nameless methods {@link #doPage(Integer, Integer, Class)} and {@link #doList(Integer, Integer, Class)} - * are ambigous. - * @return - * @throws IOException - */ - @RequestMapping(method = RequestMethod.GET) - public Pager doPage(HttpServletRequest request, HttpServletResponse response) throws IOException{ - return doPage(null, null, null, request, response); - } - - /** - * @param start - * The offset index from the start of the list. The first entity - * has the index = 0 - required parameter - * @param limit - * The maximum number of entities returned. - optional parameter - * If limit is set to a value < 1 all entities will be returned - * @param type - * Further restricts the type of entities to be returned. - * If null the base type <T> is being used. - optional parameter - * @return a List of entities - */ - @RequestMapping(method = RequestMethod.GET, params = "start") - public List doList( - @RequestParam(value = "start", required = true) Integer start, - @RequestParam(value = "limit", required = false) Integer limit, - @RequestParam(value = "class", required = false) Class type) { - - //if(start == null){ start = 0;} - if(limit == null){ limit = DEFAULT_PAGESIZE;} - if(limit < 1){ limit = null;} - return service.list(type, limit, start, null, DEFAULT_INIT_STRATEGY); - } - - /* TODO + binder.registerCustomEditor(UUID.class, new UUIDPropertyEditor()); + binder.registerCustomEditor(Class.class, new CdmTypePropertyEditor()); + } + + + /** + * NOTE: The indices for pages are 0-based see {@link Pager} + * + * @param pageIndex + * the index of the page to be returned, the first page has the + * pageIndex = 0 - optional parameter + * @param pageSize + * the maximum number of entities returned per page (can be null + * to return all entities in a single page) - optional + * parameter + * @param type + * Further restricts the type of entities to be returned. + * If null the base type <T> is being used. - optional parameter + * @return + * @throws IOException + */ + @RequestMapping(method = RequestMethod.GET, params = "pageNumber") + public Pager doPage( + @RequestParam(value = "pageNumber") Integer pageIndex, + @RequestParam(value = "pageSize", required = false) Integer pageSize, + @RequestParam(value = "class", required = false) Class type, + HttpServletRequest request, + HttpServletResponse response) throws IOException + { + + PagerParameters pagerParameters = new PagerParameters(pageSize, pageIndex); + pagerParameters.normalizeAndValidate(response); + + return service.page(type, pagerParameters.getPageSize(), pagerParameters.getPageIndex(), null, DEFAULT_INIT_STRATEGY); + } + + /** + * Parameter less method to be used as default when request without parameter are made. Otherwise + * the nameless methods {@link #doPage(Integer, Integer, Class)} and {@link #doList(Integer, Integer, Class)} + * are ambigous. + * @return + * @throws IOException + */ + @RequestMapping(method = RequestMethod.GET) + public Pager doPage(HttpServletRequest request, HttpServletResponse response) throws IOException{ + return doPage(null, null, null, request, response); + } + + /** + * @param start + * The offset index from the start of the list. The first entity + * has the index = 0 - required parameter + * @param limit + * The maximum number of entities returned. - optional parameter + * If limit is set to a value < 1 all entities will be returned + * @param type + * Further restricts the type of entities to be returned. + * If null the base type <T> is being used. - optional parameter + * @return a List of entities + */ + @RequestMapping(method = RequestMethod.GET, params = "start") + public List doList( + @RequestParam(value = "start", required = true) Integer start, + @RequestParam(value = "limit", required = false) Integer limit, + @RequestParam(value = "class", required = false) Class type) { + + //if(start == null){ start = 0;} + if(limit == null){ limit = PagerParameters.DEFAULT_PAGESIZE;} + if(limit < 1){ limit = null;} + return service.list(type, limit, start, null, DEFAULT_INIT_STRATEGY); + } + + /* TODO @RequestMapping(method = RequestMethod.POST) public T doPost(@ModelAttribute("object") T object, BindingResult result) { validator.validate(object, result);