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

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

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

revision 2861 by torben, Thu Jan 28 11:03:14 2016 UTC revision 2874 by torben, Thu Jan 28 17:15:42 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;
4  import java.io.InputStream;  import java.util.List;
 import java.io.InputStreamReader;  
 import java.nio.charset.Charset;  
5    
6  import org.apache.commons.fileupload.FileItem;  import org.apache.commons.fileupload.FileItem;
7    
8    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  import dk.daoas.adressevedligehold.util.SafeParsers;  import dk.daoas.adressevedligehold.util.SafeParsers;
12    
13  public class AddressSourceBK implements AddressSource {  public class AddressSourceBK extends AbstractAddressSource {
           
         DeduplicateHelper<String> dirigeringsCache = new DeduplicateHelper<String>();  
           
         FileItem file;  
14                    
         InputStream is;  
         InputStreamReader isr;  
         BufferedReader br;  
           
         int lineCount = 0;  
15                                    
16          public AddressSourceBK(FileItem file) throws Exception {          public AddressSourceBK(FileItem file) throws Exception {
17                  this.file = file;                  super(file);
   
   
   
18          }          }
19    
         @Override  
         public String getFilename() {  
                 return file.getName();  
         }  
20                    
21          @Override          @Override
22          public void validate() throws IOException {          public void validate() throws IOException {
23                  try (                            super.validatNoHeaderLine(14, ';');
                                 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 != 13) {  
                                 throw new IOException("Not enough fields in CSV file. Found " + parts.length + ", expected 13");  
                         }  
                 }  
   
                   
                 is = file.getInputStream();  
                 isr = new InputStreamReader(is, Charset.forName("ISO-8859-1") );  
                 br = new BufferedReader(isr);  
24          }          }
25    
26          //TODO: Skal csv parsning klares med Apache Commons CSV ?          //TODO: Skal csv parsning klares med Apache Commons CSV ?
# Line 78  public class AddressSourceBK implements Line 39  public class AddressSourceBK implements
39                  AddressSourceEntry entry = new AddressSourceEntry( EntryType.TypeSingleAddress);                  AddressSourceEntry entry = new AddressSourceEntry( EntryType.TypeSingleAddress);
40                  entry.distributor = "BK";                  entry.distributor = "BK";
41                                    
42                  String[] parts = line.split(";");                  List<String> parts =Splitter.on(';').splitToList(line);
43                                    
44                  if (parts.length != 13) {                  if (parts.size() != 14) {
45                          throw new IOException("Not enough fields in CSV file. Found " + parts.length + ", expected 13");                          throw new IOException("Not enough fields in CSV file. Found " + parts.size() + ", expected 14");
46                  }                  }
47                                    
48                  entry.postnr = Short.parseShort( parts[0]);                  entry.postnr = Short.parseShort( parts.get(0) );
49                  entry.vejnavn = parts[1].replace("\"", "");                  entry.vejnavn = parts.get( 1 ).replace("\"", "");
50                  entry.husnr = Short.parseShort( parts[2] );                  entry.husnr = Short.parseShort( parts.get( 2 ) );
51                  entry.litra = parts[3].replace("\"", "");                  entry.litra = parts.get( 3 ).replace("\"", "");
52                  entry.vejkode = SafeParsers.parseShort( parts[4] );                  entry.vejkode = SafeParsers.parseShort( parts.get( 4)  );
53                  entry.kommunekode = SafeParsers.parseShort( parts[5] );                  entry.kommunekode = SafeParsers.parseShort( parts.get( 5)  );
54                  entry.gadeid = Integer.parseInt( parts[6] );                  entry.gadeid = Integer.parseInt( parts.get( 6 ) );
55                                    
56                  String ugedage = parts[7].replace("\"", "");                  String ugedage = parts.get( 7 ).replace("\"", "");
57                                    
58                  String laesnr = parts[8].replace("\"", "");                  String laesnr = parts.get( 8 ).replace("\"", "");
59                                    
60                  //String distnr = parts[9].replace("\"", ""); //Bruges ikke                  //String distnr = parts.get( 9 ).replace("\"", ""); //Bruges ikke
61                  String foede = parts[10].replace("\"", "");                  String foede = parts.get( 10 ).replace("\"", "");
62                  String jobnr = parts[11].replace("\"", "");                  String jobnr = parts.get( 11 ).replace("\"", "");
63                  String tklaes = parts[12].replace("\"", "");                  String tklaes = parts.get( 12 ).replace("\"", "");
64                                    
65                  entry.rute = laesnr;                  entry.rute = laesnr;
66                  entry.koreliste = "/" + foede + "/" + tklaes + "/" + jobnr;                  entry.koreliste = "/" + foede + "/" + tklaes + "/" + jobnr;
# Line 135  public class AddressSourceBK implements Line 96  public class AddressSourceBK implements
96                  return "BK";                  return "BK";
97          }          }
98    
         @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() );  
                 }  
                   
                   
         }  
   
99  }  }

Legend:
Removed from v.2861  
changed lines
  Added in v.2874

  ViewVC Help
Powered by ViewVC 1.1.20