fixing #4179 (improve performance of portal/taxon/{uuid}/media)
[cdmlib.git] / cdmlib-services / src / main / java / eu / etaxonomy / cdm / api / service / TaxonServiceImpl.java
index 1046f8d963f00c92608ccdab90c915b0337ff542..75566cc5a3be1cc00daeb00d0e0fdaae2d2ef646 100644 (file)
@@ -93,7 +93,6 @@ import eu.etaxonomy.cdm.model.location.NamedArea;
 import eu.etaxonomy.cdm.model.media.Media;\r
 import eu.etaxonomy.cdm.model.media.MediaRepresentation;\r
 import eu.etaxonomy.cdm.model.media.MediaUtils;\r
-import eu.etaxonomy.cdm.model.molecular.Amplification;\r
 import eu.etaxonomy.cdm.model.molecular.AmplificationResult;\r
 import eu.etaxonomy.cdm.model.molecular.DnaSample;\r
 import eu.etaxonomy.cdm.model.molecular.Sequence;\r
@@ -347,7 +346,7 @@ public class TaxonServiceImpl extends IdentifiableServiceBase<TaxonBase,ITaxonDa
 \r
 \r
         // Switch groups\r
-        oldHomotypicalGroup.removeTypifiedName(synonymName);\r
+        oldHomotypicalGroup.removeTypifiedName(synonymName, false);\r
         newHomotypicalGroup.addTypifiedName(synonymName);\r
 \r
         //remove existing basionym relationships\r
@@ -683,13 +682,12 @@ public class TaxonServiceImpl extends IdentifiableServiceBase<TaxonBase,ITaxonDa
                results = dao.getTaxaByNameForEditor(configurator.isDoTaxa(), configurator.isDoSynonyms(), configurator.isDoNamesWithoutTaxa(), configurator.isDoMisappliedNames(),configurator.getTitleSearchStringSqlized(), configurator.getClassification(), configurator.getMatchMode(), configurator.getNamedAreas());\r
         }\r
         if (configurator.isDoTaxaByCommonNames()) {\r
-            \r
-            if(configurator.getPageSize() == null ){\r
+            //if(configurator.getPageSize() == null ){\r
                 List<UuidAndTitleCache<IdentifiableEntity>> commonNameResults = dao.getTaxaByCommonNameForEditor(configurator.getTitleSearchStringSqlized(), configurator.getClassification(), configurator.getMatchMode(), configurator.getNamedAreas());\r
                 if(commonNameResults != null){\r
                     results.addAll(commonNameResults);\r
                 }\r
-            }\r
+           // }\r
         }\r
         return results;\r
     }\r
