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.csv.in;
|
10
|
|
11
|
import java.io.IOException;
|
12
|
import java.io.InputStream;
|
13
|
import java.io.InputStreamReader;
|
14
|
import java.net.URL;
|
15
|
|
16
|
import eu.etaxonomy.cdm.common.URI;
|
17
|
import eu.etaxonomy.cdm.database.ICdmDataSource;
|
18
|
import eu.etaxonomy.cdm.io.common.ImportConfiguratorBase;
|
19
|
import eu.etaxonomy.cdm.io.common.mapping.IInputTransformer;
|
20
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
21
|
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
|
22
|
|
23
|
/**
|
24
|
* Base class for {@link CsvImportBase csv import} configuration.
|
25
|
*
|
26
|
* @author a.mueller
|
27
|
* @since 08.07.2017
|
28
|
*/
|
29
|
public abstract class CsvImportConfiguratorBase
|
30
|
extends ImportConfiguratorBase<CsvImportState<CsvImportConfiguratorBase>, InputStreamReader>{
|
31
|
|
32
|
private static final long serialVersionUID = -6735627744555323225L;
|
33
|
|
34
|
private char fieldSeparator = ',';
|
35
|
private int transactionLineCount = 1000;
|
36
|
private URI originalUri;
|
37
|
|
38
|
// ****************** CONSTRUCTOR *****************************/
|
39
|
protected CsvImportConfiguratorBase(InputStreamReader inputStream,
|
40
|
ICdmDataSource cdmDestination){
|
41
|
super(null);
|
42
|
setSource(inputStream);
|
43
|
setDestination(cdmDestination);
|
44
|
}
|
45
|
|
46
|
protected CsvImportConfiguratorBase(InputStreamReader inputStream,
|
47
|
ICdmDataSource cdmDestination, IInputTransformer transformer){
|
48
|
super(transformer);
|
49
|
setSource(inputStream);
|
50
|
setDestination(cdmDestination);
|
51
|
}
|
52
|
|
53
|
protected CsvImportConfiguratorBase(URI uri,
|
54
|
ICdmDataSource cdmDestination, IInputTransformer transformer) throws IOException{
|
55
|
super(transformer);
|
56
|
setSource(toStream(uri));
|
57
|
setDestination(cdmDestination);
|
58
|
this.originalUri = uri;
|
59
|
}
|
60
|
|
61
|
protected InputStreamReader newInputStream() throws IOException{
|
62
|
return toStream(originalUri);
|
63
|
}
|
64
|
|
65
|
private static InputStreamReader toStream(URI uri) throws IOException {
|
66
|
URL url = uri.toURL();
|
67
|
InputStream stream = url.openStream();
|
68
|
InputStreamReader inputStreamReader = new InputStreamReader(stream, "UTF8");
|
69
|
return inputStreamReader;
|
70
|
}
|
71
|
|
72
|
@Override
|
73
|
public Reference getSourceReference() {
|
74
|
if (this.sourceReference == null){
|
75
|
sourceReference = ReferenceFactory.newGeneric();
|
76
|
if (this.getSource() == null){
|
77
|
sourceReference.setTitleCache("CSV Import " + getDateString(), true);
|
78
|
}else{
|
79
|
sourceReference.setTitleCache(getSource().toString(), true);
|
80
|
}
|
81
|
}
|
82
|
return sourceReference;
|
83
|
}
|
84
|
|
85
|
@Override
|
86
|
public CsvImportState getNewState() {
|
87
|
return new CsvImportState<CsvImportConfiguratorBase>(this);
|
88
|
}
|
89
|
|
90
|
/**
|
91
|
* Returns the field separator. Default is ','.
|
92
|
* In future we may add other types like
|
93
|
*/
|
94
|
public char getFieldSeparator() {
|
95
|
return fieldSeparator;
|
96
|
}
|
97
|
|
98
|
public void setFieldSeparator(char fieldSeparator) {
|
99
|
this.fieldSeparator = fieldSeparator;
|
100
|
}
|
101
|
|
102
|
/**
|
103
|
* @return the transactionLineCount
|
104
|
*/
|
105
|
public int getTransactionLineCount() {
|
106
|
return transactionLineCount;
|
107
|
}
|
108
|
|
109
|
/**
|
110
|
* @param transactionLineCount the transactionLineCount to set
|
111
|
*/
|
112
|
public void setTransactionLineCount(int transactionLineCount) {
|
113
|
this.transactionLineCount = transactionLineCount;
|
114
|
}
|
115
|
}
|