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

Annotation of /dao/DaoAdresseVedligehold/src/main/java/dk/daoas/adressevedligehold/coveragefileupload/AddressSourceFD.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2868 - (hide annotations) (download)
Thu Jan 28 16:12:28 2016 UTC (8 years, 4 months ago) by torben
Original Path: dao/DaoAdresseVedligehold/src/main/java/dk/daoas/adressevedligehold/AddressSourceFD.java
File size: 2852 byte(s)
Refactor common code from AddressSource* to AbstractAddressSource
1 torben 2852 package dk.daoas.adressevedligehold;
2    
3     import java.io.BufferedReader;
4     import java.io.IOException;
5     import java.io.InputStreamReader;
6     import java.nio.charset.Charset;
7     import java.util.List;
8    
9     import org.apache.commons.fileupload.FileItem;
10    
11     import com.google.common.base.Splitter;
12    
13     import dk.daoas.adressevedligehold.AddressSourceEntry.EntryType;
14    
15 torben 2868 public class AddressSourceFD extends AbstractAddressSource {
16    
17 torben 2852
18     String filenameFirst2;
19    
20     public AddressSourceFD(FileItem file) throws Exception {
21 torben 2868 super(file);
22 torben 2852
23     filenameFirst2 = file.getName().substring(0, 2).toUpperCase();
24     }
25    
26     @Override
27     public void validate() throws IOException {
28     try {
29     is = file.getInputStream();
30     isr = new InputStreamReader(is, Charset.forName("ISO-8859-1") );
31     br = new BufferedReader(isr);
32    
33     String line = br.readLine();
34 torben 2861
35     if (line == null) {
36     throw new IOException("Can't read 1st line - is file empty?");
37     }
38    
39 torben 2852 String[] parts = line.split(";");
40 torben 2861 if (parts.length != 9) {
41     throw new IOException("Not enough fields in CSV file. Found " + parts.length + ", expected 9");
42 torben 2852 }
43    
44    
45    
46     } catch (Exception e) {
47     try {
48     br.close();
49     isr.close();
50     is.close();
51     } catch (Exception e2) {
52     System.out.println("Error cleaning up resources");
53     }
54    
55     throw e; // Re-throw
56    
57     }
58     }
59    
60    
61     //TODO: Skal csv parsning klares med Apache Commons CSV ?
62    
63     @Override
64     public AddressSourceEntry getNextEntry() throws IOException {
65     String line = br.readLine();
66     if (line == null) // end of file
67     return null;
68    
69     if (line.trim().equals(""))
70     return null;
71    
72     //System.out.println(line);
73    
74     AddressSourceEntry entry = new AddressSourceEntry( EntryType.TypeSingleAddress);
75     entry.distributor = "FD";
76    
77     List<String> parts = Splitter.on(';').splitToList(line);
78    
79     if (parts.size() != 9) {
80     throw new IOException("Not enough fields in line " + line);
81     }
82    
83     entry.postnr = Short.parseShort( parts.get(0) );
84     entry.vejnavn = parts.get(1);
85     entry.husnr = Short.parseShort( parts.get(2) );
86     entry.litra = parts.get(3);
87     entry.vejkode = Short.parseShort( parts.get(4) );
88     entry.kommunekode = Short.parseShort( parts.get(5) );
89     entry.gadeid = Integer.parseInt( parts.get(6) );
90     entry.rute = dirigeringsCache.getInstance( parts.get(7) );
91     entry.koreliste = dirigeringsCache.getInstance( parts.get(8) );
92    
93     switch(filenameFirst2) {
94     case "HV":
95     entry.ugedage = EntryUgedage.MAN_FRE;
96     break;
97     case "LO":
98     entry.ugedage = EntryUgedage.LOR;
99     break;
100     case "SO":
101     entry.ugedage = EntryUgedage.SON;
102     break;
103     default:
104     throw new IOException("Ukendt ugedage " + filenameFirst2);
105     }
106    
107    
108     lineCount++;
109    
110     return entry;
111     }
112    
113     @Override
114     public String getDistributor() {
115     return "FD";
116     }
117    
118     }

  ViewVC Help
Powered by ViewVC 1.1.20