ref #6627: add import of taxon interactions to cdmlight
authorKatja Luther <k.luther@bgbm.org>
Thu, 21 Mar 2019 09:35:55 +0000 (10:35 +0100)
committerKatja Luther <k.luther@bgbm.org>
Thu, 21 Mar 2019 09:35:55 +0000 (10:35 +0100)
cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/cdmLight/CdmLightClassificationExport.java
cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/cdmLight/CdmLightExportTable.java

index 299f90f18144a2c6c9c638665a670998fafeca17..9dc64fd3e8f5adb5eb2cb83c015d6fdb42458dc4 100755 (executable)
@@ -53,6 +53,7 @@ import eu.etaxonomy.cdm.model.description.Feature;
 import eu.etaxonomy.cdm.model.description.IndividualsAssociation;
 import eu.etaxonomy.cdm.model.description.SpecimenDescription;
 import eu.etaxonomy.cdm.model.description.TaxonDescription;
+import eu.etaxonomy.cdm.model.description.TaxonInteraction;
 import eu.etaxonomy.cdm.model.description.TaxonNameDescription;
 import eu.etaxonomy.cdm.model.description.TextData;
 import eu.etaxonomy.cdm.model.location.NamedArea;
@@ -130,29 +131,6 @@ public class CdmLightClassificationExport
             CdmLightExportConfigurator config = state.getConfig();
             config.setFieldsTerminatedBy(",");
 
-//            if (config.getTaxonNodeFilter().getTaxonNodesFilter().isEmpty() && config.getTaxonNodeFilter().getClassificationFilter().isEmpty()){
-//                //TODO
-//                state.setEmptyData();
-//                return;
-//            }
-
-
-
-//            for (LogicFilter<Classification> classificationFilter : config.getTaxonNodeFilter().getClassificationFilter()){
-//                UUID classificationUuid = classificationFilter.getUuid();
-//                Classification classification = getClassificationService().find(classificationUuid);
-//                if (classification == null){
-//                    String message = String.format("Classification for given classification UUID not found. No data imported for %s", classificationUuid.toString());
-//                    state.getResult().addWarning(message);
-//                }else{
-//                    TaxonNode root = classification.getRootNode();
-//                    UUID uuid = root.getUuid();
-//                    root = getTaxonNodeService().load(uuid);
-//                    handleSingleClassification(state, root.getUuid());
-//                }
-//            }
-
-
             @SuppressWarnings("unchecked")
             TaxonNodeOutStreamPartitioner<XmlExportState> partitioner
                   = TaxonNodeOutStreamPartitioner.NewInstance(
@@ -316,6 +294,7 @@ public class CdmLightClassificationExport
             List<DescriptionElementBase> simpleFacts = new ArrayList<>();
             List<DescriptionElementBase> specimenFacts = new ArrayList<>();
             List<DescriptionElementBase> distributionFacts = new ArrayList<>();
+            List<DescriptionElementBase> taxonInteractionsFacts = new ArrayList<>();
             List<DescriptionElementBase> commonNameFacts = new ArrayList<>();
             List<DescriptionElementBase> usageFacts = new ArrayList<>();
             for (TaxonDescription description: descriptions){
@@ -328,6 +307,8 @@ public class CdmLightClassificationExport
                             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);
                         }
@@ -346,6 +327,9 @@ public class CdmLightClassificationExport
             if (!simpleFacts.isEmpty()){
                 handleSimpleFacts(state, taxon, simpleFacts);
             }
+            if (!taxonInteractionsFacts.isEmpty()){
+                handleTaxonInteractionsFacts(state, taxon, taxonInteractionsFacts);
+            }
         } else if (cdmBase instanceof TaxonName){
             TaxonName name = CdmBase.deproxy(cdmBase, TaxonName.class);
             Set<TaxonNameDescription> descriptions = name.getDescriptions();
@@ -415,6 +399,35 @@ public class CdmLightClassificationExport
         }
     }
 
+    /**
+     * @param state
+     * @param taxon
+     * @param simpleFacts
+     */
+    private void handleTaxonInteractionsFacts(CdmLightExportState state, CdmBase cdmBase,
+            List<DescriptionElementBase> taxonInteractionsFacts) {
+        CdmLightExportTable table = CdmLightExportTable.TAXON_INTERACTION_FACT;
+        for (DescriptionElementBase element: taxonInteractionsFacts){
+            try {
+
+                    String[] csvLine = new  String[table.getSize()];
+
+                    csvLine[table.getIndex(CdmLightExportTable.FACT_ID)] = getId(state, element);
+                    handleSource(state, element, table);
+                    csvLine[table.getIndex(CdmLightExportTable.TAXON_FK)] = getId(state, cdmBase);
+                    csvLine[table.getIndex(CdmLightExportTable.TAXON2_FK)] = getId(state, ((TaxonInteraction)element).getTaxon2());
+                    csvLine[table.getIndex(CdmLightExportTable.DESCRIPTION)] =  createMultilanguageString(((TaxonInteraction)element).getDescription());
+                    state.getProcessor().put(table, element, csvLine);
+
+            } catch (Exception e) {
+                state.getResult().addException(e, "An unexpected error occurred when handling taxon interaction" +
+                        cdmBaseStr(element) + ": " + e.getMessage());
+            }
+        }
+
+    }
+
+
     /**
      * @param state
      * @param cdmBase
index ca3060b2d39b735b196e5317f0620bc9add54673..2bb2ff120a0789079c68693d0553d1a410ab0c8e 100644 (file)
@@ -27,6 +27,7 @@ public enum CdmLightExportTable {
     SYNONYM("Synonym", synonymColumns()),
     REFERENCE("Reference", referenceColumns()),
     SIMPLE_FACT("SimpleFact", simpleFactsColumns()),
+    TAXON_INTERACTION_FACT("SimpleFact", taxonInteractionFactsColumns()),
     SPECIMEN_FACT("SpecimenFact", specimenFactsColumns()),
     GEOGRAPHIC_AREA_FACT("GeographicAreaFact", geographicAreaFactsColumns()),
     COMMON_NAME_FACT("CommonNameFact", commonNameFactsColumns()),
@@ -191,6 +192,10 @@ public enum CdmLightExportTable {
     protected static final String SPECIMEN_NOTES = "Specimen Notes";
     protected static final String SPECIMEN_DESCRIPTION = "Specimen Description";
 
+    // TaxonInteraction Facts
+    protected static final String TAXON2_FK = "Taxon2_FK";
+    protected static final String DESCRIPTION = "Description";
+
 
     //Geographic Area Facts
     protected static final String AREA_LABEL = "AreaLabel";
@@ -219,7 +224,9 @@ public enum CdmLightExportTable {
     final static String[] nameFactColumns() {
         return new String[]{FACT_ID, NAME_FK, FACT_TEXT, LANGUAGE, MEDIA_URI, FACT_CATEGORY};
     }
-
+    final static String[] taxonInteractionFactsColumns(){
+        return new String[]{FACT_ID, TAXON_FK, TAXON2_FK, DESCRIPTION};
+    }
 
     final static String[] identifierColumns() {