Project

General

Profile

Revision 246110b6

ID246110b64ada659959176d3ba75c89651e72fcd3
Parent 209db2e9
Child 0e29da7a

Added by Alexander Oppermann over 8 years ago

rename package

View differences:

.gitattributes
390 390
cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/excel/common/ExcelTaxonOrSpecimenImportBase.java -text
391 391
cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/excel/distribution/DistributionImport.java -text
392 392
cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/excel/distribution/DistributionImportConfigurator.java -text
393
cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/excel/stream/ExcelMapReader.java -text
394
cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/excel/stream/ExcelRecordStream.java -text
395
cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/excel/stream/ExcelStreamImport.java -text
396
cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/excel/stream/ExcelStreamImportConfigurator.java -text
397
cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/excel/stream/ExcelStreamImportState.java -text
398
cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/excel/stream/ExcelToStreamConverter.java -text
393 399
cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/excel/taxa/NormalExplicitImport.java -text
394 400
cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/excel/taxa/NormalExplicitImportConfigurator.java -text
395 401
cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/excel/taxa/NormalExplicitRow.java -text
cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/excel/stream/ExcelMapReader.java
1
// $Id$
2
/**
3
* Copyright (C) 2009 EDIT
4
* European Distributed Institute of Taxonomy 
5
* http://www.e-taxonomy.eu
6
* 
7
* The contents of this file are subject to the Mozilla Public License Version 1.1
8
* See LICENSE.TXT at the top of this package for the full license terms.
9
*/
10
package eu.etaxonomy.cdm.io.excel.stream;
11

  
12
import java.util.HashMap;
13
import java.util.List;
14
import java.util.Set;
15

  
16
import eu.etaxonomy.cdm.io.common.ObservableBase;
17
import eu.etaxonomy.cdm.io.common.events.IIoObserver;
18
import eu.etaxonomy.cdm.io.dwca.TermUri;
19
import eu.etaxonomy.cdm.io.stream.IItemStream;
20
import eu.etaxonomy.cdm.io.stream.StreamItem;
21

  
22
/**
23
 * @author a.oppermann
24
 * @date 24.05.2013
25
 *
26
 */
