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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2852 - (show annotations) (download)
Tue Jan 26 22:24:13 2016 UTC (8 years, 3 months ago) by torben
File size: 3453 byte(s)
Implement FD
1 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 String[] parts = line.split(";");
52 int numFields = parts.length;
53 if (numFields != 9) {
54 throw new IOException("Not enough fields in CSV file. Found " + numFields + ", expected 9");
55 }
56
57
58
59 } catch (Exception e) {
60 try {
61 br.close();
62 isr.close();
63 is.close();
64 } catch (Exception e2) {
65 System.out.println("Error cleaning up resources");
66 }
67
68 throw e; // Re-throw
69
70 }
71 }
72
73
74 //TODO: Skal csv parsning klares med Apache Commons CSV ?
75
76 @Override
77 public AddressSourceEntry getNextEntry() throws IOException {
78 String line = br.readLine();
79 if (line == null) // end of file
80 return null;
81
82 if (line.trim().equals(""))
83 return null;
84
85 //System.out.println(line);
86
87 AddressSourceEntry entry = new AddressSourceEntry( EntryType.TypeSingleAddress);
88 entry.distributor = "FD";
89
90 List<String> parts = Splitter.on(';').splitToList(line);
91
92 if (parts.size() != 9) {
93 throw new IOException("Not enough fields in line " + line);
94 }
95
96 entry.postnr = Short.parseShort( parts.get(0) );
97 entry.vejnavn = parts.get(1);
98 entry.husnr = Short.parseShort( parts.get(2) );
99 entry.litra = parts.get(3);
100 entry.vejkode = Short.parseShort( parts.get(4) );
101 entry.kommunekode = Short.parseShort( parts.get(5) );
102 entry.gadeid = Integer.parseInt( parts.get(6) );
103 entry.rute = dirigeringsCache.getInstance( parts.get(7) );
104 entry.koreliste = dirigeringsCache.getInstance( parts.get(8) );
105
106 switch(filenameFirst2) {
107 case "HV":
108 entry.ugedage = EntryUgedage.MAN_FRE;
109 break;
110 case "LO":
111 entry.ugedage = EntryUgedage.LOR;
112 break;
113 case "SO":
114 entry.ugedage = EntryUgedage.SON;
115 break;
116 default:
117 throw new IOException("Ukendt ugedage " + filenameFirst2);
118 }
119
120
121 lineCount++;
122
123 return entry;
124 }
125
126 @Override
127 public String getDistributor() {
128 return "FD";
129 }
130
131 @Override //AutoCloseable
132 public void close() throws Exception {
133 System.out.println("Closing FD after lines " + lineCount);
134 try {
135 br.close();
136 isr.close();
137 is.close();
138
139 file.delete();
140
141 } catch (Exception e) {
142 System.out.println("Error on closing " + e.getMessage() );
143 }
144
145
146 }
147
148 }

  ViewVC Help
Powered by ViewVC 1.1.20