cdmlib related part for fixing #2931 (Free text search - hit highlighting implemented...
[cdmlib.git] / cdmlib-remote / src / main / java / eu / etaxonomy / cdm / remote / controller / TaxonPortalController.java
index 368f1f7f93896548881e0887e6c1fce1dc71ffc2..550164881e2067cbcb89ad8917f6f0f623ead88a 100644 (file)
@@ -29,6 +29,7 @@ import javax.servlet.http.HttpServletResponse;
 import org.apache.commons.lang.ObjectUtils;\r
 import org.apache.http.HttpRequest;\r
 import org.apache.log4j.Logger;\r
+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
@@ -52,9 +53,12 @@ import eu.etaxonomy.cdm.api.service.ITermService;
 import eu.etaxonomy.cdm.api.service.config.ITaxonServiceConfigurator;\r
 import eu.etaxonomy.cdm.api.service.config.TaxonServiceConfiguratorImpl;\r
 import eu.etaxonomy.cdm.api.service.pager.Pager;\r
+import eu.etaxonomy.cdm.api.service.search.SearchResult;\r
 import eu.etaxonomy.cdm.database.UpdatableRoutingDataSource;\r
 import eu.etaxonomy.cdm.model.common.DefinedTermBase;\r
 import eu.etaxonomy.cdm.model.common.IdentifiableEntity;\r
+import eu.etaxonomy.cdm.model.common.IdentifiableSource;\r
+import eu.etaxonomy.cdm.model.common.Language;\r
 import eu.etaxonomy.cdm.model.common.Marker;\r
 import eu.etaxonomy.cdm.model.common.MarkerType;\r
 import eu.etaxonomy.cdm.model.common.RelationshipBase.Direction;\r
@@ -75,6 +79,7 @@ import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
 import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;\r
 import eu.etaxonomy.cdm.model.taxon.Classification;\r
 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.CdmTypePropertyEditor;\r
 import eu.etaxonomy.cdm.remote.editor.MatchModePropertyEditor;\r
