fix #6296: fix the parameter settings in search queries
authorKatja Luther <k.luther@bgbm.org>
Mon, 23 Jan 2017 11:06:32 +0000 (12:06 +0100)
committerKatja Luther <k.luther@bgbm.org>
Mon, 23 Jan 2017 11:06:32 +0000 (12:06 +0100)
cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/persistence/dao/hibernate/taxon/TaxonDaoHibernateImpl.java
cdmlib-services/src/test/java/eu/etaxonomy/cdm/api/service/TaxonServiceSearchTaxaAndNamesTest.java

index 3e14fd49dc8ba749401bcc6ba402b1a6a32dd7d5..6c3e5e6dda403b7964612516ba7247a44832ffdc 100644 (file)
@@ -404,13 +404,8 @@ public class TaxonDaoHibernateImpl extends IdentifiableDaoBase<TaxonBase> implem
                 taxa.addAll(subCommonNames.list());
             }
 
-            //FIXME : the fourth element of the result should be a boolean, but in the case of a synonym
-            // (which does require a check) a constant boolean (false) value needs to set. It seems that
-            // hql cannot parse a constant boolean value in the select list clause. This implies that the
-            // resulting object could be a Boolean or a String. The workaround for this is to convert the
-            // resutling object into a String (using toString) and then create a new Boolean object from
-            // String.
-            if (doTaxa && doSynonyms){
+
+           // if (doTaxa && doSynonyms){
                 if(synonyms.size()>0 && taxa.size()>0){
                     hql = "select " + selectWhat;
                     // in doNotReturnFullEntities mode it is nesscary to also return the type of the matching entities:
@@ -446,54 +441,13 @@ public class TaxonDaoHibernateImpl extends IdentifiableDaoBase<TaxonBase> implem
                 } else{
                     hql = "select " + selectWhat + " from %s t";
                 }
-            } else if(doTaxa || doCommonNames){
-                if  (taxa.size()>0){
-                    hql = "select " + selectWhat;
-                    // in doNotReturnFullEntities mode it is nesscary to also return the type of the matching entities:
-                    // also return the computed isOrphaned flag
-                    if (doNotReturnFullEntities){
-                        hql += ", 'taxon', " +
-                                " case when t.taxonNodes is empty and t.relationsFromThisTaxon is empty and t.relationsToThisTaxon is empty then true else false end ";
-                    }
-                    hql +=  " from %s t " +
-                            " where t.id in (:taxa) ";
-
-                }else{
-                    hql = "select " + selectWhat + " from %s t";
-                }
-            } else if(doSynonyms){
-                if (synonyms.size()>0){
-
-                    hql = "select " + selectWhat;
-                    // in doNotReturnFullEntities mode it is nesscary to also return the type of the matching entities:
-                    // also return the computed isOrphaned flag
-                    if (doNotReturnFullEntities){
-                        hql += ", 'synonym', 'false' ";
-                    }
-                    hql +=  " from %s t " +
-                            " where t.id in (:synonyms) ";
-                }else{
-                    hql = "select " + selectWhat + " from %s t";
-                }
-            } else if (doIncludeMisappliedNames){
-                hql = "select " + selectWhat;
-                // in doNotReturnFullEntities mode it is nesscary to also return the type of the matching entities:
-                // also return the computed isOrphaned flag
-                if (doNotReturnFullEntities){
-                    hql += ", 'taxon', " +
-                            " case when t.taxonNodes is empty and t.relationsFromThisTaxon is empty and t.relationsToThisTaxon is empty then true else false end ";
-                }
-                hql +=  " from %s t " +
-                        " where t.id in (:taxa) ";
-
-            }
 
             String classString;
