refactoring taxon controllers, reducing code duplication
[cdmlib.git] / cdmlib-remote / src / main / java / eu / etaxonomy / cdm / remote / controller / TaxonListController.java
index cde22b1c4213b312e71a54a8946fd039c845cf98..6b8679a91d0c2eb189898bd42f66799e5aab435f 100644 (file)
@@ -12,7 +12,9 @@ package eu.etaxonomy.cdm.remote.controller;
 import java.io.IOException;\r
 import java.util.ArrayList;\r
 import java.util.Arrays;\r
+import java.util.Collection;\r
 import java.util.EnumSet;\r
+import java.util.HashSet;\r
 import java.util.List;\r
 import java.util.Set;\r
 import java.util.UUID;\r
@@ -24,6 +26,9 @@ import org.apache.commons.lang.BooleanUtils;
 import org.apache.lucene.queryParser.ParseException;\r
 import org.springframework.beans.factory.annotation.Autowired;\r
 import org.springframework.stereotype.Controller;\r
+import org.springframework.web.bind.WebDataBinder;\r
+import org.springframework.web.bind.annotation.InitBinder;\r
+import org.springframework.web.bind.annotation.PathVariable;\r
 import org.springframework.web.bind.annotation.RequestMapping;\r
 import org.springframework.web.bind.annotation.RequestMethod;\r
 import org.springframework.web.bind.annotation.RequestParam;\r
@@ -50,6 +55,8 @@ import eu.etaxonomy.cdm.model.taxon.TaxonBase;
 import eu.etaxonomy.cdm.persistence.query.MatchMode;\r
 import eu.etaxonomy.cdm.persistence.query.OrderHint;\r
 import eu.etaxonomy.cdm.remote.controller.util.PagerParameters;\r
+import eu.etaxonomy.cdm.remote.editor.DefinedTermBaseList;\r
+import eu.etaxonomy.cdm.remote.editor.TermBaseListPropertyEditor;\r
 import eu.etaxonomy.cdm.remote.editor.UuidList;\r
 \r
 /**\r
@@ -63,6 +70,11 @@ import eu.etaxonomy.cdm.remote.editor.UuidList;
 public class TaxonListController extends IdentifiableListController<TaxonBase, ITaxonService> {\r
 \r
 \r
+    private static final List<String> SIMPLE_TAXON_INIT_STRATEGY = DEFAULT_INIT_STRATEGY;\r
+    protected List<String> getSimpleTaxonInitStrategy() {\r
+        // TODO Auto-generated method stub\r
+        return SIMPLE_TAXON_INIT_STRATEGY;\r
+    }\r
 \r
     /**\r
      *\r
@@ -87,6 +99,12 @@ public class TaxonListController extends IdentifiableListController<TaxonBase, I
     @Autowired\r
     private ITermService termService;\r
 \r
+    @InitBinder\r
+    @Override\r
+    public void initBinder(WebDataBinder binder) {\r
+        super.initBinder(binder);\r
+        binder.registerCustomEditor(DefinedTermBaseList.class, new TermBaseListPropertyEditor<NamedArea>(termService));\r
+    }\r
 \r
     /**\r
      * Find Taxa, Synonyms, Common Names by name, either globally or in a specific geographic area.\r
@@ -127,7 +145,7 @@ public class TaxonListController extends IdentifiableListController<TaxonBase, I
     public Pager<SearchResult<TaxonBase>> doSearch(\r
             @RequestParam(value = "query", required = true) String query,\r
             @RequestParam(value = "tree", required = false) UUID treeUuid,\r
-            @RequestParam(value = "area", required = false) Set<NamedArea> areas,\r
+            @RequestParam(value = "area", required = false) DefinedTermBaseList<NamedArea> areaList,\r
             @RequestParam(value = "status", required = false) Set<PresenceAbsenceTermBase<?>> status,\r
             @RequestParam(value = "pageNumber", required = false) Integer pageNumber,\r
             @RequestParam(value = "pageSize", required = false) Integer pageSize,\r
@@ -143,6 +161,13 @@ public class TaxonListController extends IdentifiableListController<TaxonBase, I
 \r
         logger.info("search : " + requestPathAndQuery(request) );\r
 \r
+        Set<NamedArea> areaSet = null;\r
+        if(areaList != null){\r
+            areaSet = new HashSet<NamedArea>(areaList.size());\r
+            areaSet.addAll(areaList);\r
+            TaxonListController.includeAllSubAreas(areaSet, termService);\r
+        }\r
+\r
         PagerParameters pagerParams = new PagerParameters(pageSize, pageNumber);\r
         pagerParams.normalizeAndValidate(response);\r
 \r
@@ -167,9 +192,9 @@ public class TaxonListController extends IdentifiableListController<TaxonBase, I
         }\r
 \r
         return service.findTaxaAndNamesByFullText(searchModes, query,\r
-                classification, areas, status, null,\r
+                classification, areaSet, status, null,\r
                 false, pagerParams.getPageSize(), pagerParams.getPageIndex(),\r
-                null, initializationStrategy);\r
+                OrderHint.NOMENCLATURAL_SORT_ORDER, getSimpleTaxonInitStrategy());\r
     }\r
 \r
     /**\r
@@ -207,8 +232,8 @@ public class TaxonListController extends IdentifiableListController<TaxonBase, I
      *         the following strategy {@link #SIMPLE_TAXON_INIT_STRATEGY}\r
      * @throws IOException\r
      */\r
