1
|
/**
|
2
|
* Copyright (C) 2017 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
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.
|
8
|
*/
|
9
|
package eu.etaxonomy.cdm.io.cdmLight;
|
10
|
|
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;
|
16
|
import java.util.Map;
|
17
|
|
18
|
import org.apache.commons.io.IOUtils;
|
19
|
|
20
|
import eu.etaxonomy.cdm.io.common.ExportType;
|
21
|
import eu.etaxonomy.cdm.model.common.ICdmBase;
|
22
|
|
23
|
|
24
|
/**
|
25
|
* @author k.luther
|
26
|
* @since 16.03.2017
|
27
|
*
|
28
|
*/
|
29
|
public class CdmLightExportResultProcessor {
|
30
|
|
31
|
private static final String HEADER = "HEADER_207dd23a-f877-4c27-b93a-8dbea3234281";
|
32
|
|
33
|
private Map<CdmLightExportTable, Map<String,String[]>> result = new HashMap<>();
|
34
|
private CdmLightExportState state;
|
35
|
|
36
|
|
37
|
/**
|
38
|
* @param state
|
39
|
*/
|
40
|
public CdmLightExportResultProcessor(CdmLightExportState state) {
|
41
|
super();
|
42
|
this.state = state;
|
43
|
}
|
44
|
|
45
|
|
46
|
/**
|
47
|
* @param taxon
|
48
|
* @param csvLine
|
49
|
*/
|
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());
|
56
|
}
|
57
|
result.put(table, resultMap);
|
58
|
}
|
59
|
String[] record = resultMap.get(id);
|
60
|
if (record == null){
|
61
|
record = csvLine;
|
62
|
|
63
|
String[] oldRecord = resultMap.put(id, record);
|
64
|
|
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);
|
68
|
}
|
69
|
}
|
70
|
}
|
71
|
|
72
|
|
73
|
|
74
|
public boolean hasRecord(CdmLightExportTable table, String id){
|
75
|
Map<String, String[]> resultMap = result.get(table);
|
76
|
if (resultMap == null){
|
77
|
return false;
|
78
|
}else{
|
79
|
return resultMap.get(id) != null;
|
80
|
}
|
81
|
}
|
82
|
|
83
|
public String[] getRecord(CdmLightExportTable table, String id){
|
84
|
return result.get(table).get(id);
|
85
|
|
86
|
}
|
87
|
|
88
|
/**
|
89
|
* @param table
|
90
|
* @param taxon
|
91
|
* @param csvLine
|
92
|
*/
|
93
|
public void put(CdmLightExportTable table, ICdmBase cdmBase, String[] csvLine) {
|
94
|
this.put(table, cdmBase.getUuid().toString(), csvLine);
|
95
|
}
|
96
|
|
97
|
|
98
|
/**
|
99
|
* @return
|
100
|
*/
|
101
|
public void createFinalResult(CdmLightExportState state) {
|
102
|
|
103
|
if (!result.isEmpty() ){
|
104
|
state.setAuthorStore(new HashMap<>());
|
105
|
state.setHomotypicalGroupStore(new HashMap<>());
|
106
|
state.setReferenceStore(new HashMap<>());
|
107
|
state.setSpecimenStore(new HashMap<>());
|
108
|
state.setNodeChildrenMap(new HashMap<>());
|
109
|
//Replace quotes by double quotes
|
110
|
for (CdmLightExportTable table: result.keySet()){
|
111
|
//schreibe jede Tabelle in einen Stream...
|
112
|
Map<String, String[]> tableData = result.get(table);
|
113
|
CdmLightExportConfigurator config = state.getConfig();
|
114
|
ByteArrayOutputStream exportStream = new ByteArrayOutputStream();
|
115
|
|
116
|
try{
|
117
|
List<String> data = new ArrayList<>();
|
118
|
String[] csvHeaderLine = tableData.get(HEADER);
|
119
|
String lineString = createCsvLine(config, csvHeaderLine);
|
120
|
lineString = lineString+ "";
|
121
|
data.add(lineString);
|
122
|
for (String key: tableData.keySet()){
|
123
|
if (!key.equals(HEADER)){
|
124
|
String[] csvLine = tableData.get(key);
|
125
|
|
126
|
lineString = createCsvLine(config, csvLine);
|
127
|
data.add(lineString);
|
128
|
}
|
129
|
}
|
130
|
IOUtils.writeLines(data,
|
131
|
null,exportStream,
|
132
|
Charset.forName("UTF-8"));
|
133
|
} catch(Exception e){
|
134
|
state.getResult().addException(e, e.getMessage());
|
135
|
}
|
136
|
|
137
|
state.getResult().putExportData(table.getTableName(), exportStream.toByteArray());
|
138
|
state.getResult().setExportType(ExportType.CDM_LIGHT);
|
139
|
|
140
|
}
|
141
|
}
|
142
|
result.clear();
|
143
|
}
|
144
|
|
145
|
|
146
|
/**
|
147
|
* @param config
|
148
|
* @param csvLine
|
149
|
* @return
|
150
|
*/
|
151
|
private String createCsvLine(CdmLightExportConfigurator config, String[] csvLine) {
|
152
|
String lineString = "";
|
153
|
boolean first = true;
|
154
|
for (String columnEntry: csvLine){
|
155
|
if (columnEntry == null){
|
156
|
columnEntry = "";
|
157
|
}
|
158
|
columnEntry = columnEntry.replace("\"", "\"\"");
|
159
|
columnEntry = columnEntry.replace(config.getLinesTerminatedBy(), "\\r");
|
160
|
//replace all line brakes according to best practices: http://code.google.com/p/gbif-ecat/wiki/BestPractices
|
161
|
columnEntry = columnEntry.replace("\r\n", "\\r");
|
162
|
columnEntry = columnEntry.replace("\r", "\\r");
|
163
|
columnEntry = columnEntry.replace("\n", "\\r");
|
164
|
if (first){
|
165
|
lineString += config.getFieldsEnclosedBy() + columnEntry + config.getFieldsEnclosedBy() ;
|
166
|
first = false;
|
167
|
}else{
|
168
|
lineString += config.getFieldsTerminatedBy() + config.getFieldsEnclosedBy() + columnEntry + config.getFieldsEnclosedBy() ;
|
169
|
}
|
170
|
}
|
171
|
|
172
|
return lineString;
|
173
|
}
|
174
|
}
|