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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2857 - (show annotations) (download)
Thu Jan 28 10:30:01 2016 UTC (8 years, 3 months ago) by torben
File size: 3643 byte(s)
Validate number of fields after split
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
9 import org.apache.commons.fileupload.FileItem;
10
11 import dk.daoas.adressevedligehold.AddressSourceEntry.EntryType;
12 import dk.daoas.adressevedligehold.util.DeduplicateHelper;
13
14 public class AddressSourceDAO implements AddressSource {
15
16 final static String DAO = "DAO";
17
18 DeduplicateHelper<String> dirigeringsCache = new DeduplicateHelper<String>();
19
20 FileItem file;
21
22 InputStream is;
23 InputStreamReader isr;
24 BufferedReader br;
25
26 int lineCount = 0;
27
28 public AddressSourceDAO(FileItem file) throws Exception {
29 this.file = file;
30
31
32
33 }
34
35 @Override
36 public String getFilename() {
37 return file.getName();
38 }
39
40 @Override
41 public void validate() throws IOException {
42 try {
43 is = file.getInputStream();
44 isr = new InputStreamReader(is, Charset.forName("ISO-8859-1") );
45 br = new BufferedReader(isr);
46
47 String line = br.readLine();
48 String[] parts = line.split(";");
49 int numFields = parts.length;
50 if (numFields != 17) {
51 throw new IOException("Not enough fields in CSV file. Found " + numFields + ", expected 17");
52 }
53
54
55
56 } catch (Exception e) {
57 try {
58 br.close();
59 isr.close();
60 is.close();
61 } catch (Exception e2) {
62 System.out.println("Error cleaning up resources");
63 }
64
65 throw e; // Re-throw
66
67 }
68 }
69
70
71 //TODO: Skal csv parsning klares med Apache Commons CSV ?
72
73 @Override
74 public AddressSourceEntry getNextEntry() throws IOException {
75 String line = br.readLine();
76 if (line == null) // end of file
77 return null;
78
79 if (line.trim().equals(""))
80 return null;
81
82 //System.out.println(line);
83
84 AddressSourceEntry entry = new AddressSourceEntry( EntryType.TypeAddressRange);
85 entry.distributor = DAO;
86
87 String[] parts = line.split(";");
88 if (parts.length != 17) {
89 throw new IOException("Not enough fields in CSV file. Found " + parts.length + ", expected 17");
90 }
91
92 entry.gadeid = Integer.parseInt( parts[0] );
93 entry.vejnavn = parts[1];
94 //String stednavn = parts[2]; //Ikke brugt
95 //String david = parts[3]; //Ikke brugt
96 entry.postnr = Short.parseShort( parts[4] );
97 short net = Short.parseShort( parts[5] );
98 entry.rute = dirigeringsCache.getInstance( parts[6].toUpperCase() ); //Nogle Bruter i gl inputfil kan stå med lille b
99 entry.husnr = Short.parseShort( parts[7] );
100 entry.litra = parts[8];
101 entry.tilHusnr = Short.parseShort( parts[9] );
102 entry.tilLitra = parts[10];
103 //String sekvens = parts[11];
104 //String fraDato = parts[12];
105 //String tilDato = parts[13];
106 //short bane = Short.parseShort( parts[14] );
107 //String distributor = parts[15];
108 entry.koreliste = dirigeringsCache.getInstance( parts[16] );
109
110 switch (net) {
111 case 5:
112 entry.ugedage = EntryUgedage.LOR;
113 break;
114 case 6:
115 entry.ugedage = EntryUgedage.MAN_FRE;
116 break;
117 case 7:
118 entry.ugedage = EntryUgedage.SON;
119 break;
120 default:
121 throw new IOException("Ukendt net" + net);
122 }
123
124
125 lineCount++;
126
127 return entry;
128 }
129
130 @Override
131 public String getDistributor() {
132 return DAO;
133 }
134
135 @Override //AutoCloseable
136 public void close() throws Exception {
137 System.out.println("Closing " + DAO + " 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