fix #9617 rename ICNB to ICNP in cdmlib
[cdmlib.git] / cdmlib-io / src / main / java / eu / etaxonomy / cdm / io / specimen / SpecimenImportBase.java
old mode 100644 (file)
new mode 100755 (executable)
index 9f8a657..2eb7ea2
@@ -11,6 +11,7 @@ package eu.etaxonomy.cdm.io.specimen;
 
 
 import java.util.ArrayList;
+import java.util.EnumSet;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
@@ -20,7 +21,7 @@ import java.util.UUID;
 
 import org.apache.log4j.Logger;
 
-import eu.etaxonomy.cdm.api.application.ICdmApplicationConfiguration;
+import eu.etaxonomy.cdm.api.application.ICdmRepository;
 import eu.etaxonomy.cdm.api.facade.DerivedUnitFacade;
 import eu.etaxonomy.cdm.api.service.config.FindOccurrencesConfigurator;
 import eu.etaxonomy.cdm.api.service.pager.Pager;
@@ -31,33 +32,32 @@ import eu.etaxonomy.cdm.io.specimen.abcd206.in.Identification;
 import eu.etaxonomy.cdm.io.specimen.abcd206.in.SpecimenImportReport;
 import eu.etaxonomy.cdm.model.agent.AgentBase;
 import eu.etaxonomy.cdm.model.agent.Institution;
+import eu.etaxonomy.cdm.model.agent.Person;
 import eu.etaxonomy.cdm.model.agent.Team;
 import eu.etaxonomy.cdm.model.common.CdmBase;
-import eu.etaxonomy.cdm.model.common.ISourceable;
 import eu.etaxonomy.cdm.model.common.IdentifiableSource;
 import eu.etaxonomy.cdm.model.common.LanguageString;
-import eu.etaxonomy.cdm.model.common.OriginalSourceBase;
-import eu.etaxonomy.cdm.model.common.OriginalSourceType;
 import eu.etaxonomy.cdm.model.description.DescriptionBase;
 import eu.etaxonomy.cdm.model.description.DescriptionElementSource;
+import eu.etaxonomy.cdm.model.description.DescriptionType;
 import eu.etaxonomy.cdm.model.description.Feature;
 import eu.etaxonomy.cdm.model.description.IndividualsAssociation;
 import eu.etaxonomy.cdm.model.description.TaxonDescription;
-import eu.etaxonomy.cdm.model.name.BacterialName;
-import eu.etaxonomy.cdm.model.name.BotanicalName;
-import eu.etaxonomy.cdm.model.name.CultivarPlantName;
+import eu.etaxonomy.cdm.model.name.INonViralName;
 import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
-import eu.etaxonomy.cdm.model.name.NonViralName;
 import eu.etaxonomy.cdm.model.name.Rank;
 import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignation;
 import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignationStatus;
-import eu.etaxonomy.cdm.model.name.TaxonNameBase;
-import eu.etaxonomy.cdm.model.name.ZoologicalName;
+import eu.etaxonomy.cdm.model.name.TaxonName;
+import eu.etaxonomy.cdm.model.name.TaxonNameFactory;
 import eu.etaxonomy.cdm.model.occurrence.Collection;
 import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
 import eu.etaxonomy.cdm.model.occurrence.DeterminationEvent;
 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationType;
+import eu.etaxonomy.cdm.model.reference.ISourceable;
+import eu.etaxonomy.cdm.model.reference.OriginalSourceBase;
+import eu.etaxonomy.cdm.model.reference.OriginalSourceType;
 import eu.etaxonomy.cdm.model.reference.Reference;
 import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
 import eu.etaxonomy.cdm.model.taxon.Classification;
@@ -65,26 +65,30 @@ 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.persistence.dto.UuidAndTitleCache;
+import eu.etaxonomy.cdm.model.term.DefinedTerm;
 import eu.etaxonomy.cdm.persistence.query.MatchMode;
 import eu.etaxonomy.cdm.strategy.parser.NonViralNameParserImpl;
 import eu.etaxonomy.cdm.strategy.parser.ParserProblem;
+import eu.etaxonomy.cdm.strategy.parser.TimePeriodParser;
 
 
 /**
  * @author p.kelbert
- * @created 20.10.2008
+ * @since 20.10.2008
  */
