*print redlist status from congruent concept relations
[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.ArrayList;
14 import java.util.Arrays;
15 import java.util.List;
16 import java.util.Set;
17 import java.util.UUID;
18
19 import javax.servlet.http.HttpServletRequest;
20 import javax.servlet.http.HttpServletResponse;
21
22 import org.apache.lucene.queryParser.ParseException;
23 import org.springframework.beans.factory.annotation.Autowired;
24 import org.springframework.stereotype.Controller;
25 import org.springframework.web.bind.annotation.RequestMapping;
26 import org.springframework.web.bind.annotation.RequestMethod;
27 import org.springframework.web.bind.annotation.RequestParam;
28
29 import eu.etaxonomy.cdm.api.service.IClassificationService;
30 import eu.etaxonomy.cdm.api.service.ITaxonService;
31 import eu.etaxonomy.cdm.api.service.ITermService;
32 import eu.etaxonomy.cdm.api.service.config.IFindTaxaAndNamesConfigurator;
33 import eu.etaxonomy.cdm.api.service.config.FindTaxaAndNamesConfiguratorImpl;
34 import eu.etaxonomy.cdm.api.service.pager.Pager;
35 import eu.etaxonomy.cdm.api.service.search.SearchResult;
36 import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
37 import eu.etaxonomy.cdm.model.common.Language;
38 import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
39 import eu.etaxonomy.cdm.model.description.Feature;
40 import eu.etaxonomy.cdm.model.location.NamedArea;
41 import eu.etaxonomy.cdm.model.taxon.Classification;
42 import eu.etaxonomy.cdm.model.taxon.Synonym;
43 import eu.etaxonomy.cdm.model.taxon.Taxon;
44 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
45 import eu.etaxonomy.cdm.persistence.query.MatchMode;
46 import eu.etaxonomy.cdm.persistence.query.OrderHint;
47 import eu.etaxonomy.cdm.remote.controller.util.PagerParameters;
48 import eu.etaxonomy.cdm.remote.editor.UuidList;
49
50 /**
51 * TODO write controller documentation
52 *
53 * @author a.kohlbecker
54 * @date 20.03.2009
55 */
56 @Controller
57 @RequestMapping(value = {"/taxon"})
58 public class TaxonListController extends IdentifiableListController<TaxonBase, ITaxonService> {
59
60
61
62 /**
63 *
64 */
65 public TaxonListController(){
66 super();
67 setInitializationStrategy(Arrays.asList(new String[]{"$","name.nomenclaturalReference"}));
68 }
69
70 /* (non-Javadoc)
71 * @see eu.etaxonomy.cdm.remote.controller.BaseListController#setService(eu.etaxonomy.cdm.api.service.IService)
72 */
73 @Override
74 @Autowired
75 public void setService(ITaxonService service) {
76 this.service = service;
77 }
78
79 @Autowired
80 private IClassificationService classificationService;
81
82 @Autowired
83 private ITermService termService;
84
85
86 /**
87 * Find Taxa, Synonyms, Common Names by name, either globally or in a specific geographic area.
88 * <p>
89 * URI: <b>&#x002F;{datasource-name}&#x002F;portal&#x002F;taxon&#x002F;find</b>
90 *
91 * @param query
92 * the string to query for. Since the wildcard character '*'
93 * internally always is appended to the query string, a search
94 * always compares the query string with the beginning of a name.
95 * - <i>required parameter</i>
96 * @param treeUuid
97 * the {@link UUID} of a {@link Classification} to which the
98 * search is to be restricted. - <i>optional parameter</i>
99 * @param areas
100 * restrict the search to a set of geographic {@link NamedArea}s.
101 * The parameter currently takes a list of TDWG area labels.
102 * - <i>optional parameter</i>
103 * @param pageNumber
104 * the number of the page to be returned, the first page has the
105 * pageNumber = 1 - <i>optional parameter</i>
106 * @param pageSize
107 * the maximum number of entities returned per page (can be -1
108 * to return all entities in a single page) - <i>optional parameter</i>
109 * @param doTaxa
110 * weather to search for instances of {@link Taxon} - <i>optional parameter</i>
111 * @param doSynonyms
112 * weather to search for instances of {@link Synonym} - <i>optional parameter</i>
113 * @param doTaxaByCommonNames
114 * for instances of {@link Taxon} by a common name used - <i>optional parameter</i>
115 * @param matchMode
116 * valid values are "EXACT", "BEGINNING", "ANYWHERE", "END" (case sensitive !!!)
117 * @return a Pager on a list of {@link IdentifiableEntity}s initialized by
118 * the following strategy {@link #SIMPLE_TAXON_INIT_STRATEGY}
119 * @throws IOException
120 */
121 @RequestMapping(method = RequestMethod.GET, value={"findTaxaAndNames"})
122 public Pager<IdentifiableEntity> doFindTaxaAndNames(
123 @RequestParam(value = "query", required = true) String query,
124 @RequestParam(value = "tree", required = false) UUID treeUuid,
125 @RequestParam(value = "area", required = false) Set<NamedArea> areas,
126 @RequestParam(value = "pageNumber", required = false) Integer pageNumber,
127 @RequestParam(value = "pageSize", required = false) Integer pageSize,
128 @RequestParam(value = "doTaxa", required = false) Boolean doTaxa,
129 @RequestParam(value = "doSynonyms", required = false) Boolean doSynonyms,
130 @RequestParam(value = "doMisappliedNames", required = false) Boolean doMisappliedNames,
131 @RequestParam(value = "doTaxaByCommonNames", required = false) Boolean doTaxaByCommonNames,
132 @RequestParam(value = "matchMode", required = false) MatchMode matchMode,
133 HttpServletRequest request,
134 HttpServletResponse response
135 )
136 throws IOException {
137
138
139 logger.info("findTaxaAndNames : " + request.getRequestURI() + "?" + request.getQueryString() );
140
141 PagerParameters pagerParams = new PagerParameters(pageSize, pageNumber);
142 pagerParams.normalizeAndValidate(response);
143
144 IFindTaxaAndNamesConfigurator config = new FindTaxaAndNamesConfiguratorImpl();
145
146 config.setTaxonPropertyPath(initializationStrategy);
147
148 config.setPageNumber(pagerParams.getPageIndex());
149 config.setPageSize(pagerParams.getPageSize());
150 config.setTitleSearchString(query);
151 config.setDoTaxa(doTaxa!= null ? doTaxa : Boolean.FALSE );
152 config.setDoSynonyms(doSynonyms != null ? doSynonyms : Boolean.FALSE );
153 config.setDoMisappliedNames(doMisappliedNames != null ? doMisappliedNames : Boolean.FALSE);
154 config.setDoTaxaByCommonNames(doTaxaByCommonNames != null ? doTaxaByCommonNames : Boolean.FALSE );
155 config.setMatchMode(matchMode != null ? matchMode : MatchMode.BEGINNING);
156 // config.setTaxonPropertyPath(SIMPLE_TAXON_INIT_STRATEGY);
157 config.setNamedAreas(areas);
158 if(treeUuid != null){
159 Classification classification = classificationService.find(treeUuid);
160 config.setClassification(classification);
161 }
162
163 return (Pager<IdentifiableEntity>) service.findTaxaAndNames(config);
164
165 }
166
167 /**
168 * @param clazz
169 * @param queryString
170 * @param treeUuid TODO unimplemented in TaxonServiceImpl !!!!
171 * @param languages
172 * @param pageNumber
173 * @param pageSize
174 * @param request
175 * @param response
176 * @return
177 * @throws IOException
178 * @throws ParseException
179 */
180 @RequestMapping(method = RequestMethod.GET, value={"findByDescriptionElementFullText"})
181 public Pager<SearchResult<TaxonBase>> dofindByDescriptionElementFullText(
182 @RequestParam(value = "clazz", required = false) Class<? extends DescriptionElementBase> clazz,
183 @RequestParam(value = "query", required = true) String queryString,
184 @RequestParam(value = "tree", required = false) UUID treeUuid,
185 @RequestParam(value = "features", required = false) UuidList featureUuids,
186 @RequestParam(value = "languages", required = false) List<Language> languages,
187 @RequestParam(value = "hl", required = false) Boolean highlighting,
188 @RequestParam(value = "pageNumber", required = false) Integer pageNumber,
189 @RequestParam(value = "pageSize", required = false) Integer pageSize,
190 HttpServletRequest request,
191 HttpServletResponse response
192 )
193 throws IOException, ParseException {
194
195 logger.info("findByDescriptionElementFullText : " + request.getRequestURI() + "?" + request.getQueryString() );
196
197 PagerParameters pagerParams = new PagerParameters(pageSize, pageNumber);
198 pagerParams.normalizeAndValidate(response);
199
200 if(highlighting == null){
201 highlighting = false;
202 }
203
204 Classification classification = null;
205 if(treeUuid != null){
206 classification = classificationService.find(treeUuid);
207 }
208
209 List<Feature> features = null;
210 if(featureUuids != null){
211 features = new ArrayList<Feature>(featureUuids.size());
212 for(UUID uuid : featureUuids){
213 features.add((Feature) termService.find(uuid));
214 }
215 }
216
217 Pager<SearchResult<TaxonBase>> pager = service.findByDescriptionElementFullText(
218 clazz, queryString, classification, features, languages, highlighting,
219 pagerParams.getPageSize(), pagerParams.getPageIndex(), ((List<OrderHint>)null),
220 initializationStrategy);
221 return pager;
222 }
223
224 @RequestMapping(method = RequestMethod.GET, value={"findByFullText"})
225 public Pager<SearchResult<TaxonBase>> dofindByFullText(
226 @RequestParam(value = "clazz", required = false) Class<? extends TaxonBase> clazz,
227 @RequestParam(value = "query", required = true) String queryString,
228 @RequestParam(value = "tree", required = false) UUID treeUuid,
229 @RequestParam(value = "languages", required = false) List<Language> languages,
230 @RequestParam(value = "hl", required = false) Boolean highlighting,
231 @RequestParam(value = "pageNumber", required = false) Integer pageNumber,
232 @RequestParam(value = "pageSize", required = false) Integer pageSize,
233 HttpServletRequest request,
234 HttpServletResponse response
235 )
236 throws IOException, ParseException {
237
238 logger.info("findByFullText : " + request.getRequestURI() + "?" + request.getQueryString() );
239
240 PagerParameters pagerParams = new PagerParameters(pageSize, pageNumber);
241 pagerParams.normalizeAndValidate(response);
242
243 if(highlighting == null){
244 highlighting = false;
245 }
246
247 Classification classification = null;
248 if(treeUuid != null){
249 classification = classificationService.find(treeUuid);
250 }
251
252 Pager<SearchResult<TaxonBase>> pager = service.findByFullText(clazz, queryString, classification, languages,
253 highlighting, pagerParams.getPageSize(), pagerParams.getPageIndex(), ((List<OrderHint>)null),
254 initializationStrategy);
255 return pager;
256 }
257
258 @RequestMapping(method = RequestMethod.GET, value={"findByEverythingFullText"})
259 public Pager<SearchResult<TaxonBase>> dofindByEverythingFullText(
260 @RequestParam(value = "clazz", required = false) Class<? extends TaxonBase> clazz,
261 @RequestParam(value = "query", required = true) String queryString,
262 @RequestParam(value = "tree", required = false) UUID treeUuid,
263 @RequestParam(value = "languages", required = false) List<Language> languages,
264 @RequestParam(value = "hl", required = false) Boolean highlighting,
265 @RequestParam(value = "pageNumber", required = false) Integer pageNumber,
266 @RequestParam(value = "pageSize", required = false) Integer pageSize,
267 HttpServletRequest request,
268 HttpServletResponse response
269 )
270 throws IOException, ParseException {
271
272 logger.info("findByEverythingFullText : " + request.getRequestURI() + "?" + request.getQueryString() );
273
274 PagerParameters pagerParams = new PagerParameters(pageSize, pageNumber);
275 pagerParams.normalizeAndValidate(response);
276
277 if(highlighting == null){
278 highlighting = false;
279 }
280
281 Classification classification = null;
282 if(treeUuid != null){
283 classification = classificationService.find(treeUuid);
284 }
285
286 Pager<SearchResult<TaxonBase>> pager = service.findByEverythingFullText(
287 queryString, classification, languages, highlighting,
288 pagerParams.getPageSize(), pagerParams.getPageIndex(),
289 ((List<OrderHint>)null), initializationStrategy);
290 return pager;
291 }
292 }