@@ -824,6 +822,9 @@ public class TaxonServiceImpl extends IdentifiableServiceBase<TaxonBase,ITaxonDa
             Boolean limitToGalleries, Boolean includeTaxonDescriptions, Boolean includeOccurrences,\r
             Boolean includeTaxonNameDescriptions, List<String> propertyPath) {\r
 \r
+    //    logger.setLevel(Level.TRACE);\r
+//        Logger.getLogger("org.hibernate.SQL").setLevel(Level.TRACE);\r
+\r
         logger.trace("listMedia() - START");\r
 \r
         Set<Taxon> taxa = new HashSet<Taxon>();\r
@@ -991,7 +992,7 @@ public class TaxonServiceImpl extends IdentifiableServiceBase<TaxonBase,ITaxonDa
      */\r
     @Override\r
     public DeleteResult deleteTaxon(Taxon taxon, TaxonDeletionConfigurator config, Classification classification)  {\r
-       \r
+\r
        if (config == null){\r
             config = new TaxonDeletionConfigurator();\r
         }\r
@@ -1181,11 +1182,16 @@ public class TaxonServiceImpl extends IdentifiableServiceBase<TaxonBase,ITaxonDa
                     //check whether taxon will be deleted or not\r
                     if ((taxon.getTaxonNodes() == null || taxon.getTaxonNodes().size()== 0) && name != null ){\r
                         taxon = (Taxon) HibernateProxyHelper.deproxy(taxon);\r
-                        name.removeTaxonBase(taxon);\r
-                        nameService.saveOrUpdate(name);\r
+                        //name.removeTaxonBase(taxon);\r
+                        //nameService.saveOrUpdate(name);\r
+                        taxon.setName(null);\r
+                        //dao.delete(taxon);\r
                         DeleteResult nameResult = new DeleteResult();\r
 \r
-                        nameResult = nameService.delete(name, config.getNameDeletionConfig());\r
+                        //remove name if possible (and required)\r
+                        if (name != null && config.isDeleteNameIfPossible()){\r
+                               nameResult = nameService.delete(name, config.getNameDeletionConfig());\r
+                        }\r
 \r
                         if (nameResult.isError()){\r
                                //result.setError();\r
@@ -1337,8 +1343,9 @@ public class TaxonServiceImpl extends IdentifiableServiceBase<TaxonBase,ITaxonDa
                 taxonSet.addAll(synonym.getAcceptedTaxa());\r
             }\r
             for (Taxon relatedTaxon : taxonSet){\r
-    //                 dao.deleteSynonymRelationships(synonym, relatedTaxon);\r
-                relatedTaxon.removeSynonym(synonym, config.isNewHomotypicGroupIfNeeded());\r
+               relatedTaxon = HibernateProxyHelper.deproxy(relatedTaxon, Taxon.class);\r
+                relatedTaxon.removeSynonym(synonym, false);\r
+                this.saveOrUpdate(relatedTaxon);\r
             }\r
             this.saveOrUpdate(synonym);\r
 \r
@@ -1346,16 +1353,19 @@ public class TaxonServiceImpl extends IdentifiableServiceBase<TaxonBase,ITaxonDa
 \r
             //remove synonym (if necessary)\r
 \r
-            UUID uuid = null;\r
             if (synonym.getSynonymRelations().isEmpty()){\r
                 TaxonNameBase<?,?> name = synonym.getName();\r
                 synonym.setName(null);\r
-                uuid = dao.delete(synonym);\r
+                dao.delete(synonym);\r
 \r
                 //remove name if possible (and required)\r
                 if (name != null && config.isDeleteNameIfPossible()){\r
 \r
-                        nameService.delete(name, config.getNameDeletionConfig());\r
+                        DeleteResult nameDeleteresult = nameService.delete(name, config.getNameDeletionConfig());\r
+                        if (nameDeleteresult.isAbort()){\r
+                               result.addExceptions(nameDeleteresult.getExceptions());\r
+                               result.addUpdatedObject(name);\r
+                        }\r
 \r
                 }\r
 \r
@@ -1365,7 +1375,7 @@ public class TaxonServiceImpl extends IdentifiableServiceBase<TaxonBase,ITaxonDa
                 return result;\r
             }\r
 \r
-        \r
+\r
         }\r
         return result;\r
 //        else{\r
@@ -1746,10 +1756,15 @@ public class TaxonServiceImpl extends IdentifiableServiceBase<TaxonBase,ITaxonDa
         // ---- search criteria\r
         luceneSearch.setCdmTypRestriction(clazz);\r
 \r
-        textQuery.add(taxonBaseQueryFactory.newTermQuery("titleCache", queryString), Occur.SHOULD);\r
-        textQuery.add(taxonBaseQueryFactory.newDefinedTermQuery("name.rank", queryString, languages), Occur.SHOULD);\r
+        if(!queryString.isEmpty() && !queryString.equals("*") && !queryString.equals("?") ) {\r
+            textQuery.add(taxonBaseQueryFactory.newTermQuery("titleCache", queryString), Occur.SHOULD);\r
+            textQuery.add(taxonBaseQueryFactory.newDefinedTermQuery("name.rank", queryString, languages), Occur.SHOULD);\r
+        }\r
+\r
+        if(textQuery.getClauses().length > 0) {\r
+            finalQuery.add(textQuery, Occur.MUST);\r
+        }\r
 \r
-        finalQuery.add(textQuery, Occur.MUST);\r
 \r
         if(classification != null){\r
             finalQuery.add(taxonBaseQueryFactory.newEntityIdQuery("taxonNodes.classification.id", classification), Occur.MUST);\r
@@ -3090,34 +3105,34 @@ public class TaxonServiceImpl extends IdentifiableServiceBase<TaxonBase,ITaxonDa
             if (!(ref instanceof TaxonNameBase)){\r
                 if (!config.isDeleteSynonymRelations() && (ref instanceof SynonymRelationship)){\r
                     message = "The Taxon can't be deleted as long as it has synonyms.";\r
-                    \r
+\r
                 }\r
                 if (!config.isDeleteDescriptions() && (ref instanceof DescriptionBase)){\r
                     message = "The Taxon can't be deleted as long as it has factual data.";\r
-                   \r
+\r
                 }\r
 \r
                 if (!config.isDeleteTaxonNodes() && (ref instanceof TaxonNode)){\r
                     message = "The Taxon can't be deleted as long as it belongs to a taxon node.";\r
-                    \r
+\r
                 }\r
                 if (!config.isDeleteTaxonRelationships() && (ref instanceof TaxonNode)){\r
                     if (!config.isDeleteMisappliedNamesAndInvalidDesignations() && (((TaxonRelationship)ref).getType().equals(TaxonRelationshipType.MISAPPLIED_NAME_FOR())|| ((TaxonRelationship)ref).getType().equals(TaxonRelationshipType.INVALID_DESIGNATION_FOR()))){\r
                         message = "The Taxon can't be deleted as long as it has misapplied names or invalid designations.";\r
-                        \r
+\r
                     } else{\r
                         message = "The Taxon can't be deleted as long as it belongs to a taxon node.";\r
-                        \r
+\r
                     }\r
                 }\r
                 if (ref instanceof PolytomousKeyNode){\r
                     message = "The Taxon can't be deleted as long as it is referenced by a polytomous key node.";\r
-                    \r
+\r
                 }\r
 \r
                 if (HibernateProxyHelper.isInstanceOf(ref, IIdentificationKey.class)){\r
                    message = "Taxon can't be deleted as it is used in an identification key. Remove from identification key prior to deleting this name";\r
-                   \r
+\r
 \r
                 }\r
 \r
@@ -3131,13 +3146,13 @@ public class TaxonServiceImpl extends IdentifiableServiceBase<TaxonBase,ITaxonDa
                 //TaxonInteraction\r
                 if (ref.isInstanceOf(TaxonInteraction.class)){\r
                     message = "Taxon can't be deleted as it is used in taxonInteraction#taxon2";\r
-                    \r
+\r
                 }\r
 \r
               //TaxonInteraction\r
                 if (ref.isInstanceOf(DeterminationEvent.class)){\r
                     message = "Taxon can't be deleted as it is used in a determination event";\r
-                    \r
+\r
                 }\r
 \r
             }\r
@@ -3277,13 +3292,13 @@ public class TaxonServiceImpl extends IdentifiableServiceBase<TaxonBase,ITaxonDa
         }\r
         return result;\r
     }\r
-    \r
+\r
     @Override\r
     public List<TaxonBase> findTaxaByName(MatchingTaxonConfigurator config){\r
         List<TaxonBase> taxonList = dao.getTaxaByName(true, false, false, config.getTaxonNameTitle(), null, MatchMode.EXACT, null, 0, 0, config.getPropertyPath());\r
         return taxonList;\r
     }\r
-    \r
+\r
        @Override\r
        @Transactional(readOnly = true)\r
        public <S extends TaxonBase> Pager<FindByIdentifierDTO<S>> findByIdentifier(\r
@@ -3293,24 +3308,30 @@ public class TaxonServiceImpl extends IdentifiableServiceBase<TaxonBase,ITaxonDa
                if (subtreeFilter == null){\r
                        return findByIdentifier(clazz, identifier, identifierType, matchmode, includeEntity, pageSize, pageNumber, propertyPaths);\r
                }\r
-               \r
+\r
                Integer numberOfResults = dao.countByIdentifier(clazz, identifier, identifierType, subtreeFilter, matchmode);\r
         List<Object[]> daoResults = new ArrayList<Object[]>();\r
         if(numberOfResults > 0) { // no point checking again\r
                daoResults = dao.findByIdentifier(clazz, identifier, identifierType, subtreeFilter,\r
                                matchmode, includeEntity, pageSize, pageNumber, propertyPaths);\r
         }\r
-        \r
+\r
         List<FindByIdentifierDTO<S>> result = new ArrayList<FindByIdentifierDTO<S>>();\r
         for (Object[] daoObj : daoResults){\r
                if (includeEntity){\r
                        result.add(new FindByIdentifierDTO<S>((DefinedTerm)daoObj[0], (String)daoObj[1], (S)daoObj[2]));\r
                }else{\r
-                       result.add(new FindByIdentifierDTO<S>((DefinedTerm)daoObj[0], (String)daoObj[1], (UUID)daoObj[2], (String)daoObj[3]));  \r
+                       result.add(new FindByIdentifierDTO<S>((DefinedTerm)daoObj[0], (String)daoObj[1], (UUID)daoObj[2], (String)daoObj[3]));\r
                }\r
         }\r
                return new DefaultPagerImpl<FindByIdentifierDTO<S>>(pageNumber, numberOfResults, pageSize, result);\r
        }\r
 \r
+       @Override\r
+       public SynonymRelationship moveSynonymToAnotherTaxon(SynonymRelationship oldSynonymRelation, UUID newTaxonUUID, boolean moveHomotypicGroup,\r
+            SynonymRelationshipType newSynonymRelationshipType, Reference reference, String referenceDetail, boolean keepReference) throws HomotypicalGroupChangeException {\r
 \r
+               Taxon newTaxon = (Taxon) dao.load(newTaxonUUID);\r
+               return moveSynonymToAnotherTaxon(oldSynonymRelation, newTaxon, moveHomotypicGroup, newSynonymRelationshipType, reference, referenceDetail, keepReference);\r
+       }\r
 }\r