cleanup
[cdmlib.git] / cdmlib-io / src / main / java / eu / etaxonomy / cdm / io / cdmLight / CdmLightClassificationExport.java
index 77165f67c812864e100731ef29c07bfdd5c9cd4a..2f19184b9024b03b450a73f7d2fb501ca2c07dd0 100755 (executable)
@@ -13,6 +13,7 @@ import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.Comparator;
+import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Iterator;
 import java.util.List;
@@ -24,15 +25,18 @@ import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 
-import eu.etaxonomy.cdm.api.service.dto.CondensedDistribution;
+import eu.etaxonomy.cdm.api.service.geo.IDistributionService;
+import eu.etaxonomy.cdm.api.service.name.TypeDesignationSetComparator;
+import eu.etaxonomy.cdm.api.service.name.TypeDesignationSetContainer;
 import eu.etaxonomy.cdm.api.service.name.TypeDesignationSetFormatter;
-import eu.etaxonomy.cdm.api.service.name.TypeDesignationSetManager;
 import eu.etaxonomy.cdm.common.CdmUtils;
 import eu.etaxonomy.cdm.common.monitor.IProgressMonitor;
 import eu.etaxonomy.cdm.compare.name.TypeComparator;
 import eu.etaxonomy.cdm.compare.taxon.HomotypicGroupTaxonComparator;
-import eu.etaxonomy.cdm.ext.geo.IEditGeoService;
 import eu.etaxonomy.cdm.filter.TaxonNodeFilter;
+import eu.etaxonomy.cdm.format.description.CategoricalDataFormatter;
+import eu.etaxonomy.cdm.format.description.QuantitativeDataFormatter;
+import eu.etaxonomy.cdm.format.description.distribution.CondensedDistribution;
 import eu.etaxonomy.cdm.format.reference.OriginalSourceFormatter;
 import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
 import eu.etaxonomy.cdm.io.common.CdmExportBase;
@@ -54,6 +58,7 @@ import eu.etaxonomy.cdm.model.common.IdentifiableSource;
 import eu.etaxonomy.cdm.model.common.Identifier;
 import eu.etaxonomy.cdm.model.common.Language;
 import eu.etaxonomy.cdm.model.common.LanguageString;
+import eu.etaxonomy.cdm.model.description.CategoricalData;
 import eu.etaxonomy.cdm.model.description.CommonTaxonName;
 import eu.etaxonomy.cdm.model.description.DescriptionBase;
 import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
@@ -61,6 +66,7 @@ import eu.etaxonomy.cdm.model.description.DescriptionElementSource;
 import eu.etaxonomy.cdm.model.description.Distribution;
 import eu.etaxonomy.cdm.model.description.Feature;
 import eu.etaxonomy.cdm.model.description.IndividualsAssociation;
+import eu.etaxonomy.cdm.model.description.QuantitativeData;
 import eu.etaxonomy.cdm.model.description.TaxonDescription;
 import eu.etaxonomy.cdm.model.description.TaxonInteraction;
 import eu.etaxonomy.cdm.model.description.TaxonNameDescription;
@@ -96,7 +102,8 @@ 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.TaxonRelationship;
-import eu.etaxonomy.cdm.model.term.DefinedTerm;
+import eu.etaxonomy.cdm.model.term.IdentifierType;
+import eu.etaxonomy.cdm.model.term.TermTree;
 import eu.etaxonomy.cdm.persistence.dto.TaxonNodeDto;
 import eu.etaxonomy.cdm.persistence.dto.TaxonNodeDtoByRankAndNameComparator;
 import eu.etaxonomy.cdm.strategy.cache.HTMLTagRules;
@@ -105,6 +112,8 @@ import eu.etaxonomy.cdm.strategy.cache.TaggedText;
 import eu.etaxonomy.cdm.strategy.exceptions.UnknownCdmTypeException;
 
 /**
+ * The export exporting a classification or a taxonomic subtree into CDM light.
+ *
  * @author k.luther
  * @since 15.03.2017
  */
