remove unused property path
[cdmlib.git] / cdmlib-services / src / main / java / eu / etaxonomy / cdm / api / service / ClassificationServiceImpl.java
index 8838655fd4092d8b3d0f56af344edff395f33451..496ace11c7fd47687d161d64d545b9bf1151d536 100644 (file)
@@ -6,7 +6,6 @@
 * The contents of this file are subject to the Mozilla Public License Version 1.1
 * See LICENSE.TXT at the top of this package for the full license terms.
 */
-
 package eu.etaxonomy.cdm.api.service;
 
 import java.util.ArrayList;
@@ -26,8 +25,9 @@ import java.util.stream.Collectors;
 import javax.persistence.EntityNotFoundException;
 
 import org.apache.commons.collections.CollectionUtils;
-import org.apache.commons.lang.StringUtils;
-import org.apache.log4j.Logger;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
@@ -35,7 +35,6 @@ import org.springframework.transaction.annotation.Transactional;
 import eu.etaxonomy.cdm.api.service.config.CreateHierarchyForClassificationConfigurator;
 import eu.etaxonomy.cdm.api.service.config.NodeDeletionConfigurator.ChildHandling;
 import eu.etaxonomy.cdm.api.service.config.TaxonDeletionConfigurator;
-import eu.etaxonomy.cdm.api.service.dto.EntityDTO;
 import eu.etaxonomy.cdm.api.service.dto.GroupedTaxonDTO;
 import eu.etaxonomy.cdm.api.service.dto.MarkedEntityDTO;
 import eu.etaxonomy.cdm.api.service.dto.TaxonInContextDTO;
@@ -44,10 +43,10 @@ import eu.etaxonomy.cdm.api.service.pager.PagerUtils;
 import eu.etaxonomy.cdm.api.service.pager.impl.AbstractPagerImpl;
 import eu.etaxonomy.cdm.api.service.pager.impl.DefaultPagerImpl;
 import eu.etaxonomy.cdm.common.monitor.IProgressMonitor;
+import eu.etaxonomy.cdm.compare.taxon.ITaxonNodeComparator;
+import eu.etaxonomy.cdm.compare.taxon.TaxonNodeSortMode;
 import eu.etaxonomy.cdm.exception.FilterException;
 import eu.etaxonomy.cdm.exception.UnpublishedException;
-import eu.etaxonomy.cdm.hibernate.HHH_9751_Util;
-import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
 import eu.etaxonomy.cdm.model.common.CdmBase;
 import eu.etaxonomy.cdm.model.common.ITreeNode;
 import eu.etaxonomy.cdm.model.common.MarkerType;
@@ -60,15 +59,12 @@ import eu.etaxonomy.cdm.model.media.MediaUtils;
 import eu.etaxonomy.cdm.model.name.INonViralName;
 import eu.etaxonomy.cdm.model.name.Rank;
 import eu.etaxonomy.cdm.model.name.TaxonName;
-import eu.etaxonomy.cdm.model.reference.Reference;
 import eu.etaxonomy.cdm.model.taxon.Classification;
-import eu.etaxonomy.cdm.model.taxon.ITaxonNodeComparator;
 import eu.etaxonomy.cdm.model.taxon.ITaxonTreeNode;
 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.model.taxon.TaxonNode;
-import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
 import eu.etaxonomy.cdm.model.term.DefinedTermBase;
 import eu.etaxonomy.cdm.persistence.dao.initializer.IBeanInitializer;
 import eu.etaxonomy.cdm.persistence.dao.taxon.IClassificationDao;
@@ -76,6 +72,7 @@ import eu.etaxonomy.cdm.persistence.dao.taxon.ITaxonDao;
 import eu.etaxonomy.cdm.persistence.dao.taxon.ITaxonNodeDao;
 import eu.etaxonomy.cdm.persistence.dao.term.IDefinedTermDao;
 import eu.etaxonomy.cdm.persistence.dto.ClassificationLookupDTO;
