missing bits for #476: Implement free-text search methods for TaxonBase and TextData
[cdmlib.git] / cdmlib-remote / src / main / java / eu / etaxonomy / cdm / remote / controller / TaxonListController.java
1 // $Id$
2 /**
3 * Copyright (C) 2009 EDIT 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
7 * 1.1 See LICENSE.TXT at the top of this package for the full license terms.
8 */
9
10 package eu.etaxonomy.cdm.remote.controller;
11
12 import java.io.IOException;
13 import java.util.Arrays;
14 import java.util.List;
15 import java.util.Set;
16 import java.util.UUID;
17
18 import javax.servlet.http.HttpServletRequest;
19 import javax.servlet.http.HttpServletResponse;
20
21 import org.apache.lucene.queryParser.ParseException;
22 import org.springframework.beans.factory.annotation.Autowired;
23 import org.springframework.stereotype.Controller;
24 import org.springframework.web.bind.annotation.RequestMapping;
25 import org.springframework.web.bind.annotation.RequestMethod;
26 import org.springframework.web.bind.annotation.RequestParam;
27
28 import eu.etaxonomy.cdm.api.service.IClassificationService;
29 import eu.etaxonomy.cdm.api.service.ITaxonService;
30 import eu.etaxonomy.cdm.api.service.config.ITaxonServiceConfigurator;
31 import eu.etaxonomy.cdm.api.service.config.TaxonServiceConfiguratorImpl;
32 import eu.etaxonomy.cdm.api.service.pager.Pager;
33 import eu.etaxonomy.cdm.api.service.search.SearchResult;
34 import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
35 import eu.etaxonomy.cdm.model.common.Language;
36 import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
37 import eu.etaxonomy.cdm.model.location.NamedArea;
38 import eu.etaxonomy.cdm.model.taxon.Classification;
39 import eu.etaxonomy.cdm.model.taxon.Synonym;
40 import eu.etaxonomy.cdm.model.taxon.Taxon;
41 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
42 import eu.etaxonomy.cdm.persistence.query.MatchMode;
43 import eu.etaxonomy.cdm.persistence.query.OrderHint;
44 import eu.etaxonomy.cdm.remote.controller.util.PagerParameters;
45
46 /**
47 * TODO write controller documentation
48 *
49 * @author a.kohlbecker
50 * @date 20.03.2009
51 */
52 @Controller
53 @RequestMapping(value = {"/taxon"})
54 public class TaxonListController extends IdentifiableListController<TaxonBase, ITaxonService> {
55
56
57
58 /**
59 *
60 */
61 public TaxonListController(){
62 super();
63 setInitializationStrategy(Arrays.asList(new String[]{"$","name.nomenclaturalReference"}));
64 }
65
66 /* (non-Javadoc)
67 * @see eu.etaxonomy.cdm.remote.controller.BaseListController#setService(eu.etaxonomy.cdm.api.service.IService)
68 */
69 @Override
70 @Autowired
71 public void setService(ITaxonService service) {
72 this.service = service;
73 }
74
75 @Autowired
76 private IClassificationService classificationService;
77
78 /**
79 * Find Taxa, Synonyms, Common Names by name, either globally or in a specific geographic area.
80 * <p>
81 * URI: <b>&#x002F;{datasource-name}&#x002F;portal&#x002F;taxon&#x002F;find</b>
82 *
83 * @param query
84 * the string to query for. Since the wildcard character '*'
85 * internally always is appended to the query string, a search
86 * always compares the query string with the beginning of a name.
87 * - <i>required parameter</i>
88 * @param treeUuid
89 * the {@link UUID} of a {@link Classification} to which the
90 * search is to be restricted. - <i>optional parameter</i>
91 * @param areas
92 * restrict the search to a set of geographic {@link NamedArea}s.
93 * The parameter currently takes a list of TDWG area labels.
94 * - <i>optional parameter</i>
95 * @param pageNumber
96 * the number of the page to be returned, the first page has the
97 * pageNumber = 1 - <i>optional parameter</i>
98 * @param pageSize
99 * the maximum number of entities returned per page (can be -1
100 * to return all entities in a single page) - <i>optional parameter</i>
101 * @param doTaxa
102 * weather to search for instances of {@link Taxon} - <i>optional parameter</i>
103 * @param doSynonyms
104 * weather to search for instances of {@link Synonym} - <i>optional parameter</i>
105 * @param doTaxaByCommonNames
106 * for instances of {@link Taxon} by a common name used - <i>optional parameter</i>
107 * @param matchMode
108 * valid values are "EXACT", "BEGINNING", "ANYWHERE", "END" (case sensitive !!!)
109 * @return a Pager on a list of {@link IdentifiableEntity}s initialized by
110 * the following strategy {@link #SIMPLE_TAXON_INIT_STRATEGY}
111 * @throws IOException
112 */
113 @RequestMapping(method = RequestMethod.GET, value={"findTaxaAndNames"})
114 public Pager<IdentifiableEntity> doFindTaxaAndNames(
115 @RequestParam(value = "query", required = true) String query,
116 @RequestParam(value = "tree", required = false) UUID treeUuid,
117 @RequestParam(value = "area", required = false) Set<NamedArea> areas,
118 @RequestParam(value = "pageNumber", required = false) Integer pageNumber,
119 @RequestParam(value = "pageSize", required = false) Integer pageSize,
120 @RequestParam(value = "doTaxa", required = false) Boolean doTaxa,
121 @RequestParam(value = "doSynonyms", required = false) Boolean doSynonyms,
122 @RequestParam(value = "doMisappliedNames", required = false) Boolean doMisappliedNames,
123 @RequestParam(value = "doTaxaByCommonNames", required = false) Boolean doTaxaByCommonNames,
124 @RequestParam(value = "matchMode", required = false) MatchMode matchMode,
125 HttpServletRequest request,
126 HttpServletResponse response
127 )
128 throws IOException {
129
130
131 logger.info("findTaxaAndNames : " + request.getRequestURI() + "?" + request.getQueryString() );
132
133 PagerParameters pagerParams = new PagerParameters(pageSize, pageNumber);
134 pagerParams.normalizeAndValidate(response);
135
136 ITaxonServiceConfigurator config = new TaxonServiceConfiguratorImpl();
137
138 config.setTaxonPropertyPath(initializationStrategy);
139
140 config.setPageNumber(pagerParams.getPageIndex());
141 config.setPageSize(pagerParams.getPageSize());
142 config.setTitleSearchString(query);
143 config.setDoTaxa(doTaxa!= null ? doTaxa : Boolean.FALSE );
144 config.setDoSynonyms(doSynonyms != null ? doSynonyms : Boolean.FALSE );
145 config.setDoMisappliedNames(doMisappliedNames != null ? doMisappliedNames : Boolean.FALSE);
146 config.setDoTaxaByCommonNames(doTaxaByCommonNames != null ? doTaxaByCommonNames : Boolean.FALSE );
147 config.setMatchMode(matchMode != null ? matchMode : MatchMode.BEGINNING);
148 // config.setTaxonPropertyPath(SIMPLE_TAXON_INIT_STRATEGY);
149 config.setNamedAreas(areas);
150 if(treeUuid != null){
151 Classification classification = classificationService.find(treeUuid);
152 config.setClassification(classification);
153 }
154
155 return (Pager<IdentifiableEntity>) service.findTaxaAndNames(config);
156
157 }
158
159 /**
160 * @param clazz
161 * @param queryString
162 * @param treeUuid TODO unimplemented in TaxonServiceImpl !!!!
163 * @param languages
164 * @param pageNumber
165 * @param pageSize
166 * @param request
167 * @param response
168 * @return
169 * @throws IOException
170 * @throws ParseException
171 */
172 @RequestMapping(method = RequestMethod.GET, value={"findByDescriptionElementFullText"})
173 public Pager<SearchResult<TaxonBase>> dofindByDescriptionElementFullText(
174 @RequestParam(value = "clazz", required = false) Class clazz,
175 @RequestParam(value = "query", required = true) String queryString,
176 @RequestParam(value = "tree", required = false) UUID treeUuid,
177 @RequestParam(value = "languages", required = false) List<Language> languages,
178 @RequestParam(value = "pageNumber", required = false) Integer pageNumber,
179 @RequestParam(value = "pageSize", required = false) Integer pageSize,
180 HttpServletRequest request,
181 HttpServletResponse response
182 )
183 throws IOException, ParseException {
184
185 logger.info("findByDescriptionElementFullText : " + request.getRequestURI() + "?" + request.getQueryString() );
186
187 PagerParameters pagerParams = new PagerParameters(pageSize, pageNumber);
188 pagerParams.normalizeAndValidate(response);
189
190 Classification classification = null;
191 if(treeUuid != null){
192 classification = classificationService.find(treeUuid);
193 }
194 Pager<SearchResult<TaxonBase>> pager = service.findByDescriptionElementFullText(clazz, queryString, classification, languages, pagerParams.getPageSize(), pagerParams.getPageIndex(), ((List<OrderHint>)null), initializationStrategy);
195 return pager;
196 }
197 }