-public abstract class SpecimenImportBase<CONFIG extends IImportConfigurator, STATE extends SpecimenImportStateBase>  extends CdmImportBase<CONFIG, STATE> {
+public abstract class SpecimenImportBase<CONFIG extends IImportConfigurator, STATE extends SpecimenImportStateBase>
+        extends CdmImportBase<CONFIG, STATE> {
 
-       @SuppressWarnings("unused")
-       private static final Logger logger = Logger.getLogger(SpecimenImportBase.class);
-       protected final boolean DEBUG = true;
+    private static final long serialVersionUID = 4423065367998125678L;
+    private static final Logger logger = Logger.getLogger(SpecimenImportBase.class);
 
        protected static final UUID SPECIMEN_SCAN_TERM = UUID.fromString("acda15be-c0e2-4ea8-8783-b9b0c4ad7f03");
 
        private static final String COLON = ":";
 
+       protected Map<String, DefinedTerm> kindOfUnitsMap;
+
+
 
        @Override
     protected abstract void doInvoke(STATE state);
@@ -94,18 +98,17 @@ public abstract class SpecimenImportBase<CONFIG extends IImportConfigurator, STA
         * @param state
         * @param item
         */
-       @SuppressWarnings("rawtypes")
        protected abstract void handleSingleUnit(STATE state, Object item) ;
 
 
 
-       protected TaxonNameBase<?, ?> getOrCreateTaxonName(String scientificName, Rank rank, boolean preferredFlag, STATE state, int unitIndexInAbcdFile){
-        TaxonNameBase<?, ?> taxonName = null;
-        SpecimenImportConfiguratorBase config = state.getConfig();
+       protected TaxonName getOrCreateTaxonName(String scientificName, Rank rank, boolean preferredFlag, STATE state, int unitIndexInAbcdFile){
+           TaxonName taxonName = null;
+        SpecimenImportConfiguratorBase<?,?,?> config = state.getConfig();
 
         //check atomised name data for rank
         //new name will be created
-        NonViralName<?> atomisedTaxonName = null;
+        TaxonName atomisedTaxonName = null;
         if (rank==null && unitIndexInAbcdFile>=0 && ((state.getDataHolder().getAtomisedIdentificationList() != null && !state.getDataHolder().getAtomisedIdentificationList().isEmpty())|| state.getDataHolder().getAtomisedIdentificationList().size() > 0)) {
             atomisedTaxonName = setTaxonNameByType(state.getDataHolder().getAtomisedIdentificationList().get(unitIndexInAbcdFile), scientificName, state);
             if(atomisedTaxonName!=null){
@@ -113,29 +116,36 @@ public abstract class SpecimenImportBase<CONFIG extends IImportConfigurator, STA
             }
         }
         if(config.isReuseExistingTaxaWhenPossible()){
-            NonViralName<?> parsedName = atomisedTaxonName;
+            TaxonName parsedName = atomisedTaxonName;
             if(parsedName==null){
-                parsedName = parseScientificName(scientificName, state, state.getReport());
+
+                parsedName = parseScientificName(scientificName, state, state.getReport(), rank);
+
             }
-            if(config.isIgnoreAuthorship() && parsedName!=null && preferredFlag){
+            atomisedTaxonName = parsedName;
+            if(config.isIgnoreAuthorship() && parsedName!=null){// && preferredFlag){
                 // do not ignore authorship for non-preferred names because they need
                 // to be created for the determination history
-                String nameCache = parsedName.getNameCache();
-                List<NonViralName> names = getNameService().findNamesByNameCache(nameCache, MatchMode.EXACT, null);
+                String nameCache = TaxonName.castAndDeproxy(parsedName).getNameCache();
+                List<TaxonName> names = getNameService().findNamesByNameCache(nameCache, MatchMode.EXACT, null);
                 if (!names.isEmpty()){
-                     return getBestMatchingName(scientificName, new ArrayList<TaxonNameBase>(names), state);
+                     taxonName = getBestMatchingName(scientificName, new ArrayList<>(names), state);
                 }
+                if (taxonName == null && !names.isEmpty()){
+                    taxonName = names.get(0);
+                }
+
             } else {
                 //search for existing names
-                List<TaxonNameBase> names = getNameService().listByTitle(TaxonNameBase.class, scientificName, MatchMode.EXACT, null, null, null, null, null);
+                List<TaxonName> names = getNameService().listByTitleWithRestrictions(TaxonName.class, scientificName, MatchMode.EXACT, null, null, null, null, null);
                 taxonName = getBestMatchingName(scientificName, names, state);
                 //still nothing found -> try with the atomised name full title cache
                 if(taxonName==null && atomisedTaxonName!=null){
-                    names = getNameService().listByTitle(TaxonNameBase.class, atomisedTaxonName.getFullTitleCache(), MatchMode.EXACT, null, null, null, null, null);
+                    names = getNameService().listByTitleWithRestrictions(TaxonName.class, atomisedTaxonName.getFullTitleCache(), MatchMode.EXACT, null, null, null, null, null);
                     taxonName = getBestMatchingName(atomisedTaxonName.getTitleCache(), names, state);
                     //still nothing found -> try with the atomised name title cache
                     if(taxonName==null){
-                        names = getNameService().listByTitle(TaxonNameBase.class, atomisedTaxonName.getTitleCache(), MatchMode.EXACT, null, null, null, null, null);
+                        names = getNameService().listByTitleWithRestrictions(TaxonName.class, atomisedTaxonName.getTitleCache(), MatchMode.EXACT, null, null, null, null, null);
                         taxonName = getBestMatchingName(atomisedTaxonName.getTitleCache(), names, state);
                     }
                 }
@@ -157,33 +167,65 @@ public abstract class SpecimenImportBase<CONFIG extends IImportConfigurator, STA
         }
         else if(taxonName==null){
             //create new taxon name
+
             if (state.getDataHolder().getNomenclatureCode().equals(NomenclaturalCode.ICNAFP)){
-                taxonName = BotanicalName.NewInstance(rank);
+                taxonName = TaxonNameFactory.NewBotanicalInstance(rank);
             }else if (state.getDataHolder().getNomenclatureCode().equals(NomenclaturalCode.ICZN)){
-                taxonName = ZoologicalName.NewInstance(rank);
+                taxonName = TaxonNameFactory.NewZoologicalInstance(rank);
             }else{
-                taxonName = NonViralName.NewInstance(rank);
+                taxonName = TaxonNameFactory.NewNonViralInstance(rank);
             }
             taxonName.setFullTitleCache(scientificName,true);
             taxonName.setTitleCache(scientificName, true);
             state.getReport().addName(taxonName);
             logger.info("Created new taxon name "+taxonName);
         }
-        save(taxonName, state);
+        if (taxonName != null){
+            state.names.put(taxonName.getNameCache(), taxonName);
+        }
+        if(!taxonName.isPersited()) {
+            save(taxonName, state);
+        }
         return taxonName;
     }
 
-        protected TaxonNameBase<?, ?> getBestMatchingName(String scientificName, java.util.Collection<TaxonNameBase> names, STATE state){
-               List<TaxonNameBase> namesWithAcceptedTaxa = new ArrayList<TaxonNameBase>();
-               for (TaxonNameBase name : names) {
+        protected TaxonName getBestMatchingName(String scientificName, java.util.Collection<TaxonName> names, STATE state){
+               Set<TaxonName> namesWithAcceptedTaxa = new HashSet<>();
+               List<TaxonName> namesWithAcceptedTaxaInClassification = new ArrayList<>();
+               for (TaxonName name : names) {
                    if(!name.getTaxa().isEmpty()){
-                       namesWithAcceptedTaxa.add(name);
+                       Set<Taxon> taxa = name.getTaxa();
+                       for (Taxon taxon:taxa){
+                           if (!taxon.getTaxonNodes().isEmpty()){
+                               //use only taxa included in a classification
+                               for (TaxonNode node:taxon.getTaxonNodes()){
+                                   if (state.getClassification() != null && node.getClassification().equals(state.getClassification())){
+                                       namesWithAcceptedTaxaInClassification.add(name);
+                                   }else {
+                                       namesWithAcceptedTaxa.add(name);
+                                   }
+                               }
+
+                           }
+                       }
+
                    }
                }
-               String message = "More than one taxon name was found for "+scientificName+"!";
-               //check for names with accepted taxa
+               String message = String.format("More than one taxon name was found for %s, maybe in other classifications!", scientificName);
+               //check for names with accepted taxa in classification
+               if(namesWithAcceptedTaxaInClassification.size()>0){
+                if(namesWithAcceptedTaxaInClassification.size()>1){
+
+                    state.getReport().addInfoMessage(message);
+                    logger.warn(message);
+                    return null;
+                }
+                return namesWithAcceptedTaxaInClassification.iterator().next();
+            }
+             //check for any names with accepted taxa
                if(namesWithAcceptedTaxa.size()>0){
                    if(namesWithAcceptedTaxa.size()>1){
+
                        state.getReport().addInfoMessage(message);
                        logger.warn(message);
                        return null;
@@ -191,13 +233,13 @@ public abstract class SpecimenImportBase<CONFIG extends IImportConfigurator, STA
                    return namesWithAcceptedTaxa.iterator().next();
                }
                //no names with accepted taxa found -> check accepted taxa of synonyms
-               List<Taxon> taxaFromSynonyms = new ArrayList<Taxon>();
-               for (TaxonNameBase name : names) {
+               List<Taxon> taxaFromSynonyms = new ArrayList<>();
+               for (TaxonName name : names) {
                    Set<TaxonBase> taxonBases = name.getTaxonBases();
                    for (TaxonBase taxonBase : taxonBases) {
                        if(taxonBase.isInstanceOf(Synonym.class)){
                            Synonym synonym = HibernateProxyHelper.deproxy(taxonBase, Synonym.class);
-                           taxaFromSynonyms.addAll(synonym.getAcceptedTaxa());
+                           taxaFromSynonyms.add(synonym.getAcceptedTaxon());
                        }
                    }
                }
@@ -209,7 +251,12 @@ public abstract class SpecimenImportBase<CONFIG extends IImportConfigurator, STA
                    }
                    return taxaFromSynonyms.iterator().next().getName();
                }
-               return null;
+               //no accepted and no synonyms -> return one of the names and create a new taxon
+               if (names.isEmpty()){
+                   return null;
+               }else{
+                   return names.iterator().next();
+               }
            }
         /**
             * Parse automatically the scientific name
@@ -218,35 +265,37 @@ public abstract class SpecimenImportBase<CONFIG extends IImportConfigurator, STA
             * @param report the import report
             * @return a parsed name
             */
-           protected NonViralName<?> parseScientificName(String scientificName, STATE state, SpecimenImportReport report) {
+
+           protected TaxonName parseScientificName(String scientificName, STATE state, SpecimenImportReport report, Rank rank) {
+
                NonViralNameParserImpl nvnpi = NonViralNameParserImpl.NewInstance();
-               NonViralName<?> taxonName = null;
+               TaxonName taxonName = null;
                boolean problem = false;
 
-               if(DEBUG){
-                   logger.info("parseScientificName " + state.getDataHolder().getNomenclatureCode().toString());
+               if (logger.isDebugEnabled()){
+                   logger.debug("parseScientificName " + state.getDataHolder().getNomenclatureCode().toString());
                }
 
-               if (state.getDataHolder().getNomenclatureCode().toString().equals("Zoological") || state.getDataHolder().getNomenclatureCode().toString().contains("ICZN")) {
-                   taxonName = nvnpi.parseFullName(scientificName, NomenclaturalCode.ICZN, null);
+               if (state.getDataHolder().getNomenclatureCode() != null && (state.getDataHolder().getNomenclatureCode().toString().equals("Zoological") || state.getDataHolder().getNomenclatureCode().toString().contains("ICZN"))) {
+                   taxonName = (TaxonName)nvnpi.parseFullName(scientificName, NomenclaturalCode.ICZN, rank);
                    if (taxonName.hasProblem()) {
                        problem = true;
                    }
                }
-               else if (state.getDataHolder().getNomenclatureCode().toString().equals("Botanical") || state.getDataHolder().getNomenclatureCode().toString().contains("ICBN")) {
-                   taxonName = nvnpi.parseFullName(scientificName, NomenclaturalCode.ICNAFP, null);
+               else if (state.getDataHolder().getNomenclatureCode() != null && (state.getDataHolder().getNomenclatureCode().toString().equals("Botanical") || state.getDataHolder().getNomenclatureCode().toString().contains("ICBN")  || state.getDataHolder().getNomenclatureCode().toString().contains("ICNAFP"))) {
+                   taxonName = (TaxonName)nvnpi.parseFullName(scientificName, NomenclaturalCode.ICNAFP, rank);
                    if (taxonName.hasProblem()) {
                        problem = true;
                    }
                }
-               else if (state.getDataHolder().getNomenclatureCode().toString().equals("Bacterial") || state.getDataHolder().getNomenclatureCode().toString().contains("ICBN")) {
-                   taxonName = nvnpi.parseFullName(scientificName, NomenclaturalCode.ICNB, null);
+               else if (state.getDataHolder().getNomenclatureCode() != null && (state.getDataHolder().getNomenclatureCode().toString().equals("Bacterial") || state.getDataHolder().getNomenclatureCode().toString().contains("ICBN"))) {
+                   taxonName = (TaxonName)nvnpi.parseFullName(scientificName, NomenclaturalCode.ICNP, rank);
                    if (taxonName.hasProblem()) {
                        problem = true;
                    }
                }
-               else if (state.getDataHolder().getNomenclatureCode().toString().equals("Cultivar") || state.getDataHolder().getNomenclatureCode().toString().contains("ICNCP")) {
-                   taxonName = nvnpi.parseFullName(scientificName, NomenclaturalCode.ICNCP, null);
+               else if (state.getDataHolder().getNomenclatureCode() != null && (state.getDataHolder().getNomenclatureCode().toString().equals("Cultivar") || state.getDataHolder().getNomenclatureCode().toString().contains("ICNCP"))) {
+                   taxonName = (TaxonName)nvnpi.parseFullName(scientificName, NomenclaturalCode.ICNCP, rank);
                    if (taxonName.hasProblem()) {
                        problem = true;
                    }
@@ -273,15 +322,15 @@ public abstract class SpecimenImportBase<CONFIG extends IImportConfigurator, STA
             * @param state
             * @return the corresponding Botanical or Zoological or... name
             */
-           protected NonViralName<?> setTaxonNameByType(
+           protected TaxonName setTaxonNameByType(
                    HashMap<String, String> atomisedMap, String fullName, STATE state) {
                boolean problem = false;
-               if(DEBUG) {
-                   logger.info("settaxonnamebytype " + state.getDataHolder().getNomenclatureCode().toString());
+               if (logger.isDebugEnabled()){
+                   logger.debug("settaxonnamebytype " + state.getDataHolder().getNomenclatureCode().toString());
                }
 
-               if (state.getDataHolder().getNomenclatureCode().equals("Zoological")) {
-                   NonViralName<ZoologicalName> taxonName = ZoologicalName.NewInstance(null);
+               if (state.getDataHolder().getNomenclatureCode().equals("Zoological") || state.getDataHolder().getNomenclatureCode().equals(NomenclaturalCode.ICZN.getUuid())) {
+                   TaxonName taxonName = TaxonNameFactory.NewZoologicalInstance(null);
                    taxonName.setFullTitleCache(fullName, true);
                    taxonName.setGenusOrUninomial(NB(getFromMap(atomisedMap, "Genus")));
                    taxonName.setInfraGenericEpithet(NB(getFromMap(atomisedMap, "SubGenus")));
@@ -339,13 +388,13 @@ public abstract class SpecimenImportBase<CONFIG extends IImportConfigurator, STA
                        return taxonName;
                    }
                }
-               else if (state.getDataHolder().getNomenclatureCode().equals("Botanical")) {
-                   BotanicalName taxonName = (BotanicalName) parseScientificName(fullName, state, state.getReport());
+               else if (state.getDataHolder().getNomenclatureCode().equals("Botanical") || state.getDataHolder().getNomenclatureCode().equals(NomenclaturalCode.ICNAFP.getUuid())) {
+                   TaxonName taxonName = parseScientificName(fullName, state, state.getReport(), null);
                    if (taxonName != null){
                        return taxonName;
                    }
                    else{
-                       taxonName = BotanicalName.NewInstance(null);
+                       taxonName = TaxonNameFactory.NewBotanicalInstance(null);
                    }
                    taxonName.setFullTitleCache(fullName, true);
                    taxonName.setGenusOrUninomial(NB(getFromMap(atomisedMap, "Genus")));
@@ -399,8 +448,8 @@ public abstract class SpecimenImportBase<CONFIG extends IImportConfigurator, STA
                        return taxonName;
                    }
                }
-               else if (state.getDataHolder().getNomenclatureCode().equals("Bacterial")) {
-                   NonViralName<BacterialName> taxonName = BacterialName.NewInstance(null);
+               else if (state.getDataHolder().getNomenclatureCode().equals("Bacterial") || state.getDataHolder().getNomenclatureCode().equals(NomenclaturalCode.ICNP.getUuid())) {
+                   TaxonName taxonName = TaxonNameFactory.NewBacterialInstance(null);
                    taxonName.setFullTitleCache(fullName, true);
                    taxonName.setGenusOrUninomial(getFromMap(atomisedMap, "Genus"));
                    taxonName.setInfraGenericEpithet(NB(getFromMap(atomisedMap, "SubGenus")));
@@ -431,7 +480,7 @@ public abstract class SpecimenImportBase<CONFIG extends IImportConfigurator, STA
                        taxonName.setBasionymAuthorship(team);
                    }
                    if (taxonName.hasProblem()) {
-                       logger.info("pb ICNB");
+                       logger.info("pb ICNP");
                        problem = true;
                    }
                    else {
@@ -439,7 +488,7 @@ public abstract class SpecimenImportBase<CONFIG extends IImportConfigurator, STA
                    }
                }
                else if (state.getDataHolder().getNomenclatureCode().equals("Cultivar")) {
-                   CultivarPlantName taxonName = CultivarPlantName.NewInstance(null);
+                   TaxonName taxonName = TaxonNameFactory.NewCultivarInstance(null);
 
                    if (taxonName.hasProblem()) {
                        logger.info("pb ICNCP");
@@ -453,11 +502,11 @@ public abstract class SpecimenImportBase<CONFIG extends IImportConfigurator, STA
 
                if (problem) {
                    logger.info("Problem im setTaxonNameByType ");
-                   NonViralName<?> taxonName = NonViralName.NewInstance(null);
+                   TaxonName taxonName = TaxonNameFactory.NewNonViralInstance(null);
                    taxonName.setFullTitleCache(fullName, true);
                    return taxonName;
                }
-               NonViralName<?> tn = NonViralName.NewInstance(null);
+               TaxonName tn = TaxonNameFactory.NewNonViralInstance(null);
                return tn;
            }
 
@@ -499,7 +548,7 @@ public abstract class SpecimenImportBase<CONFIG extends IImportConfigurator, STA
 
            /**
             * Very fast and dirty implementation to allow handling of transient objects as described in
-            * https://dev.e-taxonomy.eu/trac/ticket/3726
+            * https://dev.e-taxonomy.eu/redmine/issues/3726
             *
             * Not yet complete.
             *
@@ -507,7 +556,7 @@ public abstract class SpecimenImportBase<CONFIG extends IImportConfigurator, STA
             * @param state
             */
            protected UUID save(CdmBase cdmBase, SpecimenImportStateBase state) {
-               ICdmApplicationConfiguration cdmRepository = state.getConfig().getCdmAppController();
+               ICdmRepository cdmRepository = state.getConfig().getCdmAppController();
                if (cdmRepository == null){
                    cdmRepository = this;
                }
@@ -515,7 +564,9 @@ public abstract class SpecimenImportBase<CONFIG extends IImportConfigurator, STA
                if (cdmBase.isInstanceOf(LanguageString.class)){
                    return cdmRepository.getTermService().saveLanguageData(CdmBase.deproxy(cdmBase, LanguageString.class));
                }else if (cdmBase.isInstanceOf(SpecimenOrObservationBase.class)){
-                   return cdmRepository.getOccurrenceService().saveOrUpdate(CdmBase.deproxy(cdmBase, SpecimenOrObservationBase.class));
+                   SpecimenOrObservationBase specimen = CdmBase.deproxy(cdmBase, SpecimenOrObservationBase.class);
+
+                   return cdmRepository.getOccurrenceService().saveOrUpdate(specimen);
                }else if (cdmBase.isInstanceOf(Reference.class)){
                    return cdmRepository.getReferenceService().saveOrUpdate(CdmBase.deproxy(cdmBase, Reference.class));
                }else if (cdmBase.isInstanceOf(Classification.class)){
@@ -525,12 +576,16 @@ public abstract class SpecimenImportBase<CONFIG extends IImportConfigurator, STA
                }else if (cdmBase.isInstanceOf(Collection.class)){
                    return cdmRepository.getCollectionService().saveOrUpdate(CdmBase.deproxy(cdmBase, Collection.class));
                }else if (cdmBase.isInstanceOf(DescriptionBase.class)){
-                   return cdmRepository.getDescriptionService().saveOrUpdate(CdmBase.deproxy(cdmBase, DescriptionBase.class));
+                   DescriptionBase description = CdmBase.deproxy(cdmBase, DescriptionBase.class);
+
+                   return cdmRepository.getDescriptionService().saveOrUpdate(description);
                }else if (cdmBase.isInstanceOf(TaxonBase.class)){
                    return cdmRepository.getTaxonService().saveOrUpdate(CdmBase.deproxy(cdmBase, TaxonBase.class));
-               }else if (cdmBase.isInstanceOf(TaxonNameBase.class)){
-                   return cdmRepository.getNameService().saveOrUpdate(CdmBase.deproxy(cdmBase, TaxonNameBase.class));
-               }else{
+               }else if (cdmBase.isInstanceOf(TaxonName.class)){
+                   return cdmRepository.getNameService().saveOrUpdate(CdmBase.deproxy(cdmBase, TaxonName.class));
+               }else if (cdmBase.isInstanceOf(TaxonNode.class)){
+                return cdmRepository.getTaxonNodeService().saveOrUpdate(CdmBase.deproxy(cdmBase, TaxonNode.class));
+            }else{
                    throw new IllegalArgumentException("Class not supported in save method: " + CdmBase.deproxy(cdmBase, CdmBase.class).getClass().getSimpleName());
                }
 
@@ -538,18 +593,31 @@ public abstract class SpecimenImportBase<CONFIG extends IImportConfigurator, STA
 
 
            protected SpecimenOrObservationBase findExistingSpecimen(String unitId, SpecimenImportStateBase state){
-               ICdmApplicationConfiguration cdmAppController = state.getConfig().getCdmAppController();
+               ICdmRepository cdmAppController = state.getConfig().getCdmAppController();
                if(cdmAppController==null){
                    cdmAppController = this;
                }
                FindOccurrencesConfigurator config = new FindOccurrencesConfigurator();
                config.setSignificantIdentifier(unitId);
-               Pager<SpecimenOrObservationBase> existingSpecimens = cdmAppController.getOccurrenceService().findByTitle(config);
-               if(!existingSpecimens.getRecords().isEmpty()){
-                   if(existingSpecimens.getRecords().size()==1){
-                       return existingSpecimens.getRecords().iterator().next();
-                   }
+               List<String> propertyPaths = new ArrayList<>();
+               propertyPaths.add("derivedFrom.*");
+               config.setPropertyPaths(propertyPaths);
+               commitTransaction(state.getTx());
+               state.setTx(startTransaction());
+               try{
+                       Pager<SpecimenOrObservationBase> existingSpecimens = cdmAppController.getOccurrenceService().findByTitle(config);
+                       if(!existingSpecimens.getRecords().isEmpty()){
+                           if(existingSpecimens.getRecords().size()==1){
+                               return existingSpecimens.getRecords().iterator().next();
+                           }
+                       }
+
+               }catch(NullPointerException e){
+                       logger.error("searching for existing specimen creates NPE: " + config.getSignificantIdentifier());
+                       e.printStackTrace();
                }
+
+
                return null;
            }
 
@@ -562,7 +630,7 @@ public abstract class SpecimenImportBase<CONFIG extends IImportConfigurator, STA
             * @return DerivedUnitFacade
             */
            protected DerivedUnitFacade getFacade(STATE state) {
-               if(DEBUG) {
+               if (logger.isDebugEnabled()){
                    logger.info("getFacade()");
                }
                SpecimenOrObservationType type = null;
@@ -625,7 +693,7 @@ public abstract class SpecimenImportBase<CONFIG extends IImportConfigurator, STA
                        }catch(Exception e){logger.warn("no institution code in the db");}
                    }
                }
-               if(DEBUG) {
+               if (logger.isDebugEnabled()){
                    if(institution !=null) {
                        logger.info("getinstitution " + institution.toString());
                    }
@@ -701,28 +769,6 @@ public abstract class SpecimenImportBase<CONFIG extends IImportConfigurator, STA
            //    for our new source.
            protected IdentifiableSource getIdentifiableSource(Reference reference, String citationDetail) {
 
-             /*  List<IdentifiableSource> issTmp = getCommonService().list(IdentifiableSource.class, null, null, null, null);
-
-
-               if (reference != null){
-                   try {
-                       for (OriginalSourceBase<?> osb: issTmp){
-                           if (osb.getCitation() != null && osb.getCitation().getTitleCache().equalsIgnoreCase(reference.getTitleCache())){
-                               String osbDetail = osb.getCitationMicroReference();
-                               if ((StringUtils.isBlank(osbDetail) && StringUtils.isBlank(citationDetail))
-                                       || (osbDetail != null && osbDetail.equalsIgnoreCase(citationDetail)) ) {
-//                                 System.out.println("REFERENCE FOUND RETURN EXISTING SOURCE");
-                                   return (IdentifiableSource) osb.clone();
-                               }
-                           }
-                       }
-                   } catch (CloneNotSupportedException e) {
-                       throw new RuntimeException(e);
-                   } catch (Exception e1){
-                       e1.printStackTrace();
-                   }
-               }
-       */
                IdentifiableSource sour = IdentifiableSource.NewInstance(OriginalSourceType.Import,null,null, reference,citationDetail);
                return sour;
            }
@@ -734,7 +780,7 @@ public abstract class SpecimenImportBase<CONFIG extends IImportConfigurator, STA
             * @param state: the ABCD import state
             */
            protected void addParentTaxon(Taxon taxon, STATE state, boolean preferredFlag, Classification classification){
-               NonViralName<?>  nvname = CdmBase.deproxy(taxon.getName(), NonViralName.class);
+               INonViralName  nvname = taxon.getName();
                Rank rank = nvname.getRank();
                Taxon genus =null;
                Taxon subgenus =null;
@@ -744,8 +790,11 @@ public abstract class SpecimenImportBase<CONFIG extends IImportConfigurator, STA
                if(rank!=null){
                    if (rank.isLower(Rank.GENUS() )){
                        String genusOrUninomial = nvname.getGenusOrUninomial();
-                       TaxonNameBase<?,?> taxonName = getOrCreateTaxonName(genusOrUninomial, Rank.GENUS(), preferredFlag, state, -1);
+                       TaxonName taxonName = getOrCreateTaxonName(genusOrUninomial, Rank.GENUS(), preferredFlag, state, -1);
                        genus = getOrCreateTaxonForName(taxonName, state);
+                       if (genus == null){
+                           logger.debug("The genus should not be null " + taxonName);
+                       }
                        if (preferredFlag) {
                            parent = linkParentChildNode(null, genus, classification, state);
                        }
@@ -755,7 +804,7 @@ public abstract class SpecimenImportBase<CONFIG extends IImportConfigurator, STA
                        String prefix = nvname.getGenusOrUninomial();
                        String name = nvname.getInfraGenericEpithet();
                        if (name != null){
-                           TaxonNameBase<?,?> taxonName = getOrCreateTaxonName(prefix+" "+name, Rank.SUBGENUS(), preferredFlag, state, -1);
+                           TaxonName taxonName = getOrCreateTaxonName(prefix+" "+name, Rank.SUBGENUS(), preferredFlag, state, -1);
                            subgenus = getOrCreateTaxonForName(taxonName, state);
                            if (preferredFlag) {
                                parent = linkParentChildNode(genus, subgenus, classification, state);
@@ -767,7 +816,7 @@ public abstract class SpecimenImportBase<CONFIG extends IImportConfigurator, STA
                            String name = nvname.getInfraGenericEpithet();
                            String spe = nvname.getSpecificEpithet();
                            if (spe != null){
-                               TaxonNameBase<?,?> taxonName = getOrCreateTaxonName(prefix+" "+name+" "+spe, Rank.SPECIES(), preferredFlag, state, -1);
+                               TaxonName taxonName = getOrCreateTaxonName(prefix+" "+name+" "+spe, Rank.SPECIES(), preferredFlag, state, -1);
                                species = getOrCreateTaxonForName(taxonName, state);
                                if (preferredFlag) {
                                    parent = linkParentChildNode(subgenus, species, classification, state);
@@ -778,7 +827,7 @@ public abstract class SpecimenImportBase<CONFIG extends IImportConfigurator, STA
                            String prefix = nvname.getGenusOrUninomial();
                            String name = nvname.getSpecificEpithet();
                            if (name != null){
-                               TaxonNameBase<?,?> taxonName = getOrCreateTaxonName(prefix+" "+name, Rank.SPECIES(), preferredFlag, state, -1);
+                               TaxonName taxonName = getOrCreateTaxonName(prefix+" "+name, Rank.SPECIES(), preferredFlag, state, -1);
                                species = getOrCreateTaxonForName(taxonName, state);
                                if (preferredFlag) {
                                    parent = linkParentChildNode(genus, species, classification, state);
@@ -787,14 +836,38 @@ public abstract class SpecimenImportBase<CONFIG extends IImportConfigurator, STA
                        }
                    }
                    if (rank.isLower(Rank.INFRASPECIES())){
-                       TaxonNameBase<?,?> taxonName = getOrCreateTaxonName(nvname.getFullTitleCache(), Rank.SUBSPECIES(), preferredFlag, state, -1);
+                       TaxonName taxonName = getOrCreateTaxonName(nvname.getFullTitleCache(), Rank.SUBSPECIES(), preferredFlag, state, -1);
                        subspecies = getOrCreateTaxonForName(taxonName, state);
                        if (preferredFlag) {
                            parent = linkParentChildNode(species, subspecies, classification, state);
                        }
                    }
+               }else{
+                   //handle cf. and aff. taxa
+                   String genusEpithet = null;
+                   if (nvname.getTitleCache().contains("cf.")){
+                       genusEpithet = nvname.getTitleCache().substring(0, nvname.getTitleCache().indexOf("cf."));
+                   } else if (nvname.getTitleCache().contains("aff.")){
+                       genusEpithet = nvname.getTitleCache().substring(0, nvname.getTitleCache().indexOf("aff."));
+                   }
+                   if (genusEpithet != null){
+                   genusEpithet = genusEpithet.trim();
+                   TaxonName taxonName = null;
+                   if (genusEpithet.contains(" ")){
+                       taxonName = getOrCreateTaxonName(genusEpithet, Rank.SPECIES(), preferredFlag, state, -1);
+                   }else{
+                       taxonName = getOrCreateTaxonName(genusEpithet, Rank.GENUS(), preferredFlag, state, -1);
+                   }
+                   genus = getOrCreateTaxonForName(taxonName, state);
+                    if (genus == null){
+                        logger.debug("The genus should not be null " + taxonName);
+                    }
+                    if (preferredFlag) {
+                        parent = linkParentChildNode(null, genus, classification, state);
+                    }
+                   }
                }
-               if (preferredFlag && parent!=taxon) {
+               if (preferredFlag && parent!=taxon ) {
                    linkParentChildNode(parent, taxon, classification, state);
                }
            }
@@ -809,9 +882,12 @@ public abstract class SpecimenImportBase<CONFIG extends IImportConfigurator, STA
             */
            protected Taxon linkParentChildNode(Taxon parent, Taxon child, Classification classification, STATE state) {
                TaxonNode node =null;
+               List<String> propertyPaths = new ArrayList<>();
+            propertyPaths.add("childNodes");
                if (parent != null) {
-                   parent = (Taxon) getTaxonService().find(parent.getUuid());
-                   child = (Taxon) getTaxonService().find(child.getUuid());
+
+                   parent = (Taxon) getTaxonService().load(parent.getUuid(), propertyPaths);
+                   child = (Taxon) getTaxonService().load(child.getUuid(), propertyPaths);
                    //here we do not have to check if the taxon nodes already exists
                    //this is done by classification.addParentChild()
                    //do not add child node if it already exists
@@ -820,10 +896,13 @@ public abstract class SpecimenImportBase<CONFIG extends IImportConfigurator, STA
                    }
                    else{
                        node = classification.addParentChild(parent, child, state.getRef(), "");
-                       save(classification, state);
+                       save(node, state);
                    }
                }
                else {
+                   if (child == null){
+                       logger.debug("The child should not be null!");
+                   }
                    child = (Taxon) getTaxonService().find(child.getUuid());
                    //do not add child node if it already exists
                    if(hasTaxonNodeInClassification(child, classification)){
@@ -831,7 +910,7 @@ public abstract class SpecimenImportBase<CONFIG extends IImportConfigurator, STA
                    }
                    else{
                        node = classification.addChildTaxon(child, state.getRef(), null);
-                       save(classification, state);
+                       save(node, state);
                    }
                }
                if(node!=null){
@@ -844,45 +923,48 @@ public abstract class SpecimenImportBase<CONFIG extends IImportConfigurator, STA
                return null;
            }
 
-           protected Taxon getOrCreateTaxonForName(TaxonNameBase<?, ?> taxonNameBase, STATE state){
-
-               Set<Taxon> acceptedTaxa = taxonNameBase.getTaxa();
-               if(acceptedTaxa.size()>0){
-                   Taxon firstAcceptedTaxon = acceptedTaxa.iterator().next();
-                   if(acceptedTaxa.size()>1){
-                       String message = "More than one accepted taxon was found for taxon name: "
-                               + taxonNameBase.getTitleCache() + "!\n" + firstAcceptedTaxon + "was chosen for "+state.getDerivedUnitBase();
-                       state.getReport().addInfoMessage(message);
-                       logger.warn(message);
-                   }
-                   else{
-                       return firstAcceptedTaxon;
-                   }
-               }
-               else{
-                   Set<TaxonBase> taxonAndSynonyms = taxonNameBase.getTaxonBases();
-                   for (TaxonBase taxonBase : taxonAndSynonyms) {
-                       if(taxonBase.isInstanceOf(Synonym.class)){
-                           Synonym synonym = HibernateProxyHelper.deproxy(taxonBase, Synonym.class);
-                           Set<Taxon> acceptedTaxaOfSynonym = synonym.getAcceptedTaxa();
-                           if(acceptedTaxaOfSynonym.size()!=1){
-                               String message = "No accepted taxa could be found for taxon name: "
-                                       + taxonNameBase.getTitleCache()
-                                       + "!\nEither it is a pro parte synonym or has no accepted taxa";
-                               state.getReport().addInfoMessage(message);
-                               logger.warn(message);
-                           }
-                           else{
-                               return acceptedTaxaOfSynonym.iterator().next();
-                           }
-                       }
-                   }
+           protected Taxon getOrCreateTaxonForName(TaxonName taxonName, STATE state){
+               if (taxonName != null){
+               Set<Taxon> acceptedTaxa = taxonName.getTaxa();
+               if(acceptedTaxa.size()>0){
+                   Taxon firstAcceptedTaxon = acceptedTaxa.iterator().next();
+                   if(acceptedTaxa.size()>1){
+                       String message = "More than one accepted taxon was found for taxon name: "
+                               + taxonName.getTitleCache() + "!\n" + firstAcceptedTaxon + "was chosen for "+state.getDerivedUnitBase();
+                       state.getReport().addInfoMessage(message);
+                       logger.warn(message);
+                   }
+                   else{
+                       return firstAcceptedTaxon;
+                   }
+               }
+               else{
+                   Set<TaxonBase> taxonAndSynonyms = taxonName.getTaxonBases();
+                   for (TaxonBase taxonBase : taxonAndSynonyms) {
+                       if(taxonBase.isInstanceOf(Synonym.class)){
+                           Synonym synonym = HibernateProxyHelper.deproxy(taxonBase, Synonym.class);
+                           Taxon acceptedTaxonOfSynonym = synonym.getAcceptedTaxon();
+                           if(acceptedTaxonOfSynonym == null){
+                               String message = "No accepted taxon could be found for taxon name: "
+                                       + taxonName.getTitleCache()
+                                       + "!";
+                               state.getReport().addInfoMessage(message);
+                               logger.warn(message);
+                           }
+                           else{
+                               return acceptedTaxonOfSynonym;
+                           }
+                       }
+                   }
+               }
+               Taxon taxon = Taxon.NewInstance(taxonName, state.getRef());
+               save(taxon, state);
+               state.getReport().addTaxon(taxon);
+               logger.info("Created new taxon "+ taxon);
+               return taxon;
                }
-               Taxon taxon = Taxon.NewInstance(taxonNameBase, state.getRef());
-               save(taxon, state);
-               state.getReport().addTaxon(taxon);
-               logger.info("Created new taxon "+ taxon);
-               return taxon;
+               return null;
+
            }
 
            private boolean hasTaxonNodeInClassification(Taxon taxon, Classification classification){
@@ -913,6 +995,7 @@ public abstract class SpecimenImportBase<CONFIG extends IImportConfigurator, STA
                    if (config.getNomenclaturalCode() != null){
                        if (config.getNomenclaturalCode() != null){
                            state.getDataHolder().setNomenclatureCode(config.getNomenclaturalCode().toString());
+
                        }
                    }
                }
@@ -922,11 +1005,14 @@ public abstract class SpecimenImportBase<CONFIG extends IImportConfigurator, STA
                    scientificName = identification.getScientificName().replaceAll(" et ", " & ");
 
                    String preferred = identification.getPreferred();
-                   if (preferred.equals("1") || preferred.toLowerCase().indexOf("true") != -1 || state.getDataHolder().getIdentificationList().size()==1) {
-                       preferredFlag = true;
-                   }
-                   else {
-                       preferredFlag = false;
+                   preferredFlag = false;
+                   if (preferred != null || state.getDataHolder().getIdentificationList().size()==1){
+                       if (state.getDataHolder().getIdentificationList().size()==1){
+                           preferredFlag = true;
+                       }else if (preferred != null && (preferred.equals("1") || preferred.toLowerCase().indexOf("true") != -1) ) {
+                       preferredFlag = true;
+                   }
+
                    }
                    if (identification.getCode() != null){
                    if (identification.getCode().indexOf(':') != -1) {
@@ -936,10 +1022,10 @@ public abstract class SpecimenImportBase<CONFIG extends IImportConfigurator, STA
                        state.getDataHolder().setNomenclatureCode(identification.getCode());
                    }
                    }
-                   TaxonNameBase<?,?> taxonName = getOrCreateTaxonName(scientificName, null, preferredFlag, state, i);
+                   TaxonName taxonName = getOrCreateTaxonName(scientificName, null, preferredFlag, state, i);
                    Taxon taxon = getOrCreateTaxonForName(taxonName, state);
                    addTaxonNode(taxon, state,preferredFlag);
-                   linkDeterminationEvent(state, taxon, preferredFlag, derivedUnitFacade);
+                   linkDeterminationEvent(state, taxon, preferredFlag, derivedUnitFacade, identification.getIdentifier(), identification.getDate(), identification.getModifier());
                }
            }
 
@@ -948,7 +1034,7 @@ public abstract class SpecimenImportBase<CONFIG extends IImportConfigurator, STA
             * @param state : the ABCD import state
             */
            protected void addTaxonNode(Taxon taxon, STATE state, boolean preferredFlag) {
-               SpecimenImportConfiguratorBase config = state.getConfig();
+               SpecimenImportConfiguratorBase<?,?,?> config = state.getConfig();
                logger.info("link taxon to a taxonNode "+taxon.getTitleCache());
                //only add nodes if not already existing in current classification or default classification
 
@@ -957,38 +1043,46 @@ public abstract class SpecimenImportBase<CONFIG extends IImportConfigurator, STA
                if (!existsInClassification(taxon,state.getClassification(), state)){
                    if(config.isMoveNewTaxaToDefaultClassification()){
                        //check if node exists in default classification
-                       if (!existsInClassification(taxon, state.getDefaultClassification(), state)){
-                           addParentTaxon(taxon, state, preferredFlag, state.getDefaultClassification());
+                       if (!existsInClassification(taxon, state.getDefaultClassification(true), state)){
+                           addParentTaxon(taxon, state, preferredFlag, state.getDefaultClassification(true));
                        }
-                   }
-                   else {
+                   }else{
                        //add non-existing taxon to current classification
                        addParentTaxon(taxon, state, preferredFlag, state.getClassification());
                    }
+
                }
            }
 
 
            private boolean existsInClassification(Taxon taxon, Classification classification, STATE state){
                boolean exist = false;
-               ICdmApplicationConfiguration cdmAppController = state.getConfig().getCdmAppController();
+               ICdmRepository cdmAppController = state.getConfig().getCdmAppController();
             if(cdmAppController==null){
                 cdmAppController = this;
             }
             if (classification != null){
-               List<UuidAndTitleCache<TaxonNode>> uuidAndTitleCacheOfAllTaxa = cdmAppController.getClassificationService().getTaxonNodeUuidAndTitleCacheOfAcceptedTaxaByClassification(classification.getUuid());
-               if (uuidAndTitleCacheOfAllTaxa != null){
-                       for (UuidAndTitleCache p : uuidAndTitleCacheOfAllTaxa){
-                           try{
-                               if(p.getTitleCache().equals(taxon.getTitleCache())) {
-                                   exist = true;
-                               }
-                           }
-                           catch(Exception e){
-                               logger.warn("TaxonNode doesn't seem to have a taxon");
-                           }
-                       }
-               }
+                if (!taxon.getTaxonNodes().isEmpty()){
+                    for (TaxonNode node:taxon.getTaxonNodes()){
+                        if (node.getClassification().equals(classification)){
+                            return true;
+                        }
+                    }
+                }
+// we do not need this because we already searched for taxa in db in the previous steps
+//             List<UuidAndTitleCache<TaxonNode>> uuidAndTitleCacheOfAllTaxa = cdmAppController.getClassificationService().getTaxonNodeUuidAndTitleCacheOfAcceptedTaxaByClassification(classification.getUuid());
+//             if (uuidAndTitleCacheOfAllTaxa != null){
+//                     for (UuidAndTitleCache p : uuidAndTitleCacheOfAllTaxa){
+//                         try{
+//                             if(p.getTitleCache().equals(taxon.getTitleCache())) {
+//                                 exist = true;
+//                             }
+//                         }
+//                         catch(Exception e){
+//                             logger.warn("TaxonNode doesn't seem to have a taxon");
+//                         }
+//                     }
+//             }
             }
                return exist;
            }
@@ -1001,35 +1095,53 @@ public abstract class SpecimenImportBase<CONFIG extends IImportConfigurator, STA
             * @param derivedFacade : the derived Unit Facade
             */
            @SuppressWarnings("rawtypes")
-        protected void linkDeterminationEvent(STATE state, Taxon taxon, boolean preferredFlag,  DerivedUnitFacade derivedFacade) {
+        protected void linkDeterminationEvent(STATE state, Taxon taxon, boolean preferredFlag,  DerivedUnitFacade derivedFacade, String identifierStr, String dateStr, String modifier) {
                SpecimenImportConfiguratorBase config = state.getConfig();
-               if(DEBUG){
+               if (logger.isDebugEnabled()){
                    logger.info("start linkdetermination with taxon:" + taxon.getUuid()+", "+taxon);
                }
 
                DeterminationEvent determinationEvent = DeterminationEvent.NewInstance();
+               //determinationEvent.setTaxon(taxon);
                determinationEvent.setTaxonName(taxon.getName());
                determinationEvent.setPreferredFlag(preferredFlag);
 
+
                determinationEvent.setIdentifiedUnit(state.getDerivedUnitBase());
+               if (state.getPersonStore().get(identifierStr) != null){
+                   determinationEvent.setActor((AgentBase)state.getPersonStore().get(identifierStr));
+               } else if (identifierStr != null){
+                   Person identifier = Person.NewTitledInstance(identifierStr);
+                   determinationEvent.setActor(identifier);
+               }
+               if (dateStr != null){
+                   determinationEvent.setTimeperiod(TimePeriodParser.parseString(dateStr));
+               }
+               if (modifier != null){
+                   if (modifier.equals("cf.")){
+                       determinationEvent.setModifier(DefinedTerm.DETERMINATION_MODIFIER_CONFER());
+                   }else if (modifier.equals("aff.")){
+                       determinationEvent.setModifier(DefinedTerm.DETERMINATION_MODIFIER_AFFINIS());
+                   }
+               }
                state.getDerivedUnitBase().addDetermination(determinationEvent);
 
-               if(DEBUG){
-                   logger.info("NB TYPES INFO: "+ state.getDataHolder().getStatusList().size());
+               if (logger.isDebugEnabled()){
+                   logger.debug("NB TYPES INFO: "+ state.getDataHolder().getStatusList().size());
                }
                for (SpecimenTypeDesignationStatus specimenTypeDesignationstatus : state.getDataHolder().getStatusList()) {
                    if (specimenTypeDesignationstatus != null) {
-                       if(DEBUG){
-                           logger.info("specimenTypeDesignationstatus :"+ specimenTypeDesignationstatus);
+                       if (logger.isDebugEnabled()){
+                           logger.debug("specimenTypeDesignationstatus :"+ specimenTypeDesignationstatus);
                        }
 
-                       ICdmApplicationConfiguration cdmAppController = config.getCdmAppController();
+                       ICdmRepository cdmAppController = config.getCdmAppController();
                        if(cdmAppController == null){
                            cdmAppController = this;
                        }
-                       specimenTypeDesignationstatus = (SpecimenTypeDesignationStatus) cdmAppController.getTermService().find(specimenTypeDesignationstatus.getUuid());
+                       specimenTypeDesignationstatus = HibernateProxyHelper.deproxy(cdmAppController.getTermService().find(specimenTypeDesignationstatus.getUuid()), SpecimenTypeDesignationStatus.class);
                        //Designation
-                       TaxonNameBase<?,?> name = taxon.getName();
+                       TaxonName name = taxon.getName();
                        SpecimenTypeDesignation designation = SpecimenTypeDesignation.NewInstance();
 
                        designation.setTypeStatus(specimenTypeDesignationstatus);
@@ -1037,6 +1149,7 @@ public abstract class SpecimenImportBase<CONFIG extends IImportConfigurator, STA
                        name.addTypeDesignation(designation, true);
                    }
                }
+               save(state.getDerivedUnitBase(), state);
 
                for (String[] fullReference : state.getDataHolder().getReferenceList()) {
 
@@ -1044,7 +1157,7 @@ public abstract class SpecimenImportBase<CONFIG extends IImportConfigurator, STA
                    String strReference=fullReference[0];
                    String citationDetail = fullReference[1];
                    String citationURL = fullReference[2];
-                   List<Reference> references = getReferenceService().listByTitle(Reference.class, "strReference", MatchMode.EXACT, null, null, null, null, null);
+                   List<Reference> references = getReferenceService().listByTitleWithRestrictions(Reference.class, "strReference", MatchMode.EXACT, null, null, null, null, null);
 
                    if (!references.isEmpty()){
                        Reference reference = null;
@@ -1066,8 +1179,8 @@ public abstract class SpecimenImportBase<CONFIG extends IImportConfigurator, STA
 
                if (config.isAddIndividualsAssociationsSuchAsSpecimenAndObservations() && preferredFlag) {
                    //do not add IndividualsAssociation to non-preferred taxa
-                   if(DEBUG){
-                       logger.info("isDoCreateIndividualsAssociations");
+                   if (logger.isDebugEnabled()){
+                       logger.debug("isDoCreateIndividualsAssociations");
                    }
 
                    makeIndividualsAssociation(state, taxon, determinationEvent);
@@ -1083,52 +1196,45 @@ public abstract class SpecimenImportBase<CONFIG extends IImportConfigurator, STA
             * @param determinationEvent:the determinationevent
             */
            protected void makeIndividualsAssociation(STATE state, Taxon taxon, DeterminationEvent determinationEvent) {
-               SpecimenImportConfiguratorBase config = state.getConfig();
+               SpecimenImportConfiguratorBase<?,?,?> config = state.getConfig();
                SpecimenUserInteraction sui = config.getSpecimenUserInteraction();
 
-               if (DEBUG) {
+               if (logger.isDebugEnabled()){
                    logger.info("MAKE INDIVIDUALS ASSOCIATION");
                }
 
                TaxonDescription taxonDescription = null;
                Set<TaxonDescription> descriptions= taxon.getDescriptions();
-//             if (((Abcd206ImportConfigurator) state.getConfig()).isInteractWithUser()){
-//                 if(!state.isDescriptionGroupSet()){
-//                     taxonDescription = sui.askForDescriptionGroup(descriptions);
-//                     state.setDescriptionGroup(taxonDescription);
-//                     state.setDescriptionGroupSet(true);
-//                 }else{
-//                     taxonDescription=state.getDescriptionGroup();
-//                 }
-//             } else {
-                   for (TaxonDescription description : descriptions){
-                       Set<IdentifiableSource> sources =  new HashSet<>();
-                       sources.addAll(description.getTaxon().getSources());
-                       sources.addAll(description.getSources());
-                       for (IdentifiableSource source:sources){
-                           if(state.getRef().equals(source.getCitation())) {
-                               taxonDescription = description;
-                           }
-                       }
-                   }
-             //  }
-               if (taxonDescription == null){
+               if (state.getIndividualsAssociationDescriptionPerTaxon(taxon.getUuid()) != null){
+                   taxonDescription = state.getIndividualsAssociationDescriptionPerTaxon(taxon.getUuid());
+               }
+              if (taxonDescription == null && !descriptions.isEmpty() && state.getConfig().isReuseExistingDescriptiveGroups()){
+                  for (TaxonDescription desc: descriptions){
+                      if (desc.getTypes().contains(DescriptionType.INDIVIDUALS_ASSOCIATION)){
+                          taxonDescription = desc;
+                          break;
+                      }
+                  }
+              }
+
+              if (taxonDescription == null){
                    taxonDescription = TaxonDescription.NewInstance(taxon, false);
+                   taxonDescription.setTypes(EnumSet.of(DescriptionType.INDIVIDUALS_ASSOCIATION));
                    if(sourceNotLinkedToElement(taxonDescription,state.getRef(),null)) {
                        taxonDescription.addSource(OriginalSourceType.Import, null, null, state.getRef(), null);
                    }
-                   state.setDescriptionGroup(taxonDescription);
+                   state.setIndividualsAssociationDescriptionPerTaxon(taxonDescription);
                    taxon.addDescription(taxonDescription);
                }
 
                //PREPARE REFERENCE QUESTIONS
 
-               Map<String,OriginalSourceBase<?>> sourceMap = new HashMap<String, OriginalSourceBase<?>>();
+               Map<String,OriginalSourceBase> sourceMap = new HashMap<>();
 
                List<IdentifiableSource> issTmp = new ArrayList<>();//getCommonService().list(IdentifiableSource.class, null, null, null, null);
                List<DescriptionElementSource> issTmp2 = new ArrayList<>();//getCommonService().list(DescriptionElementSource.class, null, null, null, null);
 
-               Set<OriginalSourceBase> osbSet = new HashSet<OriginalSourceBase>();
+               Set<OriginalSourceBase> osbSet = new HashSet<>();
                if(issTmp2!=null) {
                    osbSet.addAll(issTmp2);
                }
@@ -1139,119 +1245,34 @@ public abstract class SpecimenImportBase<CONFIG extends IImportConfigurator, STA
 
                addToSourceMap(sourceMap, osbSet);
 
-//             if (((Abcd206ImportConfigurator) state.getConfig()).isInteractWithUser()){
-//                 List<OriginalSourceBase<?>> res = null;
-//                 if(!state.isDescriptionSourcesSet()){
-//                     res = sui.askForSource(sourceMap, "the description group ("+taxon+")",
-//                             "The current reference is "+state.getRef().getTitleCache(),getReferenceService(), state.getDataHolder().docSources);
-//                     state.setDescriptionRefs(res);
-//                     state.setDescriptionSourcesSet(true);
-//                 }
-//                 else{
-//                     res=state.getDescriptionRefs();
-//                 }
-//                 if(res !=null) {
-//                     for (OriginalSourceBase<?> sour:res){
-//                         if(sour.isInstanceOf(IdentifiableSource.class)){
-//                             try {
-//                                 if(sourceNotLinkedToElement(taxonDescription,sour)) {
-//                                     taxonDescription.addSource((IdentifiableSource)sour.clone());
-//                                 }
-//                             } catch (CloneNotSupportedException e) {
-//                                 logger.warn("no cloning?");
-//                             }
-//                         }else{
-//                             if(sourceNotLinkedToElement(taxonDescription,sour)) {
-//                                 taxonDescription.addSource(OriginalSourceType.Import,null, null, sour.getCitation(),sour.getCitationMicroReference());
-//                             }
-//                         }
-//                     }
-//                 }
-//             }
-//             else {
-                   if(sourceNotLinkedToElement(taxonDescription,state.getRef(),null)) {
-                       taxonDescription.addSource(OriginalSourceType.Import,null, null, state.getRef(), null);
-                   }
-//             }
-               state.setDescriptionGroup(taxonDescription);
+
+            if(sourceNotLinkedToElement(taxonDescription,state.getRef(),null)) {
+                taxonDescription.addSource(OriginalSourceType.Import,null, null, state.getRef(), null);
+            }
+
+               state.setIndividualsAssociationDescriptionPerTaxon(taxonDescription);
 
                IndividualsAssociation indAssociation = IndividualsAssociation.NewInstance();
                Feature feature = makeFeature(state.getDerivedUnitBase());
                indAssociation.setAssociatedSpecimenOrObservation(state.getDerivedUnitBase());
                indAssociation.setFeature(feature);
-//
-//             if (((Abcd206ImportConfigurator) state.getConfig()).isInteractWithUser()){
-//                 sourceMap = new HashMap<String, OriginalSourceBase<?>>();
-//
-//                 issTmp = getCommonService().list(IdentifiableSource.class, null, null, null, null);
-//                 issTmp2 = getCommonService().list(DescriptionElementSource.class, null, null, null, null);
-//
-//                 osbSet = new HashSet<OriginalSourceBase>();
-//                 if(issTmp2!=null) {
-//                     osbSet.addAll(issTmp2);
-//                 }
-//                 if(issTmp!=null) {
-//                     osbSet.addAll(issTmp);
-//                 }
-//
-//
-//                 addToSourceMap(sourceMap, osbSet);
-//
-//                 List<OriginalSourceBase<?>> sources =null;
-//                 if(!state.isAssociationSourcesSet()) {
-//                     sources = sui.askForSource(sourceMap,  "descriptive element (association) ",taxon.toString(),
-//                             getReferenceService(),state.getDataHolder().getDocSources());
-//                     state.setAssociationRefs(sources);
-//                     state.setAssociationSourcesSet(true);
-//                 }
-//                 else{
-//                     sources=state.getAssociationRefs();
-//                 }
-//                 if(sources !=null) {
-//                     for (OriginalSourceBase<?> source: sources) {
-//                         if(source !=null) {
-//                             if(source.isInstanceOf(DescriptionElementSource.class)){
-//                                 try {
-//                                     if(sourceNotLinkedToElement(indAssociation,source)) {
-//                                         indAssociation.addSource((DescriptionElementSource)source.clone());
-//                                     }
-//                                 } catch (CloneNotSupportedException e) {
-//                                     logger.warn("clone forbidden?");
-//                                 }
-//                             }else{
-//                                 if(sourceNotLinkedToElement(indAssociation,source)) {
-//                                     indAssociation.addSource(OriginalSourceType.Import,null, null, source.getCitation(),source.getCitationMicroReference());
-//                                 }
-//                                 try {
-//                                     if(sourceNotLinkedToElement(state.getDerivedUnitBase(), source)) {
-//                                         state.getDerivedUnitBase().addSource((IdentifiableSource) source.clone());
-//                                     }
-//                                 } catch (CloneNotSupportedException e) {
-//                                     // TODO Auto-generated catch block
-//                                     e.printStackTrace();
-//                                 }
-//                             }
-//
-//                         }
-//                     }
-//                 }
-//             }else {
-                   if(sourceNotLinkedToElement(indAssociation,state.getRef(),null)) {
-                       indAssociation.addSource(OriginalSourceType.Import,null, null, state.getRef(), null);
-                   }
-                   if(sourceNotLinkedToElement(state.getDerivedUnitBase(), state.getRef(),null)) {
-                       state.getDerivedUnitBase().addSource(OriginalSourceType.Import,null, null, state.getRef(), null);
-                   }
-                   for (Reference citation : determinationEvent.getReferences()) {
-                       if(sourceNotLinkedToElement(indAssociation,citation,null))
-                       {
-                           indAssociation.addSource(DescriptionElementSource.NewInstance(OriginalSourceType.Import, null, null, citation, null));
-                       }
-                       if(sourceNotLinkedToElement(state.getDerivedUnitBase(), state.getRef(),null)) {
-                           state.getDerivedUnitBase().addSource(OriginalSourceType.Import,null, null, state.getRef(), null);
-                       }
-                   }
-          //     }
+
+            if(sourceNotLinkedToElement(indAssociation,state.getImportReference(state.getActualAccessPoint()),null)) {
+                indAssociation.addSource(OriginalSourceType.Import,null, null, state.getImportReference(state.getActualAccessPoint()), null);
+            }
+            if(sourceNotLinkedToElement(state.getDerivedUnitBase(), state.getImportReference(state.getActualAccessPoint()),null)) {
+                state.getDerivedUnitBase().addSource(OriginalSourceType.Import,null, null, state.getImportReference(state.getActualAccessPoint()), null);
+            }
+            for (Reference citation : determinationEvent.getReferences()) {
+                if(sourceNotLinkedToElement(indAssociation,citation,null))
+                {
+                    indAssociation.addSource(DescriptionElementSource.NewInstance(OriginalSourceType.Import, null, null, citation, null));
+                }
+                if(sourceNotLinkedToElement(state.getDerivedUnitBase(), state.getImportReference(state.getActualAccessPoint()),null)) {
+                    state.getDerivedUnitBase().addSource(OriginalSourceType.Import,null, null, state.getImportReference(state.getActualAccessPoint()), null);
+                }
+            }
+
 
                taxonDescription.addElement(indAssociation);
 
@@ -1309,7 +1330,7 @@ public abstract class SpecimenImportBase<CONFIG extends IImportConfigurator, STA
                return true;
            }
 
-           private <T extends OriginalSourceBase<?>> boolean  sourceNotLinkedToElement(ISourceable<T> sourcable, Reference reference, String microReference) {
+           private <T extends OriginalSourceBase> boolean  sourceNotLinkedToElement(ISourceable<T> sourcable, Reference reference, String microReference) {
                Set<T> linkedSources = sourcable.getSources();
                for (T is:linkedSources){
                    Reference unitReference = is.getCitation();
@@ -1367,7 +1388,7 @@ public abstract class SpecimenImportBase<CONFIG extends IImportConfigurator, STA
                    //            return getFeature("Specimen or observation");
                }else{
                    String message = "Unhandled record basis '%s' for defining individuals association feature type. Use default.";
-                   logger.warn(String.format(message, type.getMessage()));
+                   logger.warn(String.format(message, type.getLabel()));
                    return Feature.OBSERVATION();
                    //            return getFeature("Specimen or observation");
 
@@ -1379,8 +1400,8 @@ public abstract class SpecimenImportBase<CONFIG extends IImportConfigurator, STA
             * @param sourceMap
             * @param osbSet
             */
-           protected void addToSourceMap(Map<String, OriginalSourceBase<?>> sourceMap, Set<OriginalSourceBase> osbSet) {
-               for( OriginalSourceBase<?> osb:osbSet) {
+           protected void addToSourceMap(Map<String, OriginalSourceBase> sourceMap, Set<OriginalSourceBase> osbSet) {
+               for( OriginalSourceBase osb:osbSet) {
                    if(osb.getCitation()!=null && osb.getCitationMicroReference() !=null  && !osb.getCitationMicroReference().isEmpty()) {
                        try{
                            sourceMap.put(osb.getCitation().getTitleCache()+ "---"+osb.getCitationMicroReference(),osb);
@@ -1393,4 +1414,7 @@ public abstract class SpecimenImportBase<CONFIG extends IImportConfigurator, STA
                }
            }
 
-}
\ No newline at end of file
+
+
+
+}