ref #10206 add publish flag to CdmLight and ColDP export in cdmlib
[cdmlib.git] / cdmlib-io / src / main / java / eu / etaxonomy / cdm / io / cdmLight / CdmLightClassificationExport.java
index 8fcbfb51ecce6723f904ac33d1611feb40cd6156..551b1410ce11dba609ea41484bc1a8877ff729c2 100755 (executable)
@@ -10,9 +10,11 @@ package eu.etaxonomy.cdm.io.cdmLight;
 
 import java.io.File;
 import java.util.ArrayList;
+import java.util.Arrays;
 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 +26,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 +59,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 +67,8 @@ 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.PresenceAbsenceTerm;
+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 +104,9 @@ 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.dao.term.ITermTreeDao;
 import eu.etaxonomy.cdm.persistence.dto.TaxonNodeDto;
 import eu.etaxonomy.cdm.persistence.dto.TaxonNodeDtoByRankAndNameComparator;
 import eu.etaxonomy.cdm.strategy.cache.HTMLTagRules;
@@ -105,6 +115,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 +125,14 @@ 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
+    private IDistributionService distributionService;
 
     @Autowired
-    IEditGeoService geoService;
+    private ITermTreeDao termTreeDao;
 
     public CdmLightClassificationExport() {
-        super();
         this.ioName = this.getClass().getSimpleName();
     }
 
@@ -164,7 +172,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 +207,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 +229,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();
         }
@@ -259,14 +263,13 @@ public class CdmLightClassificationExport
                 if (root.hasChildNodes()) {
                     childNodes = new ArrayList<>();
                     for (TaxonNode child : root.getChildNodes()) {
-                       if (child != null) {
-                               childNodes.add(new TaxonNodeDto(child));
-                       }
+                        if (child != null) {
+                            childNodes.add(new TaxonNodeDto(child));
+                        }
                     }
                     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 +281,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,42 +305,50 @@ 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);