27
public class ExcelMapReader extends ObservableBase implements IItemStream {
28

  
29
	private List<HashMap<String, String>> innerStream;
30
	int index = 0;
31
	private TermUri termUri;
32
	private String streamLocation;
33
	
34
	/**
35
	 * @param innerStream
36
	 */
37
	public ExcelMapReader(List<HashMap<String, String>> innerStream, TermUri termUri, String streamLocation) {
38
		super();
39
		this.innerStream = innerStream;
40
		index = 0;
41
		this.termUri = termUri;
42
		this.streamLocation = streamLocation;
43
	}
44

  
45
	/* (non-Javadoc)
46
	 * @see eu.etaxonomy.cdm.io.dwca.in.IReader#read()
47
	 */
48
	@Override
49
	public StreamItem read() {
50
		HashMap<String, String> map = innerStream.get(index++);
51
		StreamItem item = convertMapToItem(map);
52
		return item;
53
	}
54

  
55
	/**
56
	 * @param map
57
	 * @return
58
	 */
59
	private StreamItem convertMapToItem(HashMap<String, String> map) {
60
		StreamItem result = new StreamItem(termUri, map, streamLocation);
61
		return result;
62
	}
63

  
64
	/* (non-Javadoc)
65
	 * @see eu.etaxonomy.cdm.io.dwca.in.IReader#hasNext()
66
	 */
67
	@Override
68
	public boolean hasNext() {
69
		return innerStream.size() > index;
70
	}
71

  
72
	/* (non-Javadoc)
73
	 * @see eu.etaxonomy.cdm.io.stream.IItemStream#getTerm()
74
	 */
75
	@Override
76
	public TermUri getTerm() {
77
		return termUri;
78
	}
79

  
80
	/* (non-Javadoc)
81
	 * @see eu.etaxonomy.cdm.io.stream.IItemStream#getItemLocation()
82
	 */
83
	@Override
84
	public String getItemLocation() {
85
		//FIXME better dont use ExcelUtils !!
86
		return "FIXME";
87
	}
88

  
89
	/* (non-Javadoc)
90
	 * @see eu.etaxonomy.cdm.io.stream.IItemStream#getStreamLocation()
91
	 */
92
	@Override
93
	public String getStreamLocation() {
94
		return streamLocation;
95
	}
96

  
97

  
98
}
cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/excel/stream/ExcelRecordStream.java
1
// $Id$
2
/**
3
* Copyright (C) 2009 EDIT
4
* European Distributed Institute of Taxonomy 
5
* http://www.e-taxonomy.eu
6
* 
7
* The contents of this file are subject to the Mozilla Public License Version 1.1
8
* See LICENSE.TXT at the top of this package for the full license terms.
9
*/
10
package eu.etaxonomy.cdm.io.excel.stream;
11

  
12
import java.util.HashMap;
13
import java.util.Map;
14
import java.util.Set;
15

  
16
import org.apache.commons.lang.StringUtils;
17
import org.apache.log4j.Logger;
18
import org.apache.poi.hssf.usermodel.HSSFCell;
19
import org.apache.poi.hssf.usermodel.HSSFRow;
20
import org.apache.poi.hssf.usermodel.HSSFSheet;
21

  
22
import eu.etaxonomy.cdm.common.ExcelUtils;
23
import eu.etaxonomy.cdm.io.common.events.IIoObserver;
24
import eu.etaxonomy.cdm.io.dwca.TermUri;
25
import eu.etaxonomy.cdm.io.stream.IItemStream;
26
import eu.etaxonomy.cdm.io.stream.StreamItem;
27

  
28
/**
29
 * @author a.oppermann
30
 * @date 16.05.2013
31
 *
32
 */
