2 * Copyright (C) 2009 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
.stream
;
11 import java
.io
.IOException
;
12 import java
.util
.HashMap
;
15 import org
.apache
.logging
.log4j
.LogManager
;import org
.apache
.logging
.log4j
.Logger
;
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
.jaxb
.ArchiveEntryBase
;
22 import eu
.etaxonomy
.cdm
.io
.dwca
.jaxb
.Core
;
23 import eu
.etaxonomy
.cdm
.io
.dwca
.jaxb
.Extension
;
24 import eu
.etaxonomy
.cdm
.io
.dwca
.jaxb
.Field
;
25 import eu
.etaxonomy
.cdm
.io
.stream
.terms
.TermUri
;
32 public class CsvStream
extends ObservableBase
implements IIoObservable
,IItemStream
{
33 private static final long serialVersionUID
= -4828538731087241715L;
35 @SuppressWarnings("unused")
36 private static Logger logger
= LogManager
.getLogger(CsvStream
.class);
38 private final CSVReader csvReader
;
39 private final ArchiveEntryBase archiveEntry
;
40 private final TermUri term
;
43 private StreamItem next
;
45 public CsvStream (CSVReader csvReader
, ArchiveEntryBase archiveEntry
, int startLine
){
46 this.csvReader
= csvReader
;
47 this.archiveEntry
= archiveEntry
;
48 String rowType
= archiveEntry
.getRowType();
49 term
= TermUri
.valueOfUriString(rowType
);
56 public boolean hasNext(){
61 return (next
!= null);
66 public StreamItem
read(){
72 public String
getItemLocation() {
73 return CdmUtils
.concat("/", this.getStreamLocation() ,String
.valueOf(line
));
76 private StreamItem
readMe(){
77 StreamItem resultItem
;
78 Map
<String
, String
> resultMap
;
84 resultMap
= new HashMap
<>();
86 String
[] next
= csvReader
.readNext();
88 resultItem
= new StreamItem(term
, null, getItemLocation());
92 for (Field field
: archiveEntry
.getField()){
93 int index
= field
.getIndex();
94 if (index
> next
.length
-1){
95 int missingFields
= archiveEntry
.getField().size() - (next
.length
-1);
96 String message
= "Missing value for archive entry %s %s in line %d of input %s.";
97 String msgMoreLines
= missingFields
<= 1?
"": "and %d more fields" ;
98 msgMoreLines
= String
.format(msgMoreLines
,(missingFields
- 1) );
99 message
= String
.format(message
, field
.getTerm(), msgMoreLines
, line
, archiveEntry
.getFiles().getLocation());
101 if (countObservers() == 0){
102 throw new RuntimeException(message
);
104 StringBuilder messageSB
= new StringBuilder(message
);
105 messageSB
.append(" Line is only partly imported. Csv-Array is [");// + next.toString();
107 for(int i
=0;i
<next
.length
;i
++) {
108 messageSB
.append(next
[i
]);
109 if(i
< next
.length
-1){
110 messageSB
.append(",");
112 messageSB
.append("]");
115 message
= messageSB
.toString();
116 fireWarningEvent(message
, resultItem
.getLocation(), 4);
120 String value
= field
.getDefault()!= null? field
.getDefault(): next
[index
];
121 String term
= field
.getTerm();
122 resultMap
.put(term
, value
);
124 if (archiveEntry
instanceof Core
){
125 Core core
= (Core
)archiveEntry
;
126 resultMap
.put("id", next
[core
.getId().getIndex()]);
127 }else if(archiveEntry
instanceof Extension
){
128 Extension extension
= (Extension
)archiveEntry
;
129 resultMap
.put("coreId", next
[extension
.getCoreid().getIndex()]);
131 throw new RuntimeException("Unhandled archiveEntry type");
135 } catch (IOException e
) {
136 //TODO handle as event
137 throw new RuntimeException(e
);
139 resultItem
.map
= resultMap
;
140 if (resultItem
.map
== null){
149 public int getLine(){
158 public TermUri
getTerm() {
163 public String
getStreamLocation() {
164 return this.archiveEntry
.getFiles().getLocation();
167 //******************************** TO STRING **************************************/
170 public String
toString(){
171 if (archiveEntry
== null){
172 return super.toString();
174 return "CsvStream for " + CdmUtils
.Nz(archiveEntry
.getRowType() + " at line " + line
);