X-Git-Url: https://dev.e-taxonomy.eu/gitweb/cdmlib.git/blobdiff_plain/38b9c2e5c60632e497f90ae096ef138b6ff24c80..3213eac225db1dc21d21adbbc9dec311acd12106:/cdmlib-remote/src/main/java/eu/etaxonomy/cdm/remote/controller/TaxonPortalController.java diff --git a/cdmlib-remote/src/main/java/eu/etaxonomy/cdm/remote/controller/TaxonPortalController.java b/cdmlib-remote/src/main/java/eu/etaxonomy/cdm/remote/controller/TaxonPortalController.java index fa49bbf17d..0c5a7f78bb 100644 --- a/cdmlib-remote/src/main/java/eu/etaxonomy/cdm/remote/controller/TaxonPortalController.java +++ b/cdmlib-remote/src/main/java/eu/etaxonomy/cdm/remote/controller/TaxonPortalController.java @@ -1,72 +1,93 @@ // $Id: TaxonController.java 5473 2009-03-25 13:42:07Z a.kohlbecker $ /** * Copyright (C) 2007 EDIT -* European Distributed Institute of Taxonomy +* European Distributed Institute of Taxonomy * http://www.e-taxonomy.eu -* +* * The contents of this file are subject to the Mozilla Public License Version 1.1 * See LICENSE.TXT at the top of this package for the full license terms. */ package eu.etaxonomy.cdm.remote.controller; +import static java.net.HttpURLConnection.HTTP_BAD_REQUEST; + import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; import java.util.Hashtable; +import java.util.Iterator; import java.util.List; import java.util.Map; -import java.util.NoSuchElementException; import java.util.Set; -import java.util.SortedMap; -import java.util.TreeMap; import java.util.UUID; -import java.util.regex.Matcher; -import java.util.regex.Pattern; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.lang.ObjectUtils; +import org.apache.http.HttpRequest; import org.apache.log4j.Logger; +import org.apache.lucene.queryParser.ParseException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.WebDataBinder; import org.springframework.web.bind.annotation.InitBinder; +import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.servlet.ModelAndView; + import eu.etaxonomy.cdm.api.service.IDescriptionService; +import eu.etaxonomy.cdm.api.service.IFeatureTreeService; +import eu.etaxonomy.cdm.api.service.IMarkerService; import eu.etaxonomy.cdm.api.service.INameService; -import eu.etaxonomy.cdm.api.service.IReferenceService; +import eu.etaxonomy.cdm.api.service.IOccurrenceService; +import eu.etaxonomy.cdm.api.service.IService; import eu.etaxonomy.cdm.api.service.ITaxonService; -import eu.etaxonomy.cdm.api.service.ITaxonTreeService; -import eu.etaxonomy.cdm.api.service.config.ITaxonServiceConfigurator; -import eu.etaxonomy.cdm.api.service.config.impl.TaxonServiceConfiguratorImpl; +import eu.etaxonomy.cdm.api.service.IClassificationService; +import eu.etaxonomy.cdm.api.service.ITermService; +import eu.etaxonomy.cdm.api.service.config.IFindTaxaAndNamesConfigurator; +import eu.etaxonomy.cdm.api.service.config.FindTaxaAndNamesConfiguratorImpl; import eu.etaxonomy.cdm.api.service.pager.Pager; +import eu.etaxonomy.cdm.api.service.search.SearchResult; import eu.etaxonomy.cdm.database.UpdatableRoutingDataSource; +import eu.etaxonomy.cdm.model.common.DefinedTermBase; import eu.etaxonomy.cdm.model.common.IdentifiableEntity; +import eu.etaxonomy.cdm.model.common.IdentifiableSource; +import eu.etaxonomy.cdm.model.common.Language; +import eu.etaxonomy.cdm.model.common.Marker; +import eu.etaxonomy.cdm.model.common.MarkerType; +import eu.etaxonomy.cdm.model.common.RelationshipBase.Direction; import eu.etaxonomy.cdm.model.description.DescriptionElementBase; +import eu.etaxonomy.cdm.model.description.Feature; import eu.etaxonomy.cdm.model.description.TaxonDescription; -import eu.etaxonomy.cdm.model.description.TaxonNameDescription; import eu.etaxonomy.cdm.model.location.NamedArea; +import eu.etaxonomy.cdm.model.location.NamedAreaLevel; import eu.etaxonomy.cdm.model.media.Media; import eu.etaxonomy.cdm.model.media.MediaRepresentation; +import eu.etaxonomy.cdm.model.media.MediaRepresentationPart; +import eu.etaxonomy.cdm.model.media.MediaUtils; import eu.etaxonomy.cdm.model.name.NameRelationship; -import eu.etaxonomy.cdm.model.name.TaxonNameBase; import eu.etaxonomy.cdm.model.name.TypeDesignationBase; import eu.etaxonomy.cdm.model.taxon.Synonym; import eu.etaxonomy.cdm.model.taxon.Taxon; import eu.etaxonomy.cdm.model.taxon.TaxonBase; +import eu.etaxonomy.cdm.model.taxon.TaxonNode; import eu.etaxonomy.cdm.model.taxon.TaxonRelationship; import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType; -import eu.etaxonomy.cdm.model.taxon.TaxonomicTree; +import eu.etaxonomy.cdm.model.taxon.Classification; import eu.etaxonomy.cdm.persistence.query.MatchMode; +import eu.etaxonomy.cdm.persistence.query.OrderHint; +import eu.etaxonomy.cdm.remote.controller.util.PagerParameters; +import eu.etaxonomy.cdm.remote.editor.CdmTypePropertyEditor; +import eu.etaxonomy.cdm.remote.editor.MatchModePropertyEditor; import eu.etaxonomy.cdm.remote.editor.NamedAreaPropertyEditor; -import eu.etaxonomy.cdm.remote.editor.UUIDPropertyEditor; +import eu.etaxonomy.cdm.remote.editor.UUIDListPropertyEditor; +import eu.etaxonomy.cdm.remote.editor.UuidList; /** * The TaxonPortalController class is a Spring MVC Controller. @@ -80,596 +101,904 @@ import eu.etaxonomy.cdm.remote.editor.UUIDPropertyEditor; * Methods mapped at type level, inherited from super classes ({@link BaseController}): *
* URI: /{datasource-name}/portal/taxon/{taxon-uuid} - * + * * Get the {@link TaxonBase} instance identified by the {taxon-uuid}. * The returned Taxon is initialized by * the following strategy {@link #TAXON_INIT_STRATEGY} *
- * + * * @author a.kohlbecker * @date 20.07.2009 * */ @Controller -@RequestMapping(value = {"/*/portal/taxon/*", "/*/portal/taxon/*/*", "/*/portal/name/*/*", "/*/portal/taxon/*/media/*/*"}) +@RequestMapping(value = {"/portal/taxon/{uuid}"}) public class TaxonPortalController extends BaseController { - public static final Logger logger = Logger.getLogger(TaxonPortalController.class); - - @Autowired - private INameService nameService; - @Autowired - private IDescriptionService descriptionService; - @Autowired - private IReferenceService referenceService; - - @Autowired - private ITaxonTreeService taxonTreeService; - - private static final List TAXON_INIT_STRATEGY = Arrays.asList(new String []{ - "*", - // taxon relations - "relationsToThisName.fromTaxon.name.taggedName", - // the name - "name.$", - "name.taggedName", - "name.rank.representations", - "name.status.type.representations", - - // taxon descriptions - "descriptions.elements.$", - "descriptions.elements.area", - "descriptions.elements.area.$", - "descriptions.elements.multilanguageText", - "descriptions.elements.media.representations.parts", - -// // typeDesignations -// "name.typeDesignations.$", -// "name.typeDesignations.citation.authorTeam", -// "name.typeDesignations.typeName.$", -// "name.typeDesignations.typeStatus.representations", -// "name.typeDesignations.typeSpecimen.media.representations.parts" - - }); - - private static final List SIMPLE_TAXON_INIT_STRATEGY = Arrays.asList(new String []{ - "*", - // taxon relations - "relationsToThisName.fromTaxon.name.taggedName", - // the name - "name.$", - "name.taggedName", - "name.rank.representations", - "name.status.type.representations" - }); - - private static final List SYNONYMY_INIT_STRATEGY = Arrays.asList(new String []{ - // initialize homotypical and heterotypical groups; needs synonyms - "synonymRelations.$", - "synonymRelations.synonym.$", - "synonymRelations.synonym.name.taggedName", - "synonymRelations.synonym.name.nomenclaturalReference.inBook.authorTeam", - "synonymRelations.synonym.name.nomenclaturalReference.inJournal", - "synonymRelations.synonym.name.nomenclaturalReference.inProceedings", - "synonymRelations.synonym.name.homotypicalGroup.typifiedNames.$", - "synonymRelations.synonym.name.homotypicalGroup.typifiedNames.name.taggedName", - "synonymRelations.synonym.name.homotypicalGroup.typifiedNames.taxonBases.$", - "synonymRelations.synonym.name.homotypicalGroup.typifiedNames.taxonBases.name.taggedName", - - "name.homotypicalGroup.$", - "name.homotypicalGroup.typifiedNames.$", - "name.homotypicalGroup.typifiedNames.name.taggedName", - - "name.homotypicalGroup.typifiedNames.taxonBases.$", - "name.homotypicalGroup.typifiedNames.taxonBases.name.taggedName" - - }); - - private static final List TAXONRELATIONSHIP_INIT_STRATEGY = Arrays.asList(new String []{ - "$", - "type.inverseRepresentations", - "fromTaxon.sec.authorTeam", - "fromTaxon.name.taggedName" - }); - - private static final List NAMERELATIONSHIP_INIT_STRATEGY = Arrays.asList(new String []{ - "$", - "type.inverseRepresentations", - "fromName.taggedName", - }); - - - protected static final List TAXONDESCRIPTION_INIT_STRATEGY = Arrays.asList(new String []{ - "$", - "elements.$", - "elements.sources.citation.", - "elements.sources.citation.authorTeam", - "elements.sources.citation.authorTeam.teamMember", - "elements.multilanguageText", - "elements.media.representations.parts", - }); - - private static final List NAMEDESCRIPTION_INIT_STRATEGY = Arrays.asList(new String []{ - "uuid", - "feature", - "elements.$", - "elements.multilanguageText", - "elements.media.representations.parts", - }); - - private static final List TYPEDESIGNATION_INIT_STRATEGY = Arrays.asList(new String []{ - //"$", - "typeSpecimen.$", - "typeStatus.representations", - "citation.authorTeam", - "typeName.taggedName" - }); - - - - private static final String featureTreeUuidPattern = "^/(?:[^/]+)/taxon(?:(?:/)([^/?#&\\.]+))+.*"; - - public TaxonPortalController(){ - super(); - setInitializationStrategy(TAXON_INIT_STRATEGY); - setUuidParameterPattern("^/(?:[^/]+)/portal/(?:[^/]+)/([^/?#&\\.]+).*"); - } - - /* (non-Javadoc) - * @see eu.etaxonomy.cdm.remote.controller.GenericController#setService(eu.etaxonomy.cdm.api.service.IService) - */ - @Autowired - @Override - public void setService(ITaxonService service) { - this.service = service; - } - - @InitBinder + public static final Logger logger = Logger.getLogger(TaxonPortalController.class); + + @Autowired + private INameService nameService; + + @Autowired + private IDescriptionService descriptionService; + + @Autowired + private IOccurrenceService occurrenceService; + + @Autowired + private IClassificationService classificationService; + + @Autowired + private ITaxonService taxonService; + + @Autowired + private ITermService termService; + + @Autowired + private IFeatureTreeService featureTreeService; + + private static final List TAXON_INIT_STRATEGY = Arrays.asList(new String []{ + "*", + // taxon relations + "relationsToThisName.fromTaxon.name", + // the name + "name.$", + "name.rank.representations", + "name.status.type.representations", + + // taxon descriptions + "descriptions.elements.area.$", + "descriptions.elements.multilanguageText", + "descriptions.elements.media.representations.parts", + "descriptions.elements.media.title", + + }); + + private static final List TAXON_WITH_NODES_INIT_STRATEGY = Arrays.asList(new String []{ + "taxonNodes.$", + "taxonNodes.classification.$", + "taxonNodes.childNodes.$" + }); + + private static final List SIMPLE_TAXON_INIT_STRATEGY = Arrays.asList(new String []{ + "*", + // taxon relations + "relationsToThisName.fromTaxon.name", + // the name + "name.$", + "name.rank.representations", + "name.status.type.representations", + "name.nomenclaturalReference" + }); + + private static final List SYNONYMY_INIT_STRATEGY = Arrays.asList(new String []{ + // initialize homotypical and heterotypical groups; needs synonyms + "synonymRelations.$", + "synonymRelations.synonym.$", + "synonymRelations.synonym.name.status.type.representation", + "synonymRelations.synonym.name.nomenclaturalReference.inReference", + "synonymRelations.synonym.name.homotypicalGroup.typifiedNames.$", + "synonymRelations.synonym.name.homotypicalGroup.typifiedNames.taxonBases.$", + "synonymRelations.synonym.name.combinationAuthorTeam.$", + + "name.typeDesignations", + + "name.homotypicalGroup.$", + "name.homotypicalGroup.typifiedNames.$", + "name.homotypicalGroup.typifiedNames.nomenclaturalReference.authorTeam", + + "name.homotypicalGroup.typifiedNames.taxonBases.$" + }); + + private static final List SYNONYMY_WITH_NODES_INIT_STRATEGY = Arrays.asList(new String []{ + // initialize homotypical and heterotypical groups; needs synonyms + "synonymRelations.$", + "synonymRelations.synonym.$", + "synonymRelations.synonym.name.status.type.representation", + "synonymRelations.synonym.name.nomenclaturalReference.inReference", + "synonymRelations.synonym.name.homotypicalGroup.typifiedNames.$", + "synonymRelations.synonym.name.homotypicalGroup.typifiedNames.taxonBases.$", + "synonymRelations.synonym.name.combinationAuthorTeam.$", + + "name.homotypicalGroup.$", + "name.homotypicalGroup.typifiedNames.$", + "name.homotypicalGroup.typifiedNames.nomenclaturalReference.authorTeam", + + "name.homotypicalGroup.typifiedNames.taxonBases.$", + + "taxonNodes.$", + "taxonNodes.classification.$", + "taxonNodes.childNodes.$" + }); + private static final List SIMPLE_TAXON_WITH_NODES_INIT_STRATEGY = Arrays.asList(new String []{ + "*", + // taxon relations + "relationsToThisName.fromTaxon.name", + // the name + "name.$", + "name.rank.representations", + "name.status.type.representations", + "name.nomenclaturalReference", + + "taxonNodes.$", + "taxonNodes.classification.$", + "taxonNodes.childNodes.$" + }); + + + private static final List TAXONRELATIONSHIP_INIT_STRATEGY = Arrays.asList(new String []{ + "$", + "type.inverseRepresentations", + "fromTaxon.sec", + "fromTaxon.name", + "toTaxon.sec", + "toTaxon.name" + }); + + private static final List NAMERELATIONSHIP_INIT_STRATEGY = Arrays.asList(new String []{ + "$", + "type.inverseRepresentations", + "fromName", + "toName.$", + }); + + + protected static final List TAXONDESCRIPTION_INIT_STRATEGY = Arrays.asList(new String []{ + "$", + "elements.$", + "elements.sources.citation.authorTeam", + "elements.sources.nameUsedInSource.originalNameString", + "elements.multilanguageText", + "elements.media.representations.parts", + "elements.media.title", + }); + + protected static final List TAXONUSEDESCRIPTION_INIT_STRATEGY = Arrays.asList(new String []{ + "$", + "name.$", + "name.rank.representations", + "name.status.type.representations", + "sources.$", + "elements.$", + "elements.states.$", + "elements.sources.citation.authorTeam", + "elements.sources.nameUsedInSource.originalNameString", + /*//"elements.multilanguageText", + "elements.media.representations.parts",*/ + "elements.media.title", + }); + + protected static final List DESCRIPTION_ELEMENT_INIT_STRATEGY = Arrays.asList(new String []{ + "$", + "sources.citation.authorTeam", + "sources.nameUsedInSource.originalNameString", + "multilanguageText", + "media.representations.parts", + "media.title", + }); + + +// private static final List NAMEDESCRIPTION_INIT_STRATEGY = Arrays.asList(new String []{ +// "uuid", +// "feature", +// "elements.$", +// "elements.multilanguageText", +// "elements.media.representations.parts", +// "elements.media.title", +// }); + + protected static final List TAXONDESCRIPTION_MEDIA_INIT_STRATEGY = Arrays.asList(new String []{ + "elements.media.representations.parts", + "elements.media.title" + + }); + + private static final List TYPEDESIGNATION_INIT_STRATEGY = Arrays.asList(new String []{ + "typeSpecimen.$", + "citation.authorTeam.$", + "typeName", + "typeStatus" + }); + + protected static final List TAXONNODE_WITHTAXON_INIT_STRATEGY = Arrays.asList(new String []{ + "childNodes.taxon", + }); + + protected static final List TAXONNODE_INIT_STRATEGY = Arrays.asList(new String []{ + "taxonNodes.classification" + }); + + + + private static final String featureTreeUuidPattern = "^/taxon(?:(?:/)([^/?#&\\.]+))+.*"; + + public TaxonPortalController(){ + super(); + setInitializationStrategy(TAXON_INIT_STRATEGY); + } + + /* (non-Javadoc) + * @see eu.etaxonomy.cdm.remote.controller.GenericController#setService(eu.etaxonomy.cdm.api.service.IService) + */ + @Autowired + @Override + public void setService(ITaxonService service) { + this.service = service; + } + + @InitBinder + @Override public void initBinder(WebDataBinder binder) { - binder.registerCustomEditor(UUID.class, new UUIDPropertyEditor()); - binder.registerCustomEditor(NamedArea.class, new NamedAreaPropertyEditor()); - } - - - /* (non-Javadoc) - * @see eu.etaxonomy.cdm.remote.controller.BaseController#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) - - @Override - @RequestMapping(method = RequestMethod.GET) - public TaxonBase doGet(HttpServletRequest request, HttpServletResponse response)throws IOException { - logger.info("doGet()"); - TaxonBase tb = getCdmBase(request, response, TAXON_INIT_STRATEGY, TaxonBase.class); - return tb; - } - */ - /** - * Find Taxa, Synonyms, Common Names by name, either globally or in a specific geographic area. - *

- * URI: /{datasource-name}/portal/taxon/find - * - * @param query - * the string to query for. Since the wildcard character '*' - * internally always is appended to the query string, a search - * always compares the query string with the beginning of a name. - * - required parameter - * @param treeUuid - * the {@link UUID} of a {@link TaxonomicTree} to which the - * search is to be restricted. - optional parameter - * @param areas - * restrict the search to a set of geographic {@link NamedArea}s. - * The parameter currently takes a list of TDWG area labels. - * - optional parameter - * @param page - * the number of the page to be returned, the first page has the - * pageNumber = 1 - optional parameter - * @param pageSize - * the maximum number of entities returned per page (can be null - * to return all entities in a single page) - optional parameter - * @param doTaxa - * weather to search for instances of {@link Taxon} - optional parameter - * @param doSynonyms - * weather to search for instances of {@link Synonym} - optional parameter - * @param doTaxaByCommonNames - * for instances of {@link Taxon} by a common name used - optional parameter - * @return a Pager on a list of {@link IdentifiableEntity}s initialized by - * the following strategy {@link #SIMPLE_TAXON_INIT_STRATEGY} - * @throws IOException - */ - @RequestMapping(method = RequestMethod.GET, - value = {"/*/portal/taxon/find"}) //TODO map to path /*/portal/taxon/ - public Pager doFind( - @RequestParam(value = "query", required = false) String query, - @RequestParam(value = "tree", required = false) UUID treeUuid, - @RequestParam(value = "area", required = false) Set areas, - @RequestParam(value = "page", required = false) Integer page, - @RequestParam(value = "pageSize", required = false) Integer pageSize, - @RequestParam(value = "doTaxa", required = false) Boolean doTaxa, - @RequestParam(value = "doSynonyms", required = false) Boolean doSynonyms, - @RequestParam(value = "doTaxaByCommonNames", required = false) Boolean doTaxaByCommonNames) - throws IOException { - - logger.info("doFind( " + - "query=\"" + ObjectUtils.toString(query) + "\", treeUuid=" + ObjectUtils.toString(treeUuid) + - ", area=" + ObjectUtils.toString(areas) + - ", pageSize=" + ObjectUtils.toString(pageSize) + ", page=" + ObjectUtils.toString(page) + - ", doTaxa=" + ObjectUtils.toString(doTaxa) + ", doSynonyms=" + ObjectUtils.toString(doSynonyms) +")" ); - - if(page == null){ page = BaseListController.DEFAULT_PAGE_NUMBER;} - if(pageSize == null){ pageSize = BaseListController.DEFAULT_PAGESIZE;} - - ITaxonServiceConfigurator config = new TaxonServiceConfiguratorImpl(); - config.setPageNumber(page); - config.setPageSize(pageSize); - config.setSearchString(query); - config.setDoTaxa(doTaxa!= null ? doTaxa : Boolean.FALSE ); - config.setDoSynonyms(doSynonyms != null ? doSynonyms : Boolean.FALSE ); - config.setDoTaxaByCommonNames(doTaxaByCommonNames != null ? doTaxaByCommonNames : Boolean.FALSE ); - config.setMatchMode(MatchMode.BEGINNING); - config.setTaxonPropertyPath(SIMPLE_TAXON_INIT_STRATEGY); - config.setNamedAreas(areas); - if(treeUuid != null){ - TaxonomicTree taxonomicTree = taxonTreeService.find(treeUuid); - config.setTaxonomicTree(taxonomicTree); - } - - return (Pager) service.findTaxaAndNames(config); - } - - /** - * Get the synonymy for a taxon identified by the {taxon-uuid}. - * The synonymy consists - * of two parts: The group of homotypic synonyms of the taxon and the - * heterotypic synonymy groups of the taxon. The synonymy is ordered - * historically by the type designations and by the publication date of the - * nomenclatural reference - *

- * URI: - * /{datasource-name}/portal/taxon/{taxon-uuid}/synonymy - * - * - * @param request - * @param response - * @return a Map with to entries which are mapped by the following keys: - * "homotypicSynonymsByHomotypicGroup", "heterotypicSynonymyGroups", - * containing lists of {@link Synonym}s which are initialized using the - * following initialization strategy: {@link #SYNONYMY_INIT_STRATEGY} - * - * @throws IOException - */ - @RequestMapping( - value = {"/*/portal/taxon/*/synonymy"}, - method = RequestMethod.GET) - public ModelAndView doGetSynonymy(HttpServletRequest request, HttpServletResponse response)throws IOException { - - logger.info("doGetSynonymy() " + request.getServletPath()); - ModelAndView mv = new ModelAndView(); - TaxonBase tb = getCdmBase(request, response, null, Taxon.class); - Taxon taxon = (Taxon)tb; - Map> synonymy = new Hashtable>(); - synonymy.put("homotypicSynonymsByHomotypicGroup", service.getHomotypicSynonymsByHomotypicGroup(taxon, SYNONYMY_INIT_STRATEGY)); - synonymy.put("heterotypicSynonymyGroups", service.getHeterotypicSynonymyGroups(taxon, SYNONYMY_INIT_STRATEGY)); - mv.addObject(synonymy); - return mv; - } - - /** - * Get the set of accepted {@link Taxon} entities for a given - * {@link TaxonBase} entity identified by the {taxon-uuid}. - *

- * URI: /{datasource-name}/portal/taxon/{taxon-uuid}/accepted - * - * @param request - * @param response - * @return a Set of {@link Taxon} entities which are initialized - * using the following initialization strategy: - * {@link #SYNONYMY_INIT_STRATEGY} - * @throws IOException - */ - @RequestMapping(value = "/*/portal/taxon/*/accepted", method = RequestMethod.GET) - public Set getAccepted(HttpServletRequest request, HttpServletResponse response) throws IOException { - - logger.info("getAccepted() " + request.getServletPath()); - - UUID uuid = readValueUuid(request, null); - TaxonBase tb = service.load(uuid, SYNONYMY_INIT_STRATEGY); - if(tb == null){ - response.sendError(HttpServletResponse.SC_NOT_FOUND, "A taxon with the uuid " + uuid + " does not exist"); - return null; - } - HashSet resultset = new HashSet(); - if(tb instanceof Taxon){ - //the taxon already is accepted - //FIXME take the current view into account once views are implemented!!! - resultset.add((Taxon)tb); - } else { - Synonym syn = (Synonym)tb; - for(TaxonBase accepted : syn.getAcceptedTaxa()){ - accepted = service.load(accepted.getUuid(), SIMPLE_TAXON_INIT_STRATEGY); - resultset.add(accepted); - } - } - return resultset; - } - - /** - * Get the list of {@link TaxonRelationship}s for the given - * {@link TaxonBase} instance identified by the {taxon-uuid}. - *

- * URI: /{datasource-name}/portal/taxon/{taxon-uuid}/taxonRelationships - * - * @param request - * @param response - * @return a List of {@link TaxonRelationship} entities which are initialized - * using the following initialization strategy: - * {@link #TAXONRELATIONSHIP_INIT_STRATEGY} - * @throws IOException - */ - @RequestMapping( - value = {"/*/portal/taxon/*/taxonRelationships"}, - method = RequestMethod.GET) - public List doGetTaxonRelations(HttpServletRequest request, HttpServletResponse response)throws IOException { - - logger.info("doGetTaxonRelations()" + request.getServletPath()); - TaxonBase tb = getCdmBase(request, response, null, Taxon.class); - Taxon taxon = (Taxon)tb; - List relations = new ArrayList(); - List results = service.listToTaxonRelationships(taxon, TaxonRelationshipType.MISAPPLIED_NAME_FOR(), null, null, null, TAXONRELATIONSHIP_INIT_STRATEGY); - relations.addAll(results); - results = service.listToTaxonRelationships(taxon, TaxonRelationshipType.INVALID_DESIGNATION_FOR(), null, null, null, TAXONRELATIONSHIP_INIT_STRATEGY); - relations.addAll(results); - - return relations; - } - - /** - * Get the list of {@link NameRelationship}s of the Name associated with the - * {@link TaxonBase} instance identified by the {taxon-uuid}. - *

- * URI: /{datasource-name}/portal/taxon/{taxon-uuid}/nameRelationships - * - * @param request - * @param response - * @return a List of {@link NameRelationship} entities which are initialized - * using the following initialization strategy: - * {@link #NAMERELATIONSHIP_INIT_STRATEGY} - * @throws IOException - */ - @RequestMapping( - value = {"/*/portal/taxon/*/nameRelationships"}, - method = RequestMethod.GET) - public List doGetNameRelations(HttpServletRequest request, HttpServletResponse response)throws IOException { - logger.info("doGetNameRelations()" + request.getServletPath()); - TaxonBase tb = getCdmBase(request, response, SIMPLE_TAXON_INIT_STRATEGY, Taxon.class); - List list = nameService.listToNameRelationships(tb.getName(), null, null, null, null, NAMERELATIONSHIP_INIT_STRATEGY); - return list; - } - - /** - * Get the list of {@link TaxonNameDescription}s of the Name associated with the - * {@link TaxonNameBase} instance identified by the {name-uuid}. - *

- * URI: /{datasource-name}/portal/name/{name-uuid}/descriptions - * - * @param request - * @param response - * @return a List of {@link TaxonNameDescription} entities which are initialized - * using the following initialization strategy: - * {@link #NAMEDESCRIPTION_INIT_STRATEGY} - * @throws IOException - */ - @RequestMapping( - value = {"/*/portal/name/*/descriptions"}, - method = RequestMethod.GET) - public List doGetNameDescriptions(HttpServletRequest request, HttpServletResponse response)throws IOException { - logger.info("doGetNameDescriptions()" + request.getServletPath()); - UUID nameUuuid = readValueUuid(request, null); - TaxonNameBase tnb = nameService.load(nameUuuid, null); - Pager p = descriptionService.getTaxonNameDescriptions(tnb, null, null, NAMEDESCRIPTION_INIT_STRATEGY); - return p.getRecords(); - } - - /** - * Get the list of {@link TypeDesignationBase}s of the - * {@link TaxonBase} instance identified by the {taxon-uuid}. - *

- * URI: /{datasource-name}/portal/taxon/{taxon-uuid}/nameTypeDesignations - * - * @param request - * @param response - * @return a List of {@link TypeDesignationBase} entities which are initialized - * using the following initialization strategy: - * {@link #TYPEDESIGNATION_INIT_STRATEGY} - * @throws IOException - */ - @RequestMapping( - value = {"/*/portal/taxon/*/nameTypeDesignations"}, - method = RequestMethod.GET) - public List doGetNameTypeDesignations(HttpServletRequest request, HttpServletResponse response)throws IOException { - logger.info("doGetNameTypeDesignations()" + request.getServletPath()); - TaxonBase tb = getCdmBase(request, response, SIMPLE_TAXON_INIT_STRATEGY, Taxon.class); - Pager p = nameService.getTypeDesignations(tb.getName(), null, null, null, TYPEDESIGNATION_INIT_STRATEGY); - return p.getRecords(); - } - - /** - * Get the list of {@link TaxonDescription}s of the - * {@link Taxon} instance identified by the {taxon-uuid}. - *

- * URI: /{datasource-name}/portal/taxon/{taxon-uuid}/descriptions - * - * @param request - * @param response - * @return a List of {@link TaxonDescription} entities which are initialized - * using the following initialization strategy: - * {@link #TAXONDESCRIPTION_INIT_STRATEGY} - * @throws IOException - */ - @RequestMapping( - value = {"/*/portal/taxon/*/descriptions"}, - method = RequestMethod.GET) - public List doGetDescriptions(HttpServletRequest request, HttpServletResponse response)throws IOException { - logger.info("doGetDescriptions()" + request.getServletPath()); - Taxon t = getCdmBase(request, response, null, Taxon.class); - Pager p = descriptionService.getTaxonDescriptions(t, null, null, null, null, - TAXONDESCRIPTION_INIT_STRATEGY); - return p.getRecords(); - } - - /** - * Get the {@link Media} attached to the {@link Taxon} instance - * identified by the {taxon-uuid}. - * - * Usage /{datasource-name}/portal/taxon/{taxon- - * uuid}/media/{mime type - * list}/{size}[,[widthOrDuration}][,{height}]/ - * - * Whereas - *

    - *
  • {mime type list}: a comma separated list of mime types, in the - * order of preference. The forward slashes contained in the mime types must - * be replaced by a colon. Regular expressions can be used. Each media - * associated with this given taxon is being searched whereas the first - * matching mime type matching a representation always rules.
  • - *
  • {size},{widthOrDuration},{height}: not jet implemented - * valid values are an integer or the asterisk '*' as a wildcard
  • - *
- * - * @param request - * @param response - * @return a List of {@link Media} entities which are initialized - * using the following initialization strategy: - * {@link #TAXONDESCRIPTION_INIT_STRATEGY} - * @throws IOException - */ - @RequestMapping( - value = {"/*/portal/taxon/*/media/*/*"}, - method = RequestMethod.GET) - public List doGetMedia(HttpServletRequest request, HttpServletResponse response)throws IOException { - logger.info("doGetMedia()" + request.getServletPath()); - Taxon t = getCdmBase(request, response, null, Taxon.class); - Pager p = - descriptionService.getTaxonDescriptions(t, null, null, null, null, TAXONDESCRIPTION_INIT_STRATEGY); - - // pars the media and quality parameters - - - // collect all media of the given taxon - boolean limitToGalleries = false; - List taxonMedia = new ArrayList(); - for(TaxonDescription desc : p.getRecords()){ - if(!limitToGalleries || desc.isImageGallery()){ - for(DescriptionElementBase element : desc.getElements()){ - for(Media media : element.getMedia()){ - taxonMedia.add(media); - } - } - } - } - - // move into media ... - - // find best matching representations of each media - String path = request.getServletPath(); - String[] pathTokens = path.split("/"); - String[] mimeTypes = pathTokens[6].split(","); - String[] sizeTokens = pathTokens[7].split(","); - Integer widthOrDuration = null; - Integer height = null; - Integer size = null; - - for(int i=0; i 0){ - try { - size = Integer.valueOf(sizeTokens[0]); - } catch (NumberFormatException nfe) { - /* IGNORE */ - } - } - if(sizeTokens.length > 1){ - try { - widthOrDuration = Integer.valueOf(sizeTokens[1]); - } catch (NumberFormatException nfe) { - /* IGNORE */ - } - } - if(sizeTokens.length > 2){ - try { - height = Integer.valueOf(sizeTokens[2]); - } catch (NumberFormatException nfe) { - /* IGNORE */ - } - } - - List returnMedia = new ArrayList(taxonMedia.size()); - for(Media media : taxonMedia){ - SortedMap prefRepresentations - = orderMediaRepresentations(media, mimeTypes, size, widthOrDuration, height); - try { - // take first one and remove all other representations - MediaRepresentation prefOne = prefRepresentations.get(prefRepresentations.firstKey()); - for (MediaRepresentation representation : media.getRepresentations()) { - if (representation != prefOne) { - media.removeRepresentation(representation); - } - } - returnMedia.add(media); - } catch (NoSuchElementException nse) { - /* IGNORE */ - } - } - - return returnMedia; - } - - /** - * @param media - * @param mimeTypeRegexes - * @param size - * @param widthOrDuration - * @param height - * @return - * - * TODO move into a media utils class - * TODO implement the quality filter - */ - private SortedMap orderMediaRepresentations(Media media, String[] mimeTypeRegexes, - Integer size, Integer widthOrDuration, Integer height) { - SortedMap prefRepr = new TreeMap(); - for (String mimeTypeRegex : mimeTypeRegexes) { - // getRepresentationByMimeType - Pattern mimeTypePattern = Pattern.compile(mimeTypeRegex); - int representationCnt = 0; - for (MediaRepresentation representation : media.getRepresentations()) { - Matcher mather = mimeTypePattern.matcher(representation.getMimeType()); - if (mather.matches()) { - int dwa = 0; - - /* TODO the quality filter part is being skipped - * // look for representation with the best matching parts - for (MediaRepresentationPart part : representation.getParts()) { - if (part instanceof ImageFile) { - ImageFile image = (ImageFile) part; - int dw = image.getWidth() * image.getHeight() - height * widthOrDuration; - if (dw < 0) { - dw *= -1; - } - dwa += dw; - } - dwa = (representation.getParts().size() > 0 ? dwa / representation.getParts().size() : 0); - }*/ - prefRepr.put((dwa + representationCnt++) + '_' + representation.getMimeType(), representation); - - // preferred mime type found => end loop - break; - } - } - } - return prefRepr; - } - + super.initBinder(binder); + binder.registerCustomEditor(NamedArea.class, new NamedAreaPropertyEditor()); + binder.registerCustomEditor(MatchMode.class, new MatchModePropertyEditor()); + binder.registerCustomEditor(Class.class, new CdmTypePropertyEditor()); + binder.registerCustomEditor(UuidList.class, new UUIDListPropertyEditor()); + + } + + + /* (non-Javadoc) + * @see eu.etaxonomy.cdm.remote.controller.BaseController#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) + + @Override + @RequestMapping(method = RequestMethod.GET) + public TaxonBase doGet(HttpServletRequest request, HttpServletResponse response)throws IOException { + logger.info("doGet()"); + TaxonBase tb = getCdmBase(request, response, TAXON_INIT_STRATEGY, TaxonBase.class); + return tb; + } + */ + /** + * Find Taxa, Synonyms, Common Names by name, either globally or in a specific geographic area. + *

+ * URI: /{datasource-name}/portal/taxon/find + * + * @param query + * the string to query for. Since the wildcard character '*' + * internally always is appended to the query string, a search + * always compares the query string with the beginning of a name. + * - required parameter + * @param treeUuid + * the {@link UUID} of a {@link Classification} to which the + * search is to be restricted. - optional parameter + * @param areas + * restrict the search to a set of geographic {@link NamedArea}s. + * The parameter currently takes a list of TDWG area labels. + * - optional parameter + * @param pageNumber + * the number of the page to be returned, the first page has the + * pageNumber = 1 - optional parameter + * @param pageSize + * the maximum number of entities returned per page (can be -1 + * to return all entities in a single page) - optional parameter + * @param doTaxa + * weather to search for instances of {@link Taxon} - optional parameter + * @param doSynonyms + * weather to search for instances of {@link Synonym} - optional parameter + * @param doTaxaByCommonNames + * for instances of {@link Taxon} by a common name used - optional parameter + * @param matchMode + * valid values are "EXACT", "BEGINNING", "ANYWHERE", "END" (case sensitive !!!) + * @return a Pager on a list of {@link IdentifiableEntity}s initialized by + * the following strategy {@link #SIMPLE_TAXON_INIT_STRATEGY} + * @throws IOException + */ + @RequestMapping(method = RequestMethod.GET, + value = {"/portal/taxon/find"}) //TODO map to path /*/portal/taxon/ + public Pager doFind( + @RequestParam(value = "query", required = false) String query, + @RequestParam(value = "tree", required = false) UUID treeUuid, + @RequestParam(value = "area", required = false) Set areas, + @RequestParam(value = "pageNumber", required = false) Integer pageNumber, + @RequestParam(value = "pageSize", required = false) Integer pageSize, + @RequestParam(value = "doTaxa", required = false) Boolean doTaxa, + @RequestParam(value = "doSynonyms", required = false) Boolean doSynonyms, + @RequestParam(value = "doMisappliedNames", required = false) Boolean doMisappliedNames, + @RequestParam(value = "doTaxaByCommonNames", required = false) Boolean doTaxaByCommonNames, + @RequestParam(value = "matchMode", required = false) MatchMode matchMode, + HttpServletRequest request, + HttpServletResponse response + ) + throws IOException { + + logger.info("doFind : " + request.getRequestURI() + "?" + request.getQueryString() ); + + PagerParameters pagerParams = new PagerParameters(pageSize, pageNumber); + pagerParams.normalizeAndValidate(response); + + IFindTaxaAndNamesConfigurator config = new FindTaxaAndNamesConfiguratorImpl(); + config.setPageNumber(pagerParams.getPageIndex()); + config.setPageSize(pagerParams.getPageSize()); + config.setTitleSearchString(query); + config.setDoTaxa(doTaxa!= null ? doTaxa : Boolean.FALSE ); + config.setDoSynonyms(doSynonyms != null ? doSynonyms : Boolean.FALSE ); + config.setDoMisappliedNames(doMisappliedNames != null ? doMisappliedNames : Boolean.FALSE); + config.setDoTaxaByCommonNames(doTaxaByCommonNames != null ? doTaxaByCommonNames : Boolean.FALSE ); + config.setMatchMode(matchMode != null ? matchMode : MatchMode.BEGINNING); + config.setTaxonPropertyPath(SIMPLE_TAXON_INIT_STRATEGY); + config.setNamedAreas(areas); + if(treeUuid != null){ + Classification classification = classificationService.find(treeUuid); + config.setClassification(classification); + } + + return (Pager) service.findTaxaAndNames(config); + } + + /** + * @param clazz + * @param queryString + * @param treeUuid + * @param languages + * @param features one or more feature uuids + * @param pageNumber + * @param pageSize + * @param request + * @param response + * @return + * @throws IOException + * @throws ParseException + */ + @RequestMapping(method = RequestMethod.GET, value={"/portal/taxon/findByDescriptionElementFullText"}) + public Pager> dofindByDescriptionElementFullText( + @RequestParam(value = "clazz", required = false) Class clazz, + @RequestParam(value = "query", required = true) String queryString, + @RequestParam(value = "tree", required = false) UUID treeUuid, + @RequestParam(value = "features", required = false) UuidList featureUuids, + @RequestParam(value = "languages", required = false) List languages, + @RequestParam(value = "hl", required = false) Boolean highlighting, + @RequestParam(value = "pageNumber", required = false) Integer pageNumber, + @RequestParam(value = "pageSize", required = false) Integer pageSize, + HttpServletRequest request, + HttpServletResponse response + ) + throws IOException, ParseException { + + logger.info("findByDescriptionElementFullText : " + request.getRequestURI() + "?" + request.getQueryString() ); + + PagerParameters pagerParams = new PagerParameters(pageSize, pageNumber); + pagerParams.normalizeAndValidate(response); + + if(highlighting == null){ + highlighting = false; + } + + Classification classification = null; + if(treeUuid != null){ + classification = classificationService.find(treeUuid); + } + + List features = null; + if(featureUuids != null){ + features = new ArrayList(featureUuids.size()); + for(UUID uuid : featureUuids){ + features.add((Feature) termService.find(uuid)); + } + } + + Pager> pager = service.findByDescriptionElementFullText(clazz, queryString, classification, features, languages, highlighting, pagerParams.getPageSize(), pagerParams.getPageIndex(), ((List)null), SIMPLE_TAXON_INIT_STRATEGY); + return pager; + } + + /** + * Get the synonymy for a taxon identified by the {taxon-uuid}. + * The synonymy consists + * of two parts: The group of homotypic synonyms of the taxon and the + * heterotypic synonymy groups of the taxon. The synonymy is ordered + * historically by the type designations and by the publication date of the + * nomenclatural reference + *

+ * URI: + * /{datasource-name}/portal/taxon/{taxon-uuid}/synonymy + * + * + * @param request + * @param response + * @return a Map with to entries which are mapped by the following keys: + * "homotypicSynonymsByHomotypicGroup", "heterotypicSynonymyGroups", + * containing lists of {@link Synonym}s which are initialized using the + * following initialization strategy: {@link #SYNONYMY_INIT_STRATEGY} + * + * @throws IOException + */ + @RequestMapping( + value = {"synonymy"}, + method = RequestMethod.GET) + public ModelAndView doGetSynonymy(@PathVariable("uuid") UUID uuid, + HttpServletRequest request, HttpServletResponse response)throws IOException { + + if(request != null){ + logger.info("doGetSynonymy() " + request.getServletPath()); + } + ModelAndView mv = new ModelAndView(); + Taxon taxon = getCdmBaseInstance(Taxon.class, uuid, response, (List)null); + Map> synonymy = new Hashtable>(); + synonymy.put("homotypicSynonymsByHomotypicGroup", service.getHomotypicSynonymsByHomotypicGroup(taxon, SYNONYMY_INIT_STRATEGY)); + synonymy.put("heterotypicSynonymyGroups", service.getHeterotypicSynonymyGroups(taxon, SYNONYMY_INIT_STRATEGY)); + mv.addObject(synonymy); + return mv; + } + + /** + * Get the set of accepted {@link Taxon} entities for a given + * {@link TaxonBase} entity identified by the {taxon-uuid}. + *

+ * URI: /{datasource-name}/portal/taxon/{taxon-uuid}/accepted + * + * @param request + * @param response + * @return a Set of {@link Taxon} entities which are initialized + * using the following initialization strategy: + * {@link #SYNONYMY_INIT_STRATEGY} + * @throws IOException + */ + @RequestMapping(value = "accepted/{classification_uuid}", method = RequestMethod.GET) + public Set getAccepted( + @PathVariable("uuid") UUID uuid, + @PathVariable("classification_uuid") UUID classification_uuid, + HttpServletRequest request, + HttpServletResponse response) + throws IOException { + + if(request != null){ + logger.info("getAccepted() " + request.getServletPath()); + } + + TaxonBase tb = service.load(uuid, SYNONYMY_WITH_NODES_INIT_STRATEGY); + if(tb == null){ + response.sendError(HttpServletResponse.SC_NOT_FOUND, "A taxon with the uuid " + uuid + " does not exist"); + return null; + } + + HashSet resultset = new HashSet(); + + if (tb instanceof Taxon){ + Taxon taxon = (Taxon) tb; + Set nodes = taxon.getTaxonNodes(); + for (TaxonNode taxonNode : nodes) { + if (taxonNode.getClassification().compareTo(classification_uuid) == 0){ + resultset.add((Taxon) tb); + } + } + if (resultset.size() > 1){ + //error!! A taxon is not allow to have more taxonnodes for a given classification + response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, + "A taxon with the uuid " + uuid + " has more than one taxon node for the given classification" + classification_uuid); + } + }else{ + Synonym syn = (Synonym) tb; + for(TaxonBase accepted : syn.getAcceptedTaxa()){ + tb = service.load(accepted.getUuid(), SIMPLE_TAXON_WITH_NODES_INIT_STRATEGY); + if (tb instanceof Taxon){ + Taxon taxon = (Taxon) tb; + Set nodes = taxon.getTaxonNodes(); + for (TaxonNode taxonNode : nodes) { + if (taxonNode.getClassification().compareTo(classification_uuid) == 0){ + resultset.add((Taxon) tb); + } + } + if (resultset.size() > 1){ + //error!! A taxon is not allow to have more taxonnodes for a given classification + response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, + "A taxon with the uuid " + uuid + " has more than one taxon node for the given classification" + classification_uuid); + } + }else{ + //ERROR!! perhaps missapplied name???? + //syn.getRelationType((Taxon)accepted); + } + } + } +/** + * OLD CODE!! + if(tb instanceof Taxon){ + //the taxon already is accepted + //FIXME take the current view into account once views are implemented!!! + resultset.add((Taxon)tb); + } else { + Synonym syn = (Synonym)tb; + for(TaxonBase accepted : syn.getAcceptedTaxa()){ + accepted = service.load(accepted.getUuid(), SIMPLE_TAXON_INIT_STRATEGY); + resultset.add(accepted); + } + } +*/ + return resultset; + } + + /** + * Get the list of {@link TaxonRelationship}s for the given + * {@link TaxonBase} instance identified by the {taxon-uuid}. + *

+ * URI: /{datasource-name}/portal/taxon/{taxon-uuid}/taxonRelationships + * + * @param request + * @param response + * @return a List of {@link TaxonRelationship} entities which are initialized + * using the following initialization strategy: + * {@link #TAXONRELATIONSHIP_INIT_STRATEGY} + * @throws IOException + */ + @RequestMapping( + value = {"taxonRelationships"}, + method = RequestMethod.GET) + public List doGetTaxonRelations(@PathVariable("uuid") UUID uuid, + HttpServletRequest request, HttpServletResponse response)throws IOException { + + logger.info("doGetTaxonRelations()" + request.getServletPath()); + Taxon taxon = getCdmBaseInstance(Taxon.class, uuid, response, (List)null); + List toRelationships = service.listToTaxonRelationships(taxon, null, null, null, null, TAXONRELATIONSHIP_INIT_STRATEGY); + List fromRelationships = service.listFromTaxonRelationships(taxon, null, null, null, null, TAXONRELATIONSHIP_INIT_STRATEGY); + + List allRelationships = new ArrayList(toRelationships.size() + fromRelationships.size()); + allRelationships.addAll(toRelationships); + allRelationships.addAll(fromRelationships); + + return allRelationships; + } + + /** + * Get the list of {@link NameRelationship}s of the Name associated with the + * {@link TaxonBase} instance identified by the {taxon-uuid}. + *

+ * URI: /{datasource-name}/portal/taxon/{taxon-uuid}/nameRelationships + * + * @param request + * @param response + * @return a List of {@link NameRelationship} entities which are initialized + * using the following initialization strategy: + * {@link #NAMERELATIONSHIP_INIT_STRATEGY} + * @throws IOException + */ + @RequestMapping( + value = {"toNameRelationships"}, + method = RequestMethod.GET) + public List doGetToNameRelations(@PathVariable("uuid") UUID uuid, + HttpServletRequest request, HttpServletResponse response)throws IOException { + logger.info("doGetNameRelations()" + request.getServletPath()); + TaxonBase taxonBase = getCdmBaseInstance(TaxonBase.class, uuid, response, (List)null); + List list = nameService.listNameRelationships(taxonBase.getName(), Direction.relatedTo, null, null, 0, null, NAMERELATIONSHIP_INIT_STRATEGY); + //List list = nameService.listToNameRelationships(taxonBase.getName(), null, null, null, null, NAMERELATIONSHIP_INIT_STRATEGY); + return list; + } + + /** + * Get the list of {@link NameRelationship}s of the Name associated with the + * {@link TaxonBase} instance identified by the {taxon-uuid}. + *

+ * URI: /{datasource-name}/portal/taxon/{taxon-uuid}/nameRelationships + * + * @param request + * @param response + * @return a List of {@link NameRelationship} entities which are initialized + * using the following initialization strategy: + * {@link #NAMERELATIONSHIP_INIT_STRATEGY} + * @throws IOException + */ + @RequestMapping( + value = {"fromNameRelationships"}, + method = RequestMethod.GET) + public List doGetFromNameRelations(@PathVariable("uuid") UUID uuid, + HttpServletRequest request, HttpServletResponse response)throws IOException { + logger.info("doGetNameFromNameRelations()" + request.getServletPath()); + + TaxonBase taxonbase = getCdmBaseInstance(TaxonBase.class, uuid, response, SIMPLE_TAXON_INIT_STRATEGY); + List list = nameService.listNameRelationships(taxonbase.getName(), Direction.relatedFrom, null, null, 0, null, NAMERELATIONSHIP_INIT_STRATEGY); + //List list = nameService.listFromNameRelationships(taxonbase.getName(), null, null, null, null, NAMERELATIONSHIP_INIT_STRATEGY); + return list; + } + + /** + * Get the list of {@link TypeDesignationBase}s of the + * {@link TaxonBase} instance identified by the {taxon-uuid}. + *

+ * URI: /{datasource-name}/portal/taxon/{taxon-uuid}/nameTypeDesignations + * + * @param request + * @param response + * @return a List of {@link TypeDesignationBase} entities which are initialized + * using the following initialization strategy: + * {@link #TYPEDESIGNATION_INIT_STRATEGY} + * @throws IOException + * @Deprecated use /name/{uuid}/typeDesignations & /derivedunitfacade/{uuid} instead + * also see http://dev.e-taxonomy.eu/trac/ticket/2280 + */ + @Deprecated + @RequestMapping( + value = {"nameTypeDesignations"}, + method = RequestMethod.GET) + public List doGetNameTypeDesignations(@PathVariable("uuid") UUID uuid, + HttpServletRequest request, HttpServletResponse response)throws IOException { + logger.info("doGetNameTypeDesignations()" + request.getServletPath()); + Taxon taxon = getCdmBaseInstance(Taxon.class, uuid, response, SIMPLE_TAXON_INIT_STRATEGY); + Pager p = nameService.getTypeDesignations(taxon.getName(), null, null, null, TYPEDESIGNATION_INIT_STRATEGY); + return p.getRecords(); + } + + @RequestMapping(value = "taxonNodes", method = RequestMethod.GET) + public Set doGetTaxonNodes( + @PathVariable("uuid") UUID uuid, + HttpServletRequest request, + HttpServletResponse response) throws IOException { + TaxonBase tb = service.load(uuid, TAXONNODE_INIT_STRATEGY); + if(tb instanceof Taxon){ + return ((Taxon)tb).getTaxonNodes(); + } else { + HttpStatusMessage.UUID_REFERENCES_WRONG_TYPE.send(response); + return null; + } + } + + /** + * Get the list of {@link TaxonDescription}s of the + * {@link Taxon} instance identified by the {taxon-uuid}. + *

+ * URI: /{datasource-name}/portal/taxon/{taxon-uuid}/descriptions + * + * @param request + * @param response + * @return a List of {@link TaxonDescription} entities which are initialized + * using the following initialization strategy: + * {@link #TAXONDESCRIPTION_INIT_STRATEGY} + * @throws IOException + */ + @RequestMapping( + value = {"descriptions"}, + method = RequestMethod.GET) + public List doGetDescriptions( + @PathVariable("uuid") UUID uuid, + @RequestParam(value = "markerTypes", required = false) UuidList markerTypeUUIDs, + HttpServletRequest request, + HttpServletResponse response)throws IOException { + if(request != null){ + logger.info("doGetDescriptions()" + request.getServletPath()); + } + List markerTypeTerms = null; + Set sMarkerTypeUUIDs = null; + + if(markerTypeUUIDs != null && !markerTypeUUIDs.isEmpty()){ + sMarkerTypeUUIDs = new HashSet(markerTypeUUIDs); + markerTypeTerms = termService.find(sMarkerTypeUUIDs); + } else if(markerTypeUUIDs != null && markerTypeUUIDs.isEmpty()){ + markerTypeTerms = new ArrayList(); + } + Set markerTypes = new HashSet(); + List descriptions = new ArrayList(); + if (markerTypeTerms != null) { + for (DefinedTermBase markerTypeTerm : markerTypeTerms) { + markerTypes.add((MarkerType)markerTypeTerm); + } + } + Taxon t = getCdmBaseInstance(Taxon.class, uuid, response, (List)null); + if (markerTypeTerms == null) { + + Pager p = descriptionService.getTaxonDescriptions(t, null, null, null, null, TAXONDESCRIPTION_INIT_STRATEGY); + descriptions = p.getRecords(); + } + + else if (markerTypeTerms != null && markerTypeTerms.isEmpty()) { + descriptions = descriptionService.listTaxonDescriptions(t, null, null, markerTypes, null, null, TAXONUSEDESCRIPTION_INIT_STRATEGY); + + } + else { + descriptions = descriptionService.listTaxonDescriptions(t, null, null, markerTypes, null, null, TAXONUSEDESCRIPTION_INIT_STRATEGY); + /*for (TaxonDescription description: descriptions) { + for (IdentifiableSource source :description.getSources()) { + if (source.getOriginalNameString() != null) { + description. + } + + } + + + }*/ + } + return descriptions; + } + + @RequestMapping(value = "useDescriptions", method = RequestMethod.GET) + public List doGetUseDescriptions( + @PathVariable("uuid") UUID uuid, + HttpServletRequest request, + HttpServletResponse response) throws IOException { + logger.info("doGetDescriptionElements() - " + request.getServletPath()); + + //ModelAndView mv = new ModelAndView(); + Taxon t = getCdmBaseInstance(Taxon.class, uuid, response, (List)null); + + //MarkerType useMarkerType = (MarkerType) markerTypeService.find(UUID.fromString("2e6e42d9-e92a-41f4-899b-03c0ac64f059")); + MarkerType useMarkerType = (MarkerType) termService.find(UUID.fromString("2e6e42d9-e92a-41f4-899b-03c0ac64f039")); + + //find(UUID.fromString("2e6e42d9-e92a-41f4-899b-03c0ac64f059")); + Set markerTypes = new HashSet(); + markerTypes.add(useMarkerType); + List descriptionElements = descriptionService.listTaxonDescriptions(t, null, null, markerTypes, null, null, TAXONUSEDESCRIPTION_INIT_STRATEGY); + //getDescriptionElements(description, features, type, pageSize, pageNumber, propertyPaths) load(uuid); + + /*if(!(description instanceof TaxonDescription)){ + HttpStatusMessage.UUID_REFERENCES_WRONG_TYPE.send(response); + // will terminate thread + }*/ + + //boolean hasStructuredData = service. hasStructuredData(description); + + //mv.addObject(hasStructuredData); + + return descriptionElements; + } + + @RequestMapping(value = "descriptions/elementsByType/{classSimpleName}", method = RequestMethod.GET) + public ModelAndView doGetDescriptionElementsByType( + @PathVariable("uuid") UUID uuid, + @PathVariable("classSimpleName") String classSimpleName, + @RequestParam(value = "markerTypes", required = false) UuidList markerTypeUUIDs, + @RequestParam(value = "count", required = false, defaultValue = "false") Boolean doCount, + HttpServletRequest request, + HttpServletResponse response) throws IOException { + logger.info("doGetDescriptionElementsByType() - " + request.getServletPath()); + + ModelAndView mv = new ModelAndView(); + + List allElements = new ArrayList(); + List elements; + int count = 0; + + List initStrategy = doCount ? null : DESCRIPTION_ELEMENT_INIT_STRATEGY; + + List taxonDescriptions = doGetDescriptions(uuid, markerTypeUUIDs, request, response); + try { + Class type; + type = Class.forName("eu.etaxonomy.cdm.model.description." + + classSimpleName); + if (taxonDescriptions != null) { + for (TaxonDescription description : taxonDescriptions) { + elements = descriptionService.listDescriptionElements(description, null, type, null, 0, initStrategy); + allElements.addAll(elements); + count += elements.size(); + } + + } + } catch (ClassNotFoundException e) { + HttpStatusMessage.fromString(e.getLocalizedMessage()).send(response); + } + if(doCount){ + mv.addObject(count); + } else { + mv.addObject(allElements); + } + return mv; + } + +// @RequestMapping(value = "specimens", method = RequestMethod.GET) +// public ModelAndView doGetSpecimens( +// @PathVariable("uuid") UUID uuid, +// HttpServletRequest request, +// HttpServletResponse response) throws IOException, ClassNotFoundException { +// logger.info("doGetSpecimens() - " + request.getServletPath()); +// +// ModelAndView mv = new ModelAndView(); +// +// List derivedUnitFacadeList = new ArrayList(); +// +// // find speciemens in the TaxonDescriptions +// List taxonDescriptions = doGetDescriptions(uuid, request, response); +// if (taxonDescriptions != null) { +// +// for (TaxonDescription description : taxonDescriptions) { +// derivedUnitFacadeList.addAll( occurrenceService.listDerivedUnitFacades(description, null) ); +// } +// } +// // TODO find speciemens in the NameDescriptions ?? +// +// // TODO also find type specimens +// +// mv.addObject(derivedUnitFacadeList); +// +// return mv; +// } + + /** + * Get the {@link Media} attached to the {@link Taxon} instance + * identified by the {taxon-uuid}. + * + * Usage /{datasource-name}/portal/taxon/{taxon- + * uuid}/media/{mime type + * list}/{size}[,[widthOrDuration}][,{height}]/ + * + * Whereas + *

    + *
  • {mime type list}: a comma separated list of mime types, in the + * order of preference. The forward slashes contained in the mime types must + * be replaced by a colon. Regular expressions can be used. Each media + * associated with this given taxon is being searched whereas the first + * matching mime type matching a representation always rules.
  • + *
  • {size},{widthOrDuration},{height}: not jet implemented + * valid values are an integer or the asterisk '*' as a wildcard
  • + *
+ * + * @param request + * @param response + * @return a List of {@link Media} entities which are initialized + * using the following initialization strategy: + * {@link #TAXONDESCRIPTION_INIT_STRATEGY} + * @throws IOException + */ + @RequestMapping( + value = {"media"}, + method = RequestMethod.GET) + public List doGetMedia( + @PathVariable("uuid") UUID uuid, + @RequestParam(value = "type", required = false) Class type, + @RequestParam(value = "mimeTypes", required = false) String[] mimeTypes, + @RequestParam(value = "widthOrDuration", required = false) Integer widthOrDuration, + @RequestParam(value = "height", required = false) Integer height, + @RequestParam(value = "size", required = false) Integer size, + HttpServletRequest request, HttpServletResponse response) throws IOException { + + logger.info("doGetMedia()" + request.getServletPath()); + Taxon t = getCdmBaseInstance(Taxon.class, uuid, response, (List)null); + String path = request.getServletPath(); + List returnMedia = getMediaForTaxon(t, type, mimeTypes, widthOrDuration, height, size); + return returnMedia; + } + + @RequestMapping( + value = {"subtree/media"}, + method = RequestMethod.GET) + public List doGetSubtreeMedia( + @PathVariable("uuid") UUID uuid, + @RequestParam(value = "type", required = false) Class type, + @RequestParam(value = "mimeTypes", required = false) String[] mimeTypes, + @RequestParam(value = "widthOrDuration", required = false) Integer widthOrDuration, + @RequestParam(value = "height", required = false) Integer height, + @RequestParam(value = "size", required = false) Integer size, + HttpServletRequest request, HttpServletResponse response)throws IOException { + logger.info("doGetMedia()" + request.getServletPath()); + Taxon taxon = getCdmBaseInstance(Taxon.class, uuid, response, TAXON_WITH_NODES_INIT_STRATEGY); + String requestPath = request.getServletPath(); + List returnMedia = getMediaForTaxon(taxon, type, mimeTypes, widthOrDuration, height, size); + TaxonNode node; + //looking for all medias of genus + if (taxon.getTaxonNodes().size()>0){ + Set nodes = taxon.getTaxonNodes(); + Iterator iterator = nodes.iterator(); + //TaxonNode holen + node = iterator.next(); + //Check if TaxonNode belongs to the current tree + + node = classificationService.loadTaxonNode(node, TAXONNODE_WITHTAXON_INIT_STRATEGY); + Set children = node.getChildNodes(); + Taxon childTaxon; + for (TaxonNode child : children){ + childTaxon = child.getTaxon(); + childTaxon = (Taxon)taxonService.load(childTaxon.getUuid(), null); + returnMedia.addAll(getMediaForTaxon(childTaxon, type, mimeTypes, widthOrDuration, height, size)); + } + } + return returnMedia; + } + + + private List getMediaForTaxon(Taxon taxon, Class type, String[] mimeTypes, + Integer widthOrDuration, Integer height, Integer size){ + + List taxonGalleryMedia = service.listTaxonDescriptionMedia(taxon, false, TAXONDESCRIPTION_MEDIA_INIT_STRATEGY); + + Map mediaRepresentationMap = MediaUtils.findPreferredMedia(taxonGalleryMedia, type, + mimeTypes, null, widthOrDuration, height, size); + + List filteredMedia = new ArrayList(mediaRepresentationMap.size()); + for(Media media : mediaRepresentationMap.keySet()){ + media.getRepresentations().clear(); + media.addRepresentation(mediaRepresentationMap.get(media)); + filteredMedia.add(media); + } + + return filteredMedia; + } + + // ---------------------- code snippet preserved for possible later use -------------------- // @RequestMapping( // value = {"/*/portal/taxon/*/descriptions"}, @@ -679,13 +1008,13 @@ public class TaxonPortalController extends BaseController generalize // UUID featureTreeUuid = readValueUuid(request, featureTreeUuidPattern); -// +// // FeatureTree featureTree = descriptionService.getFeatureTreeByUuid(featureTreeUuid); // Pager p = descriptionService.getTaxonDescriptions((Taxon)tb, null, null, null, null, TAXONDESCRIPTION_INIT_STRATEGY); // List descriptions = p.getRecords(); -// +// // if(!featureTree.isDescriptionSeparated()){ -// +// // TaxonDescription superDescription = TaxonDescription.NewInstance(); // //put all descriptionElements in superDescription and make it invisible // for(TaxonDescription description: descriptions){