33
public class ExcelRecordStream  implements IItemStream{
34
	private static Logger logger = Logger.getLogger(ExcelRecordStream.class);
35
	
36
	private ExcelStreamImportState state;
37
	
38
	private HSSFSheet sheet;
39
//	private ArchiveEntryBase archiveEntry;
40
	private TermUri term;
41
	private int line = 0;
42
	
43
	private Map<Integer,String> mapping;
44
	
45
	private StreamItem next;
46
	
47
	
48
	
49

  
50
	/**
51
	 * Constructor. 
52
	 * @param term 
53
	 */
54
	public ExcelRecordStream(ExcelStreamImportState state, HSSFSheet sheet, TermUri term) {
55
		this.state = state;
56
		this.sheet = sheet;
57
		this.term = term;
58
	}
59
	
60
	/* (non-Javadoc)
61
	 * @see eu.etaxonomy.cdm.io.dwca.in.IReader#read()
62
	 */
63
	@Override
64
	public StreamItem read() {
65
		//FIXME:
66
		return readMe();
67
	}
68

  
69
	/* (non-Javadoc)
70
	 * @see eu.etaxonomy.cdm.io.dwca.in.IReader#hasNext()
71
	 */
72
	@Override
73
	public boolean hasNext() {
74
		if (next != null){
75
			return true;
76
		}else{
77
			next = readMe();
78
			return (next != null);
79
		}
80
	}
81

  
82

  
83
	private StreamItem readMe() {
84
		StreamItem resultItem;
85
		if (next != null){
86
			resultItem = next;
87
			next = null;
88
			return resultItem;
89
		}else{
90
		
91
			if (mapping == null){
92
				mapping = getHeaderMapping(sheet.getRow(line++));
93
			}
94
	//		int rows = sheet.getPhysicalNumberOfRows();
95
			HSSFRow row = sheet.getRow(line++);
96
			int i = 0;
97
			while (row == null && i++ < 10){
98
				row = sheet.getRow(line++);
99
				continue;
100
			}
101
			if (row == null){
102
				return null;
103
			}else{
104
				int cells = row.getPhysicalNumberOfCells();
105
				logger.info("\nROW " + row.getRowNum() + " has " + cells + " cell(s).");
106
				Map<String, String> map = new HashMap<String, String>();
107
				
108
				for (int c :mapping.keySet()){
109
					HSSFCell cell = row.getCell(c);
110
					String value = ExcelUtils.getCellValue(cell);
111
					map.put(mapping.get(c), value);
112
					logger.info("CELL col=" + cell.getColumnIndex() + " VALUE=" + value);
113
				}
114
				
115
				resultItem = new StreamItem(term, map, String.valueOf(line));
116
				
117
				return resultItem;
118
			}
119
		}
120
	}
121
	
122
	/**
123
	 * @param row
124
	 * @return
125
	 */
126
	private Map<Integer,String> getHeaderMapping(HSSFRow row) {
127
		Map<Integer,String> result = new HashMap<Integer, String>();
128
		
129
		int cells = row.getPhysicalNumberOfCells();
130
		logger.info("\nROW " + row.getRowNum() + " has " + cells + " cell(s).");
131
		for (int c = 0; c < cells; c++) {
132
			HSSFCell cell = row.getCell(c);
133
			String value = ExcelUtils.getCellValue(cell);
134
			String termUri = convert2semanticKey(value);
135
			if (termUri != null){
136
				result.put(c, termUri);
137
			}else{
138
				//TODO fire Event
139
				String message = "No mapping defined for column %d '%s'";
140
				logger.warn(String.format(message, c + 1, value));
141
			}
142
		}
143
		
144
		return result;
145
	}
146

  
147
	/**
148
	 * @param value
149
	 * @return
150
	 */
151
	private String convert2semanticKey(String key) {
152
		if (key.equalsIgnoreCase("id")){
153
			return "id";
154
		}else if (key.equalsIgnoreCase("ParentId")){
155
			return TermUri.DWC_PARENT_NAME_USAGE_ID.getUriString();
156
		}else if (key.equalsIgnoreCase("NameStatus")){
157
			return TermUri.DWC_TAXONOMIC_STATUS.getUriString();
158
		}else if (key.equalsIgnoreCase("Rank")){
159
			return TermUri.DWC_TAXON_RANK.getUriString();
160
		}else if (key.equalsIgnoreCase("ScientificName")){
161
			return TermUri.DWC_SCIENTIFIC_NAME.getUriString();
162
		}else if (key.equalsIgnoreCase("Author")){
163
			return TermUri.DWC_SCIENTIFIC_NAME_AUTHORS.getUriString();
164
			//TODO Taxon Remarks
165
		}else if (key.equalsIgnoreCase("Comments")){
166
			return TermUri.DWC_TAXON_REMARKS.getUriString();
167
		}else if (key.equalsIgnoreCase("Language")){
168
			return TermUri.DC_LANGUAGE.getUriString();
169
		}else if (key.equalsIgnoreCase("TDWG_1")){
170
			return TermUri.DWC_COUNTRY_CODE.getUriString();
171
		}else if (key.equalsIgnoreCase("VernacularName")){
172
			return TermUri.DWC_VERNACULAR_NAME.getUriString();
173
		}else if (key.equalsIgnoreCase("ExternalId_sysCode")){
174
			return TermUri.CDM_SOURCE_IDINSOURCE.getUriString();
175
		}else if (key.equalsIgnoreCase("External_source")){
176
			return TermUri.CDM_SOURCE_REFERENCE.getUriString();
177
		}else if (key.equalsIgnoreCase("IdNamespace")){
178
			return TermUri.CDM_SOURCE_IDNAMESPACE.getUriString();
179
		}else{
180
			//TODO fire Event
181
			String message = "Key '%s' does not (yet) exist for import";
182
			logger.warn(String.format(message, key));
183
			return null;
184
		}
185
		
186
	//Language	TDWG_1	TDWG_2	VernacularName	SysCode
187
	
188
	}
189

  
190
	/* (non-Javadoc)
191
	 * @see eu.etaxonomy.cdm.io.stream.IItemStream#getTerm()
192
	 */
193
	@Override
194
	public TermUri getTerm() {
195
		return this.term;
196
	}
197

  
198
	/* (non-Javadoc)
199
	 * @see eu.etaxonomy.cdm.io.stream.IItemStream#getItemLocation()
200
	 */
201
	@Override
202
	public String getItemLocation() {
203
		// TODO Auto-generated method stub
204
		return null;
205
	}
206

  
207
	/* (non-Javadoc)
208
	 * @see eu.etaxonomy.cdm.io.stream.IItemStream#getStreamLocation()
209
	 */
210
	@Override
211
	public String getStreamLocation() {
212
		// TODO Auto-generated method stub
213
		return null;
214
	}
215

  
216
	/* (non-Javadoc)
217
	 * @see eu.etaxonomy.cdm.io.stream.IItemStream#addObservers(java.util.Set)
218
	 */
219
	@Override
220
	public void addObservers(Set<IIoObserver> observers) {
221
		// TODO Auto-generated method stub
222
		logger.warn("addObservers Not yet implemented");
223
	}
224
	
225
//******************** toString *******************************************
226

  
227
	/* (non-Javadoc)
228
	 * @see java.lang.Object#toString()
229
	 */
230
	@Override
231
	public String toString() {
232
		if (sheet != null && StringUtils.isNotBlank(sheet.getSheetName())){
233
			return sheet.getSheetName();
234
		}else{
235
			return super.toString();
236
		}
237
	}
238
	
239
	
240
	
241

  
242
}
cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/excel/stream/ExcelStreamImport.java
1
package eu.etaxonomy.cdm.io.excel.stream;
2

  
3
import java.io.IOException;
4
import java.net.URI;
5
import java.util.ArrayList;
6
import java.util.HashMap;
7

  
8
import org.apache.http.HttpException;
9
import org.apache.log4j.Logger;
10
import org.springframework.stereotype.Component;
11

  
12
import eu.etaxonomy.cdm.io.dwca.TermUri;
13
import eu.etaxonomy.cdm.io.dwca.in.DwcTaxonCsv2CdmTaxonRelationConverter;
14
import eu.etaxonomy.cdm.io.dwca.in.DwcTaxonStreamItem2CdmTaxonConverter;
15
import eu.etaxonomy.cdm.io.dwca.in.DwcaDataImportBase;
16
import eu.etaxonomy.cdm.io.dwca.in.IPartitionableConverter;
17
import eu.etaxonomy.cdm.io.dwca.in.IReader;
18
import eu.etaxonomy.cdm.io.stream.StreamItem;
19
import eu.etaxonomy.cdm.model.common.CdmBase;
20

  
21
/**
22
 * 
23
 * @author a.oppermann
24
 * @date 08.05.2013
25
 *
26
 */
