/[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 2838 by torben, Sun Jan 24 21:48:55 2016 UTC revision 2868 by torben, Thu Jan 28 16:12:28 2016 UTC
# Line 9  import java.nio.charset.Charset; Line 9  import java.nio.charset.Charset;
9  import org.apache.commons.fileupload.FileItem;  import org.apache.commons.fileupload.FileItem;
10    
11  import dk.daoas.adressevedligehold.AddressSourceEntry.EntryType;  import dk.daoas.adressevedligehold.AddressSourceEntry.EntryType;
 import dk.daoas.adressevedligehold.util.DeduplicateHelper;  
12  import dk.daoas.adressevedligehold.util.SafeParsers;  import dk.daoas.adressevedligehold.util.SafeParsers;
13    
14  public class AddressSourceBK implements AddressSource {  public class AddressSourceBK extends AbstractAddressSource {
15                    
         DeduplicateHelper<String> dirigeringsCache = new DeduplicateHelper<String>();  
           
         FileItem file;  
           
         InputStream is;  
         InputStreamReader isr;  
         BufferedReader br;  
           
         int lineCount = 0;  
16                                    
17          public AddressSourceBK(FileItem file) throws Exception {          public AddressSourceBK(FileItem file) throws Exception {
18                  this.file = file;                  super(file);
19            }
20    
21            
22            @Override
23            public void validate() throws IOException {
24                    try (          
25                                    InputStream is1 = file.getInputStream();
26                                    InputStreamReader isr1 = new InputStreamReader(is1, Charset.forName("ISO-8859-1") );
27                                    BufferedReader br1 = new BufferedReader(isr1)
28                            ) {
29                            String line = br1.readLine();
30                            
31                            if (line == null) {
32                                    throw new IOException("Can't read 1st line - is file empty?");
33                            }
34                            
35                            String[] parts = line.split(";");
36    
37                            if (parts.length != 13) {
38                                    throw new IOException("Not enough fields in CSV file. Found " + parts.length + ", expected 13");
39                            }
40                    }
41    
42                    
43                  is = file.getInputStream();                  is = file.getInputStream();
44                  isr = new InputStreamReader(is, Charset.forName("ISO-8859-1") );                  isr = new InputStreamReader(is, Charset.forName("ISO-8859-1") );
45                  br = new BufferedReader(isr);                  br = new BufferedReader(isr);
                                   
46          }          }
47    
         @Override  
         public String getFilename() {  
                 return file.getName();  
         }  
           
48          //TODO: Skal csv parsning klares med Apache Commons CSV ?          //TODO: Skal csv parsning klares med Apache Commons CSV ?
49    
50          @Override          @Override
# Line 56  public class AddressSourceBK implements Line 62  public class AddressSourceBK implements
62                  entry.distributor = "BK";                  entry.distributor = "BK";
63                                    
64                  String[] parts = line.split(";");                  String[] parts = line.split(";");
65                    
66                    if (parts.length != 13) {
67                            throw new IOException("Not enough fields in CSV file. Found " + parts.length + ", expected 13");
68                    }
69                    
70                  entry.postnr = Short.parseShort( parts[0]);                  entry.postnr = Short.parseShort( parts[0]);
71                  entry.vejnavn = parts[1].replace("\"", "");                  entry.vejnavn = parts[1].replace("\"", "");
72                  entry.husnr = Short.parseShort( parts[2] );                  entry.husnr = Short.parseShort( parts[2] );
# Line 68  public class AddressSourceBK implements Line 79  public class AddressSourceBK implements
79                                    
80                  String laesnr = parts[8].replace("\"", "");                  String laesnr = parts[8].replace("\"", "");
81                                    
82                  @SuppressWarnings("unused") String distnr = parts[9].replace("\"", "");                  //String distnr = parts[9].replace("\"", ""); //Bruges ikke
83                  String foede = parts[10].replace("\"", "");                  String foede = parts[10].replace("\"", "");
84                  String jobnr = parts[11].replace("\"", "");                  String jobnr = parts[11].replace("\"", "");
85                  String tklaes = parts[12].replace("\"", "");                  String tklaes = parts[12].replace("\"", "");
# Line 107  public class AddressSourceBK implements Line 118  public class AddressSourceBK implements
118                  return "BK";                  return "BK";
119          }          }
120    
         @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() );  
                 }  
                   
                   
         }  
   
121  }  }

Legend:
Removed from v.2838  
changed lines
  Added in v.2868

  ViewVC Help
Powered by ViewVC 1.1.20