Revert "getUuidAndTitleCache of classificationservice does not need the excluded...
authorKatja Luther <k.luther@bgbm.org>
Tue, 19 Jul 2016 12:13:05 +0000 (14:13 +0200)
committerAndreas Müller <a.mueller@bgbm.org>
Thu, 6 Oct 2016 15:01:59 +0000 (17:01 +0200)
This reverts commit 1b553592f0055e0fec1ab3878236fa0ecad1bd17.

cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/specimen/abcd206/in/Abcd206ImportReport.java [new file with mode: 0644]
cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/specimen/abcd206/in/AbcdImportUtility.java [new file with mode: 0644]
cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/service/ClassificationServiceImpl.java
cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/service/IClassificationService.java

diff --git a/cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/specimen/abcd206/in/Abcd206ImportReport.java b/cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/specimen/abcd206/in/Abcd206ImportReport.java
new file mode 100644 (file)
index 0000000..be3ce18
--- /dev/null
@@ -0,0 +1,284 @@
+// $Id$
+/**
+* Copyright (C) 2015 EDIT
+* European Distributed Institute of Taxonomy
+* http://www.e-taxonomy.eu
+*
+* 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.io.specimen.abcd206.in;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.PrintStream;
+import java.io.PrintWriter;
+import java.io.StringWriter;
+import java.net.URI;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Set;
+
+import org.apache.log4j.Logger;
+
+import eu.etaxonomy.cdm.model.name.TaxonNameBase;
+import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
+import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
+import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationType;
+import eu.etaxonomy.cdm.model.taxon.Taxon;
+import eu.etaxonomy.cdm.model.taxon.TaxonNode;
+
+/**
+ * Gathers information about the ABCD import and presents them in a suitable way.
+ * @author pplitzner
+ * @date Jan 23, 2015
+ *
+ */
+public class Abcd206ImportReport {
+
+    static private final Logger logger = Logger.getLogger(Abcd206ImportReport.class);
+
+
+    private final List<Taxon> createdTaxa = new ArrayList<Taxon>();
+    private final Map<Taxon, List<UnitIdSpecimen>> taxonToAssociatedSpecimens =  new HashMap<Taxon, List<UnitIdSpecimen>>();
+    private final Map<UnitIdSpecimen, List<UnitIdSpecimen>> derivateMap = new HashMap<UnitIdSpecimen, List<UnitIdSpecimen>>();
+    private final List<UnitIdSpecimen> alreadyExistingSpecimens = new ArrayList<UnitIdSpecimen>();
+    private final List<TaxonNameBase<?, ?>> createdNames = new ArrayList<TaxonNameBase<?,?>>();
+    private final List<TaxonNode> createdTaxonNodes = new ArrayList<TaxonNode>();
+    private final List<String> infoMessages = new ArrayList<String>();
+
+    public void addTaxon(Taxon taxon){
+        createdTaxa.add(taxon);
+    }
+
+    public void addName(TaxonNameBase<?, ?> taxonName){
+        createdNames.add(taxonName);
+    }
+
+    public void addTaxonNode(TaxonNode taxonNode){
+        createdTaxonNodes.add(taxonNode);
+    }
+
+    public void addDerivate(DerivedUnit parent, Abcd206ImportConfigurator config){
+        addDerivate(parent, null, config);
+    }
+
+    public void addDerivate(DerivedUnit parent, DerivedUnit child, Abcd206ImportConfigurator config){
+        UnitIdSpecimen parentUnitIdSpecimen = new UnitIdSpecimen(AbcdImportUtility.getUnitID(parent, config), parent);
+        List<UnitIdSpecimen> children = derivateMap.get(parentUnitIdSpecimen);
+        if(children==null){
+            children = new ArrayList<UnitIdSpecimen>();
+        }
+        if(child!=null){
+            children.add(new UnitIdSpecimen(AbcdImportUtility.getUnitID(child, config), child));
+        }
+        derivateMap.put(parentUnitIdSpecimen, children);
+    }
+
+    public void addIndividualAssociation(Taxon taxon, String derivedUnitId, DerivedUnit derivedUnitBase) {
+        UnitIdSpecimen derivedUnitIdSpecimen = new UnitIdSpecimen(derivedUnitId, derivedUnitBase);
+        List<UnitIdSpecimen> associatedSpecimens = taxonToAssociatedSpecimens.get(taxon);
+        if(associatedSpecimens==null){
+            associatedSpecimens = new ArrayList<UnitIdSpecimen>();
+        }
+        associatedSpecimens.add(derivedUnitIdSpecimen);
+        taxonToAssociatedSpecimens.put(taxon, associatedSpecimens);
+    }
+
+    public void addAlreadyExistingSpecimen(String unitId, DerivedUnit derivedUnit){
+        alreadyExistingSpecimens.add(new UnitIdSpecimen(unitId, derivedUnit));
+    }
+
+    public void addException(String message, Exception e) {
+        StringWriter errors = new StringWriter();
+        e.printStackTrace(new PrintWriter(errors));
+        infoMessages.add(message+"\n"+e.getMessage()+"\n"+errors.toString());
+    }
+
+    public void addInfoMessage(String message) {
+        infoMessages.add(message);
+    }
+
+    public void printReport(URI reportUri) {
+        PrintStream out;
+        if(reportUri != null){
+            try {
+                out = new PrintStream(new File(reportUri));
+            } catch (FileNotFoundException e) {
+                logger.warn("Report file could not be found.");
+                out = System.out;
+            }
+        }
+        else{
+            out = System.out;
+        }
+        printReport(out);
+    }
+
+
+    public void printReport(PrintStream out) {
+
+        out.println("++++++++Import Report+++++++++");
+      //all specimens
+        Set<UnitIdSpecimen> allSpecimens = new HashSet<UnitIdSpecimen>();
+        for (Entry<UnitIdSpecimen, List<UnitIdSpecimen>> entry : derivateMap.entrySet()) {
+            UnitIdSpecimen parentSpecimen = entry.getKey();
+            allSpecimens.add(parentSpecimen);
+            for (UnitIdSpecimen childSpecimen : entry.getValue()) {
+                allSpecimens.add(childSpecimen);
+            }
+        }
+        out.println("Specimens created: "+allSpecimens.size());
+        Map<SpecimenOrObservationType, Integer> specimenTypeToCount = new HashMap<SpecimenOrObservationType, Integer>();
+        for (UnitIdSpecimen unitIdSpecimen : allSpecimens) {
+            incrementSpecimenTypeCount(specimenTypeToCount, unitIdSpecimen);
+        }
+        for(Entry<SpecimenOrObservationType, Integer> entry:specimenTypeToCount.entrySet()){
+            SpecimenOrObservationType type = entry.getKey();
+            out.println(type+": "+entry.getValue());
+        }
+        out.println("\n");
+
+        //taxon name
+        out.println("---Created Taxon Names ("+createdNames.size()+")---");
+        for (TaxonNameBase<?, ?> taxonName : createdNames) {
+            out.println(taxonName.getTitleCache());
+        }
+        out.println("\n");
+
+        //taxa
+        out.println("---Created Taxa ("+createdTaxa.size()+")---");
+        for (Taxon taxon : createdTaxa) {
+            out.println(taxon.getTitleCache());
+        }
+        out.println("\n");
+
+        //taxon nodes
+        out.println("---Created Taxon Nodes ("+createdTaxonNodes.size()+")---");
+        for (TaxonNode taxonNode : createdTaxonNodes) {
+            String nodeString = taxonNode.toString();
+            if(taxonNode.getTaxon()!=null){
+                nodeString += " ("+taxonNode.getTaxon().getTitleCache()+")";
+            }
+            if(taxonNode.getParent()!=null){
+                nodeString += " with parent "+taxonNode.getParent();
+                if(taxonNode.getParent().getTaxon()!=null){
+                    nodeString += " ("+taxonNode.getParent().getTaxon().getTitleCache()+")";
+                }
+            }
+            out.println(nodeString);
+        }
+        out.println("\n");
+
+        //not imported
+        out.println("---Already existing specimen (not imported)---");
+        for(UnitIdSpecimen specimen:alreadyExistingSpecimens){
+            out.println(formatSpecimen(specimen));
+        }
+        out.println("\n");
+
+        //taxa with associated specimens
+        out.println("---Taxa with associated specimens---");
+        for(Entry<Taxon, List<UnitIdSpecimen>> entry:taxonToAssociatedSpecimens.entrySet()){
+            Taxon taxon = entry.getKey();
+            List<UnitIdSpecimen> specimens = entry.getValue();
+            out.println(taxon.getTitleCache() + " ("+specimens.size()+")");
+            for (UnitIdSpecimen derivedUnit : specimens) {
+                out.println("\t- "+formatSpecimen(derivedUnit));
+                //check for derivatives
+                List<UnitIdSpecimen> list = derivateMap.get(derivedUnit);
+                for (UnitIdSpecimen derivate : list) {
+                    out.println("\t\t- "+formatSpecimen(derivate));
+                }
+            }
+        }
+        out.println("\n");
+        out.println("\n");
+        //info messages
+        out.println("---Info messages---");
+        for(String message:infoMessages){
+            out.println(message);
+            out.println("---");
+        }
+        if(out!=System.out){
+            out.close();
+        }
+    }
+
+    private void incrementSpecimenTypeCount(Map<SpecimenOrObservationType, Integer> specimenTypeToCount,
+            UnitIdSpecimen specimen) {
+        SpecimenOrObservationType specimenType = specimen.getSpecimen().getRecordBasis();
+        Integer count = specimenTypeToCount.get(specimenType);
+        if(count==null){
+            count = 1;
+        }
+        else{
+            count++;
+        }
+        specimenTypeToCount.put(specimenType, count);
+    }
+
+    private String formatSpecimen(UnitIdSpecimen specimen){
+        return "("+specimen.getUnitId()+") ["+specimen.getSpecimen().getRecordBasis()+"] "+specimen.getSpecimen().getTitleCache();
+    }
+
+    private class UnitIdSpecimen{
+        private final String unitId;
+        private final SpecimenOrObservationBase<?> specimen;
+
+
+        public UnitIdSpecimen(String unitId, SpecimenOrObservationBase<?> specimen) {
+            super();
+            this.unitId = unitId;
+            this.specimen = specimen;
+        }
+        public String getUnitId() {
+            return unitId;
+        }
+        public SpecimenOrObservationBase<?> getSpecimen() {
+            return specimen;
+        }
+        @Override
+        public int hashCode() {
+            final int prime = 31;
+            int result = 1;
+            result = prime * result + ((specimen == null) ? 0 : specimen.hashCode());
+            result = prime * result + ((unitId == null) ? 0 : unitId.hashCode());
+            return result;
+        }
+        @Override
+        public boolean equals(Object obj) {
+            if (this == obj) {
+                return true;
+            }
+            if (obj == null) {
+                return false;
+            }
+            if (getClass() != obj.getClass()) {
+                return false;
+            }
+            UnitIdSpecimen other = (UnitIdSpecimen) obj;
+            if (specimen == null) {
+                if (other.specimen != null) {
+                    return false;
+                }
+            } else if (!specimen.equals(other.specimen)) {
+                return false;
+            }
+            if (unitId == null) {
+                if (other.unitId != null) {
+                    return false;
+                }
+            } else if (!unitId.equals(other.unitId)) {
+                return false;
+            }
+            return true;
+        }
+
+    }
+
+}
diff --git a/cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/specimen/abcd206/in/AbcdImportUtility.java b/cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/specimen/abcd206/in/AbcdImportUtility.java
new file mode 100644 (file)
index 0000000..4826b43
--- /dev/null
@@ -0,0 +1,45 @@
+// $Id$
+/**
+* Copyright (C) 2015 EDIT
+* European Distributed Institute of Taxonomy
+* http://www.e-taxonomy.eu
+*
+* 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.io.specimen.abcd206.in;
+
+import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
+
+/**
+ * @author pplitzner
+ * @date 16.06.2015
+ *
+ */
+public class AbcdImportUtility {
+
+    public static String getUnitID(DerivedUnit derivedUnit, Abcd206ImportConfigurator config){
+        if(config.isMapUnitIdToAccessionNumber()){
+            return derivedUnit.getAccessionNumber();
+        }
+        if(config.isMapUnitIdToBarcode()){
+            return derivedUnit.getBarcode();
+        }
+        return derivedUnit.getCatalogNumber();
+    }
+
+    public static void setUnitID(DerivedUnit derivedUnit, String unitId, Abcd206ImportConfigurator config){
+        if(config.isMapUnitIdToCatalogNumber()
+                || !(config.isMapUnitIdToAccessionNumber() || config.isMapUnitIdToBarcode() || config.isMapUnitIdToCatalogNumber())){
+            // set catalog number (default if nothing is set)
+            derivedUnit.setCatalogNumber(unitId);
+        }
+        if(config.isMapUnitIdToAccessionNumber()){
+            derivedUnit.setAccessionNumber(unitId);
+        }
+        if(config.isMapUnitIdToBarcode()){
+            derivedUnit.setBarcode(unitId);
+        }
+    }
+
+}
index 295443b39d1ffe36a4640208885ca694b4aedcbd..8b5f057fe5825c50bc71b8db98dba2667299a9c8 100644 (file)
@@ -332,23 +332,23 @@ public class ClassificationServiceImpl extends IdentifiableServiceBase<Classific
     }
 
     @Override
