1 |
package dk.daoas.adressevedligehold; |
package dk.daoas.adressevedligehold.fileupload; |
2 |
|
|
3 |
import java.io.BufferedReader; |
import java.io.BufferedReader; |
4 |
|
import java.io.IOException; |
5 |
import java.io.InputStream; |
import java.io.InputStream; |
6 |
import java.io.InputStreamReader; |
import java.io.InputStreamReader; |
7 |
|
import java.nio.charset.Charset; |
8 |
|
import java.util.List; |
9 |
|
|
10 |
import org.apache.commons.fileupload.FileItem; |
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; |
import dk.daoas.adressevedligehold.util.DeduplicateHelper; |
16 |
|
|
17 |
public abstract class AbstractAddressSource implements AddressSource { |
public abstract class AbstractAddressSource implements AddressSource { |
18 |
|
|
19 |
|
private TaskLogger logger = TaskLogger.getInstance(); |
20 |
|
|
21 |
protected DeduplicateHelper<String> dirigeringsCache = new DeduplicateHelper<String>(); |
protected DeduplicateHelper<String> dirigeringsCache = new DeduplicateHelper<String>(); |
22 |
|
|
23 |
protected FileItem file; |
protected FileItem file; |
40 |
|
|
41 |
@Override //AutoCloseable |
@Override //AutoCloseable |
42 |
public void close() throws Exception { |
public void close() throws Exception { |
43 |
System.out.println("Closing BK after lines " + lineCount); |
logger.info("Closing BK after lines " + lineCount); |
44 |
try { |
try { |
45 |
if (br != null) |
if (br != null) |
46 |
br.close(); |
br.close(); |
52 |
file.delete(); |
file.delete(); |
53 |
|
|
54 |
} catch (Exception e) { |
} catch (Exception e) { |
55 |
System.out.println("Error on closing " + e.getMessage() ); |
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 |
} |
} |