ref #7903: add comparator for taxa in cdm light configurator
[cdmlib.git] / cdmlib-io / src / main / java / eu / etaxonomy / cdm / io / cdmLight / CdmLightExportResultProcessor.java
index 0ca779d26fc280fa75e850ade6066b5182196831..3899075959ebbb84d817858aeed8e199a9190754 100755 (executable)
@@ -9,7 +9,6 @@
 package eu.etaxonomy.cdm.io.cdmLight;
 
 import java.io.ByteArrayOutputStream;
-import java.io.IOException;
 import java.nio.charset.Charset;
 import java.util.ArrayList;
 import java.util.HashMap;
@@ -18,14 +17,13 @@ import java.util.Map;
 
 import org.apache.commons.io.IOUtils;
 
-import eu.etaxonomy.cdm.io.common.ExportResult;
-import eu.etaxonomy.cdm.io.common.ExportResultType;
+import eu.etaxonomy.cdm.io.common.ExportType;
 import eu.etaxonomy.cdm.model.common.ICdmBase;
 
 
 /**
  * @author k.luther
- * @date 16.03.2017
+ * @since 16.03.2017
  *
  */
 public class CdmLightExportResultProcessor {
@@ -33,7 +31,7 @@ public class CdmLightExportResultProcessor {
     private static final String HEADER = "HEADER_207dd23a-f877-4c27-b93a-8dbea3234281";
 
     private Map<CdmLightExportTable, Map<String,String[]>> result = new HashMap<>();
-    //private CdmLightExportState state;
+    private CdmLightExportState state;
 
 
     /**
@@ -41,7 +39,7 @@ public class CdmLightExportResultProcessor {
      */
     public CdmLightExportResultProcessor(CdmLightExportState state) {
         super();
-        //this.state = state;
+        this.state = state;
     }
 
 
@@ -49,7 +47,7 @@ public class CdmLightExportResultProcessor {
      * @param taxon
      * @param csvLine
      */
-    public void put(CdmLightExportTable table, String id, String[] csvLine, CdmLightExportState state) {
+    public void put(CdmLightExportTable table, String id, String[] csvLine) {
         Map<String,String[]> resultMap = result.get(table);
         if (resultMap == null ){
             resultMap = new HashMap<>();
@@ -65,7 +63,8 @@ public class CdmLightExportResultProcessor {
             String[] oldRecord = resultMap.put(id, record);
 
             if (oldRecord != null){
-                System.out.println("This should not happen");
+                String message = "Output processor already has a record for id " + id + ". This should not happen.";
+                state.getResult().addWarning(message);
             }
         }
     }
@@ -81,14 +80,18 @@ public class CdmLightExportResultProcessor {
         }
     }
 
+    public  String[] getRecord(CdmLightExportTable table, String id){
+        return result.get(table).get(id);
+
+    }
 
     /**
      * @param table
      * @param taxon
      * @param csvLine
      */
-    public void put(CdmLightExportTable table, ICdmBase cdmBase, String[] csvLine, CdmLightExportState state) {
-       this.put(table, String.valueOf(cdmBase.getId()), csvLine, state);
+    public void put(CdmLightExportTable table, ICdmBase cdmBase, String[] csvLine) {
+       this.put(table, cdmBase.getUuid().toString(), csvLine);
     }
 
 
@@ -96,9 +99,13 @@ public class CdmLightExportResultProcessor {
      * @return
      */
     public void createFinalResult(CdmLightExportState state) {
-        ExportResult finalResult = ExportResult.NewInstance(ExportResultType.MAP_BYTE_ARRAY);
 
         if (!result.isEmpty() ){
+            state.setAuthorStore(new HashMap<>());
+            state.setHomotypicalGroupStore(new HashMap<>());
+            state.setReferenceStore(new HashMap<>());
+            state.setSpecimenStore(new HashMap<>());
+            state.setNodeChildrenMap(new HashMap<>());
             //Replace quotes by double quotes
             for (CdmLightExportTable table: result.keySet()){
                 //schreibe jede Tabelle in einen Stream...
@@ -107,7 +114,7 @@ public class CdmLightExportResultProcessor {
                 ByteArrayOutputStream exportStream = new ByteArrayOutputStream();
 
                 try{
-                    List<String> data = new ArrayList<String>();
+                    List<String> data = new ArrayList<>();
                     String[] csvHeaderLine = tableData.get(HEADER);
                     String lineString = createCsvLine(config, csvHeaderLine);
                     lineString = lineString+ "";
@@ -123,17 +130,16 @@ public class CdmLightExportResultProcessor {
                     IOUtils.writeLines(data,
                             null,exportStream,
                             Charset.forName("UTF-8"));
-                } catch(IOException e){
-                    finalResult.addException(e, e.getMessage());
+                } catch(Exception e){
+                    state.getResult().addException(e, e.getMessage());
                 }
 
-                finalResult.putExportData(table.getTableName(), exportStream.toByteArray());
+                state.getResult().putExportData(table.getTableName(), exportStream.toByteArray());
+                state.getResult().setExportType(ExportType.CDM_LIGHT);
 
             }
         }
         result.clear();
-
-        state.setResult(finalResult);
     }