ref #6241 change @date to @since
[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
84 /**
85 * @param table
86 * @param taxon
87 * @param csvLine
88 */
89 public void put(CdmLightExportTable table, ICdmBase cdmBase, String[] csvLine) {
90 this.put(table, String.valueOf(cdmBase.getId()), csvLine);
91 }
92
93
94 /**
95 * @return
96 */
97 public void createFinalResult(CdmLightExportState state) {
98
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();
110
111 try{
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);
120
121 lineString = createCsvLine(config, csvLine);
122 data.add(lineString);
123 }
124 }
125 IOUtils.writeLines(data,
126 null,exportStream,
127 Charset.forName("UTF-8"));
128 } catch(Exception e){
129 state.getResult().addException(e, e.getMessage());
130 }
131
132 state.getResult().putExportData(table.getTableName(), exportStream.toByteArray());
133 state.getResult().setExportType(ExportType.CDM_LIGHT);
134
135 }
136 }
137 result.clear();
138 }
139
140
141 /**
142 * @param config
143 * @param csvLine
144 * @return
145 */
146 private String createCsvLine(CdmLightExportConfigurator config, String[] csvLine) {
147 String lineString = "";
148 boolean first = true;
149 for (String columnEntry: csvLine){
150 if (columnEntry == null){
151 columnEntry = "";
152 }
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");
159 if (first){
160 lineString += config.getFieldsEnclosedBy() + columnEntry + config.getFieldsEnclosedBy() ;
161 first = false;
162 }else{
163 lineString += config.getFieldsTerminatedBy() + config.getFieldsEnclosedBy() + columnEntry + config.getFieldsEnclosedBy() ;
164 }
165 }
166
167 return lineString;
168 }
169 }