1 |
package dk.daoas.adressevedligehold; |
package dk.daoas.adressevedligehold.coveragefileupload; |
2 |
|
|
3 |
import java.io.BufferedReader; |
import java.io.BufferedReader; |
4 |
import java.io.IOException; |
import java.io.IOException; |
11 |
|
|
12 |
import com.google.common.base.Splitter; |
import com.google.common.base.Splitter; |
13 |
|
|
14 |
|
import dk.daoas.adressevedligehold.tasks.TaskLogger; |
15 |
import dk.daoas.adressevedligehold.util.DeduplicateHelper; |
import dk.daoas.adressevedligehold.util.DeduplicateHelper; |
16 |
|
|
17 |
public abstract class AbstractAddressSource implements AddressSource { |
public abstract class AbstractAddressSource implements AddressSource { |
18 |
|
|
19 |
|
private TaskLogger logger = TaskLogger.getInstance(); |
20 |
|
|
21 |
protected DeduplicateHelper<String> dirigeringsCache = new DeduplicateHelper<String>(); |
protected DeduplicateHelper<String> dirigeringsCache = new DeduplicateHelper<String>(); |
22 |
|
|
23 |
protected FileItem file; |
protected FileItem file; |
34 |
|
|
35 |
@Override |
@Override |
36 |
public String getFilename() { |
public String getFilename() { |
|
// TODO Auto-generated method stub |
|
37 |
return file.getName(); |
return file.getName(); |
38 |
} |
} |
39 |
|
|
40 |
@Override //AutoCloseable |
@Override //AutoCloseable |
41 |
public void close() throws Exception { |
public void close() throws Exception { |
42 |
System.out.println("Closing BK after lines " + lineCount); |
logger.info("Closing BK after lines " + lineCount); |
43 |
try { |
try { |
44 |
if (br != null) |
if (br != null) |
45 |
br.close(); |
br.close(); |
51 |
file.delete(); |
file.delete(); |
52 |
|
|
53 |
} catch (Exception e) { |
} catch (Exception e) { |
54 |
System.out.println("Error on closing " + e.getMessage() ); |
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 { |
protected void validatNoHeaderLine(int exptectedFieldCount, char seperator) throws IOException { |
66 |
try ( |
try ( |
67 |
InputStream is1 = file.getInputStream(); |
InputStream is1 = file.getInputStream(); |
86 |
br = new BufferedReader(isr); |
br = new BufferedReader(isr); |
87 |
} |
} |
88 |
|
|
89 |
|
|
90 |
protected void validateWithHeader(int expectedFieldCount, char seperator) throws IOException { |
protected void validateWithHeader(int expectedFieldCount, char seperator) throws IOException { |
91 |
try { |
try { |
92 |
is = file.getInputStream(); |
is = file.getInputStream(); |
111 |
isr.close(); |
isr.close(); |
112 |
is.close(); |
is.close(); |
113 |
} catch (Exception e2) { |
} catch (Exception e2) { |
114 |
System.out.println("Error cleaning up resources"); |
logger.warning("Error cleaning up resources", e2); |
115 |
} |
} |
116 |
|
|
117 |
throw e; // Re-throw |
throw e; // Re-throw |