1 |
package dk.daoas.adressevedligehold; |
package dk.daoas.adressevedligehold.coveragefileupload; |
2 |
|
|
3 |
|
import java.io.Serializable; |
4 |
|
import java.util.ArrayList; |
5 |
|
import java.util.Collections; |
6 |
|
import java.util.Comparator; |
7 |
import java.util.HashMap; |
import java.util.HashMap; |
8 |
import java.util.List; |
import java.util.List; |
9 |
import java.util.Properties; |
import java.util.Map.Entry; |
10 |
|
|
11 |
import javax.mail.Message; |
import org.apache.commons.lang3.builder.CompareToBuilder; |
|
import javax.mail.MessagingException; |
|
|
import javax.mail.Session; |
|
|
import javax.mail.Transport; |
|
|
import javax.mail.internet.InternetAddress; |
|
|
import javax.mail.internet.MimeMessage; |
|
12 |
|
|
13 |
|
import dk.daoas.adressevedligehold.MailSender; |
14 |
import dk.daoas.adressevedligehold.beans.Address; |
import dk.daoas.adressevedligehold.beans.Address; |
15 |
import dk.daoas.adressevedligehold.beans.Address.AddressState; |
import dk.daoas.adressevedligehold.beans.Address.AddressState; |
16 |
|
import dk.daoas.adressevedligehold.tasks.TaskLogger; |
17 |
import dk.daoas.adressevedligehold.util.TimingHelper; |
import dk.daoas.adressevedligehold.util.TimingHelper; |
18 |
|
|
19 |
/* |
/* |
20 |
* TODO: detail rapport pr postnr |
* |
|
* TODO: ved mangel oversigten - skeln mellem om det er nye/CREATED eller om det er gamle kendte adresser |
|
21 |
*/ |
*/ |
22 |
|
|
23 |
public class AddressReport { |
public class AddressReport { |
24 |
|
|
25 |
static class NumEntry { |
static class NumEntry { |
26 |
int open = 0; |
int open = 0; |
27 |
int closed = 0; |
int closed = 0; |
28 |
int modified = 0; |
int modified = 0; |
29 |
int created = 0; |
int created = 0; |
30 |
|
|
31 |
public void updateFromState(AddressState st) { |
public void updateFromState(AddressState st) { |
32 |
if (st == AddressState.OPENED) |
if (st == AddressState.OPENED) |
33 |
open++; |
open++; |
51 |
NumEntry fre = new NumEntry(); |
NumEntry fre = new NumEntry(); |
52 |
NumEntry lor = new NumEntry(); |
NumEntry lor = new NumEntry(); |
53 |
NumEntry son = new NumEntry(); |
NumEntry son = new NumEntry(); |
54 |
|
|
55 |
public void incrementFromOther(WeekEntry other) { |
public void incrementFromOther(WeekEntry other) { |
56 |
man.updateFromOther(other.man); |
man.updateFromOther(other.man); |
57 |
tir.updateFromOther(other.tir); |
tir.updateFromOther(other.tir); |
61 |
lor.updateFromOther(other.lor); |
lor.updateFromOther(other.lor); |
62 |
son.updateFromOther(other.son); |
son.updateFromOther(other.son); |
63 |
} |
} |
64 |
|
|
65 |
public void incrementCreated() { |
public void incrementCreated() { |
66 |
man.created++; |
man.created++; |
67 |
tir.created++; |
tir.created++; |
72 |
son.created++; |
son.created++; |
73 |
} |
} |
74 |
} |
} |
75 |
|
|
76 |
|
private static TaskLogger logger = TaskLogger.getInstance(); |
77 |
|
|
78 |
AddressManager addrManager; |
AddressManager addrManager; |
79 |
List<Address> addressList; |
List<Address> addressList; |
80 |
int entryCount; |
int entryCount; |
82 |
AddressSource source; |
AddressSource source; |
83 |
TimingHelper timer; |
TimingHelper timer; |
84 |
boolean writeBack; |
boolean writeBack; |
85 |
|
int duplicateCount; |
86 |
//TODO: Afviste adresser |
|
87 |
public AddressReport(AddressManager addrManager, int entryCount, EntryUgedage ugedage, AddressSource source, TimingHelper timer, boolean writeBack ) { |
|
88 |
|
public AddressReport(AddressManager addrManager, int entryCount, EntryUgedage ugedage, AddressSource source, TimingHelper timer, boolean writeBack, int duplicateCount ) { |
89 |
this.addrManager = addrManager; |
this.addrManager = addrManager; |
90 |
addressList = addrManager.getAddressList(); |
addressList = addrManager.getAddressList(); |
91 |
this.entryCount = entryCount; |
this.entryCount = entryCount; |
93 |
this.source = source; |
this.source = source; |
94 |
this.timer = timer; |
this.timer = timer; |
95 |
this.writeBack = writeBack; |
this.writeBack = writeBack; |
96 |
|
this.duplicateCount = duplicateCount; |
97 |
} |
} |
98 |
|
|
99 |
public void sendRapport() { |
public void sendRapport() { |
100 |
System.out.println("Sending mail"); |
logger.info("Sending mail"); |
101 |
|
|
102 |
String reportData = getReportData(); |
String reportData = getReportData(); |
103 |
String subject = "Indlæsnings rapport for " + source.getDistributor(); |
String subject = "Indlæsnings rapport for " + source.getDistributor(); |
104 |
|
|
105 |
AddressReport.sendMail(subject, reportData); |
String newOpenCloseAttachment = getAttachement(); |
106 |
|
|
107 |
} |
MailSender.sendMailWithAttachment(subject, reportData, "Details.csv", newOpenCloseAttachment); |
108 |
|
|
|
public static void sendMail(String subject, String htmlBody) { |
|
|
Properties props = new Properties(); |
|
|
props.put("mail.smtp.host", "10.30.1.28"); |
|
|
//props.put("mail.smtp.host", "192.168.12.10"); |
|
|
|
|
|
try { |
|
|
// create some properties and get the default Session |
|
|
Session session = Session.getDefaultInstance(props, null); |
|
|
session.setDebug(false); |
|
|
|
|
|
MimeMessage msg = new MimeMessage(session); |
|
|
msg.setFrom( new InternetAddress("no-reply@daoas.dk") ); |
|
|
|
|
|
|
|
|
msg.setRecipient(Message.RecipientType.TO, new InternetAddress("thn@daoas.dk") );//TODO: mulighed for at ændre modtager mail |
|
|
msg.setSubject( subject ); |
|
|
msg.setContent(htmlBody, "text/html; charset=utf-8"); |
|
|
|
|
|
Transport.send(msg); |
|
|
|
|
|
|
|
|
} catch (MessagingException e) { |
|
|
System.out.println("Unable to send report mail " + e.getMessage() ); |
|
|
} |
|
109 |
} |
} |
110 |
|
|
111 |
|
|
112 |
|
|
113 |
public String getReportData() { |
public String getReportData() { |
114 |
|
|
115 |
WeekEntry total = new WeekEntry(); |
WeekEntry total = new WeekEntry(); |
116 |
HashMap<Short, WeekEntry> postnrEntries = new HashMap<Short,WeekEntry>(); |
HashMap<Short, WeekEntry> postnrEntries = new HashMap<Short,WeekEntry>(); |
117 |
|
|
118 |
|
int createFailCount = 0; |
119 |
int antalUdenKommunekodeNy = 0; |
|
120 |
|
int antalUdenGadeid = 0; |
121 |
int antalUdenKommunekode = 0; |
int antalUdenKommunekode = 0; |
122 |
int antalUdenLatitude = 0; |
int antalUdenLatitude = 0; |
123 |
int antalUdenDBKBane = 0; |
int antalUdenDBKBane = 0; |
124 |
|
|
125 |
// first gather the intel |
// first gather the intel |
126 |
for (Address a : addressList) { |
for (Address a : addressList) { |
127 |
if (a.kommunekode == 0 || a.vejkode == 0) { |
|
128 |
if (a.state == AddressState.CREATED) { |
if (a.state == AddressState.CREATE_FAIL) { |
129 |
antalUdenKommunekodeNy++; |
createFailCount++; |
130 |
} |
continue; |
131 |
|
} |
132 |
|
|
133 |
|
if (a.gadeid == 0) { |
134 |
|
antalUdenGadeid++; |
135 |
|
} |
136 |
|
|
137 |
|
if (a.kommunekode == 0 || a.vejkode == 0) { |
138 |
antalUdenKommunekode++; |
antalUdenKommunekode++; |
139 |
} |
} |
140 |
|
|
141 |
if (a.latitude == 0 || a.longitude == 0) { |
if (a.latitude == 0 || a.longitude == 0) { |
142 |
antalUdenLatitude++; |
antalUdenLatitude++; |
143 |
} |
} |
144 |
|
|
145 |
if (a.state == AddressState.NOT_CHANGED) { |
if (a.state == AddressState.NOT_CHANGED) { |
146 |
continue; |
continue; |
147 |
} |
} |
148 |
if (a.dbkBane == 0) { |
if (a.dbkBane == 0) { |
149 |
antalUdenDBKBane++; |
antalUdenDBKBane++; |
150 |
} |
} |
151 |
|
|
152 |
WeekEntry weekEntry = postnrEntries.get(a.postnr); |
WeekEntry weekEntry = postnrEntries.get(a.postnr); |
153 |
if (weekEntry == null) { |
if (weekEntry == null) { |
154 |
weekEntry = new WeekEntry(); |
weekEntry = new WeekEntry(); |
155 |
postnrEntries.put(a.postnr, weekEntry); |
postnrEntries.put(a.postnr, weekEntry); |
156 |
} |
} |
157 |
|
|
158 |
|
|
159 |
|
|
160 |
if (a.state == AddressState.CREATED) { |
if (a.state == AddressState.CREATED) { |
161 |
weekEntry.incrementCreated(); |
weekEntry.incrementCreated(); |
162 |
} else { |
} else { |
167 |
weekEntry.fre.updateFromState(a.stateFre); |
weekEntry.fre.updateFromState(a.stateFre); |
168 |
weekEntry.lor.updateFromState(a.stateLor); |
weekEntry.lor.updateFromState(a.stateLor); |
169 |
weekEntry.son.updateFromState(a.stateSon); |
weekEntry.son.updateFromState(a.stateSon); |
170 |
|
|
171 |
} |
} |
172 |
} |
} |
173 |
for (WeekEntry w : postnrEntries.values()) { |
for (WeekEntry w : postnrEntries.values()) { |
174 |
total.incrementFromOther(w); |
total.incrementFromOther(w); |
175 |
} |
} |
176 |
|
|
177 |
StringBuilder sb = new StringBuilder(); |
StringBuilder sb = new StringBuilder(); |
178 |
|
|
179 |
sb.append("<h1>Indlæsnings rapport for " + source.getDistributor() + " </h1>"); |
sb.append("<h1>Indlæsnings rapport for " + source.getDistributor() + " </h1>"); |
180 |
sb.append("Filnavn: ").append( source.getFilename() ).append("<br>"); |
sb.append("Filnavn: ").append( source.getFilename() ).append("<br>"); |
181 |
sb.append("Tidsforbrug: ").append( timer.getElapsed() ).append("ms. <br>"); |
sb.append("Tidsforbrug: ").append( timer.getElapsed() ).append("ms. <br>"); |
182 |
sb.append("Skriv til DB: ").append( writeBack ).append("<br>"); |
sb.append("Skriv til DB: ").append( writeBack ).append("<br>"); |
183 |
sb.append("Antal entries: " ).append( entryCount ).append("<br>"); |
sb.append("Antal entries: " ).append( entryCount ).append("<br>"); |
184 |
sb.append("Antal afviste: " ).append( addrManager.getAfvistAntal() ).append("<br>"); |
sb.append("Antal afviste: " ).append( addrManager.getRejectedCount() ).append("<br>"); |
185 |
|
sb.append("Antal dubletter: " ).append( duplicateCount ).append("<br>"); |
186 |
|
sb.append("Antal fejlede inserts: ").append(createFailCount).append("<br>"); |
187 |
|
sb.append("Filen indeholder data til: "); |
188 |
|
if (ugedage.mandag) sb.append("Mandag, "); |
189 |
|
if (ugedage.tirsdag) sb.append("Tirsdag, "); |
190 |
|
if (ugedage.onsdag) sb.append("Onsdag, "); |
191 |
|
if (ugedage.torsdag) sb.append("Torsdag, "); |
192 |
|
if (ugedage.fredag) sb.append("Fredag, "); |
193 |
|
if (ugedage.lordag) sb.append("Lørdag, "); |
194 |
|
if (ugedage.sondag) sb.append("Søndag, "); |
195 |
|
sb.append("<br>"); |
196 |
|
|
197 |
sb.append("<h2>Data Mangler</h2>\n"); |
sb.append("<h2>Data Mangler</h2>\n"); |
198 |
sb.append("<pre>"); |
sb.append("<pre>"); |
199 |
sb.append("Mangler kommune/vejkode: ").append(antalUdenKommunekode).append("\n"); |
sb.append("Mangler kommune/vejkode: ").append(antalUdenKommunekode).append("\n"); |
200 |
|
sb.append("Mangler gadeid: ").append(antalUdenGadeid).append("\n"); |
201 |
sb.append("Mangler DBK bane: ").append(antalUdenDBKBane).append("\n"); |
sb.append("Mangler DBK bane: ").append(antalUdenDBKBane).append("\n"); |
202 |
sb.append("Mangler latitude/longitude: ").append(antalUdenLatitude).append("\n"); |
sb.append("Mangler latitude/longitude: ").append(antalUdenLatitude).append("\n"); |
203 |
sb.append("</pre>"); |
sb.append("</pre>"); |
|
|
|
204 |
|
|
205 |
|
|
206 |
|
|
207 |
sb.append("<h2>Ændrings oversigt</h2>\n"); |
sb.append("<h2>Ændrings oversigt</h2>\n"); |
208 |
sb.append("<table>"); |
sb.append("<table>"); |
209 |
|
|
210 |
sb.append("<tr><td> </td><td>Mandag</td><td>Tirsdag</td><td>Onsdag</td><td>Torsdag</td><td>Fredag</td><td>Lørdag</td><td>Søndag</td></tr>"); |
sb.append("<tr><td> </td><td>Mandag</td><td>Tirsdag</td><td>Onsdag</td><td>Torsdag</td><td>Fredag</td><td>Lørdag</td><td>Søndag</td></tr>"); |
211 |
sb.append("<tr><td>Oprettet:</td><td>").append(total.man.created).append("</td><td>").append(total.tir.created).append("</td><td>").append(total.ons.created).append("</td><td>").append(total.tor.created).append("</td><td>").append(total.fre.created).append("</td><td>").append(total.lor.created).append("</td><td>").append(total.son.created).append("</td></tr>"); |
sb.append("<tr><td>Oprettet:</td><td>").append(total.man.created).append("</td><td>").append(total.tir.created).append("</td><td>").append(total.ons.created).append("</td><td>").append(total.tor.created).append("</td><td>").append(total.fre.created).append("</td><td>").append(total.lor.created).append("</td><td>").append(total.son.created).append("</td></tr>"); |
212 |
sb.append("<tr><td>Åbnet:</td><td>").append(total.man.open).append("</td><td>").append(total.tir.open).append("</td><td>").append(total.ons.open).append("</td><td>").append(total.tor.open).append("</td><td>").append(total.fre.open).append("</td><td>").append(total.lor.open).append("</td><td>").append(total.son.open).append("</td></tr>"); |
sb.append("<tr><td>Åbnet:</td><td>").append(total.man.open).append("</td><td>").append(total.tir.open).append("</td><td>").append(total.ons.open).append("</td><td>").append(total.tor.open).append("</td><td>").append(total.fre.open).append("</td><td>").append(total.lor.open).append("</td><td>").append(total.son.open).append("</td></tr>"); |
213 |
sb.append("<tr><td>Ændret:</td><td>").append(total.man.modified).append("</td><td>").append(total.tir.modified).append("</td><td>").append(total.ons.modified).append("</td><td>").append(total.tor.modified).append("</td><td>").append(total.fre.modified).append("</td><td>").append(total.lor.modified).append("</td><td>").append(total.son.modified).append("</td></tr>"); |
sb.append("<tr><td>Ændret:</td><td>").append(total.man.modified).append("</td><td>").append(total.tir.modified).append("</td><td>").append(total.ons.modified).append("</td><td>").append(total.tor.modified).append("</td><td>").append(total.fre.modified).append("</td><td>").append(total.lor.modified).append("</td><td>").append(total.son.modified).append("</td></tr>"); |
214 |
sb.append("<tr><td>Lukket:</td><td>").append(total.man.closed).append("</td><td>").append(total.tir.closed).append("</td><td>").append(total.ons.closed).append("</td><td>").append(total.tor.closed).append("</td><td>").append(total.fre.closed).append("</td><td>").append(total.lor.closed).append("</td><td>").append(total.son.closed).append("</td></tr>"); |
sb.append("<tr><td>Lukket:</td><td>").append(total.man.closed).append("</td><td>").append(total.tir.closed).append("</td><td>").append(total.ons.closed).append("</td><td>").append(total.tor.closed).append("</td><td>").append(total.fre.closed).append("</td><td>").append(total.lor.closed).append("</td><td>").append(total.son.closed).append("</td></tr>"); |
215 |
|
sb.append("</table>"); |
216 |
|
|
217 |
|
sb.append("<br>"); |
218 |
|
sb.append("<h2>Nye/Ukendte gader</h2>"); |
219 |
|
sb.append("<table>"); |
220 |
|
sb.append("<tr><th>Gadeid</th><th>Vejnavn</th></tr>"); |
221 |
|
for( Entry<Integer,String> unknownStreet : addrManager.getUnknownStreets().entrySet() ) { |
222 |
|
sb.append("<tr><td>").append(unknownStreet.getKey()).append("</td><td>").append(unknownStreet.getValue()).append("</td></tr>"); |
223 |
|
} |
224 |
|
sb.append("</table>"); |
225 |
|
|
226 |
|
|
227 |
|
sb.append("</table><br>\n"); |
228 |
|
|
229 |
|
return sb.toString(); |
230 |
|
} |
231 |
|
|
232 |
|
public String getAttachement() { |
233 |
|
StringBuilder sb = new StringBuilder(); |
234 |
|
sb.append("Status;Gadeid;Post;Vejnavn;Husnr;Litra\r\n"); |
235 |
|
|
236 |
|
ArrayList<Address> attachmentData = new ArrayList<Address>(); |
237 |
|
for (Address a : addressList) { |
238 |
|
if (a.state == AddressState.CLOSED || a.state == AddressState.CREATED || a.state == AddressState.OPENED) { |
239 |
|
attachmentData.add(a); |
240 |
|
} |
241 |
|
} |
242 |
|
|
243 |
sb.append("</table><br>\n"); |
Collections.sort(attachmentData, new AttachmentComparator()); |
244 |
|
|
245 |
|
for (Address a : attachmentData) { |
246 |
|
switch (a.state) { |
247 |
|
case CLOSED: |
248 |
|
sb.append("Closed;"); |
249 |
|
break; |
250 |
|
case CREATED: |
251 |
|
sb.append("Created;"); |
252 |
|
break; |
253 |
|
case OPENED: |
254 |
|
sb.append("Opened;"); |
255 |
|
break; |
256 |
|
default: |
257 |
|
break; |
258 |
|
} |
259 |
|
|
260 |
|
//tilføj resten af dataene |
261 |
|
sb.append( a.gadeid + ";" + a.postnr + ";" + a.vejnavn + ";" + a.husnr + ";" + a.husnrbogstav + "\r\n"); |
262 |
|
} |
263 |
|
|
264 |
return sb.toString(); |
return sb.toString(); |
265 |
} |
} |
266 |
|
|
267 |
|
|
268 |
|
/* |
269 |
|
* Serializable is to silence findbugs |
270 |
|
*/ |
271 |
|
static class AttachmentComparator implements Comparator<Address>, Serializable { |
272 |
|
private static final long serialVersionUID = 1L; |
273 |
|
|
274 |
|
@Override |
275 |
|
public int compare(Address a1, Address a2) { |
276 |
|
|
277 |
|
|
278 |
|
//Brug apache commons lang3 |
279 |
|
return new CompareToBuilder() |
280 |
|
.append(a1.state, a2.state) |
281 |
|
.append(a1.postnr, a2.postnr) |
282 |
|
.append(a1.vejnavn, a2.vejnavn) |
283 |
|
.append(a1.husnr, a2.husnr) |
284 |
|
.append(a1.husnrbogstav, a2.husnrbogstav) |
285 |
|
.toComparison(); |
286 |
|
} |
287 |
|
|
288 |
|
} |
289 |
} |
} |