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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2949 - (show annotations) (download)
Tue Feb 16 09:38:13 2016 UTC (8 years, 3 months ago) by torben
File size: 3326 byte(s)
Cleanup TODO:
1 package dk.daoas.adressevedligehold.fileupload;
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.tasks.TaskLogger;
15 import dk.daoas.adressevedligehold.util.DeduplicateHelper;
16
17 public abstract class AbstractAddressSource implements AddressSource {
18
19 private TaskLogger logger = TaskLogger.getInstance();
20
21 protected DeduplicateHelper<String> dirigeringsCache = new DeduplicateHelper<String>();
22
23 protected FileItem file;
24
25 protected InputStream is;
26 protected InputStreamReader isr;
27 protected BufferedReader br;
28
29 protected int lineCount = 0;
30
31 public AbstractAddressSource(FileItem file) {
32 this.file = file;
33 }
34
35 @Override
36 public String getFilename() {
37 return file.getName();
38 }
39
40 @Override //AutoCloseable
41 public void close() throws Exception {
42 logger.info("Closing BK after lines " + lineCount);
43 try {
44 if (br != null)
45 br.close();
46 if (isr != null)
47 isr.close();
48 if (is != null)
49 is.close();
50
51 file.delete();
52
53 } catch (Exception e) {
54 logger.warning("Error on closing ", e );
55 }
56 }
57 /**
58 * Reads the first line, validates that there are the expected number of fields, and then
59 * re-initilze the inputstream - this is for file formats that does not use a header line.
60 *
61 * @param exptectedFieldCount
62 * @param seperator
63 * @throws IOException
64 */
65 protected void validatNoHeaderLine(int exptectedFieldCount, char seperator) throws IOException {
66 try (
67 InputStream is1 = file.getInputStream();
68 InputStreamReader isr1 = new InputStreamReader(is1, Charset.forName("ISO-8859-1") );
69 BufferedReader br1 = new BufferedReader(isr1)
70 ) {
71 String line = br1.readLine();
72
73 if (line == null) {
74 throw new IOException("Can't read 1st line - is file empty?");
75 }
76
77 List<String> parts = Splitter.on(seperator).splitToList(line);
78 if (parts.size() != exptectedFieldCount) {
79 throw new IOException("Not enough fields in CSV file. Found " + parts.size() + ", expected " + exptectedFieldCount);
80 }
81 }
82
83
84 is = file.getInputStream();
85 isr = new InputStreamReader(is, Charset.forName("ISO-8859-1") );
86 br = new BufferedReader(isr);
87 }
88
89
90 protected void validateWithHeader(int expectedFieldCount, char seperator) throws IOException {
91 try {
92 is = file.getInputStream();
93 isr = new InputStreamReader(is, Charset.forName("ISO-8859-1") );
94 br = new BufferedReader(isr);
95
96 String line = br.readLine();
97 if (line == null) {
98 throw new IOException("Can't read 1st line - is file empty?");
99 }
100
101 List<String> parts = Splitter.on(seperator).splitToList(line);
102 if (parts.size() != expectedFieldCount) {
103 throw new IOException("Not enough fields in CSV file. Found " + parts.size() + ", expected " + expectedFieldCount);
104 }
105
106
107
108 } catch (Exception e) {
109 try {
110 br.close();
111 isr.close();
112 is.close();
113 } catch (Exception e2) {
114 logger.warning("Error cleaning up resources", e2);
115 }
116
117 throw e; // Re-throw
118 }
119 }
120 }

  ViewVC Help
Powered by ViewVC 1.1.20