X-Git-Url: https://dev.e-taxonomy.eu/gitweb/cdmlib.git/blobdiff_plain/d9f1f8e5d03b88ea4b3ff8859ef4424235204e72..fe3d023fce484d02ce1c8da54979fe2ad983d5ca:/cdmlib-remote/src/main/java/eu/etaxonomy/cdm/remote/controller/TaxonListController.java diff --git a/cdmlib-remote/src/main/java/eu/etaxonomy/cdm/remote/controller/TaxonListController.java b/cdmlib-remote/src/main/java/eu/etaxonomy/cdm/remote/controller/TaxonListController.java index 7c85f5f00e..6b8679a91d 100644 --- a/cdmlib-remote/src/main/java/eu/etaxonomy/cdm/remote/controller/TaxonListController.java +++ b/cdmlib-remote/src/main/java/eu/etaxonomy/cdm/remote/controller/TaxonListController.java @@ -10,32 +10,54 @@ package eu.etaxonomy.cdm.remote.controller; import java.io.IOException; +import java.util.ArrayList; import java.util.Arrays; +import java.util.Collection; +import java.util.EnumSet; +import java.util.HashSet; +import java.util.List; import java.util.Set; import java.util.UUID; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import org.apache.commons.lang.BooleanUtils; +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 eu.etaxonomy.cdm.api.service.IClassificationService; import eu.etaxonomy.cdm.api.service.ITaxonService; -import eu.etaxonomy.cdm.api.service.config.ITaxonServiceConfigurator; -import eu.etaxonomy.cdm.api.service.config.TaxonServiceConfiguratorImpl; +import eu.etaxonomy.cdm.api.service.ITermService; +import eu.etaxonomy.cdm.api.service.TaxaAndNamesSearchMode; +import eu.etaxonomy.cdm.api.service.config.FindTaxaAndNamesConfiguratorImpl; +import eu.etaxonomy.cdm.api.service.config.IFindTaxaAndNamesConfigurator; import eu.etaxonomy.cdm.api.service.pager.Pager; +import eu.etaxonomy.cdm.api.service.search.LuceneMultiSearchException; +import eu.etaxonomy.cdm.api.service.search.SearchResult; import eu.etaxonomy.cdm.model.common.IdentifiableEntity; +import eu.etaxonomy.cdm.model.common.Language; +import eu.etaxonomy.cdm.model.description.DescriptionElementBase; +import eu.etaxonomy.cdm.model.description.Feature; +import eu.etaxonomy.cdm.model.description.PresenceAbsenceTermBase; import eu.etaxonomy.cdm.model.location.NamedArea; import eu.etaxonomy.cdm.model.taxon.Classification; 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.persistence.query.MatchMode; +import eu.etaxonomy.cdm.persistence.query.OrderHint; import eu.etaxonomy.cdm.remote.controller.util.PagerParameters; +import eu.etaxonomy.cdm.remote.editor.DefinedTermBaseList; +import eu.etaxonomy.cdm.remote.editor.TermBaseListPropertyEditor; +import eu.etaxonomy.cdm.remote.editor.UuidList; /** * TODO write controller documentation @@ -47,17 +69,22 @@ import eu.etaxonomy.cdm.remote.controller.util.PagerParameters; @RequestMapping(value = {"/taxon"}) public class TaxonListController extends IdentifiableListController { - - + + private static final List SIMPLE_TAXON_INIT_STRATEGY = DEFAULT_INIT_STRATEGY; + protected List getSimpleTaxonInitStrategy() { + // TODO Auto-generated method stub + return SIMPLE_TAXON_INIT_STRATEGY; + } + /** - * - */ - public TaxonListController(){ + * + */ + public TaxonListController(){ super(); setInitializationStrategy(Arrays.asList(new String[]{"$","name.nomenclaturalReference"})); } - /* (non-Javadoc) + /* (non-Javadoc) * @see eu.etaxonomy.cdm.remote.controller.BaseListController#setService(eu.etaxonomy.cdm.api.service.IService) */ @Override @@ -65,10 +92,111 @@ public class TaxonListController extends IdentifiableListController(termService)); + } + + /** + * 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 + * @return a Pager on a list of {@link IdentifiableEntity}s initialized by + * the following strategy {@link #SIMPLE_TAXON_INIT_STRATEGY} + * @throws IOException + * @throws LuceneMultiSearchException + * @throws ParseException + */ + @RequestMapping(method = RequestMethod.GET, value={"search"}) + public Pager> doSearch( + @RequestParam(value = "query", required = true) String query, + @RequestParam(value = "tree", required = false) UUID treeUuid, + @RequestParam(value = "area", required = false) DefinedTermBaseList areaList, + @RequestParam(value = "status", required = false) Set> status, + @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, + HttpServletRequest request, + HttpServletResponse response + ) + throws IOException, ParseException, LuceneMultiSearchException { + + + logger.info("search : " + requestPathAndQuery(request) ); + + Set areaSet = null; + if(areaList != null){ + areaSet = new HashSet(areaList.size()); + areaSet.addAll(areaList); + TaxonListController.includeAllSubAreas(areaSet, termService); + } + + PagerParameters pagerParams = new PagerParameters(pageSize, pageNumber); + pagerParams.normalizeAndValidate(response); + + // TODO change type of do* parameters to TaxaAndNamesSearchMode + EnumSet searchModes = EnumSet.noneOf(TaxaAndNamesSearchMode.class); + if(BooleanUtils.toBoolean(doTaxa)) { + searchModes.add(TaxaAndNamesSearchMode.doTaxa); + } + if(BooleanUtils.toBoolean(doSynonyms)) { + searchModes.add(TaxaAndNamesSearchMode.doSynonyms); + } + if(BooleanUtils.toBoolean(doMisappliedNames)) { + searchModes.add(TaxaAndNamesSearchMode.doMisappliedNames); + } + if(BooleanUtils.toBoolean(doTaxaByCommonNames)) { + searchModes.add(TaxaAndNamesSearchMode.doTaxaByCommonNames); + } + + Classification classification = null; + if(treeUuid != null){ + classification = classificationService.find(treeUuid); + } + + return service.findTaxaAndNamesByFullText(searchModes, query, + classification, areaSet, status, null, + false, pagerParams.getPageSize(), pagerParams.getPageIndex(), + OrderHint.NOMENCLATURAL_SORT_ORDER, getSimpleTaxonInitStrategy()); + } + /** * Find Taxa, Synonyms, Common Names by name, either globally or in a specific geographic area. *