27
@Component
28
public class ExcelStreamImport extends DwcaDataImportBase<ExcelStreamImportConfigurator, ExcelStreamImportState>{
29
	private static final Logger logger = Logger.getLogger(ExcelStreamImport.class);
30

  
31
	private ArrayList<HashMap<String, String>> recordList = null;
32
	
33
	
34
	/* (non-Javadoc)
35
	 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#doInvoke(eu.etaxonomy.cdm.io.common.IoStateBase)
36
	 */
37
	@Override
38
	protected void doInvoke(ExcelStreamImportState state) {
39
		
40
		makeSourceRef(state);
41
		URI source = state.getConfig().getSource();
42
		ExcelToStreamConverter<ExcelStreamImportState> excelStreamConverter = ExcelToStreamConverter.NewInstance(source);
43

  
44
		try {
45
			IReader<ExcelRecordStream> worksheetStream = excelStreamConverter.getWorksheetStream(state);
46
		
47
			while (worksheetStream.hasNext()){
48
				ExcelRecordStream recordStream = worksheetStream.read();
49
				try {
50
					handleSingleRecord(state, recordStream);
51
				} catch (Exception e) {
52
					String message = "Exception (%s) occurred while handling worksheet stream %s";
53
					message = String.format(message, e.getMessage(), recordStream.toString());
54
					fireWarningEvent (message, recordStream.toString(), 14);
55
				}
56
			}
57
			state.finish();
58
		} catch (IOException e1) {
59
			// TODO Auto-generated catch block
60
			e1.printStackTrace();
61
		} catch (HttpException e1) {
62
			// TODO Auto-generated catch block
63
			e1.printStackTrace();
64
		}
65
		
66
		return;
67
	}
68

  
69

  
70

  
71
	/* (non-Javadoc)
72
	 * @see eu.etaxonomy.cdm.io.dwca.in.StreamImportBase#getConverter(eu.etaxonomy.cdm.io.dwca.TermUri, eu.etaxonomy.cdm.io.dwca.in.StreamImportStateBase)
73
	 */
74
	@Override
75
	protected IPartitionableConverter<StreamItem, IReader<CdmBase>, String> getConverter(
76
			TermUri namespace, ExcelStreamImportState state) {
77
		
78
		
79
		if (namespace.equals(TermUri.DWC_TAXON)){
80
			if (! state.isTaxaCreated()){
81
				return new DwcTaxonStreamItem2CdmTaxonConverter(state);
82
			}else{
83
				return new DwcTaxonCsv2CdmTaxonRelationConverter(state);
84
			}
85
		}else{
86
			String message = "No converter available for %s";
87
			logger.error(String.format(message, namespace));
88
			return null;
89
		}
90
	}
91
	
92
	
93
	/* (non-Javadoc)
94
	 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#doCheck(eu.etaxonomy.cdm.io.common.IoStateBase)
95
	 */
96
	@Override
97
	protected boolean doCheck(ExcelStreamImportState state) {
98
		return state.isCheck();
99
	}
100

  
101
	/* (non-Javadoc)
102
	 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IoStateBase)
103
	 */
104
	@Override
105
	protected boolean isIgnore(ExcelStreamImportState state) {
106
		return false;  //we only have 1 import class for excel stream import
107
	}
108

  
109
}
cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/excel/stream/ExcelStreamImportConfigurator.java
1
package eu.etaxonomy.cdm.io.excel.stream;
2

  
3
import java.net.URI;
4

  
5
import org.apache.log4j.Logger;
6

  
7
import eu.etaxonomy.cdm.database.DbSchemaValidation;
8
import eu.etaxonomy.cdm.database.ICdmDataSource;
9
import eu.etaxonomy.cdm.io.common.IImportConfigurator;
10
import eu.etaxonomy.cdm.io.common.mapping.IInputTransformer;
11
import eu.etaxonomy.cdm.io.dwca.in.DwcaDataImportConfiguratorBase;
12
import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
13
/**
14
 * 
15
 * @author a.oppermann
16
 * @date 08.05.2013
17
 *
18
 */
