1 |
package dk.daoas.adressevedligehold;
|
2 |
|
3 |
import org.apache.commons.lang3.exception.ExceptionUtils;
|
4 |
|
5 |
import dk.daoas.adressevedligehold.tasks.Task;
|
6 |
import dk.daoas.adressevedligehold.util.TimingHelper;
|
7 |
|
8 |
/*
|
9 |
* TODO: trigger indlæsning
|
10 |
*
|
11 |
*/
|
12 |
|
13 |
public class AddressTask extends Task {
|
14 |
|
15 |
AddressSource source;
|
16 |
boolean writeBack;
|
17 |
|
18 |
public AddressTask(AddressSource source, boolean writeBack) {
|
19 |
|
20 |
this.source = source;
|
21 |
this.writeBack = writeBack;
|
22 |
}
|
23 |
|
24 |
|
25 |
|
26 |
@Override
|
27 |
protected void taskRun() throws Exception {
|
28 |
|
29 |
try {
|
30 |
TimingHelper timer = new TimingHelper();
|
31 |
|
32 |
System.out.println("Validating Source");
|
33 |
source.validate();
|
34 |
|
35 |
System.out.println("Reading data from DB");
|
36 |
AddressManager addrManager = new AddressManager();
|
37 |
|
38 |
System.out.println("Data is ready");
|
39 |
|
40 |
EntryUgedage ugedage = new EntryUgedage(); //saml op på hvilke ugedage der er blevet behandlet
|
41 |
|
42 |
|
43 |
int entryCount = 0;
|
44 |
|
45 |
AddressSourceEntry entry;
|
46 |
while ( (entry = source.getNextEntry()) != null) {
|
47 |
if (super.abort) {
|
48 |
break;
|
49 |
}
|
50 |
|
51 |
addrManager.visit(entry);
|
52 |
ugedage.merge(entry.ugedage);
|
53 |
entryCount++;
|
54 |
}
|
55 |
|
56 |
addrManager.closeUnvisitedAddresses( source.getDistributor(), ugedage );
|
57 |
|
58 |
|
59 |
|
60 |
if (writeBack) {
|
61 |
addrManager.writeBackChanges();
|
62 |
} else {
|
63 |
addrManager.dumpChanged();
|
64 |
}
|
65 |
|
66 |
AddressReport report = new AddressReport(addrManager, entryCount, ugedage, source, timer, writeBack);
|
67 |
report.sendRapport();
|
68 |
|
69 |
|
70 |
|
71 |
} catch(Exception e) {
|
72 |
|
73 |
String subject = "Error loading data for " + source.getDistributor();
|
74 |
String body = "File: " + source.getFilename();
|
75 |
body += "<br><br>";
|
76 |
body += "Exception:<br>";
|
77 |
body += ExceptionUtils.getStackTrace(e);
|
78 |
AddressReport.sendMail(subject, body);
|
79 |
|
80 |
throw e; //rethrow exception
|
81 |
} finally {
|
82 |
|
83 |
try {
|
84 |
System.out.println("Cleaning up resources");
|
85 |
source.close();
|
86 |
} catch (Exception e) {
|
87 |
System.out.println("Error during cleanup: " + e.getMessage() );
|
88 |
}
|
89 |
}
|
90 |
|
91 |
}
|
92 |
|
93 |
@Override
|
94 |
public String getDescription() {
|
95 |
return "AddressTask " + source.getDistributor();
|
96 |
}
|
97 |
|
98 |
@Override
|
99 |
public double getPercentCompleted() {
|
100 |
return -1;
|
101 |
}
|
102 |
|
103 |
@Override
|
104 |
public String getDetail() {
|
105 |
return source.getFilename();
|
106 |
}
|
107 |
|
108 |
}
|