3ec3ddff885bcb97d890dac465b5ac9f23c9ef2e
[cdmlib.git] / cdmlib-io / src / main / java / eu / etaxonomy / cdm / io / cdmLight / CdmLightExportResultProcessor.java
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 //Replace quotes by double quotes
109 for (CdmLightExportTable table: result.keySet()){
110 //schreibe jede Tabelle in einen Stream...
111 Map<String, String[]> tableData = result.get(table);
112 CdmLightExportConfigurator config = state.getConfig();
113 ByteArrayOutputStream exportStream = new ByteArrayOutputStream();
114
115 try{
116 List<String> data = new ArrayList<>();
117 String[] csvHeaderLine = tableData.get(HEADER);
118 String lineString = createCsvLine(config, csvHeaderLine);
119 lineString = lineString+ "";
120 data.add(lineString);
121 for (String key: tableData.keySet()){
122 if (!key.equals(HEADER)){
123 String[] csvLine = tableData.get(key);
124
125 lineString = createCsvLine(config, csvLine);
126 data.add(lineString);
127 }
128 }
129 IOUtils.writeLines(data,
130 null,exportStream,
131 Charset.forName("UTF-8"));
132 } catch(Exception e){
133 state.getResult().addException(e, e.getMessage());
134 }
135
136 state.getResult().putExportData(table.getTableName(), exportStream.toByteArray());
137 state.getResult().setExportType(ExportType.CDM_LIGHT);
138
139 }
140 }
141 result.clear();
142 }
143
144
145 /**
146 * @param config
147 * @param csvLine
148 * @return
149 */
150 private String createCsvLine(CdmLightExportConfigurator config, String[] csvLine) {
151 String lineString = "";
152 boolean first = true;
153 for (String columnEntry: csvLine){
154 if (columnEntry == null){
155 columnEntry = "";
156 }
157 columnEntry = columnEntry.replace("\"", "\"\"");
158 columnEntry = columnEntry.replace(config.getLinesTerminatedBy(), "\\r");
159 //replace all line brakes according to best practices: http://code.google.com/p/gbif-ecat/wiki/BestPractices
160 columnEntry = columnEntry.replace("\r\n", "\\r");
161 columnEntry = columnEntry.replace("\r", "\\r");
162 columnEntry = columnEntry.replace("\n", "\\r");
163 if (first){
164 lineString += config.getFieldsEnclosedBy() + columnEntry + config.getFieldsEnclosedBy() ;
165 first = false;
166 }else{
167 lineString += config.getFieldsTerminatedBy() + config.getFieldsEnclosedBy() + columnEntry + config.getFieldsEnclosedBy() ;
168 }
169 }
170
171 return lineString;
172 }
173 }