1 |
package dk.daoas.adressevedligehold; |
package dk.daoas.adressevedligehold.fileupload; |
2 |
|
|
|
import java.io.BufferedReader; |
|
3 |
import java.io.IOException; |
import java.io.IOException; |
|
import java.io.InputStream; |
|
|
import java.io.InputStreamReader; |
|
|
import java.nio.charset.Charset; |
|
4 |
|
|
5 |
import org.apache.commons.fileupload.FileItem; |
import org.apache.commons.fileupload.FileItem; |
6 |
|
|
7 |
import dk.daoas.adressevedligehold.AddressSourceEntry.EntryType; |
import dk.daoas.adressevedligehold.fileupload.AddressSourceEntry.EntryType; |
|
import dk.daoas.adressevedligehold.util.DeduplicateHelper; |
|
8 |
|
|
9 |
public class AddressSourceDAO implements AddressSource { |
public class AddressSourceDAO extends AbstractAddressSource { |
10 |
|
|
11 |
final static String DAO = "DAO"; |
final static String DAO = "DAO"; |
12 |
|
|
|
DeduplicateHelper<String> dirigeringsCache = new DeduplicateHelper<String>(); |
|
|
|
|
|
FileItem file; |
|
|
|
|
|
InputStream is; |
|
|
InputStreamReader isr; |
|
|
BufferedReader br; |
|
|
|
|
|
int lineCount = 0; |
|
13 |
|
|
14 |
public AddressSourceDAO(FileItem file) throws Exception { |
public AddressSourceDAO(FileItem file) throws Exception { |
15 |
this.file = file; |
super(file); |
|
|
|
16 |
|
|
|
|
|
|
} |
|
|
|
|
|
@Override |
|
|
public String getFilename() { |
|
|
return file.getName(); |
|
17 |
} |
} |
18 |
|
|
19 |
@Override |
@Override |
20 |
public void validate() throws IOException { |
public void validate() throws IOException { |
21 |
try { |
super.validateWithHeader(17, ';'); |
|
is = file.getInputStream(); |
|
|
isr = new InputStreamReader(is, Charset.forName("ISO-8859-1") ); |
|
|
br = new BufferedReader(isr); |
|
|
|
|
|
String line = br.readLine(); |
|
|
String[] parts = line.split(";"); |
|
|
int numFields = parts.length; |
|
|
if (numFields != 17) { |
|
|
throw new IOException("Not enough fields in CSV file. Found " + numFields + ", expected 17"); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} catch (Exception e) { |
|
|
try { |
|
|
br.close(); |
|
|
isr.close(); |
|
|
is.close(); |
|
|
} catch (Exception e2) { |
|
|
System.out.println("Error cleaning up resources"); |
|
|
} |
|
|
|
|
|
throw e; // Re-throw |
|
|
|
|
|
} |
|
22 |
} |
} |
23 |
|
|
24 |
|
|
33 |
if (line.trim().equals("")) |
if (line.trim().equals("")) |
34 |
return null; |
return null; |
35 |
|
|
|
//System.out.println(line); |
|
36 |
|
|
37 |
AddressSourceEntry entry = new AddressSourceEntry( EntryType.TypeAddressRange); |
AddressSourceEntry entry = new AddressSourceEntry( EntryType.TypeAddressRange); |
38 |
entry.distributor = DAO; |
entry.distributor = DAO; |
39 |
|
|
40 |
String[] parts = line.split(";"); |
String[] parts = line.split(";"); |
41 |
|
if (parts.length != 17) { |
42 |
|
throw new IOException("Not enough fields in CSV file. Found " + parts.length + ", expected 17"); |
43 |
|
} |
44 |
|
|
45 |
entry.gadeid = Integer.parseInt( parts[0] ); |
entry.gadeid = Integer.parseInt( parts[0] ); |
46 |
entry.vejnavn = parts[1]; |
entry.vejnavn = parts[1]; |
47 |
//String stednavn = parts[2]; //Ikke brugt |
//String stednavn = parts[2]; //Ikke brugt |
85 |
return DAO; |
return DAO; |
86 |
} |
} |
87 |
|
|
|
@Override //AutoCloseable |
|
|
public void close() throws Exception { |
|
|
System.out.println("Closing " + DAO + " after lines " + lineCount); |
|
|
try { |
|
|
br.close(); |
|
|
isr.close(); |
|
|
is.close(); |
|
|
|
|
|
file.delete(); |
|
|
|
|
|
} catch (Exception e) { |
|
|
System.out.println("Error on closing " + e.getMessage() ); |
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
|
|
88 |
} |
} |