-                    HomotypicalGroup homotypicGroup = taxon.getHomotypicGroup();
-                    int index = 0;
-                    int homotypicGroupIndex = 0;
-                    handleHomotypicalGroup(state, homotypicGroup, taxon, homotypicGroupIndex);
-                    homotypicGroupIndex++;
-                    for (Synonym syn : taxon.getSynonymsInGroup(homotypicGroup)) {
-                        handleSynonym(state, syn, index);
-                        index++;
-                    }
-                    List<HomotypicalGroup> heterotypicHomotypicGroups = taxon.getHeterotypicSynonymyGroups();
-                    for (HomotypicalGroup group: heterotypicHomotypicGroups){
-                        handleHomotypicalGroup(state, group, taxon, homotypicGroupIndex);
-                        for (Synonym syn : taxon.getSynonymsInGroup(group)) {
+
+                    if (state.getConfig().isDoSynonyms()) {
+
+                        //homotypic group / synonyms
+                        HomotypicalGroup homotypicGroup = taxon.getHomotypicGroup();
+                        int index = 0;
+                        int homotypicGroupIndex = 0;
+                        handleHomotypicalGroup(state, homotypicGroup, taxon, homotypicGroupIndex);
+                        homotypicGroupIndex++;
+                        for (Synonym syn : taxon.getSynonymsInGroup(homotypicGroup)) {
                             handleSynonym(state, syn, index);
                             index++;
                         }
-                        homotypicGroupIndex++;
-                    }
-
-                    index = 0;
-                    for (Taxon tax : taxon.getAllProParteSynonyms()) {
-                        handleProPartePartialMisapplied(state, tax, taxon, true, false, index);
-                        index++;
-                    }
+                        List<HomotypicalGroup> heterotypicHomotypicGroups = taxon.getHeterotypicSynonymyGroups();
+                        for (HomotypicalGroup group: heterotypicHomotypicGroups){
+                            handleHomotypicalGroup(state, group, taxon, homotypicGroupIndex);
+                            for (Synonym syn : taxon.getSynonymsInGroup(group)) {
+                                handleSynonym(state, syn, index);
+                                index++;
+                            }
+                            homotypicGroupIndex++;
+                        }
 
+                        //pro parte synonyms
+                        index = 0;
+                        for (Taxon tax : taxon.getAllProParteSynonyms()) {
+                            handleProPartePartialMisapplied(state, tax, taxon, true, false, index);
+                            index++;
+                        }
 
-                    for (Taxon tax : taxon.getAllMisappliedNames()) {
-                        handleProPartePartialMisapplied(state, tax, taxon, false, true, 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 +356,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 +373,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 +414,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 +433,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 +440,9 @@ public class CdmLightClassificationExport
     }
 
     private void handleDescriptions(CdmLightExportState state, CdmBase cdmBase) {
+        if (!state.getConfig().isDoFactualData()) {
+            return;
+        }
         String titleCache = null;
         try {
 
@@ -418,20 +458,22 @@ public class CdmLightClassificationExport
                 List<DescriptionElementBase> usageFacts = new ArrayList<>();
                 for (TaxonDescription description : descriptions) {
                     if (description.getElements() != null) {
-                        for (DescriptionElementBase element : description.getElements()) {
-                            element = CdmBase.deproxy(element);
-                            handleAnnotations(element);
-                            if (element.getFeature().equals(Feature.COMMON_NAME())) {
-                                commonNameFacts.add(element);
-                            } else if (element.getFeature().equals(Feature.DISTRIBUTION())) {
-                                distributionFacts.add(element);
-                            } else if (element instanceof IndividualsAssociation
-                                    || isSpecimenFeature(element.getFeature())) {
-                                specimenFacts.add(element);
-                            } else if (element.getFeature().isSupportsTaxonInteraction()) {
-                                taxonInteractionsFacts.add(element);
-                            } else {
-                                simpleFacts.add(element);
+                        if (description.isPublish() || state.getConfig().isIncludeUnpublishedFacts()){
+                            for (DescriptionElementBase element : description.getElements()) {
+                                element = CdmBase.deproxy(element);
+                                handleAnnotations(element);
+                                if (element.getFeature().equals(Feature.COMMON_NAME())) {
+                                    commonNameFacts.add(element);
+                                } else if (element.getFeature().equals(Feature.DISTRIBUTION())) {
+                                    distributionFacts.add(element);
+                                } else if (element instanceof IndividualsAssociation
+                                        || isSpecimenFeature(element.getFeature())) {
+                                    specimenFacts.add(element);
+                                } else if (element.getFeature().isSupportsTaxonInteraction()) {
+                                    taxonInteractionsFacts.add(element);
+                                } else {
+                                    simpleFacts.add(element);
+                                }
                             }
                         }
                     }
@@ -651,6 +693,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 +825,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 +849,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,
@@ -821,31 +888,46 @@ public class CdmLightClassificationExport
                         + cdmBaseStr(element) + ": " + e.getMessage());
             }
         }
-         if(state.getConfig().isCreateCondensedDistributionString()){
-             List<Language> langs = new ArrayList<>();
-             langs.add(Language.ENGLISH());
-
-             CondensedDistribution conDis = geoService.getCondensedDistribution(
-                     //TODO add CondensedDistributionConfiguration to export configuration
-                     distributions, true, null, state.getConfig().getCondensedDistributionRecipe().toConfiguration(), langs);
-             CdmLightExportTable tableCondensed =
-                     CdmLightExportTable.SIMPLE_FACT;
-             String[] csvLine = new String[tableCondensed.getSize()];
-             //the computed fact has no uuid, TODO: remember the uuid for later reference assignment
-             UUID randomUuid = UUID.randomUUID();
-             csvLine[tableCondensed.getIndex(CdmLightExportTable.FACT_ID)] =
-                     randomUuid.toString();
-             csvLine[tableCondensed.getIndex(CdmLightExportTable.TAXON_FK)] =
-                     getId(state, taxon);
-             csvLine[tableCondensed.getIndex(CdmLightExportTable.FACT_TEXT)] =
-                     conDis.toString();
-             csvLine[tableCondensed.getIndex(CdmLightExportTable.LANGUAGE)] =Language.ENGLISH().toString();
-
-             csvLine[tableCondensed.getIndex(CdmLightExportTable.FACT_CATEGORY)] =
-                     "CondensedDistribution";
-
-             state.getProcessor().put(tableCondensed, taxon, csvLine);
-         }
+        if(state.getConfig().isCreateCondensedDistributionString()){
+            List<Language> langs = new ArrayList<>();
+            langs.add(Language.ENGLISH());
+            TermTree<NamedArea> areaTree = null; //TODO
+            TermTree<PresenceAbsenceTerm> statusTree = getPersistentStatusTree(state.getConfig());
+
+            CondensedDistribution conDis = distributionService.getCondensedDistribution(
+                    //TODO add CondensedDistributionConfiguration to export configuration
+                    distributions, areaTree, statusTree, true, null,
+                    state.getConfig().getCondensedDistributionConfiguration(), langs);
+            CdmLightExportTable tableCondensed =
+                    CdmLightExportTable.SIMPLE_FACT;
+            String[] csvLine = new String[tableCondensed.getSize()];
+            //the computed fact has no uuid, TODO: remember the uuid for later reference assignment
+            UUID randomUuid = UUID.randomUUID();
+            csvLine[tableCondensed.getIndex(CdmLightExportTable.FACT_ID)] =
+                    randomUuid.toString();
+            csvLine[tableCondensed.getIndex(CdmLightExportTable.TAXON_FK)] =
+                    getId(state, taxon);
+            csvLine[tableCondensed.getIndex(CdmLightExportTable.FACT_TEXT)] =
+                    conDis.toString();
+            csvLine[tableCondensed.getIndex(CdmLightExportTable.LANGUAGE)] =Language.ENGLISH().toString();
+
+            csvLine[tableCondensed.getIndex(CdmLightExportTable.FACT_CATEGORY)] =
+                    "CondensedDistribution";
+
+            state.getProcessor().put(tableCondensed, taxon, csvLine);
+        }
+    }
+
+    private TermTree<PresenceAbsenceTerm> getPersistentStatusTree(CdmLightExportConfigurator config) {
+        UUID statusTreeUuid = config.getStatusTree();
+        if (statusTreeUuid == null) {
+            return null;
+        }
+        //TODO property path
+        String[] propertyPath = new String[] {};
+        @SuppressWarnings("unchecked")
+        TermTree<PresenceAbsenceTerm> statusTree = termTreeDao.load(statusTreeUuid, Arrays.asList(propertyPath));
+        return statusTree;
     }
 
     private void handleCommonNameFacts(CdmLightExportState state, Taxon taxon,
@@ -942,11 +1024,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 +1153,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 +1207,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) {
@@ -1137,7 +1218,7 @@ public class CdmLightClassificationExport
                 csvLine[table.getIndex(CdmLightExportTable.PUBLICATION_TYPE)] = nomRef.getType().name();
                 if (nomRef.getVolume() != null) {
                     csvLine[table.getIndex(CdmLightExportTable.VOLUME_ISSUE)] = nomRef.getVolume();
-                    csvLine[table.getIndex(CdmLightExportTable.COLLATION)] = createCollatation(name);
+                    csvLine[table.getIndex(CdmLightExportTable.COLLATION)] = createCollation(name);
                 }
                 if (nomRef.getDatePublished() != null) {
                     csvLine[table.getIndex(CdmLightExportTable.DATE_PUBLISHED)] = nomRef.getTimePeriodPublishedString();
@@ -1159,7 +1240,7 @@ public class CdmLightClassificationExport
                     }
                     if (nomRef.getVolume() == null && inReference.getVolume() != null) {
                         csvLine[table.getIndex(CdmLightExportTable.VOLUME_ISSUE)] = inReference.getVolume();
-                        csvLine[table.getIndex(CdmLightExportTable.COLLATION)] = createCollatation(name);
+                        csvLine[table.getIndex(CdmLightExportTable.COLLATION)] = createCollation(name);
                     }
                     if (inReference.getInReference() != null) {
                         inReference = inReference.getInReference();
@@ -1182,7 +1263,7 @@ public class CdmLightClassificationExport
                     if (author != null
                             && (nomRef.isOfType(ReferenceType.BookSection) || nomRef.isOfType(ReferenceType.Section))) {
                         csvLine[table.getIndex(CdmLightExportTable.ABBREV_REF_AUTHOR)] = author.isProtectedTitleCache()
-                                ? author.getTitleCache() : CdmUtils.Nz(author.getNomenclaturalTitle());
+                                ? author.getTitleCache() : CdmUtils.Nz(author.getNomenclaturalTitleCache());
                         csvLine[table.getIndex(CdmLightExportTable.FULL_REF_AUTHOR)] = CdmUtils
                                 .Nz(author.getTitleCache());
                     } else {
@@ -1206,7 +1287,7 @@ public class CdmLightClassificationExport
                     TeamOrPersonBase<?> author = nomRef.getAuthorship();
                     if (author != null) {
                         csvLine[table.getIndex(CdmLightExportTable.ABBREV_REF_AUTHOR)] = author.isProtectedTitleCache()
-                                ? author.getTitleCache() : CdmUtils.Nz(author.getNomenclaturalTitle());
+                                ? author.getTitleCache() : CdmUtils.Nz(author.getNomenclaturalTitleCache());
                         csvLine[table.getIndex(CdmLightExportTable.FULL_REF_AUTHOR)] = CdmUtils
                                 .Nz(author.getTitleCache());
                     } else {
@@ -1254,17 +1335,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,13 +1403,12 @@ 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());
             }
 
-
             Integer seqNumber = typifiedNames.indexOf(name);
             csvLine[table.getIndex(CdmLightExportTable.HOMOTYPIC_GROUP_SEQ)] = String.valueOf(seqNumber);
             state.getProcessor().put(table, name, csvLine);
@@ -1341,6 +1422,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 +1576,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,18 +1588,15 @@ 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);
             }
-
-
         }
     }
 
-    private String createCollatation(TaxonName name) {
+    private String createCollation(TaxonName name) {
         String collation = "";
         if (name.getNomenclaturalReference() != null) {
             Reference ref = name.getNomenclaturalReference();
@@ -1426,36 +1631,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 +1760,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;
     }
@@ -1594,7 +1826,7 @@ public class CdmLightClassificationExport
             String[] csvLineMember = new String[table.getSize()];
             csvLine[table.getIndex(CdmLightExportTable.AUTHOR_ID)] = getId(state, author);
             csvLine[table.getIndex(CdmLightExportTable.ABBREV_AUTHOR)] = author.isProtectedTitleCache()
-                    ? author.getTitleCache() : author.getNomenclaturalTitle();
+                    ? author.getTitleCache() : author.getNomenclaturalTitleCache();
             csvLine[table.getIndex(CdmLightExportTable.AUTHOR_TITLE)] = author.getTitleCache();
             author = HibernateProxyHelper.deproxy(author);
             if (author instanceof Person) {
@@ -1622,7 +1854,7 @@ public class CdmLightClassificationExport
                         csvLineMember = new String[table.getSize()];
                         csvLineMember[table.getIndex(CdmLightExportTable.AUTHOR_ID)] = getId(state, member);
                         csvLineMember[table.getIndex(CdmLightExportTable.ABBREV_AUTHOR)] = member
-                                .isProtectedTitleCache() ? member.getTitleCache() : member.getNomenclaturalTitle();
+                                .isProtectedTitleCache() ? member.getTitleCache() : member.getNomenclaturalTitleCache();
                         csvLineMember[table.getIndex(CdmLightExportTable.AUTHOR_TITLE)] = member.getTitleCache();
                         csvLineMember[table.getIndex(CdmLightExportTable.AUTHOR_GIVEN_NAME)] = member.getGivenName();
                         csvLineMember[table.getIndex(CdmLightExportTable.AUTHOR_FAMILY_NAME)] = member.getFamilyName();
@@ -1698,7 +1930,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,23 +1970,41 @@ 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<>();
 
                 for (NameRelationship rel: relatedList){
-                    // 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()))){
-                        nonNames.add(rel);
-                    }else if (!rel.getType().isBasionymRelation()){
-                        otherRelationships.add(rel);
+                    //no inverse relations
+                    if (rel.getFromName().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);
+                        }
+                    }
+                    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);
+//                               }
+                         }
                     }
                 }
 
