/[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 2844 - (show annotations) (download)
Mon Jan 25 21:43:59 2016 UTC (8 years, 4 months ago) by torben
File size: 3504 byte(s)
Implemented AdressManager.visitRange()

Added AddressSource.validate() to let manager do a pre-validation before loading data from DB

Added support for DAO

Make Task and webpage show error messages
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 entry.gadeid = Integer.parseInt( parts[0] );
89 entry.vejnavn = parts[1];
90 //String stednavn = parts[2]; //Ikke brugt
91 //String david = parts[3]; //Ikke brugt
92 entry.postnr = Short.parseShort( parts[4] );
93 short net = Short.parseShort( parts[5] );
94 entry.rute = dirigeringsCache.getInstance( parts[6].toUpperCase() ); //Nogle Bruter i gl inputfil kan stå med lille b
95 entry.husnr = Short.parseShort( parts[7] );
96 entry.litra = parts[8];
97 entry.tilHusnr = Short.parseShort( parts[9] );
98 entry.tilLitra = parts[10];
99 //String sekvens = parts[11];
100 //String fraDato = parts[12];
101 //String tilDato = parts[13];
102 //short bane = Short.parseShort( parts[14] );
103 //String distributor = parts[15];
104 entry.koreliste = dirigeringsCache.getInstance( parts[16] );
105
106 switch (net) {
107 case 5:
108 entry.ugedage = EntryUgedage.LOR;
109 break;
110 case 6:
111 entry.ugedage = EntryUgedage.MAN_FRE;
112 break;
113 case 7:
114 entry.ugedage = EntryUgedage.SON;
115 break;
116 default:
117 throw new IOException("Ukendt net" + net);
118 }
119
120
121 lineCount++;
122
123 return entry;
124 }
125
126 @Override
127 public String getDistributor() {
128 return DAO;
129 }
130
131 @Override //AutoCloseable
132 public void close() throws Exception {
133 System.out.println("Closing " + DAO + " 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