refactoring taxon controllers, reducing code duplication
[cdmlib.git] / cdmlib-remote / src / main / java / eu / etaxonomy / cdm / remote / controller / IdentifiableListController.java
1 // $Id$
2 /**
3 * Copyright (C) 2009 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 package eu.etaxonomy.cdm.remote.controller;
11
12 import java.io.IOException;
13
14 import javax.servlet.http.HttpServletRequest;
15 import javax.servlet.http.HttpServletResponse;
16
17 import org.springframework.web.bind.annotation.RequestMapping;
18 import org.springframework.web.bind.annotation.RequestMethod;
19 import org.springframework.web.bind.annotation.RequestParam;
20
21 import eu.etaxonomy.cdm.api.service.IIdentifiableEntityService;
22 import eu.etaxonomy.cdm.api.service.pager.Pager;
23 import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
24 import eu.etaxonomy.cdm.persistence.query.MatchMode;
25 import eu.etaxonomy.cdm.remote.controller.util.PagerParameters;
26
27 /**
28 * @author l.morris
29 * @date 27 Mar 2012
30 *
31 */
32 public abstract class IdentifiableListController <T extends IdentifiableEntity, SERVICE extends IIdentifiableEntityService<T>> extends BaseListController<T,SERVICE> {
33
34
35 /**
36 * Find IdentifiableEntity objects by name
37 * <p>
38 *
39 * @param query
40 * the string to query for. Since the wildcard character '*'
41 * internally always is appended to the query string, a search
42 * always compares the query string with the beginning of a name.
43 * - <i>required parameter</i>
44 * @param pageNumber
45 * the number of the page to be returned, the first page has the
46 * pageNumber = 1 - <i>optional parameter</i>
47 * @param pageSize
48 * the maximum number of entities returned per page (can be -1
49 * to return all entities in a single page) - <i>optional parameter</i>
50 * @param matchMode
51 * valid values are "EXACT", "BEGINNING", "ANYWHERE", "END" (case sensitive !!!)
52 * @return a Pager on a list of {@link IdentifiableEntity}s
53 * @throws IOException
54 */
55 @RequestMapping(method = RequestMethod.GET, value={"findByTitle"})
56 public Pager<T> doFindByTitle(
57 @RequestParam(value = "query", required = true) String query,
58 @RequestParam(value = "pageNumber", required = false) Integer pageNumber,
59 @RequestParam(value = "pageSize", required = false) Integer pageSize,
60 @RequestParam(value = "matchMode", required = false) MatchMode matchMode,
61 HttpServletRequest request,
62 HttpServletResponse response
63 )
64 throws IOException {
65
66
67
68 logger.info("doFind : " + request.getRequestURI() + "?" + request.getQueryString() );
69
70 PagerParameters pagerParams = new PagerParameters(pageSize, pageNumber);
71 pagerParams.normalizeAndValidate(response);
72
73 matchMode = matchMode != null ? matchMode : MatchMode.BEGINNING;
74
75 return service.findByTitle(null, query, matchMode, null, pagerParams.getPageSize(), pagerParams.getPageIndex(), null, initializationStrategy);
76
77 }
78
79
80 }