19
public class ExcelStreamImportConfigurator extends DwcaDataImportConfiguratorBase<ExcelStreamImportState> implements IImportConfigurator {
20
	@SuppressWarnings("unused")
21
	private static final Logger logger = Logger.getLogger(ExcelStreamImportConfigurator.class);
22

  
23
	private static final String DEFAULT_REF_TITLE = "Excel Stream Import";
24

  
25
	
26
	private static IInputTransformer defaultTransformer = null;
27

  
28
	
29
	/**
30
	 * Factory method.
31
	 * @param uri
32
	 * @param destination
33
	 * @return
34
	 */
35
	public static ExcelStreamImportConfigurator NewInstance(URI uri, ICdmDataSource destination, NomenclaturalCode nomenclaturalCode, DbSchemaValidation dbSchemaValidation){
36
		return new ExcelStreamImportConfigurator(uri, destination, nomenclaturalCode, dbSchemaValidation);
37
	}
38
	
39
//	/**
40
//	 * @param transformer
41
//	 */
42
//	public ExcelStreamImportConfigurator(IInputTransformer transformer) {
43
//		super(transformer);
44
//	}
45

  
46
	/**
47
	 * Constructor.
48
	 * @param uri
49
	 * @param destination
50
	 */
51
	private ExcelStreamImportConfigurator(URI uri, ICdmDataSource destination, NomenclaturalCode nomenclaturalCode, DbSchemaValidation dbSchemaValidation) {
52
		super(uri, destination, defaultTransformer);
53
		setDbSchemaValidation(dbSchemaValidation);
54
		setNomenclaturalCode(nomenclaturalCode);	
55
	}
56
	
57
	/* (non-Javadoc)
58
	 * @see eu.etaxonomy.cdm.io.common.IImportConfigurator#getNewState()
59
	 */
60
	@Override
61
	public ExcelStreamImportState getNewState() {
62
		return new ExcelStreamImportState(this);
63
	}
64

  
65
	/* (non-Javadoc)
66
	 * @see eu.etaxonomy.cdm.io.common.ImportConfiguratorBase#makeIoClassList()
67
	 */
68
	@SuppressWarnings("unchecked")
69
	@Override
70
	protected void makeIoClassList() {
71
		ioClassList = new Class[]{
72
				ExcelStreamImport.class
73
		};
74
	}
75

  
76
	
77
	/* (non-Javadoc)
78
	 * @see eu.etaxonomy.cdm.io.dwca.in.StreamImportConfiguratorBase#getDefaultSourceReferenceTitle()
79
	 */
80
	@Override
81
	protected String getDefaultSourceReferenceTitle() {
82
		return DEFAULT_REF_TITLE;
83
	}
84

  
85
}
cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/excel/stream/ExcelStreamImportState.java
1
// $Id$
2
/**
3
* Copyright (C) 2007 EDIT
4
* European Distributed Institute of Taxonomy 
5
* http://www.e-taxonomy.eu
6
* 
7
* The contents of this file are subject to the Mozilla Public License Version 1.1
8
* See LICENSE.TXT at the top of this package for the full license terms.
9
*/
10

  
11
package eu.etaxonomy.cdm.io.excel.stream;
12

  
13
import org.apache.log4j.Logger;
14

  
15
import eu.etaxonomy.cdm.io.dwca.in.DwcaDataImportStateBase;
16
import eu.etaxonomy.cdm.io.dwca.in.DwcaImportState;
17

  
18
/**
19
 * 
20
 * @author a.oppermann
21
 * @date 08.05.2013
22
 *
23
 *
24
 *<ROW extends ExcelRowBase>
25
 */
