server side fix for #2609 (Implement Concept Relationships for Dataportal)
[cdmlib.git] / cdmlib-remote / src / main / java / eu / etaxonomy / cdm / remote / controller / TaxonPortalController.java
1 // $Id: TaxonController.java 5473 2009-03-25 13:42:07Z a.kohlbecker $
2 /**
3 * Copyright (C) 2007 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
11 package eu.etaxonomy.cdm.remote.controller;
12
13 import static java.net.HttpURLConnection.HTTP_BAD_REQUEST;
14
15 import java.io.IOException;
16 import java.util.ArrayList;
17 import java.util.Arrays;
18 import java.util.HashSet;
19 import java.util.Hashtable;
20 import java.util.Iterator;
21 import java.util.List;
22 import java.util.Map;
23 import java.util.Set;
24 import java.util.UUID;
25
26 import javax.servlet.http.HttpServletRequest;
27 import javax.servlet.http.HttpServletResponse;
28
29 import org.apache.commons.lang.ObjectUtils;
30 import org.apache.http.HttpRequest;
31 import org.apache.log4j.Logger;
32 import org.springframework.beans.factory.annotation.Autowired;
33 import org.springframework.stereotype.Controller;
34 import org.springframework.web.bind.WebDataBinder;
35 import org.springframework.web.bind.annotation.InitBinder;
36 import org.springframework.web.bind.annotation.PathVariable;
37 import org.springframework.web.bind.annotation.RequestMapping;
38 import org.springframework.web.bind.annotation.RequestMethod;
39 import org.springframework.web.bind.annotation.RequestParam;
40 import org.springframework.web.servlet.ModelAndView;
41
42 import eu.etaxonomy.cdm.api.service.IDescriptionService;
43 import eu.etaxonomy.cdm.api.service.IFeatureTreeService;
44 import eu.etaxonomy.cdm.api.service.INameService;
45 import eu.etaxonomy.cdm.api.service.IOccurrenceService;
46 import eu.etaxonomy.cdm.api.service.ITaxonService;
47 import eu.etaxonomy.cdm.api.service.IClassificationService;
48 import eu.etaxonomy.cdm.api.service.config.ITaxonServiceConfigurator;
49 import eu.etaxonomy.cdm.api.service.config.TaxonServiceConfiguratorImpl;
50 import eu.etaxonomy.cdm.api.service.pager.Pager;
51 import eu.etaxonomy.cdm.database.UpdatableRoutingDataSource;
52 import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
53 import eu.etaxonomy.cdm.model.common.RelationshipBase.Direction;
54 import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
55 import eu.etaxonomy.cdm.model.description.TaxonDescription;
56 import eu.etaxonomy.cdm.model.location.NamedArea;
57 import eu.etaxonomy.cdm.model.media.Media;
58 import eu.etaxonomy.cdm.model.media.MediaRepresentationPart;
59 import eu.etaxonomy.cdm.model.media.MediaUtils;
60 import eu.etaxonomy.cdm.model.name.NameRelationship;
61 import eu.etaxonomy.cdm.model.name.TypeDesignationBase;
62 import eu.etaxonomy.cdm.model.taxon.Synonym;
63 import eu.etaxonomy.cdm.model.taxon.Taxon;
64 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
65 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
66 import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
67 import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
68 import eu.etaxonomy.cdm.model.taxon.Classification;
69 import eu.etaxonomy.cdm.persistence.query.MatchMode;
70 import eu.etaxonomy.cdm.remote.controller.util.PagerParameters;
71 import eu.etaxonomy.cdm.remote.editor.CdmTypePropertyEditor;
72 import eu.etaxonomy.cdm.remote.editor.MatchModePropertyEditor;
73 import eu.etaxonomy.cdm.remote.editor.NamedAreaPropertyEditor;
74
75 /**
76 * The TaxonPortalController class is a Spring MVC Controller.
77 * <p>
78 * The syntax of the mapped service URIs contains the the {datasource-name} path element.
79 * The available {datasource-name}s are defined in a configuration file which
80 * is loaded by the {@link UpdatableRoutingDataSource}. If the
81 * UpdatableRoutingDataSource is not being used in the actual application
82 * context any arbitrary {datasource-name} may be used.
83 * <p>
84 * Methods mapped at type level, inherited from super classes ({@link BaseController}):
85 * <blockquote>
86 * URI: <b>&#x002F;{datasource-name}&#x002F;portal&#x002F;taxon&#x002F;{taxon-uuid}</b>
87 *
88 * Get the {@link TaxonBase} instance identified by the <code>{taxon-uuid}</code>.
89 * The returned Taxon is initialized by
90 * the following strategy {@link #TAXON_INIT_STRATEGY}
91 * </blockquote>
92 *
93 * @author a.kohlbecker
94 * @date 20.07.2009
95 *
96 */
97 @Controller
98 @RequestMapping(value = {"/portal/taxon/{uuid}"})
99 public class TaxonPortalController extends BaseController<TaxonBase, ITaxonService>
100 {
101 public static final Logger logger = Logger.getLogger(TaxonPortalController.class);
102
103 @Autowired
104 private INameService nameService;
105
106 @Autowired
107 private IDescriptionService descriptionService;
108
109 @Autowired
110 private IOccurrenceService occurrenceService;
111
112 @Autowired
113 private IClassificationService classificationService;
114
115 @Autowired
116 private ITaxonService taxonService;
117
118 @Autowired
119 private IFeatureTreeService featureTreeService;
120
121 private static final List<String> TAXON_INIT_STRATEGY = Arrays.asList(new String []{
122 "*",
123 // taxon relations
124 "relationsToThisName.fromTaxon.name",
125 // the name
126 "name.$",
127 "name.rank.representations",
128 "name.status.type.representations",
129
130 // taxon descriptions
131 "descriptions.elements.area.$",
132 "descriptions.elements.multilanguageText",
133 "descriptions.elements.media.representations.parts",
134 "descriptions.elements.media.title",
135
136 });
137
138 private static final List<String> TAXON_WITH_NODES_INIT_STRATEGY = Arrays.asList(new String []{
139 "taxonNodes.$",
140 "taxonNodes.classification.$",
141 "taxonNodes.childNodes.$"
142 });
143
144 private static final List<String> SIMPLE_TAXON_INIT_STRATEGY = Arrays.asList(new String []{
145 "*",
146 // taxon relations
147 "relationsToThisName.fromTaxon.name",
148 // the name
149 "name.$",
150 "name.rank.representations",
151 "name.status.type.representations",
152 "name.nomenclaturalReference"
153 });
154
155 private static final List<String> SYNONYMY_INIT_STRATEGY = Arrays.asList(new String []{
156 // initialize homotypical and heterotypical groups; needs synonyms
157 "synonymRelations.$",
158 "synonymRelations.synonym.$",
159 "synonymRelations.synonym.name.status.type.representation",
160 "synonymRelations.synonym.name.nomenclaturalReference.inReference",
161 "synonymRelations.synonym.name.homotypicalGroup.typifiedNames.$",
162 "synonymRelations.synonym.name.homotypicalGroup.typifiedNames.taxonBases.$",
163 "synonymRelations.synonym.name.combinationAuthorTeam.$",
164
165 "name.typeDesignations",
166
167 "name.homotypicalGroup.$",
168 "name.homotypicalGroup.typifiedNames.$",
169 "name.homotypicalGroup.typifiedNames.nomenclaturalReference.authorTeam",
170
171 "name.homotypicalGroup.typifiedNames.taxonBases.$"
172 });
173
174 private static final List<String> SYNONYMY_WITH_NODES_INIT_STRATEGY = Arrays.asList(new String []{
175 // initialize homotypical and heterotypical groups; needs synonyms
176 "synonymRelations.$",
177 "synonymRelations.synonym.$",
178 "synonymRelations.synonym.name.status.type.representation",
179 "synonymRelations.synonym.name.nomenclaturalReference.inReference",
180 "synonymRelations.synonym.name.homotypicalGroup.typifiedNames.$",
181 "synonymRelations.synonym.name.homotypicalGroup.typifiedNames.taxonBases.$",
182 "synonymRelations.synonym.name.combinationAuthorTeam.$",
183
184 "name.homotypicalGroup.$",
185 "name.homotypicalGroup.typifiedNames.$",
186 "name.homotypicalGroup.typifiedNames.nomenclaturalReference.authorTeam",
187
188 "name.homotypicalGroup.typifiedNames.taxonBases.$",
189
190 "taxonNodes.$",
191 "taxonNodes.classification.$",
192 "taxonNodes.childNodes.$"
193 });
194 private static final List<String> SIMPLE_TAXON_WITH_NODES_INIT_STRATEGY = Arrays.asList(new String []{
195 "*",
196 // taxon relations
197 "relationsToThisName.fromTaxon.name",
198 // the name
199 "name.$",
200 "name.rank.representations",
201 "name.status.type.representations",
202 "name.nomenclaturalReference",
203
204 "taxonNodes.$",
205 "taxonNodes.classification.$",
206 "taxonNodes.childNodes.$"
207 });
208
209
210 private static final List<String> TAXONRELATIONSHIP_INIT_STRATEGY = Arrays.asList(new String []{
211 "$",
212 "type.inverseRepresentations",
213 "fromTaxon.sec",
214 "fromTaxon.name"
215 });
216
217 private static final List<String> NAMERELATIONSHIP_INIT_STRATEGY = Arrays.asList(new String []{
218 "$",
219 "type.inverseRepresentations",
220 "fromName",
221 "toName.$",
222 });
223
224
225 protected static final List<String> TAXONDESCRIPTION_INIT_STRATEGY = Arrays.asList(new String []{
226 "$",
227 "elements.$",
228 "elements.sources.citation.authorTeam",
229 "elements.sources.nameUsedInSource.originalNameString",
230 "elements.multilanguageText",
231 "elements.media.representations.parts",
232 "elements.media.title",
233 });
234
235 protected static final List<String> DESCRIPTION_ELEMENT_INIT_STRATEGY = Arrays.asList(new String []{
236 "$",
237 "sources.citation.authorTeam",
238 "sources.nameUsedInSource.originalNameString",
239 "multilanguageText",
240 "media.representations.parts",
241 "media.title",
242 });
243
244
245 // private static final List<String> NAMEDESCRIPTION_INIT_STRATEGY = Arrays.asList(new String []{
246 // "uuid",
247 // "feature",
248 // "elements.$",
249 // "elements.multilanguageText",
250 // "elements.media.representations.parts",
251 // "elements.media.title",
252 // });
253
254 protected static final List<String> TAXONDESCRIPTION_MEDIA_INIT_STRATEGY = Arrays.asList(new String []{
255 "elements.media.representations.parts",
256 "elements.media.title"
257
258 });
259
260 private static final List<String> TYPEDESIGNATION_INIT_STRATEGY = Arrays.asList(new String []{
261 //"$",
262 "typeSpecimen.$",
263 "citation.authorTeam.$",
264 "typeName",
265 });
266
267 protected static final List<String> TAXONNODE_WITHTAXON_INIT_STRATEGY = Arrays.asList(new String []{
268 "childNodes.taxon",
269 });
270
271 protected static final List<String> TAXONNODE_INIT_STRATEGY = Arrays.asList(new String []{
272 "taxonNodes.classification"
273 });
274
275
276
277 private static final String featureTreeUuidPattern = "^/taxon(?:(?:/)([^/?#&\\.]+))+.*";
278
279 public TaxonPortalController(){
280 super();
281 setInitializationStrategy(TAXON_INIT_STRATEGY);
282 }
283
284 /* (non-Javadoc)
285 * @see eu.etaxonomy.cdm.remote.controller.GenericController#setService(eu.etaxonomy.cdm.api.service.IService)
286 */
287 @Autowired
288 @Override
289 public void setService(ITaxonService service) {
290 this.service = service;
291 }
292
293 @InitBinder
294 @Override
295 public void initBinder(WebDataBinder binder) {
296 super.initBinder(binder);
297 binder.registerCustomEditor(NamedArea.class, new NamedAreaPropertyEditor());
298 binder.registerCustomEditor(MatchMode.class, new MatchModePropertyEditor());
299 binder.registerCustomEditor(Class.class, new CdmTypePropertyEditor());
300 }
301
302
303 /* (non-Javadoc)
304 * @see eu.etaxonomy.cdm.remote.controller.BaseController#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
305
306 @Override
307 @RequestMapping(method = RequestMethod.GET)
308 public TaxonBase doGet(HttpServletRequest request, HttpServletResponse response)throws IOException {
309 logger.info("doGet()");
310 TaxonBase tb = getCdmBase(request, response, TAXON_INIT_STRATEGY, TaxonBase.class);
311 return tb;
312 }
313 */
314 /**
315 * Find Taxa, Synonyms, Common Names by name, either globally or in a specific geographic area.
316 * <p>
317 * URI: <b>&#x002F;{datasource-name}&#x002F;portal&#x002F;taxon&#x002F;find</b>
318 *
319 * @param query
320 * the string to query for. Since the wildcard character '*'
321 * internally always is appended to the query string, a search
322 * always compares the query string with the beginning of a name.
323 * - <i>required parameter</i>
324 * @param treeUuid
325 * the {@link UUID} of a {@link Classification} to which the
326 * search is to be restricted. - <i>optional parameter</i>
327 * @param areas
328 * restrict the search to a set of geographic {@link NamedArea}s.
329 * The parameter currently takes a list of TDWG area labels.
330 * - <i>optional parameter</i>
331 * @param pageNumber
332 * the number of the page to be returned, the first page has the
333 * pageNumber = 1 - <i>optional parameter</i>
334 * @param pageSize
335 * the maximum number of entities returned per page (can be -1
336 * to return all entities in a single page) - <i>optional parameter</i>
337 * @param doTaxa
338 * weather to search for instances of {@link Taxon} - <i>optional parameter</i>
339 * @param doSynonyms
340 * weather to search for instances of {@link Synonym} - <i>optional parameter</i>
341 * @param doTaxaByCommonNames
342 * for instances of {@link Taxon} by a common name used - <i>optional parameter</i>
343 * @param matchMode
344 * valid values are "EXACT", "BEGINNING", "ANYWHERE", "END" (case sensitive !!!)
345 * @return a Pager on a list of {@link IdentifiableEntity}s initialized by
346 * the following strategy {@link #SIMPLE_TAXON_INIT_STRATEGY}
347 * @throws IOException
348 */
349 @RequestMapping(method = RequestMethod.GET,
350 value = {"/portal/taxon/find"}) //TODO map to path /*/portal/taxon/
351 public Pager<IdentifiableEntity> doFind(
352 @RequestParam(value = "query", required = false) String query,
353 @RequestParam(value = "tree", required = false) UUID treeUuid,
354 @RequestParam(value = "area", required = false) Set<NamedArea> areas,
355 @RequestParam(value = "pageNumber", required = false) Integer pageNumber,
356 @RequestParam(value = "pageSize", required = false) Integer pageSize,
357 @RequestParam(value = "doTaxa", required = false) Boolean doTaxa,
358 @RequestParam(value = "doSynonyms", required = false) Boolean doSynonyms,
359 @RequestParam(value = "doTaxaByCommonNames", required = false) Boolean doTaxaByCommonNames,
360 @RequestParam(value = "matchMode", required = false) MatchMode matchMode,
361 HttpServletRequest request,
362 HttpServletResponse response
363 )
364 throws IOException {
365
366 logger.info("doFind : " + request.getRequestURI() + "?" + request.getQueryString() );
367
368 PagerParameters pagerParams = new PagerParameters(pageSize, pageNumber);
369 pagerParams.normalizeAndValidate(response);
370
371 ITaxonServiceConfigurator config = new TaxonServiceConfiguratorImpl();
372 config.setPageNumber(pagerParams.getPageIndex());
373 config.setPageSize(pagerParams.getPageSize());
374 config.setTitleSearchString(query);
375 config.setDoTaxa(doTaxa!= null ? doTaxa : Boolean.FALSE );
376 config.setDoSynonyms(doSynonyms != null ? doSynonyms : Boolean.FALSE );
377 config.setDoTaxaByCommonNames(doTaxaByCommonNames != null ? doTaxaByCommonNames : Boolean.FALSE );
378 config.setMatchMode(matchMode != null ? matchMode : MatchMode.BEGINNING);
379 config.setTaxonPropertyPath(SIMPLE_TAXON_INIT_STRATEGY);
380 config.setNamedAreas(areas);
381 if(treeUuid != null){
382 Classification classification = classificationService.find(treeUuid);
383 config.setClassification(classification);
384 }
385
386 return (Pager<IdentifiableEntity>) service.findTaxaAndNames(config);
387 }
388
389 /**
390 * Get the synonymy for a taxon identified by the <code>{taxon-uuid}</code>.
391 * The synonymy consists
392 * of two parts: The group of homotypic synonyms of the taxon and the
393 * heterotypic synonymy groups of the taxon. The synonymy is ordered
394 * historically by the type designations and by the publication date of the
395 * nomenclatural reference
396 * <p>
397 * URI:
398 * <b>&#x002F;{datasource-name}&#x002F;portal&#x002F;taxon&#x002F;{taxon-uuid}&#x002F;synonymy</b>
399 *
400 *
401 * @param request
402 * @param response
403 * @return a Map with to entries which are mapped by the following keys:
404 * "homotypicSynonymsByHomotypicGroup", "heterotypicSynonymyGroups",
405 * containing lists of {@link Synonym}s which are initialized using the
406 * following initialization strategy: {@link #SYNONYMY_INIT_STRATEGY}
407 *
408 * @throws IOException
409 */
410 @RequestMapping(
411 value = {"synonymy"},
412 method = RequestMethod.GET)
413 public ModelAndView doGetSynonymy(@PathVariable("uuid") UUID uuid,
414 HttpServletRequest request, HttpServletResponse response)throws IOException {
415
416 if(request != null){
417 logger.info("doGetSynonymy() " + request.getServletPath());
418 }
419 ModelAndView mv = new ModelAndView();
420 Taxon taxon = getCdmBaseInstance(Taxon.class, uuid, response, (List<String>)null);
421 Map<String, List<?>> synonymy = new Hashtable<String, List<?>>();
422 synonymy.put("homotypicSynonymsByHomotypicGroup", service.getHomotypicSynonymsByHomotypicGroup(taxon, SYNONYMY_INIT_STRATEGY));
423 synonymy.put("heterotypicSynonymyGroups", service.getHeterotypicSynonymyGroups(taxon, SYNONYMY_INIT_STRATEGY));
424 mv.addObject(synonymy);
425 return mv;
426 }
427
428 /**
429 * Get the set of accepted {@link Taxon} entities for a given
430 * {@link TaxonBase} entity identified by the <code>{taxon-uuid}</code>.
431 * <p>
432 * URI: <b>&#x002F;{datasource-name}&#x002F;portal&#x002F;taxon&#x002F;{taxon-uuid}&#x002F;accepted</b>
433 *
434 * @param request
435 * @param response
436 * @return a Set of {@link Taxon} entities which are initialized
437 * using the following initialization strategy:
438 * {@link #SYNONYMY_INIT_STRATEGY}
439 * @throws IOException
440 */
441 @RequestMapping(value = "accepted/{classification_uuid}", method = RequestMethod.GET)
442 public Set<TaxonBase> getAccepted(
443 @PathVariable("uuid") UUID uuid,
444 @PathVariable("classification_uuid") UUID classification_uuid,
445 HttpServletRequest request,
446 HttpServletResponse response)
447 throws IOException {
448
449 if(request != null){
450 logger.info("getAccepted() " + request.getServletPath());
451 }
452
453 TaxonBase tb = service.load(uuid, SYNONYMY_WITH_NODES_INIT_STRATEGY);
454 if(tb == null){
455 response.sendError(HttpServletResponse.SC_NOT_FOUND, "A taxon with the uuid " + uuid + " does not exist");
456 return null;
457 }
458
459 HashSet<TaxonBase> resultset = new HashSet<TaxonBase>();
460
461 if (tb instanceof Taxon){
462 Taxon taxon = (Taxon) tb;
463 Set<TaxonNode> nodes = taxon.getTaxonNodes();
464 for (TaxonNode taxonNode : nodes) {
465 if (taxonNode.getClassification().compareTo(classification_uuid) == 0){
466 resultset.add((Taxon) tb);
467 }
468 }
469 if (resultset.size() > 1){
470 //error!! A taxon is not allow to have more taxonnodes for a given classification
471 response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
472 "A taxon with the uuid " + uuid + " has more than one taxon node for the given classification" + classification_uuid);
473 }
474 }else{
475 Synonym syn = (Synonym) tb;
476 for(TaxonBase accepted : syn.getAcceptedTaxa()){
477 tb = service.load(accepted.getUuid(), SIMPLE_TAXON_WITH_NODES_INIT_STRATEGY);
478 if (tb instanceof Taxon){
479 Taxon taxon = (Taxon) tb;
480 Set<TaxonNode> nodes = taxon.getTaxonNodes();
481 for (TaxonNode taxonNode : nodes) {
482 if (taxonNode.getClassification().compareTo(classification_uuid) == 0){
483 resultset.add((Taxon) tb);
484 }
485 }
486 if (resultset.size() > 1){
487 //error!! A taxon is not allow to have more taxonnodes for a given classification
488 response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
489 "A taxon with the uuid " + uuid + " has more than one taxon node for the given classification" + classification_uuid);
490 }
491 }else{
492 //ERROR!! perhaps missapplied name????
493 //syn.getRelationType((Taxon)accepted);
494 }
495 }
496 }
497 /**
498 * OLD CODE!!
499 if(tb instanceof Taxon){
500 //the taxon already is accepted
501 //FIXME take the current view into account once views are implemented!!!
502 resultset.add((Taxon)tb);
503 } else {
504 Synonym syn = (Synonym)tb;
505 for(TaxonBase accepted : syn.getAcceptedTaxa()){
506 accepted = service.load(accepted.getUuid(), SIMPLE_TAXON_INIT_STRATEGY);
507 resultset.add(accepted);
508 }
509 }
510 */
511 return resultset;
512 }
513
514 /**
515 * Get the list of {@link TaxonRelationship}s for the given
516 * {@link TaxonBase} instance identified by the <code>{taxon-uuid}</code>.
517 * <p>
518 * URI: <b>&#x002F;{datasource-name}&#x002F;portal&#x002F;taxon&#x002F;{taxon-uuid}&#x002F;taxonRelationships</b>
519 *
520 * @param request
521 * @param response
522 * @return a List of {@link TaxonRelationship} entities which are initialized
523 * using the following initialization strategy:
524 * {@link #TAXONRELATIONSHIP_INIT_STRATEGY}
525 * @throws IOException
526 */
527 @RequestMapping(
528 value = {"taxonRelationships"},
529 method = RequestMethod.GET)
530 public List<TaxonRelationship> doGetTaxonRelations(@PathVariable("uuid") UUID uuid,
531 HttpServletRequest request, HttpServletResponse response)throws IOException {
532
533 logger.info("doGetTaxonRelations()" + request.getServletPath());
534 Taxon taxon = getCdmBaseInstance(Taxon.class, uuid, response, (List<String>)null);
535 List<TaxonRelationship> toRelationships = service.listToTaxonRelationships(taxon, null, null, null, null, TAXONRELATIONSHIP_INIT_STRATEGY);
536 List<TaxonRelationship> fromRelationships = service.listFromTaxonRelationships(taxon, null, null, null, null, TAXONRELATIONSHIP_INIT_STRATEGY);
537
538 List<TaxonRelationship> allRelationships = new ArrayList<TaxonRelationship>(toRelationships.size() + fromRelationships.size());
539 allRelationships.addAll(toRelationships);
540 allRelationships.addAll(fromRelationships);
541
542 return allRelationships;
543 }
544
545 /**
546 * Get the list of {@link NameRelationship}s of the Name associated with the
547 * {@link TaxonBase} instance identified by the <code>{taxon-uuid}</code>.
548 * <p>
549 * URI: <b>&#x002F;{datasource-name}&#x002F;portal&#x002F;taxon&#x002F;{taxon-uuid}&#x002F;nameRelationships</b>
550 *
551 * @param request
552 * @param response
553 * @return a List of {@link NameRelationship} entities which are initialized
554 * using the following initialization strategy:
555 * {@link #NAMERELATIONSHIP_INIT_STRATEGY}
556 * @throws IOException
557 */
558 @RequestMapping(
559 value = {"toNameRelationships"},
560 method = RequestMethod.GET)
561 public List<NameRelationship> doGetToNameRelations(@PathVariable("uuid") UUID uuid,
562 HttpServletRequest request, HttpServletResponse response)throws IOException {
563 logger.info("doGetNameRelations()" + request.getServletPath());
564 TaxonBase taxonBase = getCdmBaseInstance(TaxonBase.class, uuid, response, (List<String>)null);
565 List<NameRelationship> list = nameService.listNameRelationships(taxonBase.getName(), Direction.relatedTo, null, null, 0, null, NAMERELATIONSHIP_INIT_STRATEGY);
566 //List<NameRelationship> list = nameService.listToNameRelationships(taxonBase.getName(), null, null, null, null, NAMERELATIONSHIP_INIT_STRATEGY);
567 return list;
568 }
569
570 /**
571 * Get the list of {@link NameRelationship}s of the Name associated with the
572 * {@link TaxonBase} instance identified by the <code>{taxon-uuid}</code>.
573 * <p>
574 * URI: <b>&#x002F;{datasource-name}&#x002F;portal&#x002F;taxon&#x002F;{taxon-uuid}&#x002F;nameRelationships</b>
575 *
576 * @param request
577 * @param response
578 * @return a List of {@link NameRelationship} entities which are initialized
579 * using the following initialization strategy:
580 * {@link #NAMERELATIONSHIP_INIT_STRATEGY}
581 * @throws IOException
582 */
583 @RequestMapping(
584 value = {"fromNameRelationships"},
585 method = RequestMethod.GET)
586 public List<NameRelationship> doGetFromNameRelations(@PathVariable("uuid") UUID uuid,
587 HttpServletRequest request, HttpServletResponse response)throws IOException {
588 logger.info("doGetNameFromNameRelations()" + request.getServletPath());
589
590 TaxonBase taxonbase = getCdmBaseInstance(TaxonBase.class, uuid, response, SIMPLE_TAXON_INIT_STRATEGY);
591 List<NameRelationship> list = nameService.listNameRelationships(taxonbase.getName(), Direction.relatedFrom, null, null, 0, null, NAMERELATIONSHIP_INIT_STRATEGY);
592 //List<NameRelationship> list = nameService.listFromNameRelationships(taxonbase.getName(), null, null, null, null, NAMERELATIONSHIP_INIT_STRATEGY);
593 return list;
594 }
595
596 /**
597 * Get the list of {@link TypeDesignationBase}s of the
598 * {@link TaxonBase} instance identified by the <code>{taxon-uuid}</code>.
599 * <p>
600 * URI: <b>&#x002F;{datasource-name}&#x002F;portal&#x002F;taxon&#x002F;{taxon-uuid}&#x002F;nameTypeDesignations</b>
601 *
602 * @param request
603 * @param response
604 * @return a List of {@link TypeDesignationBase} entities which are initialized
605 * using the following initialization strategy:
606 * {@link #TYPEDESIGNATION_INIT_STRATEGY}
607 * @throws IOException
608 * @Deprecated use &#x002F;name&#x002F;{uuid}&#x002F;typeDesignations & &#x002F;derivedunitfacade&#x002F;{uuid} instead
609 * also see http://dev.e-taxonomy.eu/trac/ticket/2280
610 */
611 @Deprecated
612 @RequestMapping(
613 value = {"nameTypeDesignations"},
614 method = RequestMethod.GET)
615 public List<TypeDesignationBase> doGetNameTypeDesignations(@PathVariable("uuid") UUID uuid,
616 HttpServletRequest request, HttpServletResponse response)throws IOException {
617 logger.info("doGetNameTypeDesignations()" + request.getServletPath());
618 Taxon taxon = getCdmBaseInstance(Taxon.class, uuid, response, SIMPLE_TAXON_INIT_STRATEGY);
619 Pager<TypeDesignationBase> p = nameService.getTypeDesignations(taxon.getName(), null, null, null, TYPEDESIGNATION_INIT_STRATEGY);
620 return p.getRecords();
621 }
622
623 @RequestMapping(value = "taxonNodes", method = RequestMethod.GET)
624 public Set<TaxonNode> doGetTaxonNodes(
625 @PathVariable("uuid") UUID uuid,
626 HttpServletRequest request,
627 HttpServletResponse response) throws IOException {
628 TaxonBase tb = service.load(uuid, TAXONNODE_INIT_STRATEGY);
629 if(tb instanceof Taxon){
630 return ((Taxon)tb).getTaxonNodes();
631 } else {
632 HttpStatusMessage.UUID_REFERENCES_WRONG_TYPE.send(response);
633 return null;
634 }
635 }
636
637 /**
638 * Get the list of {@link TaxonDescription}s of the
639 * {@link Taxon} instance identified by the <code>{taxon-uuid}</code>.
640 * <p>
641 * URI: <b>&#x002F;{datasource-name}&#x002F;portal&#x002F;taxon&#x002F;{taxon-uuid}&#x002F;descriptions</b>
642 *
643 * @param request
644 * @param response
645 * @return a List of {@link TaxonDescription} entities which are initialized
646 * using the following initialization strategy:
647 * {@link #TAXONDESCRIPTION_INIT_STRATEGY}
648 * @throws IOException
649 */
650 @RequestMapping(
651 value = {"descriptions"},
652 method = RequestMethod.GET)
653 public List<TaxonDescription> doGetDescriptions(
654 @PathVariable("uuid") UUID uuid,
655 HttpServletRequest request,
656 HttpServletResponse response)throws IOException {
657 if(request != null){
658 logger.info("doGetDescriptions()" + request.getServletPath());
659 }
660 Taxon t = getCdmBaseInstance(Taxon.class, uuid, response, (List<String>)null);
661 Pager<TaxonDescription> p = descriptionService.getTaxonDescriptions(t, null, null, null, null, TAXONDESCRIPTION_INIT_STRATEGY);
662 return p.getRecords();
663 }
664
665 @RequestMapping(value = "descriptions/elementsByType/{classSimpleName}", method = RequestMethod.GET)
666 public ModelAndView doGetDescriptionElementsByType(
667 @PathVariable("uuid") UUID uuid,
668 @PathVariable("classSimpleName") String classSimpleName,
669 @RequestParam(value = "count", required = false, defaultValue = "false") Boolean doCount,
670 HttpServletRequest request,
671 HttpServletResponse response) throws IOException {
672 logger.info("doGetDescriptionElementsByType() - " + request.getServletPath());
673
674 ModelAndView mv = new ModelAndView();
675
676 List<DescriptionElementBase> allElements = new ArrayList<DescriptionElementBase>();
677 List<DescriptionElementBase> elements;
678 int count = 0;
679
680 List<String> initStrategy = doCount ? null : DESCRIPTION_ELEMENT_INIT_STRATEGY;
681
682 List<TaxonDescription> taxonDescriptions = doGetDescriptions(uuid, request, response);
683 try {
684 Class type;
685 type = Class.forName("eu.etaxonomy.cdm.model.description."
686 + classSimpleName);
687 if (taxonDescriptions != null) {
688 for (TaxonDescription description : taxonDescriptions) {
689 elements = descriptionService.listDescriptionElements(description, null, type, null, 0, initStrategy);
690 allElements.addAll(elements);
691 count += elements.size();
692 }
693
694 }
695 } catch (ClassNotFoundException e) {
696 HttpStatusMessage.fromString(e.getLocalizedMessage()).send(response);
697 }
698 if(doCount){
699 mv.addObject(count);
700 } else {
701 mv.addObject(allElements);
702 }
703 return mv;
704 }
705
706 // @RequestMapping(value = "specimens", method = RequestMethod.GET)
707 // public ModelAndView doGetSpecimens(
708 // @PathVariable("uuid") UUID uuid,
709 // HttpServletRequest request,
710 // HttpServletResponse response) throws IOException, ClassNotFoundException {
711 // logger.info("doGetSpecimens() - " + request.getServletPath());
712 //
713 // ModelAndView mv = new ModelAndView();
714 //
715 // List<DerivedUnitFacade> derivedUnitFacadeList = new ArrayList<DerivedUnitFacade>();
716 //
717 // // find speciemens in the TaxonDescriptions
718 // List<TaxonDescription> taxonDescriptions = doGetDescriptions(uuid, request, response);
719 // if (taxonDescriptions != null) {
720 //
721 // for (TaxonDescription description : taxonDescriptions) {
722 // derivedUnitFacadeList.addAll( occurrenceService.listDerivedUnitFacades(description, null) );
723 // }
724 // }
725 // // TODO find speciemens in the NameDescriptions ??
726 //
727 // // TODO also find type specimens
728 //
729 // mv.addObject(derivedUnitFacadeList);
730 //
731 // return mv;
732 // }
733
734 /**
735 * Get the {@link Media} attached to the {@link Taxon} instance
736 * identified by the <code>{taxon-uuid}</code>.
737 *
738 * Usage &#x002F;{datasource-name}&#x002F;portal&#x002F;taxon&#x002F;{taxon-
739 * uuid}&#x002F;media&#x002F;{mime type
740 * list}&#x002F;{size}[,[widthOrDuration}][,{height}]&#x002F;
741 *
742 * Whereas
743 * <ul>
744 * <li><b>{mime type list}</b>: a comma separated list of mime types, in the
745 * order of preference. The forward slashes contained in the mime types must
746 * be replaced by a colon. Regular expressions can be used. Each media
747 * associated with this given taxon is being searched whereas the first
748 * matching mime type matching a representation always rules.</li>
749 * <li><b>{size},{widthOrDuration},{height}</b>: <i>not jet implemented</i>
750 * valid values are an integer or the asterisk '*' as a wildcard</li>
751 * </ul>
752 *
753 * @param request
754 * @param response
755 * @return a List of {@link Media} entities which are initialized
756 * using the following initialization strategy:
757 * {@link #TAXONDESCRIPTION_INIT_STRATEGY}
758 * @throws IOException
759 */
760 @RequestMapping(
761 value = {"media"},
762 method = RequestMethod.GET)
763 public List<Media> doGetMedia(
764 @PathVariable("uuid") UUID uuid,
765 @RequestParam(value = "type", required = false) Class<? extends MediaRepresentationPart> type,
766 @RequestParam(value = "mimeTypes", required = false) String[] mimeTypes,
767 @RequestParam(value = "widthOrDuration", required = false) Integer widthOrDuration,
768 @RequestParam(value = "height", required = false) Integer height,
769 @RequestParam(value = "size", required = false) Integer size,
770 HttpServletRequest request, HttpServletResponse response) throws IOException {
771
772 logger.info("doGetMedia()" + request.getServletPath());
773 Taxon t = getCdmBaseInstance(Taxon.class, uuid, response, (List<String>)null);
774 String path = request.getServletPath();
775 List<Media> returnMedia = getMediaForTaxon(t, type, mimeTypes, widthOrDuration, height, size);
776 return returnMedia;
777 }
778
779 @RequestMapping(
780 value = {"subtree/media"},
781 method = RequestMethod.GET)
782 public List<Media> doGetSubtreeMedia(
783 @PathVariable("uuid") UUID uuid,
784 @RequestParam(value = "type", required = false) Class<? extends MediaRepresentationPart> type,
785 @RequestParam(value = "mimeTypes", required = false) String[] mimeTypes,
786 @RequestParam(value = "widthOrDuration", required = false) Integer widthOrDuration,
787 @RequestParam(value = "height", required = false) Integer height,
788 @RequestParam(value = "size", required = false) Integer size,
789 HttpServletRequest request, HttpServletResponse response)throws IOException {
790 logger.info("doGetMedia()" + request.getServletPath());
791 Taxon taxon = getCdmBaseInstance(Taxon.class, uuid, response, TAXON_WITH_NODES_INIT_STRATEGY);
792 String requestPath = request.getServletPath();
793 List<Media> returnMedia = getMediaForTaxon(taxon, type, mimeTypes, widthOrDuration, height, size);
794 TaxonNode node;
795 //looking for all medias of genus
796 if (taxon.getTaxonNodes().size()>0){
797 Set<TaxonNode> nodes = taxon.getTaxonNodes();
798 Iterator<TaxonNode> iterator = nodes.iterator();
799 //TaxonNode holen
800 node = iterator.next();
801 //Check if TaxonNode belongs to the current tree
802
803 node = classificationService.loadTaxonNode(node, TAXONNODE_WITHTAXON_INIT_STRATEGY);
804 Set<TaxonNode> children = node.getChildNodes();
805 Taxon childTaxon;
806 for (TaxonNode child : children){
807 childTaxon = child.getTaxon();
808 childTaxon = (Taxon)taxonService.load(childTaxon.getUuid(), null);
809 returnMedia.addAll(getMediaForTaxon(childTaxon, type, mimeTypes, widthOrDuration, height, size));
810 }
811 }
812 return returnMedia;
813 }
814
815
816 private List<Media> getMediaForTaxon(Taxon taxon, Class<? extends MediaRepresentationPart> type, String[] mimeTypes,
817 Integer widthOrDuration, Integer height, Integer size){
818
819 Pager<TaxonDescription> p =
820 descriptionService.getTaxonDescriptions(taxon, null, null, null, null, TAXONDESCRIPTION_MEDIA_INIT_STRATEGY);
821
822 // pars the media and quality parameters
823
824
825 // collect all media of the given taxon
826 boolean limitToGalleries = false;
827 List<Media> taxonMedia = new ArrayList<Media>();
828 List<Media> taxonGalleryMedia = new ArrayList<Media>();
829 for(TaxonDescription desc : p.getRecords()){
830
831 if(desc.isImageGallery()){
832 for(DescriptionElementBase element : desc.getElements()){
833 for(Media media : element.getMedia()){
834 taxonGalleryMedia.add(media);
835 }
836 }
837 } else if(!limitToGalleries){
838 for(DescriptionElementBase element : desc.getElements()){
839 for(Media media : element.getMedia()){
840 taxonMedia.add(media);
841 }
842 }
843 }
844
845 }
846
847 taxonGalleryMedia.addAll(taxonMedia);
848
849 List<Media> returnMedia = MediaUtils.findPreferredMedia(taxonGalleryMedia, type,
850 mimeTypes, null, widthOrDuration, height, size);
851
852 return returnMedia;
853 }
854
855
856 // ---------------------- code snippet preserved for possible later use --------------------
857 // @RequestMapping(
858 // value = {"/*/portal/taxon/*/descriptions"},
859 // method = RequestMethod.GET)
860 // public List<TaxonDescription> doGetDescriptionsbyFeatureTree(HttpServletRequest request, HttpServletResponse response)throws IOException {
861 // TaxonBase tb = getCdmBase(request, response, null, Taxon.class);
862 // if(tb instanceof Taxon){
863 // //T O D O this is a quick and dirty implementation -> generalize
864 // UUID featureTreeUuid = readValueUuid(request, featureTreeUuidPattern);
865 //
866 // FeatureTree featureTree = descriptionService.getFeatureTreeByUuid(featureTreeUuid);
867 // Pager<TaxonDescription> p = descriptionService.getTaxonDescriptions((Taxon)tb, null, null, null, null, TAXONDESCRIPTION_INIT_STRATEGY);
868 // List<TaxonDescription> descriptions = p.getRecords();
869 //
870 // if(!featureTree.isDescriptionSeparated()){
871 //
872 // TaxonDescription superDescription = TaxonDescription.NewInstance();
873 // //put all descriptionElements in superDescription and make it invisible
874 // for(TaxonDescription description: descriptions){
875 // for(DescriptionElementBase element: description.getElements()){
876 // superDescription.addElement(element);
877 // }
878 // }
879 // List<TaxonDescription> separatedDescriptions = new ArrayList<TaxonDescription>(descriptions.size());
880 // separatedDescriptions.add(superDescription);
881 // return separatedDescriptions;
882 // }else{
883 // return descriptions;
884 // }
885 // } else {
886 // response.sendError(HttpServletResponse.SC_NOT_FOUND, "invalid type; Taxon expected but " + tb.getClass().getSimpleName() + " found.");
887 // return null;
888 // }
889 // }
890
891 }