@@ -113,18 +122,11 @@ public class CdmLightClassificationExport
         extends CdmExportBase<CdmLightExportConfigurator, CdmLightExportState, IExportTransformer, File>{
 
     private static final long serialVersionUID = 2518643632756927053L;
-    private static final String STD_TEAM_CONCATINATION = ", ";
-    private static final String FINAL_TEAM_CONCATINATION = " & ";
-
-    private static final String IPNI_NAME_IDENTIFIER = "Ipni Name Identifier";
-    private static final String TROPICOS_NAME_IDENTIFIER = "Tropicos Name Identifier";
-    private static final String WFO_NAME_IDENTIFIER = "WFO Name Identifier";
 
     @Autowired
-    IEditGeoService geoService;
+    private IDistributionService distributionService;
 
     public CdmLightClassificationExport() {
-        super();
         this.ioName = this.getClass().getSimpleName();
     }
 
@@ -164,7 +166,7 @@ public class CdmLightClassificationExport
             if (state.getRootId() != null) {
                 List<TaxonNodeDto> childrenOfRoot = state.getNodeChildrenMap().get(state.getRootId());
 
-                Comparator<TaxonNodeDto> comp = state.getConfig().getComparator();
+                Comparator<TaxonNodeDto> comp = state.getConfig().getTaxonNodeComparator();
                 if (comp == null) {
                     comp = new TaxonNodeDtoByRankAndNameComparator();
                 }
@@ -199,10 +201,6 @@ public class CdmLightClassificationExport
 
     private void setOrderIndex(CdmLightExportState state, OrderHelper order) {
 
-        // String sortIndex = StringUtils.isBlank(sort_index)?
-        // String.valueOf(order.getOrderIndex()): sort_index+ "_"
-        // +String.valueOf(order.getOrderIndex());
-
         if (order.getTaxonUuid() != null
                 && state.getProcessor().hasRecord(CdmLightExportTable.TAXON, order.getTaxonUuid().toString())) {
             String[] csvLine = state.getProcessor().getRecord(CdmLightExportTable.TAXON,
@@ -225,7 +223,7 @@ public class CdmLightClassificationExport
         if (children == null) {
             return null;
         }
-        Comparator<TaxonNodeDto> comp = state.getConfig().getComparator();
+        Comparator<TaxonNodeDto> comp = state.getConfig().getTaxonNodeComparator();
         if (comp == null) {
             comp = new TaxonNodeDtoByRankAndNameComparator();
         }
@@ -266,7 +264,6 @@ public class CdmLightClassificationExport
                     state.getNodeChildrenMap().put(root.getUuid(), childNodes);
 
                     // add root to node map
-
                 }
                 TaxonNodeDto rootDto = new TaxonNodeDto(root);
                 UUID parentUuid = root.getParent() != null ? root.getParent().getUuid()
@@ -278,11 +275,9 @@ public class CdmLightClassificationExport
                     List<TaxonNodeDto> rootList = new ArrayList<>();
                     rootList.add(rootDto);
                     state.getNodeChildrenMap().put(parentUuid, rootList);
-
                 }
                 if (root.hasTaxon()) {
                     handleTaxon(state, root);
-
                 }
             } catch (Exception e) {
                 state.getResult().addException(e, "An unexpected error occurred when handling taxonNode "
@@ -304,11 +299,14 @@ public class CdmLightClassificationExport
                         "handleTaxon");
                 state.getResult().setState(ExportResultState.INCOMPLETE_WITH_ERROR);
             } else {
-                Taxon taxon = HibernateProxyHelper.deproxy(taxonNode.getTaxon(), Taxon.class);
+                Taxon taxon = CdmBase.deproxy(taxonNode.getTaxon());
 
                 try {
+                    //accepted name
                     TaxonName name = taxon.getName();
                     handleName(state, name, taxon, true);
+
+                    //homotypic group / synonyms
                     HomotypicalGroup homotypicGroup = taxon.getHomotypicGroup();
                     int index = 0;
                     int homotypicGroupIndex = 0;
@@ -328,18 +326,20 @@ public class CdmLightClassificationExport
                         homotypicGroupIndex++;
                     }
 
+                    //pro parte synonyms
                     index = 0;
                     for (Taxon tax : taxon.getAllProParteSynonyms()) {
                         handleProPartePartialMisapplied(state, tax, taxon, true, false, index);
                         index++;
                     }
 
-
+                    //misapplications
                     for (Taxon tax : taxon.getAllMisappliedNames()) {
                         handleProPartePartialMisapplied(state, tax, taxon, false, true, index);
                         index++;
                     }
 
+                    //taxon table
                     CdmLightExportTable table = CdmLightExportTable.TAXON;
                     String[] csvLine = new String[table.getSize()];
 
@@ -347,6 +347,8 @@ public class CdmLightClassificationExport
                     csvLine[table.getIndex(CdmLightExportTable.NAME_FK)] = getId(state, name);
                     Taxon parent = (taxonNode.getParent() == null) ? null : taxonNode.getParent().getTaxon();
                     csvLine[table.getIndex(CdmLightExportTable.PARENT_FK)] = getId(state, parent);
+
+                    //secundum reference
                     csvLine[table.getIndex(CdmLightExportTable.SEC_REFERENCE_FK)] = getId(state, taxon.getSec());
                     if (taxon.getSec() != null && taxon.getSec().getDatePublished() != null
                             && taxon.getSec().getDatePublished().getFreeText() != null) {
@@ -362,14 +364,36 @@ public class CdmLightClassificationExport
                             handleReference(state, taxon.getSec());
                         }
                     }
+                    //secundum subname
+                    TaxonName subName = taxon.getSecSource() == null? null : taxon.getSecSource().getNameUsedInSource();
+                    if (subName != null) {
+                        csvLine[table.getIndex(CdmLightExportTable.SEC_SUBNAME_FK)] = getId(state, subName);
+                        if (!state.getNameStore().containsKey((subName.getId()))) {
+                            handleName(state, subName, null);
+                        }
+                        csvLine[table.getIndex(CdmLightExportTable.SEC_SUBNAME)] = subName.getNameCache();
+                        csvLine[table.getIndex(CdmLightExportTable.SEC_SUBNAME_AUTHORS)] = subName.getAuthorshipCache();
+                    }
+
                     csvLine[table.getIndex(CdmLightExportTable.APPENDED_PHRASE)] = taxon.getAppendedPhrase();
                     csvLine[table.getIndex(CdmLightExportTable.CLASSIFICATION_ID)] = getId(state,
                             taxonNode.getClassification());
                     csvLine[table.getIndex(CdmLightExportTable.CLASSIFICATION_TITLE)] = taxonNode.getClassification()
                             .getTitleCache();
-
                     csvLine[table.getIndex(CdmLightExportTable.PUBLISHED)] = taxon.isPublish() ? "1" : "0";
+
+                    //taxon node
+                    csvLine[table.getIndex(CdmLightExportTable.INCLUDED)] = taxonNode.getStatus() == null  ? "1" : "0";
+                    csvLine[table.getIndex(CdmLightExportTable.DOUBTFUL)] = taxonNode.isDoubtful() ? "1" : "0";
+                    csvLine[table.getIndex(CdmLightExportTable.UNPLACED)] = taxonNode.isUnplaced() ? "1" : "0";
                     csvLine[table.getIndex(CdmLightExportTable.EXCLUDED)] = taxonNode.isExcluded() ? "1" : "0";
+                    csvLine[table.getIndex(CdmLightExportTable.EXCLUDED_EXACT)] = taxonNode.isExcludedExact() ? "1" : "0";
+                    csvLine[table.getIndex(CdmLightExportTable.EXCLUDED_GEO)] = taxonNode.isGeographicallyExcluded() ? "1" : "0";
+                    csvLine[table.getIndex(CdmLightExportTable.EXCLUDED_TAX)] = taxonNode.isTaxonomicallyExcluded() ? "1" : "0";
+                    csvLine[table.getIndex(CdmLightExportTable.EXCLUDED_NOM)] = taxonNode.isNomenclaturallyExcluded() ? "1" : "0";
+                    csvLine[table.getIndex(CdmLightExportTable.UNCERTAIN_APPLICATION)] = taxonNode.isUncertainApplication() ? "1" : "0";
+                    csvLine[table.getIndex(CdmLightExportTable.UNRESOLVED)] = taxonNode.isUnresolved() ? "1" : "0";
+                    csvLine[table.getIndex(CdmLightExportTable.PLACEMENT_STATUS)] = taxonNode.getStatus() == null ? null : taxonNode.getStatus().getLabel();
                     Map<Language, LanguageString> notesMap = taxonNode.getStatusNote();
                     String statusNotes = "";
                     if (!notesMap.isEmpty() && notesMap.size() == 1) {
@@ -381,11 +405,18 @@ public class CdmLightClassificationExport
                             statusNotes = notesMap.values().iterator().next().getText();
                         }
                     }
-                    csvLine[table.getIndex(CdmLightExportTable.STATUS_NOTES)] = statusNotes;
+                    csvLine[table.getIndex(CdmLightExportTable.PLACEMENT_NOTES)] = statusNotes;
 
-                    csvLine[table.getIndex(CdmLightExportTable.UNPLACED)] = taxonNode.isUnplaced() ? "1" : "0";
-                    csvLine[table.getIndex(CdmLightExportTable.DOUBTFUL)] = taxonNode.isDoubtful() ? "1" : "0";
+                    if (taxonNode.getSource() != null) {
+                        csvLine[table.getIndex(CdmLightExportTable.PLACEMENT_REF_FK)] = getId(state, taxonNode.getSource().getCitation());
+                        String sourceStr = OriginalSourceFormatter.INSTANCE.format(taxonNode.getSource());
+                        csvLine[table.getIndex(CdmLightExportTable.PLACEMENT_REFERENCE)] = sourceStr;
+                    }
+
+                    //process taxon line
                     state.getProcessor().put(table, taxon, csvLine);
+
+                    //descriptions
                     handleDescriptions(state, taxon);
                 } catch (Exception e) {
                     state.getResult().addException(e,
@@ -393,9 +424,6 @@ public class CdmLightClassificationExport
                     state.getResult().setState(ExportResultState.INCOMPLETE_WITH_ERROR);
                 }
             }
