9 |
import org.apache.commons.fileupload.FileItem; |
import org.apache.commons.fileupload.FileItem; |
10 |
|
|
11 |
import dk.daoas.adressevedligehold.AddressSourceEntry.EntryType; |
import dk.daoas.adressevedligehold.AddressSourceEntry.EntryType; |
|
import dk.daoas.adressevedligehold.util.DeduplicateHelper; |
|
12 |
import dk.daoas.adressevedligehold.util.SafeParsers; |
import dk.daoas.adressevedligehold.util.SafeParsers; |
13 |
|
|
14 |
public class AddressSourceBK implements AddressSource { |
public class AddressSourceBK extends AbstractAddressSource { |
15 |
|
|
|
DeduplicateHelper<String> dirigeringsCache = new DeduplicateHelper<String>(); |
|
|
|
|
|
FileItem file; |
|
|
|
|
|
InputStream is; |
|
|
InputStreamReader isr; |
|
|
BufferedReader br; |
|
|
|
|
|
int lineCount = 0; |
|
16 |
|
|
17 |
public AddressSourceBK(FileItem file) throws Exception { |
public AddressSourceBK(FileItem file) throws Exception { |
18 |
this.file = file; |
super(file); |
|
|
|
|
|
|
|
|
|
19 |
} |
} |
20 |
|
|
|
@Override |
|
|
public String getFilename() { |
|
|
return file.getName(); |
|
|
} |
|
21 |
|
|
22 |
@Override |
@Override |
23 |
public void validate() throws IOException { |
public void validate() throws IOException { |
27 |
BufferedReader br1 = new BufferedReader(isr1) |
BufferedReader br1 = new BufferedReader(isr1) |
28 |
) { |
) { |
29 |
String line = br1.readLine(); |
String line = br1.readLine(); |
30 |
|
|
31 |
|
if (line == null) { |
32 |
|
throw new IOException("Can't read 1st line - is file empty?"); |
33 |
|
} |
34 |
|
|
35 |
String[] parts = line.split(";"); |
String[] parts = line.split(";"); |
36 |
int numFields = parts.length; |
|
37 |
if (numFields != 13) { |
if (parts.length != 13) { |
38 |
throw new IOException("Not enough fields in CSV file. Found " + numFields + ", expected 13"); |
throw new IOException("Not enough fields in CSV file. Found " + parts.length + ", expected 13"); |
39 |
} |
} |
40 |
} |
} |
41 |
|
|
62 |
entry.distributor = "BK"; |
entry.distributor = "BK"; |
63 |
|
|
64 |
String[] parts = line.split(";"); |
String[] parts = line.split(";"); |
65 |
|
|
66 |
|
if (parts.length != 13) { |
67 |
|
throw new IOException("Not enough fields in CSV file. Found " + parts.length + ", expected 13"); |
68 |
|
} |
69 |
|
|
70 |
entry.postnr = Short.parseShort( parts[0]); |
entry.postnr = Short.parseShort( parts[0]); |
71 |
entry.vejnavn = parts[1].replace("\"", ""); |
entry.vejnavn = parts[1].replace("\"", ""); |
72 |
entry.husnr = Short.parseShort( parts[2] ); |
entry.husnr = Short.parseShort( parts[2] ); |
118 |
return "BK"; |
return "BK"; |
119 |
} |
} |
120 |
|
|
|
@Override //AutoCloseable |
|
|
public void close() throws Exception { |
|
|
System.out.println("Closing BK after lines " + lineCount); |
|
|
try { |
|
|
br.close(); |
|
|
isr.close(); |
|
|
is.close(); |
|
|
|
|
|
file.delete(); |
|
|
|
|
|
} catch (Exception e) { |
|
|
System.out.println("Error on closing " + e.getMessage() ); |
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
|
|
121 |
} |
} |