1 |
package dk.daoas.adressevedligehold; |
package dk.daoas.adressevedligehold; |
2 |
|
|
|
import java.io.BufferedReader; |
|
3 |
import java.io.IOException; |
import java.io.IOException; |
4 |
import java.io.InputStream; |
import java.util.List; |
|
import java.io.InputStreamReader; |
|
|
import java.nio.charset.Charset; |
|
5 |
|
|
6 |
import org.apache.commons.fileupload.FileItem; |
import org.apache.commons.fileupload.FileItem; |
7 |
|
|
8 |
|
import com.google.common.base.CharMatcher; |
9 |
|
import com.google.common.base.Splitter; |
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 { |
|
|
|
|
DeduplicateHelper<String> dirigeringsCache = new DeduplicateHelper<String>(); |
|
|
|
|
|
FileItem file; |
|
15 |
|
|
16 |
InputStream is; |
Splitter splitter = Splitter.on(';').trimResults( CharMatcher.is('"') ); |
|
InputStreamReader isr; |
|
|
BufferedReader br; |
|
|
|
|
|
int lineCount = 0; |
|
17 |
|
|
18 |
public AddressSourceBK(FileItem file) throws Exception { |
public AddressSourceBK(FileItem file) throws Exception { |
19 |
this.file = file; |
super(file); |
|
|
|
|
|
|
|
is = file.getInputStream(); |
|
|
isr = new InputStreamReader(is, Charset.forName("ISO-8859-1") ); |
|
|
br = new BufferedReader(isr); |
|
|
|
|
20 |
} |
} |
21 |
|
|
22 |
|
|
23 |
@Override |
@Override |
24 |
public String getFilename() { |
public void validate() throws IOException { |
25 |
return file.getName(); |
super.validatNoHeaderLine(14, ';'); |
26 |
} |
} |
27 |
|
|
28 |
//TODO: Skal csv parsning klares med Apache Commons CSV ? |
//TODO: Skal csv parsning klares med Apache Commons CSV ? |
29 |
|
|
30 |
@Override |
@Override |
41 |
AddressSourceEntry entry = new AddressSourceEntry( EntryType.TypeSingleAddress); |
AddressSourceEntry entry = new AddressSourceEntry( EntryType.TypeSingleAddress); |
42 |
entry.distributor = "BK"; |
entry.distributor = "BK"; |
43 |
|
|
44 |
String[] parts = line.split(";"); |
List<String> parts = splitter.splitToList(line); |
45 |
entry.postnr = Short.parseShort( parts[0]); |
|
46 |
entry.vejnavn = parts[1].replace("\"", ""); |
if (parts.size() != 14) { |
47 |
entry.husnr = Short.parseShort( parts[2] ); |
throw new IOException("Not enough fields in CSV file. Found " + parts.size() + ", expected 14"); |
48 |
entry.litra = parts[3].replace("\"", ""); |
} |
49 |
entry.vejkode = SafeParsers.parseShort( parts[4] ); |
|
50 |
entry.kommunekode = SafeParsers.parseShort( parts[5] ); |
entry.postnr = Short.parseShort( parts.get(0) ); |
51 |
entry.gadeid = Integer.parseInt( parts[6] ); |
entry.vejnavn = parts.get( 1 ); |
52 |
|
entry.husnr = Short.parseShort( parts.get( 2 ) ); |
53 |
String ugedage = parts[7].replace("\"", ""); |
entry.litra = parts.get( 3 ); |
54 |
|
entry.vejkode = SafeParsers.parseShort( parts.get( 4) ); |
55 |
String laesnr = parts[8].replace("\"", ""); |
entry.kommunekode = SafeParsers.parseShort( parts.get( 5) ); |
56 |
|
entry.gadeid = Integer.parseInt( parts.get( 6 ) ); |
57 |
@SuppressWarnings("unused") String distnr = parts[9].replace("\"", ""); |
|
58 |
String foede = parts[10].replace("\"", ""); |
String ugedage = parts.get( 7 ); |
59 |
String jobnr = parts[11].replace("\"", ""); |
|
60 |
String tklaes = parts[12].replace("\"", ""); |
String laesnr = parts.get( 8 ); |
61 |
|
|
62 |
|
//String distnr = parts.get( 9 ); //Bruges ikke |
63 |
|
String foede = parts.get( 10 ); |
64 |
|
String jobnr = parts.get( 11 ); |
65 |
|
String tklaes = parts.get( 12 ); |
66 |
|
|
67 |
entry.rute = laesnr; |
entry.rute = laesnr; |
68 |
entry.koreliste = "/" + foede + "/" + tklaes + "/" + jobnr; |
entry.koreliste = "/" + foede + "/" + tklaes + "/" + jobnr; |
98 |
return "BK"; |
return "BK"; |
99 |
} |
} |
100 |
|
|
|
@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() ); |
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
|
|
101 |
} |
} |