2 * Copyright (C) 2017 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
9 package eu
.etaxonomy
.cdm
.io
.cdmLight
;
11 import java
.io
.ByteArrayOutputStream
;
12 import java
.nio
.charset
.Charset
;
13 import java
.util
.ArrayList
;
14 import java
.util
.HashMap
;
15 import java
.util
.List
;
18 import org
.apache
.commons
.io
.IOUtils
;
20 import eu
.etaxonomy
.cdm
.io
.common
.ExportType
;
21 import eu
.etaxonomy
.cdm
.model
.common
.ICdmBase
;
29 public class CdmLightExportResultProcessor
{
31 private static final String HEADER
= "HEADER_207dd23a-f877-4c27-b93a-8dbea3234281";
33 private Map
<CdmLightExportTable
, Map
<String
,String
[]>> result
= new HashMap
<>();
34 private CdmLightExportState state
;
40 public CdmLightExportResultProcessor(CdmLightExportState state
) {
50 public void put(CdmLightExportTable table
, String id
, String
[] csvLine
) {
51 Map
<String
,String
[]> resultMap
= result
.get(table
);
52 if (resultMap
== null ){
53 resultMap
= new HashMap
<>();
54 if (state
.getConfig().isHasHeaderLines()){
55 resultMap
.put(HEADER
, table
.getColumnNames());
57 result
.put(table
, resultMap
);
59 String
[] record
= resultMap
.get(id
);
63 String
[] oldRecord
= resultMap
.put(id
, record
);
65 if (oldRecord
!= null){
66 String message
= "Output processor already has a record for id " + id
+ ". This should not happen.";
67 state
.getResult().addWarning(message
);
74 public boolean hasRecord(CdmLightExportTable table
, String id
){
75 Map
<String
, String
[]> resultMap
= result
.get(table
);
76 if (resultMap
== null){
79 return resultMap
.get(id
) != null;
89 public void put(CdmLightExportTable table
, ICdmBase cdmBase
, String
[] csvLine
) {
90 this.put(table
, String
.valueOf(cdmBase
.getId()), csvLine
);
97 public void createFinalResult(CdmLightExportState state
) {
99 if (!result
.isEmpty() ){
100 state
.setAuthorStore(new HashMap
<>());
101 state
.setHomotypicalGroupStore(new HashMap
<>());
102 state
.setReferenceStore(new HashMap
<>());
103 state
.setSpecimenStore(new HashMap
<>());
104 //Replace quotes by double quotes
105 for (CdmLightExportTable table
: result
.keySet()){
106 //schreibe jede Tabelle in einen Stream...
107 Map
<String
, String
[]> tableData
= result
.get(table
);
108 CdmLightExportConfigurator config
= state
.getConfig();
109 ByteArrayOutputStream exportStream
= new ByteArrayOutputStream();
112 List
<String
> data
= new ArrayList
<>();
113 String
[] csvHeaderLine
= tableData
.get(HEADER
);
114 String lineString
= createCsvLine(config
, csvHeaderLine
);
115 lineString
= lineString
+ "";
116 data
.add(lineString
);
117 for (String key
: tableData
.keySet()){
118 if (!key
.equals(HEADER
)){
119 String
[] csvLine
= tableData
.get(key
);
121 lineString
= createCsvLine(config
, csvLine
);
122 data
.add(lineString
);
125 IOUtils
.writeLines(data
,
127 Charset
.forName("UTF-8"));
128 } catch(Exception e
){
129 state
.getResult().addException(e
, e
.getMessage());
132 state
.getResult().putExportData(table
.getTableName(), exportStream
.toByteArray());
133 state
.getResult().setExportType(ExportType
.CDM_LIGHT
);
146 private String
createCsvLine(CdmLightExportConfigurator config
, String
[] csvLine
) {
147 String lineString
= "";
148 boolean first
= true;
149 for (String columnEntry
: csvLine
){
150 if (columnEntry
== null){
153 columnEntry
= columnEntry
.replace("\"", "\"\"");
154 columnEntry
= columnEntry
.replace(config
.getLinesTerminatedBy(), "\\r");
155 //replace all line brakes according to best practices: http://code.google.com/p/gbif-ecat/wiki/BestPractices
156 columnEntry
= columnEntry
.replace("\r\n", "\\r");
157 columnEntry
= columnEntry
.replace("\r", "\\r");
158 columnEntry
= columnEntry
.replace("\n", "\\r");
160 lineString
+= config
.getFieldsEnclosedBy() + columnEntry
+ config
.getFieldsEnclosedBy() ;
163 lineString
+= config
.getFieldsTerminatedBy() + config
.getFieldsEnclosedBy() + columnEntry
+ config
.getFieldsEnclosedBy() ;