2 |
|
|
3 |
import java.io.BufferedReader; |
import java.io.BufferedReader; |
4 |
import java.io.IOException; |
import java.io.IOException; |
|
import java.io.InputStream; |
|
5 |
import java.io.InputStreamReader; |
import java.io.InputStreamReader; |
6 |
import java.nio.charset.Charset; |
import java.nio.charset.Charset; |
7 |
import java.util.List; |
import java.util.List; |
11 |
import com.google.common.base.Splitter; |
import com.google.common.base.Splitter; |
12 |
|
|
13 |
import dk.daoas.adressevedligehold.AddressSourceEntry.EntryType; |
import dk.daoas.adressevedligehold.AddressSourceEntry.EntryType; |
|
import dk.daoas.adressevedligehold.util.DeduplicateHelper; |
|
14 |
|
|
15 |
public class AddressSourceFD implements AddressSource { |
public class AddressSourceFD extends AbstractAddressSource { |
16 |
|
|
|
|
|
|
DeduplicateHelper<String> dirigeringsCache = new DeduplicateHelper<String>(); |
|
|
|
|
|
FileItem file; |
|
|
|
|
|
InputStream is; |
|
|
InputStreamReader isr; |
|
|
BufferedReader br; |
|
|
|
|
|
int lineCount = 0; |
|
17 |
|
|
18 |
String filenameFirst2; |
String filenameFirst2; |
19 |
|
|
20 |
public AddressSourceFD(FileItem file) throws Exception { |
public AddressSourceFD(FileItem file) throws Exception { |
21 |
this.file = file; |
super(file); |
22 |
|
|
23 |
filenameFirst2 = file.getName().substring(0, 2).toUpperCase(); |
filenameFirst2 = file.getName().substring(0, 2).toUpperCase(); |
24 |
} |
} |
|
|
|
|
@Override |
|
|
public String getFilename() { |
|
|
return file.getName(); |
|
|
} |
|
25 |
|
|
26 |
@Override |
@Override |
27 |
public void validate() throws IOException { |
public void validate() throws IOException { |
31 |
br = new BufferedReader(isr); |
br = new BufferedReader(isr); |
32 |
|
|
33 |
String line = br.readLine(); |
String line = br.readLine(); |
34 |
|
|
35 |
|
if (line == null) { |
36 |
|
throw new IOException("Can't read 1st line - is file empty?"); |
37 |
|
} |
38 |
|
|
39 |
String[] parts = line.split(";"); |
String[] parts = line.split(";"); |
40 |
int numFields = parts.length; |
if (parts.length != 9) { |
41 |
if (numFields != 9) { |
throw new IOException("Not enough fields in CSV file. Found " + parts.length + ", expected 9"); |
|
throw new IOException("Not enough fields in CSV file. Found " + numFields + ", expected 9"); |
|
42 |
} |
} |
43 |
|
|
44 |
|
|
115 |
return "FD"; |
return "FD"; |
116 |
} |
} |
117 |
|
|
|
@Override //AutoCloseable |
|
|
public void close() throws Exception { |
|
|
System.out.println("Closing FD after lines " + lineCount); |
|
|
try { |
|
|
br.close(); |
|
|
isr.close(); |
|
|
is.close(); |
|
|
|
|
|
file.delete(); |
|
|
|
|
|
} catch (Exception e) { |
|
|
System.out.println("Error on closing " + e.getMessage() ); |
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
|
|
118 |
} |
} |