1 |
package dk.daoas.adressevedligehold;
|
2 |
|
3 |
import java.io.BufferedReader;
|
4 |
import java.io.IOException;
|
5 |
import java.io.InputStream;
|
6 |
import java.io.InputStreamReader;
|
7 |
import java.nio.charset.Charset;
|
8 |
import java.util.List;
|
9 |
|
10 |
import org.apache.commons.fileupload.FileItem;
|
11 |
|
12 |
import com.google.common.base.Splitter;
|
13 |
|
14 |
import dk.daoas.adressevedligehold.tasks.TaskLogger;
|
15 |
import dk.daoas.adressevedligehold.util.DeduplicateHelper;
|
16 |
|
17 |
public abstract class AbstractAddressSource implements AddressSource {
|
18 |
|
19 |
private TaskLogger logger = TaskLogger.getInstance();
|
20 |
|
21 |
protected DeduplicateHelper<String> dirigeringsCache = new DeduplicateHelper<String>();
|
22 |
|
23 |
protected FileItem file;
|
24 |
|
25 |
protected InputStream is;
|
26 |
protected InputStreamReader isr;
|
27 |
protected BufferedReader br;
|
28 |
|
29 |
protected int lineCount = 0;
|
30 |
|
31 |
public AbstractAddressSource(FileItem file) {
|
32 |
this.file = file;
|
33 |
}
|
34 |
|
35 |
@Override
|
36 |
public String getFilename() {
|
37 |
// TODO Auto-generated method stub
|
38 |
return file.getName();
|
39 |
}
|
40 |
|
41 |
@Override //AutoCloseable
|
42 |
public void close() throws Exception {
|
43 |
logger.info("Closing BK after lines " + lineCount);
|
44 |
try {
|
45 |
if (br != null)
|
46 |
br.close();
|
47 |
if (isr != null)
|
48 |
isr.close();
|
49 |
if (is != null)
|
50 |
is.close();
|
51 |
|
52 |
file.delete();
|
53 |
|
54 |
} catch (Exception e) {
|
55 |
logger.warning("Error on closing ", e );
|
56 |
}
|
57 |
}
|
58 |
/**
|
59 |
* Reads the first line, validates that there are the expected number of fields, and then
|
60 |
* re-initilze the inputstream - this is for file formats that does not use a header line.
|
61 |
*
|
62 |
* @param exptectedFieldCount
|
63 |
* @param seperator
|
64 |
* @throws IOException
|
65 |
*/
|
66 |
protected void validatNoHeaderLine(int exptectedFieldCount, char seperator) throws IOException {
|
67 |
try (
|
68 |
InputStream is1 = file.getInputStream();
|
69 |
InputStreamReader isr1 = new InputStreamReader(is1, Charset.forName("ISO-8859-1") );
|
70 |
BufferedReader br1 = new BufferedReader(isr1)
|
71 |
) {
|
72 |
String line = br1.readLine();
|
73 |
|
74 |
if (line == null) {
|
75 |
throw new IOException("Can't read 1st line - is file empty?");
|
76 |
}
|
77 |
|
78 |
List<String> parts = Splitter.on(seperator).splitToList(line);
|
79 |
if (parts.size() != exptectedFieldCount) {
|
80 |
throw new IOException("Not enough fields in CSV file. Found " + parts.size() + ", expected " + exptectedFieldCount);
|
81 |
}
|
82 |
}
|
83 |
|
84 |
|
85 |
is = file.getInputStream();
|
86 |
isr = new InputStreamReader(is, Charset.forName("ISO-8859-1") );
|
87 |
br = new BufferedReader(isr);
|
88 |
}
|
89 |
|
90 |
|
91 |
protected void validateWithHeader(int expectedFieldCount, char seperator) throws IOException {
|
92 |
try {
|
93 |
is = file.getInputStream();
|
94 |
isr = new InputStreamReader(is, Charset.forName("ISO-8859-1") );
|
95 |
br = new BufferedReader(isr);
|
96 |
|
97 |
String line = br.readLine();
|
98 |
if (line == null) {
|
99 |
throw new IOException("Can't read 1st line - is file empty?");
|
100 |
}
|
101 |
|
102 |
List<String> parts = Splitter.on(seperator).splitToList(line);
|
103 |
if (parts.size() != expectedFieldCount) {
|
104 |
throw new IOException("Not enough fields in CSV file. Found " + parts.size() + ", expected " + expectedFieldCount);
|
105 |
}
|
106 |
|
107 |
|
108 |
|
109 |
} catch (Exception e) {
|
110 |
try {
|
111 |
br.close();
|
112 |
isr.close();
|
113 |
is.close();
|
114 |
} catch (Exception e2) {
|
115 |
logger.warning("Error cleaning up resources", e2);
|
116 |
}
|
117 |
|
118 |
throw e; // Re-throw
|
119 |
}
|
120 |
}
|
121 |
}
|