@@ -253,14 +258,17 @@ public class TaxonPortalController extends BaseController<TaxonBase, ITaxonServi
 \r
     protected static final List<String> TAXONUSEDESCRIPTION_INIT_STRATEGY = Arrays.asList(new String []{\r
             "$",\r
+            "name.$",\r
+            "name.rank.representations",\r
+            "name.status.type.representations",\r
             "sources.$",\r
             "elements.$",\r
             "elements.states.$",\r
             "elements.sources.citation.authorTeam",\r
             "elements.sources.nameUsedInSource.originalNameString",\r
             /*//"elements.multilanguageText",\r
-            "elements.media.representations.parts",\r
-            "elements.media.title",*/\r
+            "elements.media.representations.parts",*/\r
+            "elements.media.title",\r
     });\r
 \r
     protected static final List<String> DESCRIPTION_ELEMENT_INIT_STRATEGY = Arrays.asList(new String []{\r
@@ -421,6 +429,50 @@ public class TaxonPortalController extends BaseController<TaxonBase, ITaxonServi
         return (Pager<IdentifiableEntity>) service.findTaxaAndNames(config);\r
     }\r
 \r
+    /**\r
+     * @param clazz\r
+     * @param queryString\r
+     * @param treeUuid TODO unimplemented in TaxonServiceImpl !!!!\r
+     * @param languages\r
+     * @param pageNumber\r
+     * @param pageSize\r
+     * @param request\r
+     * @param response\r
+     * @return\r
+     * @throws IOException\r
+     * @throws ParseException\r
+     */\r
+    @RequestMapping(method = RequestMethod.GET, value={"/portal/taxon/findByDescriptionElementFullText"})\r
+    public Pager<SearchResult<TaxonBase>> dofindByDescriptionElementFullText(\r
+            @RequestParam(value = "clazz", required = false) Class clazz,\r
+            @RequestParam(value = "query", required = true) String queryString,\r
+            @RequestParam(value = "tree", required = false) UUID treeUuid,\r
+            @RequestParam(value = "languages", required = false) List<Language> languages,\r
+            @RequestParam(value = "hl", required = false) Boolean highlighting,\r
+            @RequestParam(value = "pageNumber", required = false) Integer pageNumber,\r
+            @RequestParam(value = "pageSize", required = false) Integer pageSize,\r
+            HttpServletRequest request,\r
+            HttpServletResponse response\r
+            )\r
+             throws IOException, ParseException {\r
+\r
+         logger.info("findByDescriptionElementFullText : " + request.getRequestURI() + "?" + request.getQueryString() );\r
+\r
+         PagerParameters pagerParams = new PagerParameters(pageSize, pageNumber);\r
+         pagerParams.normalizeAndValidate(response);\r
+\r
+         if(highlighting == null){\r
+             highlighting = false;\r
+         }\r
+\r
+         Classification classification = null;\r
+        if(treeUuid != null){\r
+            classification = classificationService.find(treeUuid);\r
+        }\r
+        Pager<SearchResult<TaxonBase>> pager = service.findByDescriptionElementFullText(clazz, queryString, classification, languages, highlighting, pagerParams.getPageSize(), pagerParams.getPageIndex(), ((List<OrderHint>)null), SIMPLE_TAXON_INIT_STRATEGY);\r
+        return pager;\r
+    }\r
+\r
     /**\r
      * Get the synonymy for a taxon identified by the <code>{taxon-uuid}</code>.\r
      * The synonymy consists\r
@@ -688,75 +740,51 @@ public class TaxonPortalController extends BaseController<TaxonBase, ITaxonServi
     public List<TaxonDescription> doGetDescriptions(\r
             @PathVariable("uuid") UUID uuid,\r
             @RequestParam(value = "markerTypes", required = false) UuidList markerTypeUUIDs,\r
-//            @PathVariable("markerTypes") String markerTypeUUIDs,\r
             HttpServletRequest request,\r
             HttpServletResponse response)throws IOException {\r
         if(request != null){\r
             logger.info("doGetDescriptions()" + request.getServletPath());\r
         }\r
-        List<DefinedTermBase> markerTypeTerms = new ArrayList<DefinedTermBase>();\r
-        Set<UUID> sMarkerTypeUUIDs = new HashSet<UUID>();\r
-        //if(markerTypeUUIDs !=null) {\r
-//        if(!markerTypeUUIDs.equals("test") && !markerTypeUUIDs.equals("$1")  ) {\r
-//            String[] strMarkerTypeUUIDs = markerTypeUUIDs.split(",");\r
-//            for (String uuidMarkerType : strMarkerTypeUUIDs) {\r
-//                sMarkerTypeUUIDs.add(UUID.fromString(uuidMarkerType));\r
-//            }\r
-//\r
-//            markerTypeTerms = markerTypeService.find(sMarkerTypeUUIDs);\r
-//        }\r
-        if(markerTypeUUIDs != null){\r
+        List<DefinedTermBase> markerTypeTerms = null;\r
+        Set<UUID> sMarkerTypeUUIDs = null;\r
+\r
+        if(markerTypeUUIDs != null && !markerTypeUUIDs.isEmpty()){\r
+            sMarkerTypeUUIDs = new HashSet<UUID>(markerTypeUUIDs);\r
             markerTypeTerms = markerTypeService.find(sMarkerTypeUUIDs);\r
+        } else if(markerTypeUUIDs != null && markerTypeUUIDs.isEmpty()){\r
+            markerTypeTerms = new ArrayList<DefinedTermBase>();\r
         }\r
         Set<MarkerType> markerTypes = new HashSet<MarkerType>();\r
         List<TaxonDescription> descriptions = new ArrayList<TaxonDescription>();\r
-        for (DefinedTermBase markerTypeTerm : markerTypeTerms) {\r
-            markerTypes.add((MarkerType)markerTypeTerm);\r
+        if (markerTypeTerms != null) {\r
+            for (DefinedTermBase markerTypeTerm : markerTypeTerms) {\r
+                markerTypes.add((MarkerType)markerTypeTerm);\r
+            }\r
         }\r
         Taxon t = getCdmBaseInstance(Taxon.class, uuid, response, (List<String>)null);\r
-        if (markerTypes == null) {\r
+        if (markerTypeTerms == null) {\r
 \r
             Pager<TaxonDescription> p = descriptionService.getTaxonDescriptions(t, null, null, null, null, TAXONDESCRIPTION_INIT_STRATEGY);\r
             descriptions = p.getRecords();\r
         }\r
 \r
-        else if (sMarkerTypeUUIDs.isEmpty()) {\r
+        else if (markerTypeTerms != null && markerTypeTerms.isEmpty()) {\r
             descriptions = descriptionService.listTaxonDescriptions(t, null, null, markerTypes, null, null, TAXONUSEDESCRIPTION_INIT_STRATEGY);\r
 \r
         }\r
         else {\r
             descriptions = descriptionService.listTaxonDescriptions(t, null, null, markerTypes, null, null, TAXONUSEDESCRIPTION_INIT_STRATEGY);\r
-        }\r
-        /*Set<MarkerType> sMarkerTypes = new HashSet<MarkerType>(markerTypeService.listByTermClass(MarkerType.class, null, null, null, null));\r
-        //Set<MarkerType> markerTypesToUse = new HashSet<MarkerType>(markerTypeService.);\r
-        //MarkerType useMarkerType = (MarkerType) markerTypeService.find(UUID.fromString("2e6e42d9-e92a-41f4-899b-03c0ac64f059"));\r
-        MarkerType useMarkerType = (MarkerType) markerTypeService.find(UUID.fromString("2e6e42d9-e92a-41f4-899b-03c0ac64f039"));\r
-\r
-        markerTypes.remove(useMarkerType);\r
-\r
-        //List<TaxonDescription> descriptions = descriptionService.listTaxonDescriptions(t, null, null, markerTypes, null, null, TAXONUSEDESCRIPTION_INIT_STRATEGY);\r
-        Pager<TaxonDescription> p = descriptionService.getTaxonDescriptions(t, null, null, null, null, TAXONDESCRIPTION_INIT_STRATEGY);\r
-        //List<TaxonDescription> descriptions = p.getRecords();\r
-        Iterator<TaxonDescription> itr = descriptions.iterator();\r
-        //for(TaxonDescription taxonDesc: descriptions) {\r
-        while (itr.hasNext()) {\r
-            TaxonDescription taxonDesc = (TaxonDescription) itr.next();\r
-            Pager<Marker> pMarkers = descriptionService.getMarkers(taxonDesc, null, null, null, null, null);\r
-            Pager<Marker> useMarkers = markerService.page(useMarkerType, null, null, null, null);\r
-            List<DefinedTermBase> useMarkers2 = markerTypeService.list(MarkerType.class, null, null, null, null);\r
-            if(pMarkers.getCount() != 0) {\r
-                List<Marker> testMarkers = pMarkers.getRecords();\r
-                List<Marker> testUseMarkers = useMarkers.getRecords();\r
-                for (Marker marker: testMarkers) {\r
-                    for (Marker useMaker: testUseMarkers){\r
-                        if(marker.equals(useMaker)) {\r
-                            itr.remove();\r
-                        }\r
+            /*for (TaxonDescription description: descriptions) {\r
+                for (IdentifiableSource source :description.getSources()) {\r
+                    if (source.getOriginalNameString() != null) {\r
+                        description.\r
                     }\r
+\r
                 }\r
-            }\r
-        }*/\r
-      //return p.getRecords();\r
+\r
+\r
+            }*/\r
+        }\r
         return descriptions;\r
     }\r
 \r
@@ -795,8 +823,6 @@ public class TaxonPortalController extends BaseController<TaxonBase, ITaxonServi
     public ModelAndView doGetDescriptionElementsByType(\r
             @PathVariable("uuid") UUID uuid,\r
             @PathVariable("classSimpleName") String classSimpleName,\r
-            //@RequestParam(value = "markerTypes", required = false, defaultValue = "false") Set<UUID> markerTypes,\r
-//            @PathVariable(value = "markerTypes") String markerTypes,\r
             @RequestParam(value = "markerTypes", required = false) UuidList markerTypeUUIDs,\r
             @RequestParam(value = "count", required = false, defaultValue = "false") Boolean doCount,\r
             HttpServletRequest request,\r