package dk.daoas.adressevedligehold; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.nio.charset.Charset; import org.apache.commons.fileupload.FileItem; import dk.daoas.adressevedligehold.AddressSourceEntry.EntryType; import dk.daoas.adressevedligehold.util.DeduplicateHelper; public class AddressSourceDAO implements AddressSource { final static String DAO = "DAO"; DeduplicateHelper dirigeringsCache = new DeduplicateHelper(); FileItem file; InputStream is; InputStreamReader isr; BufferedReader br; int lineCount = 0; public AddressSourceDAO(FileItem file) throws Exception { this.file = file; } @Override public String getFilename() { return file.getName(); } @Override public void validate() throws IOException { try { 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 } } //TODO: Skal csv parsning klares med Apache Commons CSV ? @Override public AddressSourceEntry getNextEntry() throws IOException { String line = br.readLine(); if (line == null) // end of file return null; if (line.trim().equals("")) return null; //System.out.println(line); AddressSourceEntry entry = new AddressSourceEntry( EntryType.TypeAddressRange); entry.distributor = DAO; String[] parts = line.split(";"); if (parts.length != 17) { throw new IOException("Not enough fields in CSV file. Found " + parts.length + ", expected 17"); } entry.gadeid = Integer.parseInt( parts[0] ); entry.vejnavn = parts[1]; //String stednavn = parts[2]; //Ikke brugt //String david = parts[3]; //Ikke brugt entry.postnr = Short.parseShort( parts[4] ); short net = Short.parseShort( parts[5] ); entry.rute = dirigeringsCache.getInstance( parts[6].toUpperCase() ); //Nogle Bruter i gl inputfil kan stå med lille b entry.husnr = Short.parseShort( parts[7] ); entry.litra = parts[8]; entry.tilHusnr = Short.parseShort( parts[9] ); entry.tilLitra = parts[10]; //String sekvens = parts[11]; //String fraDato = parts[12]; //String tilDato = parts[13]; //short bane = Short.parseShort( parts[14] ); //String distributor = parts[15]; entry.koreliste = dirigeringsCache.getInstance( parts[16] ); switch (net) { case 5: entry.ugedage = EntryUgedage.LOR; break; case 6: entry.ugedage = EntryUgedage.MAN_FRE; break; case 7: entry.ugedage = EntryUgedage.SON; break; default: throw new IOException("Ukendt net" + net); } lineCount++; return entry; } @Override public String getDistributor() { return DAO; } @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() ); } } }