26
public class ExcelStreamImportState extends DwcaDataImportStateBase<ExcelStreamImportConfigurator>{
27
	@SuppressWarnings("unused")
28
	private static final Logger logger = Logger.getLogger(DwcaImportState.class);
29

  
30
	public ExcelStreamImportState(ExcelStreamImportConfigurator config) {
31
		super(config);
32
	}
33
	
34
}
cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/excel/stream/ExcelToStreamConverter.java
1
// $Id$
2
/**
3
* Copyright (C) 2009 EDIT
4
* European Distributed Institute of Taxonomy 
5
* http://www.e-taxonomy.eu
6
* 
7
* The contents of this file are subject to the Mozilla Public License Version 1.1
8
* See LICENSE.TXT at the top of this package for the full license terms.
9
*/
10
package eu.etaxonomy.cdm.io.excel.stream;
11

  
12
import java.io.IOException;
13
import java.net.URI;
14
import java.util.ArrayList;
15
import java.util.HashMap;
16
import java.util.List;
17
import java.util.Map;
18

  
19
import org.apache.commons.lang.StringUtils;
20
import org.apache.http.HttpException;
21
import org.apache.log4j.Logger;
22
import org.apache.poi.hssf.usermodel.HSSFSheet;
23
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
24
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
25

  
26
import eu.etaxonomy.cdm.common.UriUtils;
27
import eu.etaxonomy.cdm.common.monitor.IProgressMonitor;
28
import eu.etaxonomy.cdm.io.dwca.TermUri;
29
import eu.etaxonomy.cdm.io.dwca.in.IReader;
30
import eu.etaxonomy.cdm.io.dwca.in.ListReader;
31

  
32
/**
33
 * This class transforms excel archive in to a InputStream.
34
 * 
35
 * @author a.oppermann
36
 * @date 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
	 */
