--- dao/DaoAdresseVedligehold/src/main/java/dk/daoas/adressevedligehold/AddressSourceNS.java 2016/01/28 11:03:14 2861 +++ dao/DaoAdresseVedligehold/src/main/java/dk/daoas/adressevedligehold/AddressSourceNS.java 2016/02/12 09:55:14 2933 @@ -1,68 +1,38 @@ 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 java.util.List; import org.apache.commons.fileupload.FileItem; +import com.google.common.base.CharMatcher; +import com.google.common.base.Splitter; + import dk.daoas.adressevedligehold.AddressSourceEntry.EntryType; -import dk.daoas.adressevedligehold.util.DeduplicateHelper; -public class AddressSourceNS implements AddressSource { +public class AddressSourceNS extends AbstractAddressSource { enum NextUgedag { MANDAG,TIRSDAG,ONSDAG,TORSDAG,FREDAG,LORDAG,SONDAG } + - DeduplicateHelper dirigeringsCache = new DeduplicateHelper(); - - FileItem file; - - InputStream is; - InputStreamReader isr; - BufferedReader br; + final Splitter splitter = Splitter.on(',') + .trimResults( CharMatcher.is('"') ); - String parts[]; + List parts; - int lineCount = 0; NextUgedag nextUgedag = NextUgedag.MANDAG; public AddressSourceNS(FileItem file) throws Exception { - this.file = file; + super(file); } - @Override - public String getFilename() { - return file.getName(); - } @Override public void validate() throws IOException { - try ( - InputStream is1 = file.getInputStream(); - InputStreamReader isr1 = new InputStreamReader(is1, Charset.forName("ISO-8859-1") ); - BufferedReader br1 = new BufferedReader(isr1) - ) { - String line = br1.readLine(); - - if (line == null) { - throw new IOException("Can't read 1st line - is file empty?"); - } - - String[] parts = line.split(","); - if (parts.length != 22) { - throw new IOException("Not enough fields in CSV file. Found " + parts.length + ", expected 22"); - } - } - - - is = file.getInputStream(); - isr = new InputStreamReader(is, Charset.forName("ISO-8859-1") ); - br = new BufferedReader(isr); + super.validatNoHeaderLine(22, ','); } //TODO: Skal csv parsning klares med Apache Commons CSV ? @@ -76,10 +46,9 @@ if (line.trim().equals("")) return null; - parts = line.split(","); + parts = splitter.splitToList(line); - if (parts.length != 22) { - System.out.println( line ); + if (parts.size() != 22) { throw new IOException("Not enough fields in line " + line); } } @@ -91,55 +60,55 @@ entry.distributor = "NS"; - entry.gadeid = Integer.parseInt( parts[0] ); - entry.vejnavn = parts[1].replace("\"", ""); - //stednavn = parts[2]; - //ukendtt = parts[3]; - entry.postnr = Short.parseShort( parts[4] ); - entry.rute = dirigeringsCache.getInstance( parts[5].replace("\"", "") ); - entry.husnr = Short.parseShort( parts[6] ); - entry.litra = parts[7].replace("\"", ""); - entry.tilHusnr = Short.parseShort( parts[8] ); - entry.tilLitra = parts[9].replace("\"", ""); - //sekvens = parts[10] - //fradato = parts[11] - //tildato = parts[12] - //distributor = parts[13] - //inspektorkode = parts[21] + entry.gadeid = Integer.parseInt( parts.get(0) ); + entry.vejnavn = parts.get(1); + //stednavn = parts.get(2); + //ukendtt = parts.get(3); + entry.postnr = Short.parseShort( parts.get(4) ); + entry.rute = dirigeringsCache.getInstance( parts.get(5) ); + entry.husnr = Short.parseShort( parts.get(6) ); + entry.litra = parts.get(7); + entry.tilHusnr = Short.parseShort( parts.get(8) ); + entry.tilLitra = parts.get(9); + //sekvens = parts.get(10) + //fradato = parts.get(11) + //tildato = parts.get(12) + //distributor = parts.get(13) + //inspektorkode = parts.get(21) switch (nextUgedag) { case MANDAG: - entry.koreliste = dirigeringsCache.getInstance( parts[14] ); + entry.koreliste = dirigeringsCache.getInstance( parts.get(14) ); entry.ugedage = EntryUgedage.MAN; nextUgedag = NextUgedag.TIRSDAG; break; case TIRSDAG: - entry.koreliste = dirigeringsCache.getInstance( parts[15] ); + entry.koreliste = dirigeringsCache.getInstance( parts.get(15) ); entry.ugedage = EntryUgedage.TIR; nextUgedag = NextUgedag.ONSDAG; break; case ONSDAG: - entry.koreliste = dirigeringsCache.getInstance( parts[16] ); + entry.koreliste = dirigeringsCache.getInstance( parts.get(16) ); entry.ugedage = EntryUgedage.ONS; nextUgedag = NextUgedag.TORSDAG; break; case TORSDAG: - entry.koreliste = dirigeringsCache.getInstance( parts[17] ); + entry.koreliste = dirigeringsCache.getInstance( parts.get(17) ); entry.ugedage = EntryUgedage.TOR; nextUgedag = NextUgedag.FREDAG; break; case FREDAG: - entry.koreliste = dirigeringsCache.getInstance( parts[18] ); + entry.koreliste = dirigeringsCache.getInstance( parts.get(18) ); entry.ugedage = EntryUgedage.FRE; nextUgedag = NextUgedag.LORDAG; break; case LORDAG: - entry.koreliste = dirigeringsCache.getInstance( parts[19] ); + entry.koreliste = dirigeringsCache.getInstance( parts.get(19) ); entry.ugedage = EntryUgedage.LOR; nextUgedag = NextUgedag.SONDAG; break; case SONDAG: - entry.koreliste = dirigeringsCache.getInstance( parts[20] ); + entry.koreliste = dirigeringsCache.getInstance( parts.get(20) ); entry.ugedage = EntryUgedage.SON; nextUgedag = NextUgedag.MANDAG; break; @@ -158,21 +127,4 @@ return "NS"; } - @Override //AutoCloseable - public void close() throws Exception { - System.out.println("Closing NS after lines " + lineCount); - try { - br.close(); - isr.close(); - is.close(); - - file.delete(); - - } catch (Exception e) { - System.out.println("Error on closing " + e.getMessage() ); - } - - - } - }