-
-            taxonNode.removeNullValueFromChildren();
-
         } catch (Exception e) {
             state.getResult().addException(e, "An unexpected error occurred when handling the taxon node of "
                     + cdmBaseStr(taxonNode.getTaxon()) + ", titleCache:"+ taxonNode.getTaxon().getTitleCache()+": " + e.getMessage());
@@ -403,6 +431,9 @@ public class CdmLightClassificationExport
     }
 
     private void handleDescriptions(CdmLightExportState state, CdmBase cdmBase) {
+        if (!state.getConfig().isDoFactualData()) {
+            return;
+        }
         String titleCache = null;
         try {
 
@@ -651,6 +682,33 @@ public class CdmLightClassificationExport
                 } else {
                     state.getProcessor().put(table, textData, csvLine);
                 }
+            }else if (element instanceof CategoricalData) {
+               //use formater
+               CategoricalData categoricalData = (CategoricalData)element;
+               String cache = CategoricalDataFormatter.NewInstance(null).format(categoricalData);
+               csvLine = new String[table.getSize()];
+                csvLine[table.getIndex(CdmLightExportTable.FACT_ID)] = getId(state, element);
+                if (cdmBase instanceof Taxon) {
+                    csvLine[table.getIndex(CdmLightExportTable.TAXON_FK)] = getId(state, cdmBase);
+                } else if (cdmBase instanceof TaxonName) {
+                    csvLine[table.getIndex(CdmLightExportTable.NAME_FK)] = getId(state, cdmBase);
+                }
+                csvLine[table.getIndex(CdmLightExportTable.FACT_TEXT)] = cache;
+                csvLine[table.getIndex(CdmLightExportTable.FACT_CATEGORY)] = categoricalData.getFeature().getLabel();
+                state.getProcessor().put(table, categoricalData, csvLine);
+            }else if (element instanceof QuantitativeData) {
+               QuantitativeData quantitativeData = (QuantitativeData) element;
+               String cache = QuantitativeDataFormatter.NewInstance(null).format(quantitativeData);
+               csvLine = new String[table.getSize()];
+                csvLine[table.getIndex(CdmLightExportTable.FACT_ID)] = getId(state, element);
+                if (cdmBase instanceof Taxon) {
+                    csvLine[table.getIndex(CdmLightExportTable.TAXON_FK)] = getId(state, cdmBase);
+                } else if (cdmBase instanceof TaxonName) {
+                    csvLine[table.getIndex(CdmLightExportTable.NAME_FK)] = getId(state, cdmBase);
+                }
+                csvLine[table.getIndex(CdmLightExportTable.FACT_TEXT)] = cache;
+                csvLine[table.getIndex(CdmLightExportTable.FACT_CATEGORY)] = quantitativeData.getFeature().getLabel();
+                state.getProcessor().put(table, quantitativeData, csvLine);
             }
         } catch (Exception e) {
             state.getResult().addException(e, "An unexpected error occurred when handling single simple fact "
@@ -756,7 +814,7 @@ public class CdmLightClassificationExport
 
             for (DescriptionElementSource source : sources) {
                 if (!(source.getType().equals(OriginalSourceType.Import)
-                        && state.getConfig().isFilterImportSources())) {
+                        && state.getConfig().isExcludeImportSources())) {
                     String[] csvLine = new String[table.getSize()];
                     Reference ref = source.getCitation();
                     if ((ref == null) && (source.getNameUsedInSource() == null)) {
@@ -780,13 +838,11 @@ public class CdmLightClassificationExport
                     }
                     state.getProcessor().put(table, source, csvLine);
                 }
-
             }
         } catch (Exception e) {
             state.getResult().addException(e, "An unexpected error occurred when handling single source "
                     + cdmBaseStr(element) + ": " + e.getMessage());
         }