-    @RequestMapping(method = RequestMethod.GET, value={"findTaxaAndNames"}) // TODO should be find, see TaxonListPortaController\r
-    public Pager<IdentifiableEntity> doFindTaxaAndNames(\r
+    @RequestMapping(method = RequestMethod.GET, value={"find"})\r
+    public Pager<IdentifiableEntity> doFind(\r
             @RequestParam(value = "query", required = true) String query,\r
             @RequestParam(value = "tree", required = false) UUID treeUuid,\r
             @RequestParam(value = "area", required = false) Set<NamedArea> areas,\r
@@ -225,15 +250,12 @@ public class TaxonListController extends IdentifiableListController<TaxonBase, I
              throws IOException {\r
 \r
 \r
-        logger.info("findTaxaAndNames : " + request.getRequestURI() + "?" + request.getQueryString() );\r
+        logger.info("find : " + request.getRequestURI() + "?" + request.getQueryString() );\r
 \r
         PagerParameters pagerParams = new PagerParameters(pageSize, pageNumber);\r
         pagerParams.normalizeAndValidate(response);\r
 \r
         IFindTaxaAndNamesConfigurator config = new FindTaxaAndNamesConfiguratorImpl();\r
-\r
-        config.setTaxonPropertyPath(initializationStrategy);\r
-\r
         config.setPageNumber(pagerParams.getPageIndex());\r
         config.setPageSize(pagerParams.getPageSize());\r
         config.setTitleSearchString(query);\r
@@ -242,7 +264,7 @@ public class TaxonListController extends IdentifiableListController<TaxonBase, I
         config.setDoMisappliedNames(doMisappliedNames != null ? doMisappliedNames : Boolean.FALSE);\r
         config.setDoTaxaByCommonNames(doTaxaByCommonNames != null ? doTaxaByCommonNames : Boolean.FALSE );\r
         config.setMatchMode(matchMode != null ? matchMode : MatchMode.BEGINNING);\r
-//        config.setTaxonPropertyPath(SIMPLE_TAXON_INIT_STRATEGY);\r
+        config.setTaxonPropertyPath(getSimpleTaxonInitStrategy());\r
         config.setNamedAreas(areas);\r
         if(treeUuid != null){\r
             Classification classification = classificationService.find(treeUuid);\r
@@ -306,8 +328,8 @@ public class TaxonListController extends IdentifiableListController<TaxonBase, I
 \r
         Pager<SearchResult<TaxonBase>> pager = service.findByDescriptionElementFullText(\r
                 clazz, queryString, classification, features, languages, highlighting,\r
-                pagerParams.getPageSize(), pagerParams.getPageIndex(), ((List<OrderHint>)null),\r
-                initializationStrategy);\r
+                pagerParams.getPageSize(), pagerParams.getPageIndex(),\r
+                ((List<OrderHint>)null), getSimpleTaxonInitStrategy());\r
         return pager;\r
     }\r
 \r
@@ -340,7 +362,7 @@ public class TaxonListController extends IdentifiableListController<TaxonBase, I
         }\r
 \r
         Pager<SearchResult<TaxonBase>> pager = service.findByFullText(clazz, queryString, classification, languages,\r
-                highlighting, pagerParams.getPageSize(), pagerParams.getPageIndex(), ((List<OrderHint>)null),\r
+                highlighting, pagerParams.getPageSize(), pagerParams.getPageIndex(), ((List<OrderHint>)  null),\r
                 initializationStrategy);\r
         return pager;\r
     }\r
@@ -379,4 +401,34 @@ public class TaxonListController extends IdentifiableListController<TaxonBase, I
                 ((List<OrderHint>)null), initializationStrategy);\r
         return pager;\r
     }\r
+\r
+    /**\r
+     * @param areaSet\r
+     */\r
+    static public void includeAllSubAreas(Set<NamedArea> areaSet, ITermService termService) {\r
+        Collection<NamedArea> tmpAreas = new HashSet<NamedArea>(areaSet);\r
+        // expand all areas to include also the sub areas\r
+        Pager<NamedArea> pager = null;\r
+        while(true){\r
+            pager = termService.getIncludes(tmpAreas, 1000, null, null);\r
+            if(pager.getCount() == 0){\r
+                break;\r
+            }\r
+            tmpAreas = pager.getRecords();\r
+            tmpAreas.removeAll(areaSet);\r
+            areaSet.addAll(tmpAreas);\r
+        }\r
+    }\r
+\r
+    @RequestMapping(value = "bestMatchingTaxon/{taxonName}", method = RequestMethod.GET)\r
+    public TaxonBase doFindBestMatchingTaxon(\r
+             @PathVariable("taxonName") String taxonName,\r
+            HttpServletRequest request,\r
+            HttpServletResponse response)throws IOException {\r
+\r
+        Taxon bestMatchingTaxon =  service.findBestMatchingTaxon(taxonName);\r
+\r
+        return bestMatchingTaxon;\r
+    }\r
+\r
 }
\ No newline at end of file