@@ -1769,20 +2019,25 @@ public class CdmLightClassificationExport
                     TaxonName relatedName = null;
                     if (relName.getFromName().equals(name)){
                         relatedName = relName.getToName();
-                        nonRelNames += label + relatedName.getTitleCache() + " ";
-                    }else{
-                        label = relName.getType().getInverseLabel() + " ";
-                        relatedName = relName.getFromName();
-                        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 += " [";
                 }
@@ -1792,17 +2047,29 @@ public class CdmLightClassificationExport
                     if (rel.getFromName().equals(name)){
                         label = rel.getType().getLabel() + " ";
                         relatedName = rel.getToName();
-                    }else {
+                        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();
+                        }
                     }
-                    relNames += label + relatedName.getTitleCache() + " ";
                 }
                 relNames.trim();
                 if (otherRelationships.size() > 0){
+                    relNames = StringUtils.stripEnd(relNames, null);
                     relNames += "] ";
                 }
 
+
                 String synonymSign = "";
                 if (index > 0){
                     if (name.isInvalid()){
@@ -1810,11 +2077,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());
                      }
@@ -1833,9 +2107,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{
@@ -1849,7 +2134,12 @@ public class CdmLightClassificationExport
                         }else{
                             doubtful = "";
                         }
-                        if (tb instanceof Synonym){
+                        if (tb instanceof Synonym ){
+                            Synonym syn = CdmBase.deproxy(tb, Synonym.class);
+                            Taxon acc = syn.getAcceptedTaxon();
+                            if (acc == null || !acc.equals(acceptedTaxon)) {
+                                continue;
+                            }
                             if (StringUtils.isNotBlank(sec)){
                                 sec = " syn. sec. " + sec + " ";
                             }else {
@@ -1859,11 +2149,13 @@ public class CdmLightClassificationExport
                             break;
                         }else{
                             sec = "";
-                            if (!(((Taxon)tb).isProparteSynonym() || ((Taxon)tb).isMisapplication())){
+                            Taxon taxon = CdmBase.deproxy(tb, Taxon.class);
+                            if (!(taxon.isProparteSynonym() || taxon.isMisapplication())){
                                 isAccepted = true;
                                 break;
+                            }else {
+                                synonymSign = "\u003D ";
                             }
-
                         }
                     }
                     if (!isAccepted){
@@ -1901,15 +2193,15 @@ 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
             for (TypeDesignationBase<?> typeDes: designationList) {
-               if (typeDes instanceof TextualTypeDesignation) {
-                       typeTextDesignations = typeTextDesignations + ((TextualTypeDesignation)typeDes).getText(Language.getDefaultLanguage());
-                       String typeDesStateRefs = "";
+                if (typeDes instanceof TextualTypeDesignation) {
+                    typeTextDesignations = typeTextDesignations + ((TextualTypeDesignation)typeDes).getText(Language.getDefaultLanguage());
+                    String typeDesStateRefs = "";
                     if (typeDes.getDesignationSource() != null ){
                         typeDesStateRefs = "[";
                         NamedSource source = typeDes.getDesignationSource();
@@ -1928,21 +2220,26 @@ public class CdmLightClassificationExport
                         typeDesStateRefs += "]";
                     }
 
-                       typeTextDesignations =  typeTextDesignations + typeDesStateRefs +"; ";
+                    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("; ")) {
-               typeTextDesignations = "";
+                typeTextDesignations = "";
             }
             if (StringUtils.isNotBlank(typeTextDesignations)) {
-               typeTextDesignations = typeTextDesignations.substring(0, typeTextDesignations.length()-2);
+                typeTextDesignations = typeTextDesignations.substring(0, typeTextDesignations.length()-2);
             }
             String specimenTypeString = !list.isEmpty()? createTypeDesignationString(list, true, typifiedNames.get(0).isSpecies() || typifiedNames.get(0).isInfraSpecific()):"";
 
             if (StringUtils.isNotBlank(specimenTypeString)) {
                 if (!specimenTypeString.endsWith(".")) {
-                       specimenTypeString = specimenTypeString + ".";
+                    specimenTypeString = specimenTypeString + ".";
                 }
                 csvLine[table.getIndex(CdmLightExportTable.TYPE_STRING)] = specimenTypeString;
 
@@ -1951,7 +2248,7 @@ public class CdmLightClassificationExport
             }
             if (StringUtils.isNotBlank(typeTextDesignations)) {
                 if (!typeTextDesignations.endsWith(".")) {
-                       typeTextDesignations = typeTextDesignations + ".";
+                    typeTextDesignations = typeTextDesignations + ".";
                 }
                 csvLine[table.getIndex(CdmLightExportTable.TYPE_CACHE)] = typeTextDesignations;
 
@@ -1969,15 +2266,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){
@@ -2002,6 +2303,7 @@ public class CdmLightClassificationExport
         typeDesignations += ".";
         typeDesignations = typeDesignations.replace("..", ".");
         typeDesignations = typeDesignations.replace(". .", ".");
+        typeDesignations = typeDesignations.replace("; \u2261", " \u2261 ");
 
         if (typeDesignations.trim().equals(".")) {
             typeDesignations = null;
@@ -2154,7 +2456,7 @@ public class CdmLightClassificationExport
             String[] csvLine = new String[table.getSize()];
             csvLine[table.getIndex(CdmLightExportTable.REFERENCE_ID)] = getId(state, reference);
             // TODO short citations correctly
-            String shortCitation = OriginalSourceFormatter.INSTANCE.format(reference, null); // Should be Author(year) like in Taxon.sec
+            String shortCitation = OriginalSourceFormatter.INSTANCE_WITH_YEAR_BRACKETS.format(reference, null); // Should be Author(year) like in Taxon.sec
             csvLine[table.getIndex(CdmLightExportTable.BIBLIO_SHORT_CITATION)] = shortCitation;
             // TODO get preferred title
             csvLine[table.getIndex(CdmLightExportTable.REF_TITLE)] = reference.isProtectedTitleCache()
@@ -2220,7 +2522,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;
     }
@@ -2237,7 +2539,6 @@ public class CdmLightClassificationExport
              * Diego, El Amatal, 14.4.1993, González 159” [Auch ohne Punkt] ->
              * FieldUnit TitleCache HerbariumAbbrev = “B” [wie gehabt]
              * HerbariumCode
-             *
              */
 
             csvLine[table.getIndex(CdmLightExportTable.SPECIMEN_ID)] = specimenId;
@@ -2267,7 +2568,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();
@@ -2278,10 +2579,12 @@ public class CdmLightClassificationExport
                     Iterator<MediaRepresentation> it = mediaSpecimen.getMediaSpecimen().getRepresentations().iterator();
                     String mediaUris = extractMediaUris(it);
                     csvLine[table.getIndex(CdmLightExportTable.MEDIA_SPECIMEN_URL)] = mediaUris;
-
                 }
 
-                if (derivedUnit.getDerivedFrom() != null) {
+                if (derivedUnit.getDerivedFrom() == null) {
+                    state.getResult().addWarning("The specimen with uuid " + specimen.getUuid()
+                        + " does not have a field unit.");
+                } else {
                     for (SpecimenOrObservationBase<?> original : derivedUnit.getDerivedFrom().getOriginals()) {
                         // TODO: What to do if there are more then one
                         // FieldUnit??
@@ -2335,9 +2638,6 @@ public class CdmLightClassificationExport
                             }
                         }
                     }
-                } else {
-                    state.getResult().addError("The specimen with uuid " + specimen.getUuid()
-                            + " is not an DerivedUnit. Could not be exported.");
                 }
             }
 
@@ -2448,5 +2748,4 @@ public class CdmLightClassificationExport
     protected boolean isIgnore(CdmLightExportState state) {
         return false;
     }
-
-}
+}
\ No newline at end of file