-
     }
 
     private void handleDistributionFacts(CdmLightExportState state, Taxon taxon,
@@ -824,10 +880,11 @@ public class CdmLightClassificationExport
          if(state.getConfig().isCreateCondensedDistributionString()){
              List<Language> langs = new ArrayList<>();
              langs.add(Language.ENGLISH());
+             TermTree<NamedArea> areaTree = null; //TODO
 
-             CondensedDistribution conDis = geoService.getCondensedDistribution(
+             CondensedDistribution conDis = distributionService.getCondensedDistribution(
                      //TODO add CondensedDistributionConfiguration to export configuration
-                     distributions, true, null, state.getConfig().getCondensedDistributionRecipe().toConfiguration(), langs);
+                     distributions, areaTree, true, null, state.getConfig().getCondensedDistributionConfiguration(), langs);
              CdmLightExportTable tableCondensed =
                      CdmLightExportTable.SIMPLE_FACT;
              String[] csvLine = new String[tableCondensed.getSize()];
@@ -942,11 +999,8 @@ public class CdmLightClassificationExport
     }
 
     /**
-     * Handles Misapplied names (including pro parte and partial as well as pro
+     * Handles misapplied names (including pro parte and partial as well as pro
      * parte and partial synonyms
-     *
-     * @param state
-     * @param rel
      */
     private void handleProPartePartialMisapplied(CdmLightExportState state, Taxon taxon, Taxon accepted, boolean isProParte, boolean isMisapplied, int index) {
         try {
@@ -1074,6 +1128,8 @@ public class CdmLightClassificationExport
                 csvLine[table.getIndex(CdmLightExportTable.FULL_NAME_WITH_REF)] = name.getFullTitleCache();
             } else {
                 List<TaggedText> taggedFullTitleCache = name.getTaggedFullTitle();
+                List<TaggedText> taggedName = name.getTaggedName();
+
                 String fullTitleWithHtml = createNameWithItalics(taggedFullTitleCache);
                 // TODO: adapt the tropicos titlecache creation
                 csvLine[table.getIndex(CdmLightExportTable.FULL_NAME_WITH_REF)] = fullTitleWithHtml.trim();
@@ -1126,7 +1182,7 @@ public class CdmLightClassificationExport
             NomenclaturalSource nomenclaturalSource = name.getNomenclaturalSource();
             if (nomenclaturalSource != null &&nomenclaturalSource.getNameUsedInSource() != null){
                 handleName(state, nomenclaturalSource.getNameUsedInSource(), null);
-                csvLine[table.getIndex(CdmLightExportTable.NAME_USED_IN_SOURCE)] = getId(state, nomenclaturalSource.getNameUsedInSource());
+                csvLine[table.getIndex(CdmLightExportTable.NAME_USED_IN_SOURCE_FK)] = getId(state, nomenclaturalSource.getNameUsedInSource());
             }
 
             if (nomRef != null) {
@@ -1254,17 +1310,18 @@ public class CdmLightClassificationExport
                         textualTypeDesignations.add((TextualTypeDesignation) typeDesignation);
                     }
                 } else if (typeDesignation.isInstanceOf(SpecimenTypeDesignation.class)) {
+                    SpecimenTypeDesignation specimenType = HibernateProxyHelper.deproxy(typeDesignation, SpecimenTypeDesignation.class);
+                    specimenTypeDesignations.add(specimenType);
+                    handleSpecimenType(state, specimenType);
 
-                    specimenTypeDesignations.add(HibernateProxyHelper.deproxy(typeDesignation, SpecimenTypeDesignation.class));
 
                 }else if (typeDesignation instanceof NameTypeDesignation){
                     specimenTypeDesignations.add(HibernateProxyHelper.deproxy(typeDesignation, NameTypeDesignation.class));
                 }
             }
-            TypeDesignationSetManager manager = new TypeDesignationSetManager(specimenTypeDesignations, name);
+            TypeDesignationSetContainer manager = new TypeDesignationSetContainer(specimenTypeDesignations, name, TypeDesignationSetComparator.ORDER_BY.TYPE_STATUS);
             HTMLTagRules rules = new HTMLTagRules();
             rules.addRule(TagEnum.name, "i");
-            String test = manager.print(false, false, false, rules);;
             csvLine[table.getIndex(CdmLightExportTable.TYPE_SPECIMEN)] = manager.print(false, false, false, rules);
 
             StringBuilder stringbuilder = new StringBuilder();
@@ -1321,7 +1378,7 @@ public class CdmLightClassificationExport
                     synonymsInGroup = acceptedTaxon.getSynonymsInGroup(group, comparator);
                 }
 
-                synonymsInGroup.stream().forEach(synonym -> typifiedNames.add(HibernateProxyHelper.deproxy(synonym.getName(), TaxonName.class)));
+                synonymsInGroup.stream().forEach(synonym -> typifiedNames.add(HibernateProxyHelper.deproxy(synonym.getName())));
 
             }else{
                 typifiedNames.addAll(group.getTypifiedNames());
@@ -1341,6 +1398,133 @@ public class CdmLightClassificationExport
         }
     }
 
