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

Diff of /dao/DaoAdresseVedligehold/src/main/java/dk/daoas/adressevedligehold/fileupload/AddressSourceFD.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 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  import java.util.List;  import java.util.List;
5    
6  import org.apache.commons.fileupload.FileItem;  import org.apache.commons.fileupload.FileItem;
# Line 12  import org.apache.commons.fileupload.Fil Line 8  import org.apache.commons.fileupload.Fil
8  import com.google.common.base.Splitter;  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    
12  public class AddressSourceFD implements AddressSource {  public class AddressSourceFD extends AbstractAddressSource {
13                            
           
         DeduplicateHelper<String> dirigeringsCache = new DeduplicateHelper<String>();  
           
         FileItem file;  
           
         InputStream is;  
         InputStreamReader isr;  
         BufferedReader br;  
           
         int lineCount = 0;  
14                    
15          String filenameFirst2;          String filenameFirst2;
16                                    
17          public AddressSourceFD(FileItem file) throws Exception {          public AddressSourceFD(FileItem file) throws Exception {
18                  this.file = file;                  super(file);
19                                    
20                  filenameFirst2 = file.getName().substring(0, 2).toUpperCase();                  filenameFirst2 = file.getName().substring(0, 2).toUpperCase();
21          }          }
   
         @Override  
         public String getFilename() {  
                 return file.getName();  
         }  
22                    
23          @Override          @Override
24          public void validate() throws IOException {          public void validate() throws IOException {
25                  try {                  super.validateWithHeader(9, ';');
                         is = file.getInputStream();  
                         isr = new InputStreamReader(is, Charset.forName("ISO-8859-1") );  
                         br = new BufferedReader(isr);  
                           
                         String line = br.readLine();  
                           
                         if (line == null) {  
                                 throw new IOException("Can't read 1st line - is file empty?");  
                         }        
                           
                         String[] parts = line.split(";");  
                         if (parts.length != 9) {  
                                 throw new IOException("Not enough fields in CSV file. Found " + parts.length + ", expected 9");  
                         }  
   
   
                           
                 } catch (Exception e) {  
                         try {  
                                 br.close();  
                                 isr.close();  
                                 is.close();  
                         } catch (Exception e2) {  
                                 System.out.println("Error cleaning up resources");  
                         }  
                           
                         throw e; // Re-throw  
                           
                 }  
26          }          }
27                    
28    
# Line 132  public class AddressSourceFD implements Line 83  public class AddressSourceFD implements
83                  return "FD";                  return "FD";
84          }          }
85    
         @Override //AutoCloseable  
         public void close() throws Exception {  
                 System.out.println("Closing FD after lines " + lineCount);  
                 try {  
                         br.close();  
                         isr.close();  
                         is.close();  
   
                         file.delete();  
                           
                 } catch (Exception e) {  
                         System.out.println("Error on closing " + e.getMessage() );  
                 }  
                   
                   
         }  
   
86  }  }

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

  ViewVC Help
Powered by ViewVC 1.1.20