1 |
package dk.daoas.adressevedligehold; |
package dk.daoas.adressevedligehold; |
2 |
|
|
|
import java.io.BufferedReader; |
|
3 |
import java.io.IOException; |
import java.io.IOException; |
|
import java.io.InputStream; |
|
|
import java.io.InputStreamReader; |
|
|
import java.nio.charset.Charset; |
|
4 |
|
|
5 |
import org.apache.commons.fileupload.FileItem; |
import org.apache.commons.fileupload.FileItem; |
6 |
|
|
7 |
import dk.daoas.adressevedligehold.AddressSourceEntry.EntryType; |
import dk.daoas.adressevedligehold.AddressSourceEntry.EntryType; |
|
import dk.daoas.adressevedligehold.util.DeduplicateHelper; |
|
|
import dk.daoas.adressevedligehold.util.SafeParsers; |
|
8 |
|
|
9 |
public class AddressSourceNS implements AddressSource { |
public class AddressSourceNS extends AbstractAddressSource { |
10 |
|
|
11 |
enum NextUgedag { |
enum NextUgedag { |
12 |
MANDAG,TIRSDAG,ONSDAG,TORSDAG,FREDAG,LORDAG,SONDAG |
MANDAG,TIRSDAG,ONSDAG,TORSDAG,FREDAG,LORDAG,SONDAG |
13 |
} |
} |
14 |
|
|
|
DeduplicateHelper<String> dirigeringsCache = new DeduplicateHelper<String>(); |
|
|
|
|
|
FileItem file; |
|
|
|
|
|
InputStream is; |
|
|
InputStreamReader isr; |
|
|
BufferedReader br; |
|
|
|
|
15 |
String parts[]; |
String parts[]; |
16 |
|
|
|
int lineCount = 0; |
|
17 |
|
|
18 |
NextUgedag nextUgedag = NextUgedag.MANDAG; |
NextUgedag nextUgedag = NextUgedag.MANDAG; |
19 |
|
|
20 |
public AddressSourceNS(FileItem file) throws Exception { |
public AddressSourceNS(FileItem file) throws Exception { |
21 |
this.file = file; |
super(file); |
22 |
} |
} |
23 |
|
|
|
@Override |
|
|
public String getFilename() { |
|
|
return file.getName(); |
|
|
} |
|
24 |
|
|
25 |
@Override |
@Override |
26 |
public void validate() throws IOException { |
public void validate() throws IOException { |
27 |
try ( |
super.validatNoHeaderLine(22, ','); |
|
InputStream is1 = file.getInputStream(); |
|
|
InputStreamReader isr1 = new InputStreamReader(is1, Charset.forName("ISO-8859-1") ); |
|
|
BufferedReader br1 = new BufferedReader(isr1) |
|
|
) { |
|
|
String line = br1.readLine(); |
|
|
String[] parts = line.split(","); |
|
|
int numFields = parts.length; |
|
|
if (numFields != 22) { |
|
|
throw new IOException("Not enough fields in CSV file. Found " + numFields + ", expected 22"); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
is = file.getInputStream(); |
|
|
isr = new InputStreamReader(is, Charset.forName("ISO-8859-1") ); |
|
|
br = new BufferedReader(isr); |
|
28 |
} |
} |
29 |
|
|
30 |
//TODO: Skal csv parsning klares med Apache Commons CSV ? |
//TODO: Skal csv parsning klares med Apache Commons CSV ? |
41 |
parts = line.split(","); |
parts = line.split(","); |
42 |
|
|
43 |
if (parts.length != 22) { |
if (parts.length != 22) { |
|
System.out.println( line ); |
|
44 |
throw new IOException("Not enough fields in line " + line); |
throw new IOException("Not enough fields in line " + line); |
45 |
} |
} |
46 |
} |
} |
119 |
return "NS"; |
return "NS"; |
120 |
} |
} |
121 |
|
|
|
@Override //AutoCloseable |
|
|
public void close() throws Exception { |
|
|
System.out.println("Closing NS after lines " + lineCount); |
|
|
try { |
|
|
br.close(); |
|
|
isr.close(); |
|
|
is.close(); |
|
|
|
|
|
file.delete(); |
|
|
|
|
|
} catch (Exception e) { |
|
|
System.out.println("Error on closing " + e.getMessage() ); |
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
|
|
122 |
} |
} |