+    private void handleSpecimenType_(CdmLightExportState state, SpecimenTypeDesignation specimenType) {
+        if (specimenType.getTypeSpecimen() != null){
+            DerivedUnit specimen =  specimenType.getTypeSpecimen();
+            if(specimen != null && !state.getSpecimenStore().contains( specimen.getUuid())){
+               handleSpecimen(state, specimen);
+            }
+        }
+        CdmLightExportTable table = CdmLightExportTable.TYPE_DESIGNATION;
+        String[] csvLine = new String[table.getSize()];
+        //TYPE_ID, SPECIMEN_FK, TYPE_VERBATIM_CITATION, TYPE_STATUS, TYPE_DESIGNATED_BY_STRING, TYPE_DESIGNATED_BY_REF_FK};
+        //Specimen_Fk und den Typusangaben (Art des Typus [holo, lecto, etc.], Quelle, Designation-Quelle, +
+        Set<TaxonName> typifiedNames = specimenType.getTypifiedNames();
+        for (TaxonName name: typifiedNames){
+            csvLine[table.getIndex(CdmLightExportTable.TYPE_STATUS)] = specimenType.getTypeStatus() != null? specimenType.getTypeStatus().getDescription(): "";
+            csvLine[table.getIndex(CdmLightExportTable.TYPE_ID)] = getId(state, specimenType);
+            csvLine[table.getIndex(CdmLightExportTable.TYPIFIED_NAME_FK)] = getId(state, name);
+            csvLine[table.getIndex(CdmLightExportTable.SPECIMEN_FK)] = getId(state, specimenType.getTypeSpecimen());
+            if (specimenType.getSources() != null && !specimenType.getSources().isEmpty()){
+                String sourceString = "";
+                int index = 0;
+                for (IdentifiableSource source: specimenType.getSources()){
+                    if (source.getCitation()!= null){
+                        sourceString = sourceString.concat(source.getCitation().getCitation());
+                    }
+                    index++;
+                    if (index != specimenType.getSources().size()){
+                        sourceString.concat(", ");
+                    }
+                }
+                csvLine[table.getIndex(CdmLightExportTable.TYPE_INFORMATION_REF_STRING)] = sourceString;
+            }
+            if (specimenType.getDesignationSource() != null && specimenType.getDesignationSource().getCitation() != null && !state.getReferenceStore().contains(specimenType.getDesignationSource().getCitation().getUuid())){
+                handleReference(state, specimenType.getDesignationSource().getCitation());
+                csvLine[table.getIndex(CdmLightExportTable.TYPE_DESIGNATED_BY_REF_FK)] = specimenType.getDesignationSource() != null ? getId(state, specimenType.getDesignationSource().getCitation()): "";
+            }
+
+            state.getProcessor().put(table, specimenType, csvLine);
+        }
+    }
+
+
+    /**
+     * @param specimenType
+     */
+    private void handleSpecimenType(CdmLightExportState state, SpecimenTypeDesignation specimenType) {
+        if (specimenType.getTypeSpecimen() != null){
+            DerivedUnit specimen =  specimenType.getTypeSpecimen();
+            if(specimen != null && !state.getSpecimenStore().contains( specimen.getUuid())){
+               handleSpecimen(state, specimen);
+            }
+        }
+        CdmLightExportTable table = CdmLightExportTable.TYPE_DESIGNATION;
+        String[] csvLine = new String[table.getSize()];
+
+        csvLine[table.getIndex(CdmLightExportTable.TYPE_STATUS)] = specimenType.getTypeStatus() != null? specimenType.getTypeStatus().getDescription(): "";
+        csvLine[table.getIndex(CdmLightExportTable.TYPE_ID)] = getId(state, specimenType);
+        csvLine[table.getIndex(CdmLightExportTable.SPECIMEN_FK)] = getId(state, specimenType.getTypeSpecimen());
+        if (specimenType.getSources() != null && !specimenType.getSources().isEmpty()){
+            String sourceString = "";
+            int index = 0;
+            List<IdentifiableSource> sources = new ArrayList<>(specimenType.getSources());
+            Comparator<IdentifiableSource> compareByYear = new Comparator<IdentifiableSource>() {
+                @Override
+                public int compare(IdentifiableSource o1, IdentifiableSource o2) {
+                    if (o1 == o2){
+                        return 0;
+                    }
+                    if (o1.getCitation() == null && o2.getCitation() != null){
+                        return -1;
+                    }
+                    if (o2.getCitation() == null && o1.getCitation() != null){
+                        return 1;
+                    }
+                    if (o1.getCitation().equals(o2.getCitation())){
+                        return 0;
+                    }
+                    if (o1.getCitation().getDatePublished() == null && o2.getCitation().getDatePublished() != null){
+                        return -1;
+                    }
+                    if (o1.getCitation().getDatePublished() != null && o2.getCitation().getDatePublished() == null){
+                        return 1;
+                    }
+                    if (o1.getCitation().getDatePublished().getYear() == null && o2.getCitation().getDatePublished().getYear() != null){
+                        return -1;
+                    }
+                    if (o1.getCitation().getDatePublished().getYear() != null && o2.getCitation().getDatePublished().getYear() == null){
+                        return 1;
+                    }
+                    return o1.getCitation().getDatePublished().getYear().compareTo(o2.getCitation().getDatePublished().getYear());
+                }
+            };
+            Collections.sort(sources, compareByYear);
+            for (IdentifiableSource source: sources){
+                if (source.getCitation()!= null){
+                    sourceString = sourceString.concat(source.getCitation().getCitation());
+                    handleReference(state, source.getCitation());
+                }
+                index++;
+                if (index <= specimenType.getSources().size()){
+                    sourceString = sourceString.concat("; ");
+                }
+            }
+
+            csvLine[table.getIndex(CdmLightExportTable.TYPE_INFORMATION_REF_STRING)] = sourceString;
+            if (sources.get(0).getCitation() != null ){
+                csvLine[table.getIndex(CdmLightExportTable.TYPE_INFORMATION_REF_FK)] = getId(state, sources.get(0).getCitation());
+            }
+        }
+        if (specimenType.getDesignationSource() != null && specimenType.getDesignationSource().getCitation() != null && !state.getReferenceStore().contains(specimenType.getDesignationSource().getCitation().getUuid())){
+            handleReference(state, specimenType.getDesignationSource().getCitation());
+            csvLine[table.getIndex(CdmLightExportTable.TYPE_DESIGNATED_BY_REF_FK)] = specimenType.getDesignationSource() != null ? getId(state, specimenType.getDesignationSource().getCitation()): "";
+        }
+
+
+        Set<TaxonName> typifiedNames = specimenType.getTypifiedNames();
+
+        if (typifiedNames.size() > 1){
+            state.getResult().addWarning("Please check the specimen type  "
+                    + cdmBaseStr(specimenType) + " there are more then one typified name.");
+        }
+        if (typifiedNames.iterator().hasNext()){
+            TaxonName name = typifiedNames.iterator().next();
+            csvLine[table.getIndex(CdmLightExportTable.TYPIFIED_NAME_FK)] = getId(state, name);
+        }
+        state.getProcessor().put(table, specimenType, csvLine);
+    }
+
     private String createNameWithItalics(List<TaggedText> taggedName) {
 
         String fullTitleWithHtml = "";
@@ -1368,11 +1552,11 @@ public class CdmLightClassificationExport
             if (!state.getNameStore().containsKey(name2.getId())) {
                 handleName(state, name2, null);
             }
-
+            csvLine = new String[table.getSize()];
             csvLine[table.getIndex(CdmLightExportTable.NAME_REL_TYPE)] = type.getLabel();
             csvLine[table.getIndex(CdmLightExportTable.NAME1_FK)] = getId(state, name);
             csvLine[table.getIndex(CdmLightExportTable.NAME2_FK)] = getId(state, name2);
-            state.getProcessor().put(table, name, csvLine);
+            state.getProcessor().put(table, rel.getUuid().toString(), csvLine);
         }
 
         rels = name.getRelationsToThisName();
@@ -1380,14 +1564,11 @@ public class CdmLightClassificationExport
         csvLine = new String[table.getSize()];
 
         for (NameRelationship rel : rels) {
-            NameRelationshipType type = rel.getType();
             TaxonName name2 = rel.getFromName();
-            name2 = HibernateProxyHelper.deproxy(name2, TaxonName.class);
+            name2 = HibernateProxyHelper.deproxy(name2);
             if (!state.getNameStore().containsKey(name2.getId())) {
                 handleName(state, name2, null);
             }
-
-
         }
     }
 
