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 54c254cf9604fafd43aa65a858218034ee2f600e..551b1410ce11dba609ea41484bc1a8877ff729c2 100755 (executable)
@@ -10,6 +10,7 @@ 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;
@@ -66,6 +67,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.PresenceAbsenceTerm;
 import eu.etaxonomy.cdm.model.description.QuantitativeData;
 import eu.etaxonomy.cdm.model.description.TaxonDescription;
 import eu.etaxonomy.cdm.model.description.TaxonInteraction;
@@ -104,6 +106,7 @@ import eu.etaxonomy.cdm.model.taxon.TaxonNode;
 import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
 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;
@@ -112,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
  */
@@ -124,6 +129,9 @@ public class CdmLightClassificationExport
     @Autowired
     private IDistributionService distributionService;
 
+    @Autowired
+    private ITermTreeDao termTreeDao;
+
     public CdmLightClassificationExport() {
         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();
                 }
@@ -221,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();
         }
@@ -255,9 +263,9 @@ 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);
 
@@ -304,37 +312,40 @@ public class CdmLightClassificationExport
                     TaxonName name = taxon.getName();
                     handleName(state, name, taxon, true);
 
-                    //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++;
-                    }
-                    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++;
-                    }
+                        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++;
-                    }
+                        //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++;
+                        //misapplications
+                        for (Taxon tax : taxon.getAllMisappliedNames()) {
+                            handleProPartePartialMisapplied(state, tax, taxon, false, true, index);
+                            index++;
+                        }
                     }
 
                     //taxon table
@@ -429,6 +440,9 @@ public class CdmLightClassificationExport
     }
 
     private void handleDescriptions(CdmLightExportState state, CdmBase cdmBase) {
+        if (!state.getConfig().isDoFactualData()) {
+            return;
+        }
         String titleCache = null;
         try {
 
@@ -444,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);
+                                }
                             }
                         }
                     }
@@ -678,10 +694,10 @@ public class CdmLightClassificationExport
                     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()];
+                //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);
@@ -692,9 +708,9 @@ public class CdmLightClassificationExport
                 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()];
+                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);
@@ -872,32 +888,46 @@ public class CdmLightClassificationExport
                         + cdmBaseStr(element) + ": " + e.getMessage());
             }
         }
-         if(state.getConfig().isCreateCondensedDistributionString()){
-             List<Language> langs = new ArrayList<>();
-             langs.add(Language.ENGLISH());
-             TermTree<NamedArea> areaTree = null; //TODO
-
-             CondensedDistribution conDis = distributionService.getCondensedDistribution(
-                     //TODO add CondensedDistributionConfiguration to export configuration
-                     distributions, areaTree, 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);
-         }
+        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,
@@ -1188,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();
@@ -1210,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();
@@ -1379,7 +1409,6 @@ public class CdmLightClassificationExport
                 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);
@@ -1567,7 +1596,7 @@ public class CdmLightClassificationExport
         }
     }
 
-    private String createCollatation(TaxonName name) {
+    private String createCollation(TaxonName name) {
         String collation = "";
         if (name.getNomenclaturalReference() != null) {
             Reference ref = name.getNomenclaturalReference();
@@ -1963,7 +1992,7 @@ public class CdmLightClassificationExport
                         }
                     }
                     if (state.getConfig().isShowInverseNameRelationsInHomotypicGroup()) {
-                       if (rel.getToName().equals(name)){
+                        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())
@@ -1975,7 +2004,7 @@ public class CdmLightClassificationExport
 //                               }else if (!rel.getType().isBasionymRelation()){
                                    otherRelationships.add(rel);
 //                               }
-                           }
+                         }
                     }
                 }
 
@@ -1991,9 +2020,9 @@ public class CdmLightClassificationExport
                     if (relName.getFromName().equals(name)){
                         relatedName = relName.getToName();
                         if (state.getConfig().isAddHTML()){
-                               nonRelNames += label + createNameWithItalics(relatedName.getTaggedName())+ " ";
+                            nonRelNames += label + createNameWithItalics(relatedName.getTaggedName())+ " ";
                         }else{
-                               nonRelNames += label + relatedName.getTitleCache();
+                            nonRelNames += label + relatedName.getTitleCache();
                         }
                     }
 //                    else{
@@ -2105,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 {
@@ -2115,7 +2149,8 @@ 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 {
@@ -2164,9 +2199,9 @@ public class CdmLightClassificationExport
             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();
@@ -2185,26 +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);
-                   }
-               }
+                }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;
 
@@ -2213,7 +2248,7 @@ public class CdmLightClassificationExport
             }
             if (StringUtils.isNotBlank(typeTextDesignations)) {
                 if (!typeTextDesignations.endsWith(".")) {
-                       typeTextDesignations = typeTextDesignations + ".";
+                    typeTextDesignations = typeTextDesignations + ".";
                 }
                 csvLine[table.getIndex(CdmLightExportTable.TYPE_CACHE)] = typeTextDesignations;
 
@@ -2504,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;
@@ -2545,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??
@@ -2602,9 +2638,6 @@ public class CdmLightClassificationExport
                             }
                         }
                     }
-                } else {
-                    state.getResult().addWarning("The specimen with uuid " + specimen.getUuid()
-                            + " is not an DerivedUnit.");
                 }
             }
 
@@ -2715,5 +2748,4 @@ public class CdmLightClassificationExport
     protected boolean isIgnore(CdmLightExportState state) {
         return false;
     }
-
-}
+}
\ No newline at end of file