-    public List<UuidAndTitleCache<TaxonNode>> getTaxonNodeUuidAndTitleCacheOfAcceptedTaxaByClassification(UUID classificationUuid, Integer limit, String pattern) {
-        return taxonDao.getTaxonNodeUuidAndTitleCacheOfAcceptedTaxaByClassification(dao.load(classificationUuid), limit, pattern);
+    public List<UuidAndTitleCache<TaxonNode>> getTaxonNodeUuidAndTitleCacheOfAcceptedTaxaByClassification(UUID classificationUuid, List<UUID> excludeTaxa, Integer limit, String pattern) {
+        return taxonDao.getTaxonNodeUuidAndTitleCacheOfAcceptedTaxaByClassification(dao.load(classificationUuid), excludeTaxa, limit, pattern);
     }
 
     @Override
-    public List<UuidAndTitleCache<TaxonNode>> getTaxonNodeUuidAndTitleCacheOfAcceptedTaxaByClassification(Classification classification,  Integer limit, String pattern) {
-        return taxonDao.getTaxonNodeUuidAndTitleCacheOfAcceptedTaxaByClassification(classification,  limit, pattern);
+    public List<UuidAndTitleCache<TaxonNode>> getTaxonNodeUuidAndTitleCacheOfAcceptedTaxaByClassification(Classification classification, List<UUID> excludeTaxa, Integer limit, String pattern) {
+        return taxonDao.getTaxonNodeUuidAndTitleCacheOfAcceptedTaxaByClassification(classification, excludeTaxa, limit, pattern);
     }
 
     @Override
-    public List<UuidAndTitleCache<TaxonNode>> getTaxonNodeUuidAndTitleCacheOfAcceptedTaxaByClassification(UUID classificationUuid ) {
-        return taxonDao.getTaxonNodeUuidAndTitleCacheOfAcceptedTaxaByClassification(dao.load(classificationUuid),  null, null);
+    public List<UuidAndTitleCache<TaxonNode>> getTaxonNodeUuidAndTitleCacheOfAcceptedTaxaByClassification(UUID classificationUuid, List<UUID> excludeTaxa) {
+        return taxonDao.getTaxonNodeUuidAndTitleCacheOfAcceptedTaxaByClassification(dao.load(classificationUuid), excludeTaxa, null, null);
     }
 
     @Override
-    public List<UuidAndTitleCache<TaxonNode>> getTaxonNodeUuidAndTitleCacheOfAcceptedTaxaByClassification(Classification classification){
-        return taxonDao.getTaxonNodeUuidAndTitleCacheOfAcceptedTaxaByClassification(classification, null, null);
+    public List<UuidAndTitleCache<TaxonNode>> getTaxonNodeUuidAndTitleCacheOfAcceptedTaxaByClassification(Classification classification, List<UUID> excludeTaxa) {
+        return taxonDao.getTaxonNodeUuidAndTitleCacheOfAcceptedTaxaByClassification(classification, excludeTaxa, null, null);
     }
 
     @Override
index 3acde51a80a20823fb0bdf2c608e186597a1e3b0..0bdc9d7c4aec377e045af053bbc24c0a6696ee2c 100644 (file)
@@ -49,9 +49,9 @@ public interface IClassificationService extends IIdentifiableEntityService<Class
      * @return
      */
     public ITaxonTreeNode getTreeNodeByUuid(UUID uuid);
-
+    
     /**
-     *
+     * 
      * Returns the root node of the the given classification (specified by its UUID)
      * @param classificationUuid the uuid of the classification
      * @return the root node of the classification
@@ -187,7 +187,7 @@ public interface IClassificationService extends IIdentifiableEntityService<Class
      * @param classification
      * @return
      */
-    public List<UuidAndTitleCache<TaxonNode>> getTaxonNodeUuidAndTitleCacheOfAcceptedTaxaByClassification(Classification classification);
+    public List<UuidAndTitleCache<TaxonNode>> getTaxonNodeUuidAndTitleCacheOfAcceptedTaxaByClassification(Classification classification, List<UUID> excludeTaxa);
 
     /**
      * @param taxon
@@ -269,7 +269,7 @@ public interface IClassificationService extends IIdentifiableEntityService<Class
      * @param excludeTaxa
      * @return
      */
-    public List<UuidAndTitleCache<TaxonNode>> getTaxonNodeUuidAndTitleCacheOfAcceptedTaxaByClassification(UUID classificationUuid);
+    public List<UuidAndTitleCache<TaxonNode>> getTaxonNodeUuidAndTitleCacheOfAcceptedTaxaByClassification(UUID classificationUuid, List<UUID> excludeTaxa);
 
     /**
      * @param classificationUuid
@@ -279,7 +279,7 @@ public interface IClassificationService extends IIdentifiableEntityService<Class
      * @return
      */
     List<UuidAndTitleCache<TaxonNode>> getTaxonNodeUuidAndTitleCacheOfAcceptedTaxaByClassification(
-            UUID classificationUuid, Integer limit, String pattern);
+            UUID classificationUuid, List<UUID> excludeTaxa, Integer limit, String pattern);
 
     /**
      * @param classification
@@ -289,7 +289,7 @@ public interface IClassificationService extends IIdentifiableEntityService<Class
      * @return
      */
     List<UuidAndTitleCache<TaxonNode>> getTaxonNodeUuidAndTitleCacheOfAcceptedTaxaByClassification(
-            Classification classification, Integer limit, String pattern);
+            Classification classification, List<UUID> excludeTaxa, Integer limit, String pattern);
 
     /**
      * @param taxonUuid