+import eu.etaxonomy.cdm.persistence.dto.EntityDTO;
 import eu.etaxonomy.cdm.persistence.dto.TaxonNodeDto;
 import eu.etaxonomy.cdm.persistence.dto.TaxonStatus;
 import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
@@ -93,7 +90,7 @@ public class ClassificationServiceImpl
              extends IdentifiableServiceBase<Classification, IClassificationDao>
              implements IClassificationService {
 
-    private static final Logger logger = Logger.getLogger(ClassificationServiceImpl.class);
+    private static final Logger logger = LogManager.getLogger();
 
     @Autowired
     private ITaxonNodeDao taxonNodeDao;
@@ -116,11 +113,11 @@ public class ClassificationServiceImpl
         this.dao = dao;
     }
 
-    private Comparator<? super TaxonNode> taxonNodeComparator;
+    private Comparator<TaxonNode> taxonNodeComparator;
 
     @Autowired
-    public void setTaxonNodeComparator(ITaxonNodeComparator<? super TaxonNode> taxonNodeComparator){
-        this.taxonNodeComparator = (Comparator<? super TaxonNode>) taxonNodeComparator;
+    public void setTaxonNodeComparator(ITaxonNodeComparator<TaxonNode> taxonNodeComparator){
+        this.taxonNodeComparator = (Comparator<TaxonNode>) taxonNodeComparator;
     }
 
     @Override
@@ -135,53 +132,6 @@ public class ClassificationServiceImpl
         return taxonNodeDao.load(taxonNodeUuid, propertyPaths);
     }
 
-    @Override
-    @Transactional(readOnly = false)
-    public UpdateResult cloneClassification(UUID classificationUuid,
-               String name, Reference sec, TaxonRelationshipType relationshipType) {
-        UpdateResult result = new UpdateResult();
-       Classification classification = load(classificationUuid);
-       Classification clone = Classification.NewInstance(name);
-       clone.setReference(sec);
-
-       //clone taxa and taxon nodes
-       List<TaxonNode> childNodes = classification.getRootNode().getChildNodes();
-       for (TaxonNode taxonNode : childNodes) {
-               addChildTaxa(taxonNode, null, clone, relationshipType);
-       }
-       dao.saveOrUpdate(clone);
-       result.setCdmEntity(clone);
-       return result;
-    }
-
-    private void addChildTaxa(TaxonNode originalParentNode, TaxonNode cloneParentNode, Classification classification, TaxonRelationshipType relationshipType){
-        Reference reference = classification.getReference();
-       Taxon cloneTaxon = (Taxon) HibernateProxyHelper.deproxy(originalParentNode.getTaxon(), Taxon.class).clone();
-       cloneTaxon.setSec(reference);
-               String microReference = null;
-               List<TaxonNode> originalChildNodes = originalParentNode.getChildNodes();
-               HHH_9751_Util.removeAllNull(originalChildNodes);
-
-               //add relation between taxa
-               if (relationshipType != null){
-                   cloneTaxon.addTaxonRelation(originalParentNode.getTaxon(), relationshipType, reference, microReference);
-               }
-
-               TaxonNode cloneChildNode = null;
-       //add taxon node to either parent node or classification (no parent node)
-       if(cloneParentNode==null){
-               cloneChildNode = classification.addChildTaxon(cloneTaxon, reference, microReference);
-       }
-       else{
-               cloneChildNode = cloneParentNode.addChildTaxon(cloneTaxon, reference, microReference);
-       }
-       taxonNodeDao.saveOrUpdate(cloneChildNode);
-       //add children
-               for (TaxonNode originalChildNode : originalChildNodes) {
-               addChildTaxa(originalChildNode, cloneChildNode, classification, relationshipType);
-       }
-    }
-
     @Override
     public List<TaxonNode> listRankSpecificRootNodes(Classification classification,
             TaxonNode subtree, Rank rank,
@@ -194,7 +144,7 @@ public class ClassificationServiceImpl
             Rank rank, boolean includeUnpublished, Integer pageSize, Integer pageIndex, TaxonNodeDtoSortMode sortMode,
             List<String> propertyPaths) {
         List<TaxonNode> list = listRankSpecificRootNodes(classification, subtree, rank, includeUnpublished, pageSize, pageIndex, propertyPaths);
-        return list.stream().filter(e ->  e != null).map(e -> new TaxonNodeDto(e)).sorted(sortMode.newComparator()).collect(Collectors.toList());
+        return list.stream().filter(e ->  e != null).map(e -> new TaxonNodeDto(e)).sorted(sortMode.comparator()).collect(Collectors.toList());
     }
 
     @Override
