1 |
package dk.daoas.adressevedligehold;
|
2 |
|
3 |
import java.io.BufferedReader;
|
4 |
import java.io.IOException;
|
5 |
import java.io.InputStream;
|
6 |
import java.io.InputStreamReader;
|
7 |
import java.nio.charset.Charset;
|
8 |
|
9 |
import org.apache.commons.fileupload.FileItem;
|
10 |
|
11 |
import dk.daoas.adressevedligehold.AddressSourceEntry.EntryType;
|
12 |
import dk.daoas.adressevedligehold.util.DeduplicateHelper;
|
13 |
|
14 |
public class AddressSourceDAO implements AddressSource {
|
15 |
|
16 |
final static String DAO = "DAO";
|
17 |
|
18 |
DeduplicateHelper<String> dirigeringsCache = new DeduplicateHelper<String>();
|
19 |
|
20 |
FileItem file;
|
21 |
|
22 |
InputStream is;
|
23 |
InputStreamReader isr;
|
24 |
BufferedReader br;
|
25 |
|
26 |
int lineCount = 0;
|
27 |
|
28 |
public AddressSourceDAO(FileItem file) throws Exception {
|
29 |
this.file = file;
|
30 |
|
31 |
|
32 |
|
33 |
}
|
34 |
|
35 |
@Override
|
36 |
public String getFilename() {
|
37 |
return file.getName();
|
38 |
}
|
39 |
|
40 |
@Override
|
41 |
public void validate() throws IOException {
|
42 |
try {
|
43 |
is = file.getInputStream();
|
44 |
isr = new InputStreamReader(is, Charset.forName("ISO-8859-1") );
|
45 |
br = new BufferedReader(isr);
|
46 |
|
47 |
String line = br.readLine();
|
48 |
if (line == null) {
|
49 |
throw new IOException("Can't read 1st line - is file empty?");
|
50 |
}
|
51 |
|
52 |
String[] parts = line.split(";");
|
53 |
if (parts.length != 17) {
|
54 |
throw new IOException("Not enough fields in CSV file. Found " + parts.length + ", expected 17");
|
55 |
}
|
56 |
|
57 |
|
58 |
|
59 |
} catch (Exception e) {
|
60 |
try {
|
61 |
br.close();
|
62 |
isr.close();
|
63 |
is.close();
|
64 |
} catch (Exception e2) {
|
65 |
System.out.println("Error cleaning up resources");
|
66 |
}
|
67 |
|
68 |
throw e; // Re-throw
|
69 |
|
70 |
}
|
71 |
}
|
72 |
|
73 |
|
74 |
//TODO: Skal csv parsning klares med Apache Commons CSV ?
|
75 |
|
76 |
@Override
|
77 |
public AddressSourceEntry getNextEntry() throws IOException {
|
78 |
String line = br.readLine();
|
79 |
if (line == null) // end of file
|
80 |
return null;
|
81 |
|
82 |
if (line.trim().equals(""))
|
83 |
return null;
|
84 |
|
85 |
//System.out.println(line);
|
86 |
|
87 |
AddressSourceEntry entry = new AddressSourceEntry( EntryType.TypeAddressRange);
|
88 |
entry.distributor = DAO;
|
89 |
|
90 |
String[] parts = line.split(";");
|
91 |
if (parts.length != 17) {
|
92 |
throw new IOException("Not enough fields in CSV file. Found " + parts.length + ", expected 17");
|
93 |
}
|
94 |
|
95 |
entry.gadeid = Integer.parseInt( parts[0] );
|
96 |
entry.vejnavn = parts[1];
|
97 |
//String stednavn = parts[2]; //Ikke brugt
|
98 |
//String david = parts[3]; //Ikke brugt
|
99 |
entry.postnr = Short.parseShort( parts[4] );
|
100 |
short net = Short.parseShort( parts[5] );
|
101 |
entry.rute = dirigeringsCache.getInstance( parts[6].toUpperCase() ); //Nogle Bruter i gl inputfil kan stå med lille b
|
102 |
entry.husnr = Short.parseShort( parts[7] );
|
103 |
entry.litra = parts[8];
|
104 |
entry.tilHusnr = Short.parseShort( parts[9] );
|
105 |
entry.tilLitra = parts[10];
|
106 |
//String sekvens = parts[11];
|
107 |
//String fraDato = parts[12];
|
108 |
//String tilDato = parts[13];
|
109 |
//short bane = Short.parseShort( parts[14] );
|
110 |
//String distributor = parts[15];
|
111 |
entry.koreliste = dirigeringsCache.getInstance( parts[16] );
|
112 |
|
113 |
switch (net) {
|
114 |
case 5:
|
115 |
entry.ugedage = EntryUgedage.LOR;
|
116 |
break;
|
117 |
case 6:
|
118 |
entry.ugedage = EntryUgedage.MAN_FRE;
|
119 |
break;
|
120 |
case 7:
|
121 |
entry.ugedage = EntryUgedage.SON;
|
122 |
break;
|
123 |
default:
|
124 |
throw new IOException("Ukendt net" + net);
|
125 |
}
|
126 |
|
127 |
|
128 |
lineCount++;
|
129 |
|
130 |
return entry;
|
131 |
}
|
132 |
|
133 |
@Override
|
134 |
public String getDistributor() {
|
135 |
return DAO;
|
136 |
}
|
137 |
|
138 |
@Override //AutoCloseable
|
139 |
public void close() throws Exception {
|
140 |
System.out.println("Closing " + DAO + " after lines " + lineCount);
|
141 |
try {
|
142 |
br.close();
|
143 |
isr.close();
|
144 |
is.close();
|
145 |
|
146 |
file.delete();
|
147 |
|
148 |
} catch (Exception e) {
|
149 |
System.out.println("Error on closing " + e.getMessage() );
|
150 |
}
|
151 |
|
152 |
|
153 |
}
|
154 |
|
155 |
}
|