@@ -1426,36 +1607,63 @@ public class CdmLightClassificationExport
                 TaxonName name = (TaxonName)cdmBase;
 
                 try{
-                    Set<String> IPNIidentifiers = name.getIdentifiers(DefinedTerm.IDENTIFIER_NAME_IPNI());
-                    Set<String> tropicosIdentifiers = name.getIdentifiers(DefinedTerm.IDENTIFIER_NAME_TROPICOS());
-                    Set<String> WFOIdentifiers = name.getIdentifiers(DefinedTerm.uuidWfoNameIdentifier);
-                    if (!IPNIidentifiers.isEmpty()) {
-                        csvLine = new String[table.getSize()];
-                        csvLine[table.getIndex(CdmLightExportTable.FK)] = getId(state, name);
-                        csvLine[table.getIndex(CdmLightExportTable.REF_TABLE)] = "ScientificName";
-                        csvLine[table.getIndex(CdmLightExportTable.IDENTIFIER_TYPE)] = IPNI_NAME_IDENTIFIER;
-                        csvLine[table.getIndex(CdmLightExportTable.EXTERNAL_NAME_IDENTIFIER)] = extractIdentifier(
-                                IPNIidentifiers);
-                        state.getProcessor().put(table, name.getUuid() + ", " + IPNI_NAME_IDENTIFIER, csvLine);
-                    }
-                    if (!tropicosIdentifiers.isEmpty()) {
-                        csvLine = new String[table.getSize()];
-                        csvLine[table.getIndex(CdmLightExportTable.FK)] = getId(state, name);
-                        csvLine[table.getIndex(CdmLightExportTable.REF_TABLE)] = "ScientificName";
-                        csvLine[table.getIndex(CdmLightExportTable.IDENTIFIER_TYPE)] = name.getUuid() + ", " + IPNI_NAME_IDENTIFIER;
-                        csvLine[table.getIndex(CdmLightExportTable.EXTERNAL_NAME_IDENTIFIER)] = extractIdentifier(
-                                tropicosIdentifiers);
-                        state.getProcessor().put(table, name.getUuid() + ", " + IPNI_NAME_IDENTIFIER, csvLine);
+                    List<Identifier> identifiers = name.getIdentifiers();
+
+                    //first check which kind of identifiers are available and then sort and create table entries
+                    Map<IdentifierType, Set<Identifier>> identifierTypes = new HashMap<>();
+                    for (Identifier identifier: identifiers){
+                        IdentifierType type = identifier.getType();
+                        if (identifierTypes.containsKey(type)){
+                            identifierTypes.get(type).add(identifier);
+                        }else{
+                            Set<Identifier> tempList = new HashSet<>();
+                            tempList.add(identifier);
+                            identifierTypes.put(type, tempList);
+                        }
                     }
-                    if (!WFOIdentifiers.isEmpty()) {
+
+                    for (IdentifierType type:identifierTypes.keySet()){
+                        Set<Identifier> identifiersByType = identifierTypes.get(type);
                         csvLine = new String[table.getSize()];
                         csvLine[table.getIndex(CdmLightExportTable.FK)] = getId(state, name);
                         csvLine[table.getIndex(CdmLightExportTable.REF_TABLE)] = "ScientificName";
-                        csvLine[table.getIndex(CdmLightExportTable.IDENTIFIER_TYPE)] = WFO_NAME_IDENTIFIER;
+                        csvLine[table.getIndex(CdmLightExportTable.IDENTIFIER_TYPE)] = type.getLabel();
                         csvLine[table.getIndex(CdmLightExportTable.EXTERNAL_NAME_IDENTIFIER)] = extractIdentifier(
-                                WFOIdentifiers);
-                        state.getProcessor().put(table, name.getUuid() + ", " + WFO_NAME_IDENTIFIER, csvLine);
+                                identifiersByType);
+                        state.getProcessor().put(table, name.getUuid() + ", " + type.getLabel(), csvLine);
                     }
+
+
+//                    Set<String> IPNIidentifiers = name.getIdentifiers(DefinedTerm.IDENTIFIER_NAME_IPNI());
+//                    Set<String> tropicosIdentifiers = name.getIdentifiers(DefinedTerm.IDENTIFIER_NAME_TROPICOS());
+//                    Set<String> WFOIdentifiers = name.getIdentifiers(DefinedTerm.uuidWfoNameIdentifier);
+//                    if (!IPNIidentifiers.isEmpty()) {
+//                        csvLine = new String[table.getSize()];
+//                        csvLine[table.getIndex(CdmLightExportTable.FK)] = getId(state, name);
+//                        csvLine[table.getIndex(CdmLightExportTable.REF_TABLE)] = "ScientificName";
+//                        csvLine[table.getIndex(CdmLightExportTable.IDENTIFIER_TYPE)] = IPNI_NAME_IDENTIFIER;
+//                        csvLine[table.getIndex(CdmLightExportTable.EXTERNAL_NAME_IDENTIFIER)] = extractIdentifier(
+//                                IPNIidentifiers);
+//                        state.getProcessor().put(table, name.getUuid() + ", " + IPNI_NAME_IDENTIFIER, csvLine);
+//                    }
+//                    if (!tropicosIdentifiers.isEmpty()) {
+//                        csvLine = new String[table.getSize()];
+//                        csvLine[table.getIndex(CdmLightExportTable.FK)] = getId(state, name);
+//                        csvLine[table.getIndex(CdmLightExportTable.REF_TABLE)] = "ScientificName";
+//                        csvLine[table.getIndex(CdmLightExportTable.IDENTIFIER_TYPE)] = TROPICOS_NAME_IDENTIFIER;
+//                        csvLine[table.getIndex(CdmLightExportTable.EXTERNAL_NAME_IDENTIFIER)] = extractIdentifier(
+//                                tropicosIdentifiers);
+//                        state.getProcessor().put(table, name.getUuid() + ", " + IPNI_NAME_IDENTIFIER, csvLine);
+//                    }
+//                    if (!WFOIdentifiers.isEmpty()) {
+//                        csvLine = new String[table.getSize()];
+//                        csvLine[table.getIndex(CdmLightExportTable.FK)] = getId(state, name);
+//                        csvLine[table.getIndex(CdmLightExportTable.REF_TABLE)] = "ScientificName";
+//                        csvLine[table.getIndex(CdmLightExportTable.IDENTIFIER_TYPE)] = WFO_NAME_IDENTIFIER;
+//                        csvLine[table.getIndex(CdmLightExportTable.EXTERNAL_NAME_IDENTIFIER)] = extractIdentifier(
+//                                WFOIdentifiers);
+//                        state.getProcessor().put(table, name.getUuid() + ", " + WFO_NAME_IDENTIFIER, csvLine);
+//                    }
                 }catch(Exception e){
                     state.getResult().addWarning("Please check the identifiers for "
                             + cdmBaseStr(cdmBase) + " maybe there is an empty identifier");
@@ -1528,14 +1736,14 @@ public class CdmLightClassificationExport
         }
     }
 
