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.excel.stream;
|
10
|
|
11
|
import java.io.IOException;
|
12
|
import java.net.URI;
|
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.lang.StringUtils;
|
19
|
import org.apache.http.HttpException;
|
20
|
import org.apache.log4j.Logger;
|
21
|
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
|
22
|
import org.apache.poi.ss.usermodel.Sheet;
|
23
|
import org.apache.poi.ss.usermodel.Workbook;
|
24
|
import org.apache.poi.ss.usermodel.WorkbookFactory;
|
25
|
|
26
|
import eu.etaxonomy.cdm.common.UriUtils;
|
27
|
import eu.etaxonomy.cdm.common.monitor.IProgressMonitor;
|
28
|
import eu.etaxonomy.cdm.io.stream.IReader;
|
29
|
import eu.etaxonomy.cdm.io.stream.ListReader;
|
30
|
import eu.etaxonomy.cdm.io.stream.terms.TermUri;
|
31
|
|
32
|
/**
|
33
|
* This class transforms excel archive in to a InputStream.
|
34
|
*
|
35
|
* @author a.oppermann
|
36
|
\* @since 16.05.2013
|
37
|
*
|
38
|
*/
|
39
|
public class ExcelToStreamConverter<STATE extends ExcelStreamImportState> {
|
40
|
|
41
|
private static Logger logger = Logger.getLogger(ExcelToStreamConverter.class);
|
42
|
|
43
|
private URI source;
|
44
|
|
45
|
/**
|
46
|
*
|
47
|
* Factory
|
48
|
* @param source
|
49
|
* @return
|
50
|
*/
|
51
|
public static ExcelToStreamConverter<ExcelStreamImportState> NewInstance(URI source) {
|
52
|
return new ExcelToStreamConverter<ExcelStreamImportState>(source);
|
53
|
}
|
54
|
|
55
|
/**
|
56
|
* Constructor
|
57
|
* @param source
|
58
|
*/
|
59
|
public ExcelToStreamConverter(URI source){
|
60
|
this.source = source;
|
61
|
}
|
62
|
|
63
|
/**
|
64
|
* @param state
|
65
|
* @return
|
66
|
* @throws HttpException
|
67
|
* @throws IOException
|
68
|
* @throws InvalidFormatException
|
69
|
*/
|
70
|
public IReader<ExcelRecordStream> getWorksheetStream(STATE state) throws IOException, HttpException, InvalidFormatException{
|
71
|
// POIFSFileSystem fs = new POIFSFileSystem(UriUtils.getInputStream(source));
|
72
|
// HSSFWorkbook wb = new HSSFWorkbook(fs);
|
73
|
Workbook wb = WorkbookFactory.create(UriUtils.getInputStream(source));
|
74
|
|
75
|
Map<TermUri, Integer> map = new HashMap<TermUri, Integer>();
|
76
|
for (int i = 0 ; i < wb.getNumberOfSheets(); i++){
|
77
|
String wsName = wb.getSheetName(i);
|
78
|
TermUri termUri = convertSheetName2TermUri(wsName);
|
79
|
if (map.get(termUri) != null){
|
80
|
String message = "Worksheet type exists more then once: %s";
|
81
|
//TODO fire event
|
82
|
logger.warn(String.format(message, termUri.toString()));
|
83
|
}
|
84
|
map.put(termUri, i);
|
85
|
}
|
86
|
|
87
|
//core
|
88
|
List<ExcelRecordStream> streamList = new ArrayList<>();
|
89
|
TermUri term= TermUri.DWC_TAXON;
|
90
|
Integer i = map.get(term);
|
91
|
if (i != null){
|
92
|
Sheet ws = wb.getSheetAt(i);
|
93
|
ExcelRecordStream excelRecordStream = new ExcelRecordStream(state, ws, term);
|
94
|
streamList.add(excelRecordStream); //for taxa and names
|
95
|
}else{
|
96
|
String message = "Taxon worksheet not available for %s";
|
97
|
logger.warn(String.format(message, "taxa"));
|
98
|
state.setSuccess(false);
|
99
|
}
|
100
|
|
101
|
//core relationships
|
102
|
i = map.get(term);
|
103
|
if (i != null){
|
104
|
Sheet ws = wb.getSheetAt(i);
|
105
|
ExcelRecordStream excelRecordStream = new ExcelRecordStream(state, ws, term);
|
106
|
streamList.add(excelRecordStream); //for relationships
|
107
|
}else{
|
108
|
String message = "Taxon worksheet not available for %s";
|
109
|
logger.warn(String.format(message, "taxon relations"));
|
110
|
state.setSuccess(false);
|
111
|
}
|
112
|
|
113
|
return new ListReader<>(streamList);
|
114
|
}
|
115
|
|
116
|
|
117
|
/**
|
118
|
* @param wsName
|
119
|
* @return
|
120
|
*/
|
121
|
private TermUri convertSheetName2TermUri(String wsName) {
|
122
|
if (StringUtils.isBlank(wsName)){
|
123
|
throw new IllegalArgumentException("Worksheet name must not be null or empty");
|
124
|
//FIXME: Hard coded worksheet name should be avoided
|
125
|
}else if(wsName.equalsIgnoreCase("Sheet1")){
|
126
|
return TermUri.DWC_TAXON;
|
127
|
}else{
|
128
|
String message = "Worksheet name %s not yet handled by %s";
|
129
|
throw new IllegalArgumentException(String.format(message, wsName, this.getClass().getSimpleName()));
|
130
|
}
|
131
|
}
|
132
|
|
133
|
public void warnProgress(STATE state, String message, Throwable e) {
|
134
|
if(state.getConfig().getProgressMonitor() != null){
|
135
|
IProgressMonitor monitor = state.getConfig().getProgressMonitor();
|
136
|
if (e == null) {
|
137
|
monitor.warning(message);
|
138
|
}else{
|
139
|
monitor.warning(message, e);
|
140
|
}
|
141
|
}
|
142
|
}
|
143
|
|
144
|
}
|