1 |
package dk.daoas.adressevedligehold; |
package dk.daoas.adressevedligehold.fileupload; |
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 dk.daoas.adressevedligehold.AddressSourceEntry.EntryType; |
import com.google.common.base.CharMatcher; |
9 |
|
import com.google.common.base.Splitter; |
10 |
|
|
11 |
|
import dk.daoas.adressevedligehold.fileupload.AddressSourceEntry.EntryType; |
12 |
import dk.daoas.adressevedligehold.util.SafeParsers; |
import dk.daoas.adressevedligehold.util.SafeParsers; |
13 |
|
|
14 |
public class AddressSourceBK extends AbstractAddressSource { |
public class AddressSourceBK extends AbstractAddressSource { |
15 |
|
|
16 |
|
Splitter splitter = Splitter.on(';').trimResults( CharMatcher.is('"') ); |
17 |
|
|
18 |
public AddressSourceBK(FileItem file) throws Exception { |
public AddressSourceBK(FileItem file) throws Exception { |
19 |
super(file); |
super(file); |
22 |
|
|
23 |
@Override |
@Override |
24 |
public void validate() throws IOException { |
public void validate() throws IOException { |
25 |
try ( |
super.validatNoHeaderLine(14, ';'); |
|
InputStream is1 = file.getInputStream(); |
|
|
InputStreamReader isr1 = new InputStreamReader(is1, Charset.forName("ISO-8859-1") ); |
|
|
BufferedReader br1 = new BufferedReader(isr1) |
|
|
) { |
|
|
String line = br1.readLine(); |
|
|
|
|
|
if (line == null) { |
|
|
throw new IOException("Can't read 1st line - is file empty?"); |
|
|
} |
|
|
|
|
|
String[] parts = line.split(";"); |
|
|
|
|
|
if (parts.length != 13) { |
|
|
throw new IOException("Not enough fields in CSV file. Found " + parts.length + ", expected 13"); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
is = file.getInputStream(); |
|
|
isr = new InputStreamReader(is, Charset.forName("ISO-8859-1") ); |
|
|
br = new BufferedReader(isr); |
|
26 |
} |
} |
27 |
|
|
28 |
//TODO: Skal csv parsning klares med Apache Commons CSV ? |
//TODO: Skal csv parsning klares med Apache Commons CSV ? |
36 |
if (line.trim().equals("")) |
if (line.trim().equals("")) |
37 |
return null; |
return null; |
38 |
|
|
|
//System.out.println(line); |
|
39 |
|
|
40 |
AddressSourceEntry entry = new AddressSourceEntry( EntryType.TypeSingleAddress); |
AddressSourceEntry entry = new AddressSourceEntry( EntryType.TypeSingleAddress); |
41 |
entry.distributor = "BK"; |
entry.distributor = "BK"; |
42 |
|
|
43 |
String[] parts = line.split(";"); |
List<String> parts = splitter.splitToList(line); |
44 |
|
|
45 |
if (parts.length != 13) { |
if (parts.size() != 14) { |
46 |
throw new IOException("Not enough fields in CSV file. Found " + parts.length + ", expected 13"); |
throw new IOException("Not enough fields in CSV file. Found " + parts.size() + ", expected 14"); |
47 |
} |
} |
48 |
|
|
49 |
entry.postnr = Short.parseShort( parts[0]); |
entry.postnr = Short.parseShort( parts.get(0) ); |
50 |
entry.vejnavn = parts[1].replace("\"", ""); |
entry.vejnavn = parts.get( 1 ); |
51 |
entry.husnr = Short.parseShort( parts[2] ); |
entry.husnr = Short.parseShort( parts.get( 2 ) ); |
52 |
entry.litra = parts[3].replace("\"", ""); |
entry.litra = parts.get( 3 ); |
53 |
entry.vejkode = SafeParsers.parseShort( parts[4] ); |
entry.vejkode = SafeParsers.parseShort( parts.get( 4) ); |
54 |
entry.kommunekode = SafeParsers.parseShort( parts[5] ); |
entry.kommunekode = SafeParsers.parseShort( parts.get( 5) ); |
55 |
entry.gadeid = Integer.parseInt( parts[6] ); |
entry.gadeid = Integer.parseInt( parts.get( 6 ) ); |
56 |
|
|
57 |
String ugedage = parts[7].replace("\"", ""); |
String ugedage = parts.get( 7 ); |
58 |
|
|
59 |
String laesnr = parts[8].replace("\"", ""); |
String laesnr = parts.get( 8 ); |
60 |
|
|
61 |
//String distnr = parts[9].replace("\"", ""); //Bruges ikke |
//String distnr = parts.get( 9 ); //Bruges ikke |
62 |
String foede = parts[10].replace("\"", ""); |
String foede = parts.get( 10 ); |
63 |
String jobnr = parts[11].replace("\"", ""); |
String jobnr = parts.get( 11 ); |
64 |
String tklaes = parts[12].replace("\"", ""); |
String tklaes = parts.get( 12 ); |
65 |
|
|
66 |
entry.rute = laesnr; |
entry.rute = laesnr; |
67 |
entry.koreliste = "/" + foede + "/" + tklaes + "/" + jobnr; |
entry.koreliste = "/" + foede + "/" + tklaes + "/" + jobnr; |