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