1
|
/**
|
2
|
* Copyright (C) 2009 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.dwca.in;
|
10
|
|
11
|
import java.io.IOException;
|
12
|
import java.util.HashMap;
|
13
|
import java.util.Map;
|
14
|
|
15
|
import org.apache.log4j.Logger;
|
16
|
|
17
|
import au.com.bytecode.opencsv.CSVReader;
|
18
|
import eu.etaxonomy.cdm.common.CdmUtils;
|
19
|
import eu.etaxonomy.cdm.io.common.IIoObservable;
|
20
|
import eu.etaxonomy.cdm.io.common.ObservableBase;
|
21
|
import eu.etaxonomy.cdm.io.dwca.TermUri;
|
22
|
import eu.etaxonomy.cdm.io.dwca.jaxb.ArchiveEntryBase;
|
23
|
import eu.etaxonomy.cdm.io.dwca.jaxb.Core;
|
24
|
import eu.etaxonomy.cdm.io.dwca.jaxb.Extension;
|
25
|
import eu.etaxonomy.cdm.io.dwca.jaxb.Field;
|
26
|
import eu.etaxonomy.cdm.io.stream.IItemStream;
|
27
|
import eu.etaxonomy.cdm.io.stream.StreamItem;
|
28
|
|
29
|
/**
|
30
|
* @author a.mueller
|
31
|
* @date 17.10.2011
|
32
|
*
|
33
|
*/
|
34
|
public class CsvStream extends ObservableBase implements IIoObservable,IItemStream{
|
35
|
private static final long serialVersionUID = -4828538731087241715L;
|
36
|
|
37
|
@SuppressWarnings("unused")
|
38
|
private static Logger logger = Logger.getLogger(CsvStream.class);
|
39
|
|
40
|
private final CSVReader csvReader;
|
41
|
private final ArchiveEntryBase archiveEntry;
|
42
|
private final TermUri term;
|
43
|
private int line = 0;
|
44
|
|
45
|
private StreamItem next;
|
46
|
|
47
|
public CsvStream (CSVReader csvReader, ArchiveEntryBase archiveEntry, int startLine){
|
48
|
this.csvReader = csvReader;
|
49
|
this.archiveEntry = archiveEntry;
|
50
|
String rowType = archiveEntry.getRowType();
|
51
|
term = TermUri.valueOfUriString(rowType);
|
52
|
line = startLine;
|
53
|
|
54
|
//FIXME what if null?
|
55
|
}
|
56
|
|
57
|
@Override
|
58
|
public boolean hasNext(){
|
59
|
if (next != null){
|
60
|
return true;
|
61
|
}else{
|
62
|
next = readMe();
|
63
|
return (next != null);
|
64
|
}
|
65
|
}
|
66
|
|
67
|
@Override
|
68
|
public StreamItem read(){
|
69
|
// line++;
|
70
|
return readMe();
|
71
|
}
|
72
|
|
73
|
@Override
|
74
|
public String getItemLocation() {
|
75
|
return CdmUtils.concat("/", this.getStreamLocation() ,String.valueOf(line));
|
76
|
}
|
77
|
|
78
|
private StreamItem readMe(){
|
79
|
StreamItem resultItem;
|
80
|
Map<String, String> resultMap;
|
81
|
if (next != null){
|
82
|
resultItem = next;
|
83
|
next = null;
|
84
|
return resultItem;
|
85
|
}else{
|
86
|
resultMap = new HashMap<>();
|
87
|
try {
|
88
|
String[] next = csvReader.readNext();
|
89
|
line++;
|
90
|
resultItem = new StreamItem(term, null, getItemLocation());
|
91
|
if (next == null){
|
92
|
return null;
|
93
|
}
|
94
|
for (Field field : archiveEntry.getField()){
|
95
|
int index = field.getIndex();
|
96
|
if (index > next.length -1){
|
97
|
int missingFields = archiveEntry.getField().size() - (next.length -1);
|
98
|
String message = "Missing value for archive entry %s %s in line %d of input %s.";
|
99
|
String msgMoreLines = missingFields <= 1? "": "and %d more fields" ;
|
100
|
msgMoreLines = String.format(msgMoreLines,(missingFields - 1) );
|
101
|
message = String.format(message, field.getTerm(), msgMoreLines, line, archiveEntry.getFiles().getLocation());
|
102
|
|
103
|
if (countObservers() == 0){
|
104
|
throw new RuntimeException(message);
|
105
|
} else {
|
106
|
StringBuilder messageSB = new StringBuilder(message);
|
107
|
messageSB.append(" Line is only partly imported. Csv-Array is [");// + next.toString();
|
108
|
|
109
|
for(int i=0;i<next.length;i++) {
|
110
|
messageSB.append(next[i]);
|
111
|
if(i < next.length-1){
|
112
|
messageSB.append(",");
|
113
|
} else {
|
114
|
messageSB.append("]");
|
115
|
}
|
116
|
}
|
117
|
message = messageSB.toString();
|
118
|
fireWarningEvent(message, resultItem.getLocation(), 4);
|
119
|
break;
|
120
|
}
|
121
|
}
|
122
|
String value = field.getDefault()!= null? field.getDefault(): next[index];
|
123
|
String term = field.getTerm();
|
124
|
resultMap.put(term, value);
|
125
|
}
|
126
|
if (archiveEntry instanceof Core){
|
127
|
Core core = (Core)archiveEntry;
|
128
|
resultMap.put("id", next[core.getId().getIndex()]);
|
129
|
}else if(archiveEntry instanceof Extension){
|
130
|
Extension extension = (Extension)archiveEntry;
|
131
|
resultMap.put("coreId", next[extension.getCoreid().getIndex()]);
|
132
|
}else{
|
133
|
throw new RuntimeException("Unhandled archiveEntry type");
|
134
|
}
|
135
|
|
136
|
|
137
|
} catch (IOException e) {
|
138
|
//TODO handle as event
|
139
|
throw new RuntimeException(e);
|
140
|
}
|
141
|
resultItem.map = resultMap;
|
142
|
if (resultItem.map == null){
|
143
|
return null;
|
144
|
}else {
|
145
|
return resultItem;
|
146
|
}
|
147
|
|
148
|
}
|
149
|
}
|
150
|
|
151
|
public int getLine(){
|
152
|
return line;
|
153
|
}
|
154
|
|
155
|
|
156
|
/**
|
157
|
* @return the term
|
158
|
*/
|
159
|
@Override
|
160
|
public TermUri getTerm() {
|
161
|
return term;
|
162
|
}
|
163
|
|
164
|
@Override
|
165
|
public String getStreamLocation() {
|
166
|
return this.archiveEntry.getFiles().getLocation();
|
167
|
}
|
168
|
|
169
|
//******************************** TO STRING **************************************/
|
170
|
|
171
|
@Override
|
172
|
public String toString(){
|
173
|
if (archiveEntry == null){
|
174
|
return super.toString();
|
175
|
}else{
|
176
|
return "CsvStream for " + CdmUtils.Nz(archiveEntry.getRowType() + " at line " + line);
|
177
|
}
|
178
|
}
|
179
|
|
180
|
|
181
|
}
|