-    private String extractIdentifier(Set<String> identifierSet) {
+    private String extractIdentifier(Set<Identifier> identifierSet) {
 
         String identifierString = "";
-        for (String identifier : identifierSet) {
+        for (Identifier identifier : identifierSet) {
             if (!StringUtils.isBlank(identifierString)) {
                 identifierString += ", ";
             }
-            identifierString += identifier;
+            identifierString += identifier.getIdentifier();
         }
         return identifierString;
     }
@@ -1698,7 +1906,7 @@ public class CdmLightClassificationExport
                 if (group.equals(acceptedTaxon.getHomotypicGroup())){
                     typifiedNames.add(acceptedTaxon.getName());
                 }
-                synonymsInGroup.stream().forEach(synonym -> typifiedNames.add(HibernateProxyHelper.deproxy(synonym.getName(), TaxonName.class)));
+                synonymsInGroup.stream().forEach(synonym -> typifiedNames.add(CdmBase.deproxy(synonym.getName())));
             }
 
 
@@ -1738,13 +1946,8 @@ public class CdmLightClassificationExport
                 Set<NameRelationship> related = name.getNameRelations();
                 List<NameRelationship> relatedList = new ArrayList<>(related);
 
-                Collections.sort(relatedList, new Comparator<NameRelationship>() {
-                    @Override
-                    public int compare(NameRelationship nr1, NameRelationship nr2) {
-                        return nr1.getType().compareTo(nr2.getType());
-                    }
-
-                });
+                Collections.sort(relatedList, (nr1, nr2)-> {
+                        return nr1.getType().compareTo(nr2.getType());});
 
                 List<NameRelationship> nonNames = new ArrayList<>();
                 List<NameRelationship> otherRelationships = new ArrayList<>();
@@ -1755,12 +1958,30 @@ public class CdmLightClassificationExport
                      // alle Homonyme und inverse blocking names
                         if (rel.getType().equals(NameRelationshipType.LATER_HOMONYM())
                                 || rel.getType().equals(NameRelationshipType.TREATED_AS_LATER_HOMONYM())
-                                || (rel.getType().equals(NameRelationshipType.BLOCKING_NAME_FOR()))){
+                                || (rel.getType().equals(NameRelationshipType.BLOCKING_NAME_FOR()))
+                                || (rel.getType().equals(NameRelationshipType.UNSPECIFIC_NON()))
+                                || (rel.getType().equals(NameRelationshipType.AVOIDS_HOMONYM_OF()))
+                                ){
                             nonNames.add(rel);
                         }else if (!rel.getType().isBasionymRelation()){
                             otherRelationships.add(rel);
                         }
                     }
+                    if (state.getConfig().isShowInverseNameRelationsInHomotypicGroup()) {
+                       if (rel.getToName().equals(name)){
+                            // alle Homonyme und inverse blocking names
+//                               if (rel.getType().equals(NameRelationshipType.LATER_HOMONYM())
+//                                       || rel.getType().equals(NameRelationshipType.TREATED_AS_LATER_HOMONYM())
+//                                       || (rel.getType().equals(NameRelationshipType.BLOCKING_NAME_FOR()))
+//                                       || (rel.getType().equals(NameRelationshipType.UNSPECIFIC_NON()))
+//                                       || (rel.getType().equals(NameRelationshipType.AVOIDS_HOMONYM_OF()))
+//                                       ){
+//                                   nonNames.add(rel);
+//                               }else if (!rel.getType().isBasionymRelation()){
+                                   otherRelationships.add(rel);
+//                               }
+                           }
+                    }
                 }
 
                 String nonRelNames = "";
@@ -1774,21 +1995,25 @@ public class CdmLightClassificationExport
                     TaxonName relatedName = null;
                     if (relName.getFromName().equals(name)){
                         relatedName = relName.getToName();
-                        nonRelNames += label + relatedName.getTitleCache() + " ";
+                        if (state.getConfig().isAddHTML()){
+                               nonRelNames += label + createNameWithItalics(relatedName.getTaggedName())+ " ";
+                        }else{
+                               nonRelNames += label + relatedName.getTitleCache();
+                        }
                     }
 //                    else{
 //                        label = relName.getType().getInverseLabel() + " ";
 //                        relatedName = relName.getFromName();
 //                        nonRelNames += label + relatedName.getTitleCache() + " ";
 //                    }
-
-
                 }
-                relNames.trim();
+                nonRelNames.trim();
                 if (nonNames.size() > 0){
+                    nonRelNames = StringUtils.strip(nonRelNames, null);
                     nonRelNames += "] ";
                 }
 
+                //other relationships
                 if (otherRelationships.size() > 0){
                     relNames += " [";
                 }
@@ -1798,19 +2023,29 @@ public class CdmLightClassificationExport
                     if (rel.getFromName().equals(name)){
                         label = rel.getType().getLabel() + " ";
                         relatedName = rel.getToName();
-                        relNames += label + relatedName.getTitleCache() + " ";
+                        if (state.getConfig().isAddHTML()){
+                            relNames += label + createNameWithItalics(relatedName.getTaggedName())+ " ";
+                        }else{
+                            relNames += label + relatedName.getTitleCache();
+                        }
+                    }
+                    else {
+                        label = rel.getType().getInverseLabel() + " ";
+                        relatedName = rel.getFromName();
+                        if (state.getConfig().isAddHTML()){
+                            relNames += label + createNameWithItalics(relatedName.getTaggedName())+ " ";
+                        }else{
+                            relNames += label + relatedName.getTitleCache();
+                        }
                     }
-//                    else {
-//                        label = rel.getType().getInverseLabel() + " ";
-//                        relatedName = rel.getFromName();
-//                    }
-
                 }
                 relNames.trim();
                 if (otherRelationships.size() > 0){
+                    relNames = StringUtils.stripEnd(relNames, null);
                     relNames += "] ";
                 }
 