@@ -104,8 +232,8 @@ public class TaxonListController extends IdentifiableListController doFindTaxaAndNames( + @RequestMapping(method = RequestMethod.GET, value={"find"}) + public Pager doFind( @RequestParam(value = "query", required = true) String query, @RequestParam(value = "tree", required = false) UUID treeUuid, @RequestParam(value = "area", required = false) Set areas, @@ -120,17 +248,14 @@ public class TaxonListController extends IdentifiableListController) service.findTaxaAndNames(config); - + return service.findTaxaAndNames(config); + + } + + + /** + * @param clazz + * @param queryString + * @param treeUuid TODO unimplemented in TaxonServiceImpl !!!! + * @param languages + * @param pageNumber + * @param pageSize + * @param request + * @param response + * @return + * @throws IOException + * @throws ParseException + */ + @RequestMapping(method = RequestMethod.GET, value={"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 : " + requestPathAndQuery(request) ); + + 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), getSimpleTaxonInitStrategy()); + return pager; + } + + @RequestMapping(method = RequestMethod.GET, value={"findByFullText"}) + public Pager> dofindByFullText( + @RequestParam(value = "clazz", required = false) Class clazz, + @RequestParam(value = "query", required = true) String queryString, + @RequestParam(value = "tree", required = false) UUID treeUuid, + @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("findByFullText : " + requestPathAndQuery(request) ); + + PagerParameters pagerParams = new PagerParameters(pageSize, pageNumber); + pagerParams.normalizeAndValidate(response); + + if(highlighting == null){ + highlighting = false; + } + + Classification classification = null; + if(treeUuid != null){ + classification = classificationService.find(treeUuid); + } + + Pager> pager = service.findByFullText(clazz, queryString, classification, languages, + highlighting, pagerParams.getPageSize(), pagerParams.getPageIndex(), ((List) null), + initializationStrategy); + return pager; } + + @RequestMapping(method = RequestMethod.GET, value={"findByEverythingFullText"}) + public Pager> dofindByEverythingFullText( + @RequestParam(value = "clazz", required = false) Class clazz, + @RequestParam(value = "query", required = true) String queryString, + @RequestParam(value = "tree", required = false) UUID treeUuid, + @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, LuceneMultiSearchException { + + logger.info("findByEverythingFullText : " + requestPathAndQuery(request) ); + + PagerParameters pagerParams = new PagerParameters(pageSize, pageNumber); + pagerParams.normalizeAndValidate(response); + + if(highlighting == null){ + highlighting = false; + } + + Classification classification = null; + if(treeUuid != null){ + classification = classificationService.find(treeUuid); + } + + Pager> pager = service.findByEverythingFullText( + queryString, classification, languages, highlighting, + pagerParams.getPageSize(), pagerParams.getPageIndex(), + ((List)null), initializationStrategy); + return pager; + } + + /** + * @param areaSet + */ + static public void includeAllSubAreas(Set areaSet, ITermService termService) { + Collection tmpAreas = new HashSet(areaSet); + // expand all areas to include also the sub areas + Pager pager = null; + while(true){ + pager = termService.getIncludes(tmpAreas, 1000, null, null); + if(pager.getCount() == 0){ + break; + } + tmpAreas = pager.getRecords(); + tmpAreas.removeAll(areaSet); + areaSet.addAll(tmpAreas); + } + } + + @RequestMapping(value = "bestMatchingTaxon/{taxonName}", method = RequestMethod.GET) + public TaxonBase doFindBestMatchingTaxon( + @PathVariable("taxonName") String taxonName, + HttpServletRequest request, + HttpServletResponse response)throws IOException { + + Taxon bestMatchingTaxon = service.findBestMatchingTaxon(taxonName); + + return bestMatchingTaxon; + } + } \ No newline at end of file