-            if (doTaxa && doSynonyms){
+            if ((doTaxa || doCommonNames || doIncludeMisappliedNames) && doSynonyms){
                 classString = "TaxonBase";
             } else if (doTaxa || doCommonNames){
                 classString = "Taxon";
-            } else if (doSynonyms){
+            } else if (doSynonyms && !(doCommonNames|| doTaxa || doIncludeMisappliedNames)){
                 classString = "Synonym";
             } else{//only misappliedNames
                 classString = "Taxon";
@@ -524,7 +478,7 @@ public class TaxonDaoHibernateImpl extends IdentifiableDaoBase<TaxonBase> implem
             Query query = getSession().createQuery(hql);
 
 
-            if (doTaxa && doSynonyms){
+            if ((doTaxa || doCommonNames || doIncludeMisappliedNames) ){
                 // find taxa and synonyms
                 if (taxa.size()>0){
                     query.setParameterList("taxa", taxa);
@@ -535,28 +489,14 @@ public class TaxonDaoHibernateImpl extends IdentifiableDaoBase<TaxonBase> implem
                 if (taxa.size()== 0 && synonyms.size() == 0){
                     return null;
                 }
-            }else if(doTaxa || doCommonNames || doIncludeMisappliedNames){
-                //find taxa
-                if (taxa.size()>0){
-                    query.setParameterList("taxa", taxa );
-                }else{
-                    logger.warn("there are no taxa for the query: " + queryString);
-                    return null;
-                }
-            } else if(doSynonyms){
+            }
+            if(doSynonyms){
                 // find synonyms
                 if (synonyms.size()>0){
                     query.setParameterList("synonyms", synonyms);
                 }else{
                     return null;
                 }
-            }  else{
-                //only misappliedNames
-                if (taxa.size()>0){
-                    query.setParameterList("taxa", taxa );
-                }else{
-                    return null;
-                }
             }
 
             return query;
index 34a24267490e4fb132fe425424615feb2741bcc8..7153bb1aa635a83df5cf3642913fb0f134593206 100644 (file)
@@ -152,7 +152,7 @@ public class TaxonServiceSearchTaxaAndNamesTest extends CdmTransactionalIntegrat
 
         setTaxaAndNamesModes(conf, false, true, true, true, true);
         pager = taxonService.findTaxaAndNames(conf);
-        assertEquals(3, pager.getRecords().size());
+        assertEquals(4, pager.getRecords().size());
 
         setTaxaAndNamesModes(conf, true, false, true, true, true);
         pager = taxonService.findTaxaAndNames(conf);
@@ -171,9 +171,10 @@ public class TaxonServiceSearchTaxaAndNamesTest extends CdmTransactionalIntegrat
 
 
      // FIXME org.hibernate.QueryParameterException: could not locate named parameter [taxa]
-     //   setTaxaAndNamesModes(conf, false, true, false, true, true);
-     //   pager = taxonService.findTaxaAndNames(conf);
-     //   assertEquals(3, pager.getRecords().size());
+        setTaxaAndNamesModes(conf, false, true, false, true, true);
+        pager = taxonService.findTaxaAndNames(conf);
+        // there are one synonym, two names without taxa and a misapplied name
+        assertEquals(4, pager.getRecords().size());
 
         setTaxaAndNamesModes(conf, true, false, false, true, true);
         pager = taxonService.findTaxaAndNames(conf);
@@ -191,10 +192,10 @@ public class TaxonServiceSearchTaxaAndNamesTest extends CdmTransactionalIntegrat
         assertEquals(10, pager.getRecords().size());
 
         // FIXME the Synonym Abies subalpina missing in result set
-//        setTaxaAndNamesModes(conf, false, true, true, false, true);
-//        pager = taxonService.findTaxaAndNames(conf);
-//        logSearchResults(pager, Level.DEBUG);
-//        assertEquals(3, pager.getRecords().size());
+        setTaxaAndNamesModes(conf, false, true, true, false, true);
+        pager = taxonService.findTaxaAndNames(conf);
+        logSearchResults(pager, Level.DEBUG);
+        assertEquals(3, pager.getRecords().size());
 
         setTaxaAndNamesModes(conf, true, false, true, false, true);
         pager = taxonService.findTaxaAndNames(conf);
@@ -241,7 +242,8 @@ public class TaxonServiceSearchTaxaAndNamesTest extends CdmTransactionalIntegrat
         setTaxaAndNamesModes(conf, false, true, true, true, false);
         pager = taxonService.findTaxaAndNames(conf);
         logSearchResults(pager, Level.DEBUG);
-        assertEquals(1, pager.getRecords().size());
+        //There is one synonym and a misapplied name
+        assertEquals(2, pager.getRecords().size());
 
         setTaxaAndNamesModes(conf, true, false, true, true, false);
         pager = taxonService.findTaxaAndNames(conf);
@@ -261,10 +263,10 @@ public class TaxonServiceSearchTaxaAndNamesTest extends CdmTransactionalIntegrat
         assertEquals(8, pager.getRecords().size());
 
         // FIXME org.hibernate.QueryParameterException: could not locate named parameter [taxa]
-//        setTaxaAndNamesModes(conf, false, true, false, true, false);
-//        pager = taxonService.findTaxaAndNames(conf);
-//        logSearchResults(pager, Level.DEBUG);
-//        assertEquals(2, pager.getRecords().size());
+        setTaxaAndNamesModes(conf, false, true, false, true, false);
+        pager = taxonService.findTaxaAndNames(conf);
+        logSearchResults(pager, Level.DEBUG);
+        assertEquals(2, pager.getRecords().size());
 
         setTaxaAndNamesModes(conf, true, false, false, true, false);
         pager = taxonService.findTaxaAndNames(conf);