/[projects]/dao/DaoAdresseVedligehold/src/main/java/dk/daoas/adressevedligehold/fileupload/AddressSourceBK.java
ViewVC logotype

Diff of /dao/DaoAdresseVedligehold/src/main/java/dk/daoas/adressevedligehold/fileupload/AddressSourceBK.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 2844 by torben, Mon Jan 25 21:43:59 2016 UTC revision 2869 by torben, Thu Jan 28 16:21:36 2016 UTC
# Line 1  Line 1 
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    
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.AddressSourceEntry.EntryType;
 import dk.daoas.adressevedligehold.util.DeduplicateHelper;  
8  import dk.daoas.adressevedligehold.util.SafeParsers;  import dk.daoas.adressevedligehold.util.SafeParsers;
9    
10  public class AddressSourceBK implements AddressSource {  public class AddressSourceBK extends AbstractAddressSource {
11                    
         DeduplicateHelper<String> dirigeringsCache = new DeduplicateHelper<String>();  
           
         FileItem file;  
           
         InputStream is;  
         InputStreamReader isr;  
         BufferedReader br;  
           
         int lineCount = 0;  
12                                    
13          public AddressSourceBK(FileItem file) throws Exception {          public AddressSourceBK(FileItem file) throws Exception {
14                  this.file = file;                  super(file);
   
   
   
15          }          }
16    
         @Override  
         public String getFilename() {  
                 return file.getName();  
         }  
17                    
18          @Override          @Override
19          public void validate() throws IOException {          public void validate() throws IOException {
20                  try (                            super.validatNoHeaderLine(13, ';');
                                 InputStream is1 = file.getInputStream();  
                                 InputStreamReader isr1 = new InputStreamReader(is1, Charset.forName("ISO-8859-1") );  
                                 BufferedReader br1 = new BufferedReader(isr1)  
                         ) {  
                         String line = br1.readLine();  
                         String[] parts = line.split(";");  
                         int numFields = parts.length;  
                         if (numFields != 13) {  
                                 throw new IOException("Not enough fields in CSV file. Found " + numFields + ", expected 13");  
                         }  
                 }  
   
                   
                 is = file.getInputStream();  
                 isr = new InputStreamReader(is, Charset.forName("ISO-8859-1") );  
                 br = new BufferedReader(isr);  
21          }          }
22    
23          //TODO: Skal csv parsning klares med Apache Commons CSV ?          //TODO: Skal csv parsning klares med Apache Commons CSV ?
# Line 74  public class AddressSourceBK implements Line 37  public class AddressSourceBK implements
37                  entry.distributor = "BK";                  entry.distributor = "BK";
38                                    
39                  String[] parts = line.split(";");                  String[] parts = line.split(";");
40                    
41                    if (parts.length != 13) {
42                            throw new IOException("Not enough fields in CSV file. Found " + parts.length + ", expected 13");
43                    }
44                    
45                  entry.postnr = Short.parseShort( parts[0]);                  entry.postnr = Short.parseShort( parts[0]);
46                  entry.vejnavn = parts[1].replace("\"", "");                  entry.vejnavn = parts[1].replace("\"", "");
47                  entry.husnr = Short.parseShort( parts[2] );                  entry.husnr = Short.parseShort( parts[2] );
# Line 125  public class AddressSourceBK implements Line 93  public class AddressSourceBK implements
93                  return "BK";                  return "BK";
94          }          }
95    
         @Override //AutoCloseable  
         public void close() throws Exception {  
                 System.out.println("Closing BK after lines " + lineCount);  
                 try {  
                         br.close();  
                         isr.close();  
                         is.close();  
   
                         file.delete();  
                           
                 } catch (Exception e) {  
                         System.out.println("Error on closing " + e.getMessage() );  
                 }  
                   
                   
         }  
   
96  }  }

Legend:
Removed from v.2844  
changed lines
  Added in v.2869

  ViewVC Help
Powered by ViewVC 1.1.20