@@ -253,9 +203,6 @@ public class ClassificationServiceImpl
         return loadTreeBranch(taxonNode, null, baseRank, includeUnpublished, propertyPaths);
     }
 
-    /**
-     * {@inheritDoc}
-     */
     @Override
     public List<TaxonNode> loadTreeBranch(TaxonNode taxonNode, TaxonNode subtree, Rank baseRank,
             boolean includeUnpublished, List<String> propertyPaths) throws UnpublishedException{
@@ -333,11 +280,11 @@ public class ClassificationServiceImpl
         TaxonNode node = taxonNodeService.find(nodeUuid);
         if(node == null){
             logger.warn("The specified taxon is not found in the given tree.");
-            return null;
+            return new ArrayList<>(0);
         }else if (subtree != null && !node.isDescendant(subtree)){
             //TODO handle as exception? E.g. FilterException, AccessDeniedException?
             logger.warn("The specified taxon is not found for the given subtree.");
-            return null;
+            return new ArrayList<>(0);
         }
 
         return loadTreeBranch(node, subtree, baseRank, includeUnpublished, propertyPaths);
@@ -382,8 +329,10 @@ public class ClassificationServiceImpl
     }
 
     @Override
-    public List<TaxonNodeDto> listChildNodeDtosOfTaxon(UUID taxonUuid, UUID classificationUuid, UUID subtreeUuid, boolean includeUnpublished,
-            Integer pageSize, Integer pageIndex, TaxonNodeDtoSortMode sortMode, List<String> propertyPaths) throws FilterException{
+    public List<TaxonNodeDto> listChildNodeDtosOfTaxon(UUID taxonUuid, UUID classificationUuid,
+            UUID subtreeUuid, boolean includeUnpublished,
+            Integer pageSize, Integer pageIndex, TaxonNodeDtoSortMode sortMode) throws FilterException{
+
         Classification classification = dao.load(classificationUuid);
         Taxon taxon = (Taxon) taxonDao.load(taxonUuid);
         TaxonNode subtree = taxonNodeDao.load(subtreeUuid);
@@ -393,9 +342,12 @@ public class ClassificationServiceImpl
 
         List<TaxonNode> results = dao.listChildrenOf(
                 taxon, classification, subtree, includeUnpublished, pageSize, pageIndex, propertyPaths);
-        Comparator<TaxonNodeDto> comparator = sortMode.newComparator();
+        Comparator<TaxonNodeDto> comparator = sortMode.comparator();
         // TODO order during the hibernate query in the dao?
-        List<TaxonNodeDto> dtos = results.stream().map(e -> new TaxonNodeDto(e)).sorted(comparator).collect(Collectors.toList());
+        List<TaxonNodeDto> dtos = results.stream()
+                .map(tn -> new TaxonNodeDto(tn))
+                .sorted(comparator)
+                .collect(Collectors.toList());
         return dtos;
     }
 
@@ -447,10 +399,6 @@ public class ClassificationServiceImpl
         return dao.list(limit, start, orderHints, propertyPaths);
     }
 
-    @Override
-    public UUID removeTaxonNode(TaxonNode taxonNode) {
-        return taxonNodeDao.delete(taxonNode);
-    }
     @Override
     public UUID removeTreeNode(ITaxonTreeNode treeNode) {
         if(treeNode instanceof Classification){
@@ -460,10 +408,6 @@ public class ClassificationServiceImpl
         }
         return null;
     }
