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

Annotation of /dao/DaoAdresseVedligehold/src/main/java/dk/daoas/adressevedligehold/fileupload/AddressSourceBK.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/AddressSourceBK.java
File size: 3904 byte(s)
More findbugs
1 torben 2838 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     import dk.daoas.adressevedligehold.util.SafeParsers;
14    
15     public class AddressSourceBK implements AddressSource {
16    
17     DeduplicateHelper<String> dirigeringsCache = new DeduplicateHelper<String>();
18    
19     FileItem file;
20    
21     InputStream is;
22     InputStreamReader isr;
23     BufferedReader br;
24    
25     int lineCount = 0;
26    
27     public AddressSourceBK(FileItem file) throws Exception {
28     this.file = file;
29    
30    
31 torben 2844
32 torben 2838 }
33    
34     @Override
35     public String getFilename() {
36     return file.getName();
37     }
38    
39 torben 2844 @Override
40     public void validate() throws IOException {
41     try (
42     InputStream is1 = file.getInputStream();
43     InputStreamReader isr1 = new InputStreamReader(is1, Charset.forName("ISO-8859-1") );
44     BufferedReader br1 = new BufferedReader(isr1)
45     ) {
46     String line = br1.readLine();
47 torben 2861
48     if (line == null) {
49     throw new IOException("Can't read 1st line - is file empty?");
50     }
51    
52 torben 2844 String[] parts = line.split(";");
53 torben 2861
54     if (parts.length != 13) {
55     throw new IOException("Not enough fields in CSV file. Found " + parts.length + ", expected 13");
56 torben 2844 }
57     }
58    
59    
60     is = file.getInputStream();
61     isr = new InputStreamReader(is, Charset.forName("ISO-8859-1") );
62     br = new BufferedReader(isr);
63     }
64    
65 torben 2838 //TODO: Skal csv parsning klares med Apache Commons CSV ?
66    
67     @Override
68     public AddressSourceEntry getNextEntry() throws IOException {
69     String line = br.readLine();
70     if (line == null) // end of file
71     return null;
72    
73     if (line.trim().equals(""))
74     return null;
75    
76     //System.out.println(line);
77    
78     AddressSourceEntry entry = new AddressSourceEntry( EntryType.TypeSingleAddress);
79     entry.distributor = "BK";
80    
81     String[] parts = line.split(";");
82 torben 2857
83     if (parts.length != 13) {
84     throw new IOException("Not enough fields in CSV file. Found " + parts.length + ", expected 13");
85     }
86    
87 torben 2838 entry.postnr = Short.parseShort( parts[0]);
88     entry.vejnavn = parts[1].replace("\"", "");
89     entry.husnr = Short.parseShort( parts[2] );
90     entry.litra = parts[3].replace("\"", "");
91     entry.vejkode = SafeParsers.parseShort( parts[4] );
92     entry.kommunekode = SafeParsers.parseShort( parts[5] );
93     entry.gadeid = Integer.parseInt( parts[6] );
94    
95     String ugedage = parts[7].replace("\"", "");
96    
97     String laesnr = parts[8].replace("\"", "");
98    
99 torben 2844 //String distnr = parts[9].replace("\"", ""); //Bruges ikke
100 torben 2838 String foede = parts[10].replace("\"", "");
101     String jobnr = parts[11].replace("\"", "");
102     String tklaes = parts[12].replace("\"", "");
103    
104     entry.rute = laesnr;
105     entry.koreliste = "/" + foede + "/" + tklaes + "/" + jobnr;
106    
107     entry.rute = dirigeringsCache.getInstance(entry.rute);
108     entry.koreliste = dirigeringsCache.getInstance(entry.koreliste);
109    
110     switch (ugedage) {
111     case "1234000":
112     entry.ugedage = EntryUgedage.MAN_TOR;
113     break;
114     case "0000500":
115     entry.ugedage = EntryUgedage.FRE;
116     break;
117     case "0000060":
118     entry.ugedage = EntryUgedage.LOR;
119     break;
120     case "0000007":
121     entry.ugedage = EntryUgedage.SON;
122     break;
123     default:
124     throw new IOException("Ukendt ugedag:" + ugedage);
125     }
126    
127    
128     lineCount++;
129    
130     return entry;
131     }
132    
133     @Override
134     public String getDistributor() {
135     return "BK";
136     }
137    
138     @Override //AutoCloseable
139     public void close() throws Exception {
140     System.out.println("Closing BK after lines " + lineCount);
141     try {
142     br.close();
143     isr.close();
144     is.close();
145    
146     file.delete();
147    
148     } catch (Exception e) {
149     System.out.println("Error on closing " + e.getMessage() );
150     }
151    
152    
153     }
154    
155     }

  ViewVC Help
Powered by ViewVC 1.1.20