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

  ViewVC Help
Powered by ViewVC 1.1.20