1 // $Id: TaxonController.java 5473 2009-03-25 13:42:07Z a.kohlbecker $
3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
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.
11 package eu
.etaxonomy
.cdm
.remote
.controller
;
13 import java
.io
.IOException
;
14 import java
.util
.ArrayList
;
15 import java
.util
.Arrays
;
16 import java
.util
.HashSet
;
17 import java
.util
.Hashtable
;
18 import java
.util
.List
;
20 import java
.util
.NoSuchElementException
;
22 import java
.util
.SortedMap
;
23 import java
.util
.TreeMap
;
24 import java
.util
.UUID
;
25 import java
.util
.regex
.Matcher
;
26 import java
.util
.regex
.Pattern
;
28 import javax
.servlet
.http
.HttpServletRequest
;
29 import javax
.servlet
.http
.HttpServletResponse
;
31 import org
.apache
.commons
.lang
.ObjectUtils
;
32 import org
.apache
.log4j
.Logger
;
33 import org
.springframework
.beans
.factory
.annotation
.Autowired
;
34 import org
.springframework
.stereotype
.Controller
;
35 import org
.springframework
.web
.bind
.WebDataBinder
;
36 import org
.springframework
.web
.bind
.annotation
.InitBinder
;
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
;
42 import eu
.etaxonomy
.cdm
.api
.service
.IDescriptionService
;
43 import eu
.etaxonomy
.cdm
.api
.service
.INameService
;
44 import eu
.etaxonomy
.cdm
.api
.service
.IReferenceService
;
45 import eu
.etaxonomy
.cdm
.api
.service
.ITaxonService
;
46 import eu
.etaxonomy
.cdm
.api
.service
.ITaxonTreeService
;
47 import eu
.etaxonomy
.cdm
.api
.service
.config
.ITaxonServiceConfigurator
;
48 import eu
.etaxonomy
.cdm
.api
.service
.config
.impl
.TaxonServiceConfiguratorImpl
;
49 import eu
.etaxonomy
.cdm
.api
.service
.pager
.Pager
;
50 import eu
.etaxonomy
.cdm
.database
.UpdatableRoutingDataSource
;
51 import eu
.etaxonomy
.cdm
.model
.common
.IdentifiableEntity
;
52 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionElementBase
;
53 import eu
.etaxonomy
.cdm
.model
.description
.TaxonDescription
;
54 import eu
.etaxonomy
.cdm
.model
.description
.TaxonNameDescription
;
55 import eu
.etaxonomy
.cdm
.model
.location
.NamedArea
;
56 import eu
.etaxonomy
.cdm
.model
.media
.Media
;
57 import eu
.etaxonomy
.cdm
.model
.media
.MediaRepresentation
;
58 import eu
.etaxonomy
.cdm
.model
.name
.NameRelationship
;
59 import eu
.etaxonomy
.cdm
.model
.name
.TaxonNameBase
;
60 import eu
.etaxonomy
.cdm
.model
.name
.TypeDesignationBase
;
61 import eu
.etaxonomy
.cdm
.model
.taxon
.Synonym
;
62 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
63 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
64 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonRelationship
;
65 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonRelationshipType
;
66 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonomicTree
;
67 import eu
.etaxonomy
.cdm
.persistence
.query
.MatchMode
;
68 import eu
.etaxonomy
.cdm
.remote
.editor
.NamedAreaPropertyEditor
;
69 import eu
.etaxonomy
.cdm
.remote
.editor
.UUIDPropertyEditor
;
72 * The TaxonPortalController class is a Spring MVC Controller.
74 * The syntax of the mapped service URIs contains the the {datasource-name} path element.
75 * The available {datasource-name}s are defined in a configuration file which
76 * is loaded by the {@link UpdatableRoutingDataSource}. If the
77 * UpdatableRoutingDataSource is not being used in the actual application
78 * context any arbitrary {datasource-name} may be used.
80 * Methods mapped at type level, inherited from super classes ({@link BaseController}):
82 * URI: <b>/{datasource-name}/portal/taxon/{taxon-uuid}</b>
84 * Get the {@link TaxonBase} instance identified by the <code>{taxon-uuid}</code>.
85 * The returned Taxon is initialized by
86 * the following strategy {@link #TAXON_INIT_STRATEGY}
89 * @author a.kohlbecker
94 @RequestMapping(value
= {"/*/portal/taxon/*", "/*/portal/taxon/*/*", "/*/portal/name/*/*", "/*/portal/taxon/*/media/*/*"})
95 public class TaxonPortalController
extends BaseController
<TaxonBase
, ITaxonService
>
97 public static final Logger logger
= Logger
.getLogger(TaxonPortalController
.class);
100 private INameService nameService
;
102 private IDescriptionService descriptionService
;
104 private IReferenceService referenceService
;
107 private ITaxonTreeService taxonTreeService
;
109 private static final List
<String
> TAXON_INIT_STRATEGY
= Arrays
.asList(new String
[]{
112 "relationsToThisName.fromTaxon.name.taggedName",
116 "name.rank.representations",
117 "name.status.type.representations",
119 // taxon descriptions
120 "descriptions.elements.$",
121 "descriptions.elements.area",
122 "descriptions.elements.area.$",
123 "descriptions.elements.multilanguageText",
124 "descriptions.elements.media.representations.parts",
126 // // typeDesignations
127 // "name.typeDesignations.$",
128 // "name.typeDesignations.citation.authorTeam",
129 // "name.typeDesignations.typeName.$",
130 // "name.typeDesignations.typeStatus.representations",
131 // "name.typeDesignations.typeSpecimen.media.representations.parts"
135 private static final List
<String
> SIMPLE_TAXON_INIT_STRATEGY
= Arrays
.asList(new String
[]{
138 "relationsToThisName.fromTaxon.name.taggedName",
142 "name.rank.representations",
143 "name.status.type.representations"
146 private static final List
<String
> SYNONYMY_INIT_STRATEGY
= Arrays
.asList(new String
[]{
147 // initialize homotypical and heterotypical groups; needs synonyms
148 "synonymRelations.$",
149 "synonymRelations.synonym.$",
150 "synonymRelations.synonym.name.taggedName",
151 "synonymRelations.synonym.name.nomenclaturalReference.inBook.authorTeam",
152 "synonymRelations.synonym.name.nomenclaturalReference.inJournal",
153 "synonymRelations.synonym.name.nomenclaturalReference.inProceedings",
154 "synonymRelations.synonym.name.homotypicalGroup.typifiedNames.$",
155 "synonymRelations.synonym.name.homotypicalGroup.typifiedNames.name.taggedName",
156 "synonymRelations.synonym.name.homotypicalGroup.typifiedNames.taxonBases.$",
157 "synonymRelations.synonym.name.homotypicalGroup.typifiedNames.taxonBases.name.taggedName",
159 "name.homotypicalGroup.$",
160 "name.homotypicalGroup.typifiedNames.$",
161 "name.homotypicalGroup.typifiedNames.name.taggedName",
163 "name.homotypicalGroup.typifiedNames.taxonBases.$",
164 "name.homotypicalGroup.typifiedNames.taxonBases.name.taggedName"
168 private static final List
<String
> TAXONRELATIONSHIP_INIT_STRATEGY
= Arrays
.asList(new String
[]{
170 "type.inverseRepresentations",
171 "fromTaxon.sec.authorTeam",
172 "fromTaxon.name.taggedName"
175 private static final List
<String
> NAMERELATIONSHIP_INIT_STRATEGY
= Arrays
.asList(new String
[]{
177 "type.inverseRepresentations",
178 "fromName.taggedName",
182 protected static final List
<String
> TAXONDESCRIPTION_INIT_STRATEGY
= Arrays
.asList(new String
[]{
185 "elements.sources.citation.",
186 "elements.sources.citation.authorTeam.$",
187 //"elements.sources.citation.authorTeam.teamMembers.",
188 "elements.multilanguageText",
189 "elements.media.representations.parts",
192 private static final List
<String
> NAMEDESCRIPTION_INIT_STRATEGY
= Arrays
.asList(new String
[]{
196 "elements.multilanguageText",
197 "elements.media.representations.parts",
200 private static final List
<String
> TYPEDESIGNATION_INIT_STRATEGY
= Arrays
.asList(new String
[]{
203 "typeStatus.representations",
204 "citation.authorTeam",
205 "typeName.taggedName"
210 private static final String featureTreeUuidPattern
= "^/(?:[^/]+)/taxon(?:(?:/)([^/?#&\\.]+))+.*";
212 public TaxonPortalController(){
214 setInitializationStrategy(TAXON_INIT_STRATEGY
);
215 setUuidParameterPattern("^/(?:[^/]+)/portal/(?:[^/]+)/([^/?#&\\.]+).*");
219 * @see eu.etaxonomy.cdm.remote.controller.GenericController#setService(eu.etaxonomy.cdm.api.service.IService)
223 public void setService(ITaxonService service
) {
224 this.service
= service
;
228 public void initBinder(WebDataBinder binder
) {
229 binder
.registerCustomEditor(UUID
.class, new UUIDPropertyEditor());
230 binder
.registerCustomEditor(NamedArea
.class, new NamedAreaPropertyEditor());
235 * @see eu.etaxonomy.cdm.remote.controller.BaseController#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
238 @RequestMapping(method = RequestMethod.GET)
239 public TaxonBase doGet(HttpServletRequest request, HttpServletResponse response)throws IOException {
240 logger.info("doGet()");
241 TaxonBase tb = getCdmBase(request, response, TAXON_INIT_STRATEGY, TaxonBase.class);
246 * Find Taxa, Synonyms, Common Names by name, either globally or in a specific geographic area.
248 * URI: <b>/{datasource-name}/portal/taxon/find</b>
251 * the string to query for. Since the wildcard character '*'
252 * internally always is appended to the query string, a search
253 * always compares the query string with the beginning of a name.
254 * - <i>required parameter</i>
256 * the {@link UUID} of a {@link TaxonomicTree} to which the
257 * search is to be restricted. - <i>optional parameter</i>
259 * restrict the search to a set of geographic {@link NamedArea}s.
260 * The parameter currently takes a list of TDWG area labels.
261 * - <i>optional parameter</i>
263 * the number of the page to be returned, the first page has the
264 * pageNumber = 1 - <i>optional parameter</i>
266 * the maximum number of entities returned per page (can be null
267 * to return all entities in a single page) - <i>optional parameter</i>
269 * weather to search for instances of {@link Taxon} - <i>optional parameter</i>
271 * weather to search for instances of {@link Synonym} - <i>optional parameter</i>
272 * @param doTaxaByCommonNames
273 * for instances of {@link Taxon} by a common name used - <i>optional parameter</i>
274 * @return a Pager on a list of {@link IdentifiableEntity}s initialized by
275 * the following strategy {@link #SIMPLE_TAXON_INIT_STRATEGY}
276 * @throws IOException
278 @RequestMapping(method
= RequestMethod
.GET
,
279 value
= {"/*/portal/taxon/find"}) //TODO map to path /*/portal/taxon/
280 public Pager
<IdentifiableEntity
> doFind(
281 @RequestParam(value
= "query", required
= false) String query
,
282 @RequestParam(value
= "tree", required
= false) UUID treeUuid
,
283 @RequestParam(value
= "area", required
= false) Set
<NamedArea
> areas
,
284 @RequestParam(value
= "page", required
= false) Integer page
,
285 @RequestParam(value
= "pageSize", required
= false) Integer pageSize
,
286 @RequestParam(value
= "doTaxa", required
= false) Boolean doTaxa
,
287 @RequestParam(value
= "doSynonyms", required
= false) Boolean doSynonyms
,
288 @RequestParam(value
= "doTaxaByCommonNames", required
= false) Boolean doTaxaByCommonNames
)
291 logger
.info("doFind( " +
292 "query=\"" + ObjectUtils
.toString(query
) + "\", treeUuid=" + ObjectUtils
.toString(treeUuid
) +
293 ", area=" + ObjectUtils
.toString(areas
) +
294 ", pageSize=" + ObjectUtils
.toString(pageSize
) + ", page=" + ObjectUtils
.toString(page
) +
295 ", doTaxa=" + ObjectUtils
.toString(doTaxa
) + ", doSynonyms=" + ObjectUtils
.toString(doSynonyms
) +")" );
297 if(page
== null){ page
= BaseListController
.DEFAULT_PAGE_NUMBER
;}
298 if(pageSize
== null){ pageSize
= BaseListController
.DEFAULT_PAGESIZE
;}
300 ITaxonServiceConfigurator config
= new TaxonServiceConfiguratorImpl();
301 config
.setPageNumber(page
);
302 config
.setPageSize(pageSize
);
303 config
.setSearchString(query
);
304 config
.setDoTaxa(doTaxa
!= null ? doTaxa
: Boolean
.FALSE
);
305 config
.setDoSynonyms(doSynonyms
!= null ? doSynonyms
: Boolean
.FALSE
);
306 config
.setDoTaxaByCommonNames(doTaxaByCommonNames
!= null ? doTaxaByCommonNames
: Boolean
.FALSE
);
307 config
.setMatchMode(MatchMode
.BEGINNING
);
308 config
.setTaxonPropertyPath(SIMPLE_TAXON_INIT_STRATEGY
);
309 config
.setNamedAreas(areas
);
310 if(treeUuid
!= null){
311 TaxonomicTree taxonomicTree
= taxonTreeService
.find(treeUuid
);
312 config
.setTaxonomicTree(taxonomicTree
);
315 return (Pager
<IdentifiableEntity
>) service
.findTaxaAndNames(config
);
319 * Get the synonymy for a taxon identified by the <code>{taxon-uuid}</code>.
320 * The synonymy consists
321 * of two parts: The group of homotypic synonyms of the taxon and the
322 * heterotypic synonymy groups of the taxon. The synonymy is ordered
323 * historically by the type designations and by the publication date of the
324 * nomenclatural reference
327 * <b>/{datasource-name}/portal/taxon/{taxon-uuid}/synonymy</b>
332 * @return a Map with to entries which are mapped by the following keys:
333 * "homotypicSynonymsByHomotypicGroup", "heterotypicSynonymyGroups",
334 * containing lists of {@link Synonym}s which are initialized using the
335 * following initialization strategy: {@link #SYNONYMY_INIT_STRATEGY}
337 * @throws IOException
340 value
= {"/*/portal/taxon/*/synonymy"},
341 method
= RequestMethod
.GET
)
342 public ModelAndView
doGetSynonymy(HttpServletRequest request
, HttpServletResponse response
)throws IOException
{
344 logger
.info("doGetSynonymy() " + request
.getServletPath());
345 ModelAndView mv
= new ModelAndView();
346 TaxonBase tb
= getCdmBase(request
, response
, null, Taxon
.class);
347 Taxon taxon
= (Taxon
)tb
;
348 Map
<String
, List
<?
>> synonymy
= new Hashtable
<String
, List
<?
>>();
349 synonymy
.put("homotypicSynonymsByHomotypicGroup", service
.getHomotypicSynonymsByHomotypicGroup(taxon
, SYNONYMY_INIT_STRATEGY
));
350 synonymy
.put("heterotypicSynonymyGroups", service
.getHeterotypicSynonymyGroups(taxon
, SYNONYMY_INIT_STRATEGY
));
351 mv
.addObject(synonymy
);
356 * Get the set of accepted {@link Taxon} entities for a given
357 * {@link TaxonBase} entity identified by the <code>{taxon-uuid}</code>.
359 * URI: <b>/{datasource-name}/portal/taxon/{taxon-uuid}/accepted</b>
363 * @return a Set of {@link Taxon} entities which are initialized
364 * using the following initialization strategy:
365 * {@link #SYNONYMY_INIT_STRATEGY}
366 * @throws IOException
368 @RequestMapping(value
= "/*/portal/taxon/*/accepted", method
= RequestMethod
.GET
)
369 public Set
<TaxonBase
> getAccepted(HttpServletRequest request
, HttpServletResponse response
) throws IOException
{
371 logger
.info("getAccepted() " + request
.getServletPath());
373 UUID uuid
= readValueUuid(request
, null);
374 TaxonBase tb
= service
.load(uuid
, SYNONYMY_INIT_STRATEGY
);
376 response
.sendError(HttpServletResponse
.SC_NOT_FOUND
, "A taxon with the uuid " + uuid
+ " does not exist");
379 HashSet
<TaxonBase
> resultset
= new HashSet
<TaxonBase
>();
380 if(tb
instanceof Taxon
){
381 //the taxon already is accepted
382 //FIXME take the current view into account once views are implemented!!!
383 resultset
.add((Taxon
)tb
);
385 Synonym syn
= (Synonym
)tb
;
386 for(TaxonBase accepted
: syn
.getAcceptedTaxa()){
387 accepted
= service
.load(accepted
.getUuid(), SIMPLE_TAXON_INIT_STRATEGY
);
388 resultset
.add(accepted
);
395 * Get the list of {@link TaxonRelationship}s for the given
396 * {@link TaxonBase} instance identified by the <code>{taxon-uuid}</code>.
398 * URI: <b>/{datasource-name}/portal/taxon/{taxon-uuid}/taxonRelationships</b>
402 * @return a List of {@link TaxonRelationship} entities which are initialized
403 * using the following initialization strategy:
404 * {@link #TAXONRELATIONSHIP_INIT_STRATEGY}
405 * @throws IOException
408 value
= {"/*/portal/taxon/*/taxonRelationships"},
409 method
= RequestMethod
.GET
)
410 public List
<TaxonRelationship
> doGetTaxonRelations(HttpServletRequest request
, HttpServletResponse response
)throws IOException
{
412 logger
.info("doGetTaxonRelations()" + request
.getServletPath());
413 TaxonBase tb
= getCdmBase(request
, response
, null, Taxon
.class);
414 Taxon taxon
= (Taxon
)tb
;
415 List
<TaxonRelationship
> relations
= new ArrayList
<TaxonRelationship
>();
416 List
<TaxonRelationship
> results
= service
.listToTaxonRelationships(taxon
, TaxonRelationshipType
.MISAPPLIED_NAME_FOR(), null, null, null, TAXONRELATIONSHIP_INIT_STRATEGY
);
417 relations
.addAll(results
);
418 results
= service
.listToTaxonRelationships(taxon
, TaxonRelationshipType
.INVALID_DESIGNATION_FOR(), null, null, null, TAXONRELATIONSHIP_INIT_STRATEGY
);
419 relations
.addAll(results
);
425 * Get the list of {@link NameRelationship}s of the Name associated with the
426 * {@link TaxonBase} instance identified by the <code>{taxon-uuid}</code>.
428 * URI: <b>/{datasource-name}/portal/taxon/{taxon-uuid}/nameRelationships</b>
432 * @return a List of {@link NameRelationship} entities which are initialized
433 * using the following initialization strategy:
434 * {@link #NAMERELATIONSHIP_INIT_STRATEGY}
435 * @throws IOException
438 value
= {"/*/portal/taxon/*/nameRelationships"},
439 method
= RequestMethod
.GET
)
440 public List
<NameRelationship
> doGetNameRelations(HttpServletRequest request
, HttpServletResponse response
)throws IOException
{
441 logger
.info("doGetNameRelations()" + request
.getServletPath());
442 TaxonBase tb
= getCdmBase(request
, response
, SIMPLE_TAXON_INIT_STRATEGY
, Taxon
.class);
443 List
<NameRelationship
> list
= nameService
.listToNameRelationships(tb
.getName(), null, null, null, null, NAMERELATIONSHIP_INIT_STRATEGY
);
448 * Get the list of {@link TaxonNameDescription}s of the Name associated with the
449 * {@link TaxonNameBase} instance identified by the <code>{name-uuid}</code>.
451 * URI: <b>/{datasource-name}/portal/name/{name-uuid}/descriptions</b>
455 * @return a List of {@link TaxonNameDescription} entities which are initialized
456 * using the following initialization strategy:
457 * {@link #NAMEDESCRIPTION_INIT_STRATEGY}
458 * @throws IOException
461 value
= {"/*/portal/name/*/descriptions"},
462 method
= RequestMethod
.GET
)
463 public List
<TaxonNameDescription
> doGetNameDescriptions(HttpServletRequest request
, HttpServletResponse response
)throws IOException
{
464 logger
.info("doGetNameDescriptions()" + request
.getServletPath());
465 UUID nameUuuid
= readValueUuid(request
, null);
466 TaxonNameBase tnb
= nameService
.load(nameUuuid
, null);
467 Pager
<TaxonNameDescription
> p
= descriptionService
.getTaxonNameDescriptions(tnb
, null, null, NAMEDESCRIPTION_INIT_STRATEGY
);
468 return p
.getRecords();
472 * Get the list of {@link TypeDesignationBase}s of the
473 * {@link TaxonBase} instance identified by the <code>{taxon-uuid}</code>.
475 * URI: <b>/{datasource-name}/portal/taxon/{taxon-uuid}/nameTypeDesignations</b>
479 * @return a List of {@link TypeDesignationBase} entities which are initialized
480 * using the following initialization strategy:
481 * {@link #TYPEDESIGNATION_INIT_STRATEGY}
482 * @throws IOException
485 value
= {"/*/portal/taxon/*/nameTypeDesignations"},
486 method
= RequestMethod
.GET
)
487 public List
<TypeDesignationBase
> doGetNameTypeDesignations(HttpServletRequest request
, HttpServletResponse response
)throws IOException
{
488 logger
.info("doGetNameTypeDesignations()" + request
.getServletPath());
489 TaxonBase tb
= getCdmBase(request
, response
, SIMPLE_TAXON_INIT_STRATEGY
, Taxon
.class);
490 Pager
<TypeDesignationBase
> p
= nameService
.getTypeDesignations(tb
.getName(), null, null, null, TYPEDESIGNATION_INIT_STRATEGY
);
491 return p
.getRecords();
495 * Get the list of {@link TaxonDescription}s of the
496 * {@link Taxon} instance identified by the <code>{taxon-uuid}</code>.
498 * URI: <b>/{datasource-name}/portal/taxon/{taxon-uuid}/descriptions</b>
502 * @return a List of {@link TaxonDescription} entities which are initialized
503 * using the following initialization strategy:
504 * {@link #TAXONDESCRIPTION_INIT_STRATEGY}
505 * @throws IOException
508 value
= {"/*/portal/taxon/*/descriptions"},
509 method
= RequestMethod
.GET
)
510 public List
<TaxonDescription
> doGetDescriptions(HttpServletRequest request
, HttpServletResponse response
)throws IOException
{
511 logger
.info("doGetDescriptions()" + request
.getServletPath());
512 Taxon t
= getCdmBase(request
, response
, null, Taxon
.class);
513 Pager
<TaxonDescription
> p
= descriptionService
.getTaxonDescriptions(t
, null, null, null, null,
514 TAXONDESCRIPTION_INIT_STRATEGY
);
515 return p
.getRecords();
519 * Get the {@link Media} attached to the {@link Taxon} instance
520 * identified by the <code>{taxon-uuid}</code>.
522 * Usage /{datasource-name}/portal/taxon/{taxon-
523 * uuid}/media/{mime type
524 * list}/{size}[,[widthOrDuration}][,{height}]/
528 * <li><b>{mime type list}</b>: a comma separated list of mime types, in the
529 * order of preference. The forward slashes contained in the mime types must
530 * be replaced by a colon. Regular expressions can be used. Each media
531 * associated with this given taxon is being searched whereas the first
532 * matching mime type matching a representation always rules.</li>
533 * <li><b>{size},{widthOrDuration},{height}</b>: <i>not jet implemented</i>
534 * valid values are an integer or the asterisk '*' as a wildcard</li>
539 * @return a List of {@link Media} entities which are initialized
540 * using the following initialization strategy:
541 * {@link #TAXONDESCRIPTION_INIT_STRATEGY}
542 * @throws IOException
545 value
= {"/*/portal/taxon/*/media/*/*"},
546 method
= RequestMethod
.GET
)
547 public List
<Media
> doGetMedia(HttpServletRequest request
, HttpServletResponse response
)throws IOException
{
548 logger
.info("doGetMedia()" + request
.getServletPath());
549 Taxon t
= getCdmBase(request
, response
, null, Taxon
.class);
550 Pager
<TaxonDescription
> p
=
551 descriptionService
.getTaxonDescriptions(t
, null, null, null, null, TAXONDESCRIPTION_INIT_STRATEGY
);
553 // pars the media and quality parameters
556 // collect all media of the given taxon
557 boolean limitToGalleries
= false;
558 List
<Media
> taxonMedia
= new ArrayList
<Media
>();
559 for(TaxonDescription desc
: p
.getRecords()){
560 if(!limitToGalleries
|| desc
.isImageGallery()){
561 for(DescriptionElementBase element
: desc
.getElements()){
562 for(Media media
: element
.getMedia()){
563 taxonMedia
.add(media
);
569 // move into media ...
571 // find best matching representations of each media
572 String path
= request
.getServletPath();
573 String
[] pathTokens
= path
.split("/");
574 String
[] mimeTypes
= pathTokens
[6].split(",");
575 String
[] sizeTokens
= pathTokens
[7].split(",");
576 Integer widthOrDuration
= null;
577 Integer height
= null;
580 for(int i
=0; i
<mimeTypes
.length
; i
++){
581 mimeTypes
[i
] = mimeTypes
[i
].replace(':', '/');
584 if(sizeTokens
.length
> 0){
586 size
= Integer
.valueOf(sizeTokens
[0]);
587 } catch (NumberFormatException nfe
) {
591 if(sizeTokens
.length
> 1){
593 widthOrDuration
= Integer
.valueOf(sizeTokens
[1]);
594 } catch (NumberFormatException nfe
) {
598 if(sizeTokens
.length
> 2){
600 height
= Integer
.valueOf(sizeTokens
[2]);
601 } catch (NumberFormatException nfe
) {
606 List
<Media
> returnMedia
= new ArrayList
<Media
>(taxonMedia
.size());
607 for(Media media
: taxonMedia
){
608 SortedMap
<String
, MediaRepresentation
> prefRepresentations
609 = orderMediaRepresentations(media
, mimeTypes
, size
, widthOrDuration
, height
);
611 // take first one and remove all other representations
612 MediaRepresentation prefOne
= prefRepresentations
.get(prefRepresentations
.firstKey());
613 for (MediaRepresentation representation
: media
.getRepresentations()) {
614 if (representation
!= prefOne
) {
615 media
.removeRepresentation(representation
);
618 returnMedia
.add(media
);
619 } catch (NoSuchElementException nse
) {
629 * @param mimeTypeRegexes
631 * @param widthOrDuration
635 * TODO move into a media utils class
636 * TODO implement the quality filter
638 private SortedMap
<String
, MediaRepresentation
> orderMediaRepresentations(Media media
, String
[] mimeTypeRegexes
,
639 Integer size
, Integer widthOrDuration
, Integer height
) {
640 SortedMap
<String
, MediaRepresentation
> prefRepr
= new TreeMap
<String
, MediaRepresentation
>();
641 for (String mimeTypeRegex
: mimeTypeRegexes
) {
642 // getRepresentationByMimeType
643 Pattern mimeTypePattern
= Pattern
.compile(mimeTypeRegex
);
644 int representationCnt
= 0;
645 for (MediaRepresentation representation
: media
.getRepresentations()) {
646 Matcher mather
= mimeTypePattern
.matcher(representation
.getMimeType());
647 if (mather
.matches()) {
650 /* TODO the quality filter part is being skipped
651 * // look for representation with the best matching parts
652 for (MediaRepresentationPart part : representation.getParts()) {
653 if (part instanceof ImageFile) {
654 ImageFile image = (ImageFile) part;
655 int dw = image.getWidth() * image.getHeight() - height * widthOrDuration;
661 dwa = (representation.getParts().size() > 0 ? dwa / representation.getParts().size() : 0);
663 prefRepr
.put((dwa
+ representationCnt
++) + '_' + representation
.getMimeType(), representation
);
665 // preferred mime type found => end loop
673 // ---------------------- code snippet preserved for possible later use --------------------
675 // value = {"/*/portal/taxon/*/descriptions"},
676 // method = RequestMethod.GET)
677 // public List<TaxonDescription> doGetDescriptionsbyFeatureTree(HttpServletRequest request, HttpServletResponse response)throws IOException {
678 // TaxonBase tb = getCdmBase(request, response, null, Taxon.class);
679 // if(tb instanceof Taxon){
680 // //T O D O this is a quick and dirty implementation -> generalize
681 // UUID featureTreeUuid = readValueUuid(request, featureTreeUuidPattern);
683 // FeatureTree featureTree = descriptionService.getFeatureTreeByUuid(featureTreeUuid);
684 // Pager<TaxonDescription> p = descriptionService.getTaxonDescriptions((Taxon)tb, null, null, null, null, TAXONDESCRIPTION_INIT_STRATEGY);
685 // List<TaxonDescription> descriptions = p.getRecords();
687 // if(!featureTree.isDescriptionSeparated()){
689 // TaxonDescription superDescription = TaxonDescription.NewInstance();
690 // //put all descriptionElements in superDescription and make it invisible
691 // for(TaxonDescription description: descriptions){
692 // for(DescriptionElementBase element: description.getElements()){
693 // superDescription.addElement(element);
696 // List<TaxonDescription> separatedDescriptions = new ArrayList<TaxonDescription>(descriptions.size());
697 // separatedDescriptions.add(superDescription);
698 // return separatedDescriptions;
700 // return descriptions;
703 // response.sendError(HttpServletResponse.SC_NOT_FOUND, "invalid type; Taxon expected but " + tb.getClass().getSimpleName() + " found.");