1 |
package dk.daoas.adressevedligehold; |
package dk.daoas.adressevedligehold; |
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 |
import java.util.List; |
import java.util.List; |
5 |
|
|
6 |
import org.apache.commons.fileupload.FileItem; |
import org.apache.commons.fileupload.FileItem; |
8 |
import com.google.common.base.Splitter; |
import com.google.common.base.Splitter; |
9 |
|
|
10 |
import dk.daoas.adressevedligehold.AddressSourceEntry.EntryType; |
import dk.daoas.adressevedligehold.AddressSourceEntry.EntryType; |
|
import dk.daoas.adressevedligehold.util.DeduplicateHelper; |
|
11 |
|
|
12 |
public class AddressSourceFD implements AddressSource { |
public class AddressSourceFD extends AbstractAddressSource { |
13 |
|
|
|
|
|
|
DeduplicateHelper<String> dirigeringsCache = new DeduplicateHelper<String>(); |
|
|
|
|
|
FileItem file; |
|
|
|
|
|
InputStream is; |
|
|
InputStreamReader isr; |
|
|
BufferedReader br; |
|
|
|
|
|
int lineCount = 0; |
|
14 |
|
|
15 |
String filenameFirst2; |
String filenameFirst2; |
16 |
|
|
17 |
public AddressSourceFD(FileItem file) throws Exception { |
public AddressSourceFD(FileItem file) throws Exception { |
18 |
this.file = file; |
super(file); |
19 |
|
|
20 |
filenameFirst2 = file.getName().substring(0, 2).toUpperCase(); |
filenameFirst2 = file.getName().substring(0, 2).toUpperCase(); |
21 |
} |
} |
|
|
|
|
@Override |
|
|
public String getFilename() { |
|
|
return file.getName(); |
|
|
} |
|
22 |
|
|
23 |
@Override |
@Override |
24 |
public void validate() throws IOException { |
public void validate() throws IOException { |
25 |
try { |
super.validateWithHeader(9, ';'); |
|
is = file.getInputStream(); |
|
|
isr = new InputStreamReader(is, Charset.forName("ISO-8859-1") ); |
|
|
br = new BufferedReader(isr); |
|
|
|
|
|
String line = br.readLine(); |
|
|
|
|
|
if (line == null) { |
|
|
throw new IOException("Can't read 1st line - is file empty?"); |
|
|
} |
|
|
|
|
|
String[] parts = line.split(";"); |
|
|
if (parts.length != 9) { |
|
|
throw new IOException("Not enough fields in CSV file. Found " + parts.length + ", expected 9"); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} catch (Exception e) { |
|
|
try { |
|
|
br.close(); |
|
|
isr.close(); |
|
|
is.close(); |
|
|
} catch (Exception e2) { |
|
|
System.out.println("Error cleaning up resources"); |
|
|
} |
|
|
|
|
|
throw e; // Re-throw |
|
|
|
|
|
} |
|
26 |
} |
} |
27 |
|
|
28 |
|
|
83 |
return "FD"; |
return "FD"; |
84 |
} |
} |
85 |
|
|
|
@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() ); |
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
|
|
86 |
} |
} |