69
	public IReader<ExcelRecordStream> getWorksheetStream(STATE state) throws IOException, HttpException{
70
		POIFSFileSystem fs = new POIFSFileSystem(UriUtils.getInputStream(source));
71
		HSSFWorkbook wb = new HSSFWorkbook(fs);
72
		
73
		Map<TermUri, Integer> map = new HashMap<TermUri, Integer>();
74
		for (int i = 0 ; i < wb.getNumberOfSheets(); i++){
75
			String wsName = wb.getSheetName(i);
76
			TermUri termUri = convertSheetName2TermUri(wsName);
77
			if (map.get(termUri) != null){
78
				String message = "Worksheet type exists more then once: %s";
79
				//TODO fire event
80
				logger.warn(String.format(message, termUri.toString()));
81
			}
82
			map.put(termUri, i);
83
		}
84
		
85
		//core
86
		List<ExcelRecordStream> streamList = new ArrayList<ExcelRecordStream>();
87
		TermUri term= TermUri.DWC_TAXON;
88
		Integer i = map.get(term);
89
		if (i != null){
90
			HSSFSheet ws = wb.getSheetAt(i);
91
			ExcelRecordStream excelRecordStream = new ExcelRecordStream(state, ws, term);
92
			streamList.add(excelRecordStream); //for taxa and names
93
		}else{
94
			String message = "Taxon worksheet not available for %s";
95
			logger.warn(String.format(message, "taxa"));
96
			state.setSuccess(false);
97
		} 
98
		
99
		//core relationships
100
		i = map.get(term);
101
		if (i != null){
102
			HSSFSheet ws = wb.getSheetAt(i);
103
			ExcelRecordStream excelRecordStream = new ExcelRecordStream(state, ws, term);
104
			streamList.add(excelRecordStream); //for relationships
105
		}else{
106
			String message = "Taxon worksheet not available for %s";
107
			logger.warn(String.format(message, "taxon relations"));
108
			state.setSuccess(false);
109
		} 
110
		
111
		return new ListReader<ExcelRecordStream>(streamList);
112
	}
113

  
114

  
115
	/**
116
	 * @param wsName
117
	 * @return
118
	 */
119
	private TermUri convertSheetName2TermUri(String wsName) {
120
		if (StringUtils.isBlank(wsName)){
121
			throw new IllegalArgumentException("Worksheet name must not be null or empty");
122
			//FIXME: Hard coded worksheet name should be avoided  
123
		}else if(wsName.equalsIgnoreCase("Sheet1")){
124
			return TermUri.DWC_TAXON;
125
		}else{
126
			String message = "Worksheet name %s not yet handled by %s";
127
			throw new IllegalArgumentException(String.format(message, wsName, this.getClass().getSimpleName()));
128
		}
129
	}
130

  
131
	public void warnProgress(STATE state, String message, Throwable e) {
132
        if(state.getConfig().getProgressMonitor() != null){
133
            IProgressMonitor monitor = state.getConfig().getProgressMonitor();
134
            if (e == null) {
135
                monitor.warning(message);
136
            }else{
137
                monitor.warning(message, e);
138
            }
139
        }
140
    }
141
	
142

  
143
	
144
}

Also available in: Unified diff

Add picture from clipboard (Maximum size: 40 MB)