-    @Override
-    public UUID saveTaxonNode(TaxonNode taxonNode) {
-        return taxonNodeDao.save(taxonNode).getUuid();
-    }
 
     @Override
     public Map<UUID, TaxonNode> saveTaxonNodeAll(
@@ -617,7 +561,10 @@ public class ClassificationServiceImpl
        @Transactional(readOnly = false)
        @Override
     public UpdateResult createHierarchyInClassification(Classification classification, CreateHierarchyForClassificationConfigurator configurator){
+
         UpdateResult result = new UpdateResult();
+        Set<TaxonNode> taxonNodesToSave = new HashSet<>();
+
        classification = dao.findByUuid(classification.getUuid());
        Map<String, List<TaxonNode>> map = getSortedGenusList(classification.getAllNodes());
 
@@ -637,7 +584,7 @@ public class ClassificationServiceImpl
                        //FIXME NPE for name
                        TaxonName name = tNode.getTaxon().getName();
                        if(name.getNameCache().equalsIgnoreCase(genus)){
-                               TaxonNode clone = (TaxonNode) tNode.clone();
+                               TaxonNode clone = tNode.clone();
                                if(!tNode.hasChildNodes()){
                                        //FIXME remove classification
 //                                     parentNode = newClassification.addChildNode(clone, 0, classification.getCitation(), classification.getMicroReference());
@@ -649,8 +596,7 @@ public class ClassificationServiceImpl
                                        //get all childNodes
                                        //save prior Hierarchy and remove them from the list
                                        List<TaxonNode> copyAllChildrenToTaxonNode = copyAllChildrenToTaxonNode(tNode, clone, result);
-//                                     parentNode = newClassification.addChildNode(clone, 0, classification.getCitation(), classification.getMicroReference());
-                                       //FIXME remove classification
+//                                     //FIXME remove classification
                                        parentNode = newClassification.addChildNode(clone, 0, clone.getReference(), clone.getMicroReference());
                                        //remove taxonNode from list because just added to classification
                                        result.addUpdatedObject(tNode);
@@ -671,7 +617,9 @@ public class ClassificationServiceImpl
                        parentNode = newClassification.addChildTaxon(taxon, 0, null, null);
                        result.addUpdatedObject(parentNode);
                }
-               //iterate over the rest of the list
+               taxonNodesToSave.add(parentNode);
+
+               //iterate over the remaining list
                for(TaxonNode tn : listOfTaxonNodes){
                        //if TaxonNode has a parent and this is not the classification then skip it
                        //and add to new classification via the parentNode as children of it
@@ -682,11 +630,12 @@ public class ClassificationServiceImpl
                                continue; //skip to next taxonNode
                        }
 
-                       TaxonNode clone = (TaxonNode) tn.clone();
+                       TaxonNode clone = tn.clone();
                        //FIXME: citation from node
-                       //TODO: addchildNode without citation and references
-//                     TaxonNode taxonNode = parentNode.addChildNode(clone, classification.getCitation(), classification.getMicroReference());
+                       //TODO: addChildNode without citation and references
                        TaxonNode taxonNode = parentNode.addChildNode(clone, clone.getReference(), clone.getMicroReference());
+                       taxonNodesToSave.add(taxonNode);
+
                        result.addUnChangedObject(clone);
                        if(tn.hasChildNodes()){
                                //save hierarchy in new classification
@@ -698,6 +647,7 @@ public class ClassificationServiceImpl
                }
        }
        dao.saveOrUpdate(newClassification);
+       taxonNodeDao.saveOrUpdateAll(taxonNodesToSave);
        result.setCdmEntity(newClassification);
        return result;
     }
@@ -719,7 +669,7 @@ public class ClassificationServiceImpl
                        childNodes = copyFromNode.getChildNodes();
                }
                for(TaxonNode childNode:childNodes){
-                       TaxonNode clone = (TaxonNode) childNode.clone();
+                       TaxonNode clone = childNode.clone();
                        result.addUnChangedObject(clone);
                        if(childNode.hasChildNodes()){
                                copyAllChildrenToTaxonNode(childNode, clone, result);