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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.20