+
                 String synonymSign = "";
                 if (index > 0){
                     if (name.isInvalid()){
@@ -1818,11 +2053,18 @@ public class CdmLightClassificationExport
                     }else{
                         synonymSign = "\u2261 ";
                     }
+                }else{
+                    if (name.isInvalid() ){
+                        synonymSign = "\u2212 ";
+                    }else{
+                        synonymSign = "\u003D ";
+                    }
                 }
                 boolean isAccepted = false;
 
                 if (taxonBases.size() == 1){
                      taxonBase = HibernateProxyHelper.deproxy(taxonBases.iterator().next());
+
                      if (taxonBase.getSec() != null){
                          sec = OriginalSourceFormatter.INSTANCE_WITH_YEAR_BRACKETS.format(taxonBase.getSecSource());
                      }
@@ -1841,9 +2083,20 @@ public class CdmLightClassificationExport
                          typifiedNamesWithoutAccepted += synonymSign + doubtful + nameString + nonRelNames + relNames;
                          typifiedNamesWithoutAcceptedWithSec += synonymSign + doubtful + nameString + sec + nonRelNames + relNames;
                      }else{
-                         sec = "";
+//                         sec = "";
                          if (!(((Taxon)taxonBase).isProparteSynonym() || ((Taxon)taxonBase).isMisapplication())){
                              isAccepted = true;
+                         }else {
+                             synonymSign = "\u003D ";
+                         }
+
+                     }
+                     if (taxonBase.getAppendedPhrase() != null){
+                         if (state.getConfig().isAddHTML()){
+                             String taxonString = createNameWithItalics(taxonBase.getTaggedTitle()) ;
+                             taxonString = taxonString.replace("sec "+sec, "");
+                             String nameCacheWithItalics = createNameWithItalics(name.getTaggedName());
+                             nameString = nameString.replace(nameCacheWithItalics, taxonString);
                          }
                      }
                 }else{
@@ -1857,7 +2110,10 @@ public class CdmLightClassificationExport
                         }else{
                             doubtful = "";
                         }
-                        if (tb instanceof Synonym){
+                        if (tb instanceof Synonym ){
+                            if (!((Synonym)tb).getAcceptedTaxon().equals(acceptedTaxon)) {
+                                continue;
+                            }
                             if (StringUtils.isNotBlank(sec)){
                                 sec = " syn. sec. " + sec + " ";
                             }else {
@@ -1870,8 +2126,9 @@ public class CdmLightClassificationExport
                             if (!(((Taxon)tb).isProparteSynonym() || ((Taxon)tb).isMisapplication())){
                                 isAccepted = true;
                                 break;
+                            }else {
+                                synonymSign = "\u003D ";
                             }
-
                         }
                     }
                     if (!isAccepted){
@@ -1909,8 +2166,8 @@ public class CdmLightClassificationExport
 
             List<TaggedText> list = new ArrayList<>();
             if (!designationList.isEmpty()) {
-                TypeDesignationSetManager manager = new TypeDesignationSetManager(group);
-                list.addAll(new TypeDesignationSetFormatter(false, false, false).toTaggedText(manager));
+                TypeDesignationSetContainer manager = new TypeDesignationSetContainer(group);
+                list.addAll(new TypeDesignationSetFormatter(true, false, false).toTaggedText(manager));
             }
             String typeTextDesignations = "";
             //The typeDesignationManager does not handle the textual typeDesignations
@@ -1938,6 +2195,11 @@ public class CdmLightClassificationExport
 
                        typeTextDesignations =  typeTextDesignations + typeDesStateRefs +"; ";
 
+               }else if (typeDes instanceof SpecimenTypeDesignation){
+                   DerivedUnit specimen =  ((SpecimenTypeDesignation)typeDes).getTypeSpecimen();
+                   if(specimen != null && !state.getSpecimenStore().contains( specimen.getUuid())){
+                       handleSpecimen(state, specimen);
+                   }
                }
             }
             if (typeTextDesignations.equals("; ")) {
@@ -1977,15 +2239,19 @@ public class CdmLightClassificationExport
         StringBuffer homotypicalGroupTypeDesignationString = new StringBuffer();
 
         for (TaggedText text : list) {
-            if (text != null && text.getText() != null
-                    && (text.getText().equals("Type:") || text.getText().equals("NameType:") || (text.getType().equals(TagEnum.name) && !isHomotypicGroup))) {
+            if (text == null || text.getText() == null){
+                continue;  //just in case
+            }
+            if ((text.getText().equalsIgnoreCase("Type:")  //should not happen anymore
+                    || text.getText().equalsIgnoreCase("Nametype:")  //should not happen anymore
+                    || (text.getType().equals(TagEnum.name) && !isHomotypicGroup))) {
                 // do nothing
-            } else if (text.getType().equals(TagEnum.reference)) {
+            }else if (text.getType().equals(TagEnum.reference)) {
                 homotypicalGroupTypeDesignationString.append(text.getText());
             }else if (text.getType().equals(TagEnum.name)){
                 if (!isSpecimenTypeDesignation){
                     homotypicalGroupTypeDesignationString
-                    .append("<i>"+text.getText()+"</i> ");
+                        .append("<i>"+text.getText()+"</i> ");
                 }
             }else if (text.getType().equals(TagEnum.typeDesignation) ) {
                 if(isSpecimenTypeDesignation){
@@ -2010,6 +2276,7 @@ public class CdmLightClassificationExport
         typeDesignations += ".";
         typeDesignations = typeDesignations.replace("..", ".");
         typeDesignations = typeDesignations.replace(". .", ".");
+        typeDesignations = typeDesignations.replace("; \u2261", " \u2261 ");
 
         if (typeDesignations.trim().equals(".")) {
             typeDesignations = null;
@@ -2228,7 +2495,7 @@ public class CdmLightClassificationExport
         } else if (authorship instanceof Team) {
 
             Team authorTeam = (Team)authorship;
-            fullAuthorship = authorTeam.getCacheStrategy().getTitleCache(authorTeam);
+            fullAuthorship = authorTeam.cacheStrategy().getTitleCache(authorTeam);
         }
         return fullAuthorship;
     }
@@ -2275,7 +2542,7 @@ public class CdmLightClassificationExport
             csvLine[table.getIndex(CdmLightExportTable.SPECIMEN_IMAGE_URIS)] = extractMediaURIs(state,
                     specimen.getDescriptions(), Feature.IMAGE());
             if (specimen instanceof DerivedUnit) {
-                DerivedUnit derivedUnit = (DerivedUnit) specimen;
+                DerivedUnit derivedUnit = HibernateProxyHelper.deproxy(specimen, DerivedUnit.class);
                 if (derivedUnit.getCollection() != null) {
                     csvLine[table.getIndex(CdmLightExportTable.HERBARIUM_ABBREV)] = derivedUnit.getCollection()
                             .getCode();
@@ -2344,8 +2611,8 @@ public class CdmLightClassificationExport
                         }
                     }
                 } else {
-                    state.getResult().addError("The specimen with uuid " + specimen.getUuid()
-                            + " is not an DerivedUnit. Could not be exported.");
+                    state.getResult().addWarning("The specimen with uuid " + specimen.getUuid()
+                            + " is not an DerivedUnit.");
                 }
             }