/[projects]/dao/DaoAdresseVedligehold/src/main/java/dk/daoas/adressevedligehold/coveragefileupload/AddressReport.java
ViewVC logotype

Annotation of /dao/DaoAdresseVedligehold/src/main/java/dk/daoas/adressevedligehold/coveragefileupload/AddressReport.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3111 - (hide annotations) (download)
Fri Sep 9 09:26:55 2016 UTC (7 years, 8 months ago) by torben
File size: 9874 byte(s)
Forbered til at dbkBane kan indeholde bogstaver
1 torben 3022 package dk.daoas.adressevedligehold.coveragefileupload;
2 torben 2838
3 torben 3061 import java.io.Serializable;
4 torben 3058 import java.util.ArrayList;
5 torben 3059 import java.util.Collections;
6 torben 3058 import java.util.Comparator;
7 torben 2838 import java.util.HashMap;
8     import java.util.List;
9 torben 2873 import java.util.Map.Entry;
10 torben 2838
11 torben 3058 import org.apache.commons.lang3.builder.CompareToBuilder;
12    
13 torben 2953 import dk.daoas.adressevedligehold.MailSender;
14 torben 2838 import dk.daoas.adressevedligehold.beans.Address;
15     import dk.daoas.adressevedligehold.beans.Address.AddressState;
16 torben 2905 import dk.daoas.adressevedligehold.tasks.TaskLogger;
17 torben 2838 import dk.daoas.adressevedligehold.util.TimingHelper;
18    
19 torben 2840 /*
20 torben 2950 *
21 torben 2840 */
22    
23 torben 2838 public class AddressReport {
24 torben 2953
25 torben 2860 static class NumEntry {
26 torben 2838 int open = 0;
27     int closed = 0;
28     int modified = 0;
29     int created = 0;
30 torben 2953
31 torben 2838 public void updateFromState(AddressState st) {
32     if (st == AddressState.OPENED)
33     open++;
34     if (st == AddressState.MODIFIED)
35     modified++;
36     if (st == AddressState.CLOSED)
37     closed++;
38     }
39     public void updateFromOther(NumEntry other) {
40     this.open += other.open;
41     this.modified += other.modified;
42     this.closed += other.closed;
43     this.created += other.created;
44     }
45     }
46 torben 2860 static class WeekEntry {
47 torben 2838 NumEntry man = new NumEntry();
48     NumEntry tir = new NumEntry();
49     NumEntry ons = new NumEntry();
50     NumEntry tor = new NumEntry();
51     NumEntry fre = new NumEntry();
52     NumEntry lor = new NumEntry();
53     NumEntry son = new NumEntry();
54 torben 2953
55 torben 2838 public void incrementFromOther(WeekEntry other) {
56     man.updateFromOther(other.man);
57     tir.updateFromOther(other.tir);
58     ons.updateFromOther(other.ons);
59     tor.updateFromOther(other.tor);
60     fre.updateFromOther(other.fre);
61     lor.updateFromOther(other.lor);
62     son.updateFromOther(other.son);
63     }
64 torben 2953
65 torben 2838 public void incrementCreated() {
66     man.created++;
67     tir.created++;
68     ons.created++;
69     tor.created++;
70     fre.created++;
71     lor.created++;
72     son.created++;
73     }
74     }
75 torben 2953
76 torben 2905 private static TaskLogger logger = TaskLogger.getInstance();
77 torben 2953
78 torben 2839 AddressManager addrManager;
79 torben 2838 List<Address> addressList;
80     int entryCount;
81     EntryUgedage ugedage;
82     AddressSource source;
83     TimingHelper timer;
84     boolean writeBack;
85 torben 2924 int duplicateCount;
86 torben 2949
87 torben 2953
88 torben 2924 public AddressReport(AddressManager addrManager, int entryCount, EntryUgedage ugedage, AddressSource source, TimingHelper timer, boolean writeBack, int duplicateCount ) {
89 torben 2839 this.addrManager = addrManager;
90 torben 2838 addressList = addrManager.getAddressList();
91     this.entryCount = entryCount;
92     this.ugedage = ugedage;
93     this.source = source;
94     this.timer = timer;
95     this.writeBack = writeBack;
96 torben 2924 this.duplicateCount = duplicateCount;
97 torben 2838 }
98 torben 2953
99 torben 3084 public void sendRapport(boolean failed) {
100     logger.info("Sending mail, failed:" + failed);
101 torben 2953
102 torben 2838 String reportData = getReportData();
103 torben 3084
104 torben 3082 String subject = "Adresse indlæsnings rapport for " + source.getDistributor();
105 torben 3044
106 torben 3084 if (failed) {
107     subject = "Error: too many closed addresses for " + source.getDistributor();
108     reportData = "<h1><font color='red'>Data er IKKE skrevet til DB</font></h1><br>\r\n" + reportData;
109     }
110    
111 torben 3044 String newOpenCloseAttachment = getAttachement();
112 torben 2953
113 torben 3044 MailSender.sendMailWithAttachment(subject, reportData, "Details.csv", newOpenCloseAttachment);
114 torben 2953
115 torben 2838 }
116 torben 2953
117    
118    
119 torben 2838 public String getReportData() {
120 torben 2953
121 torben 2838 WeekEntry total = new WeekEntry();
122     HashMap<Short, WeekEntry> postnrEntries = new HashMap<Short,WeekEntry>();
123 torben 2953
124 torben 2876 int createFailCount = 0;
125 torben 2953
126 torben 2894 int antalUdenGadeid = 0;
127 torben 2838 int antalUdenKommunekode = 0;
128     int antalUdenLatitude = 0;
129     int antalUdenDBKBane = 0;
130 torben 2953
131 torben 2838 // first gather the intel
132     for (Address a : addressList) {
133 torben 2953
134 torben 2876 if (a.state == AddressState.CREATE_FAIL) {
135     createFailCount++;
136     continue;
137     }
138 torben 2953
139 torben 2894 if (a.gadeid == 0) {
140     antalUdenGadeid++;
141     }
142 torben 2953
143 torben 2876 if (a.kommunekode == 0 || a.vejkode == 0) {
144 torben 2838 antalUdenKommunekode++;
145     }
146 torben 2953
147 torben 2838 if (a.latitude == 0 || a.longitude == 0) {
148     antalUdenLatitude++;
149     }
150 torben 2953
151 torben 2838 if (a.state == AddressState.NOT_CHANGED) {
152     continue;
153     }
154 torben 3111 if (a.dbkBane == null || a.dbkBane.length() == 0) {
155 torben 2838 antalUdenDBKBane++;
156     }
157 torben 2953
158 torben 2838 WeekEntry weekEntry = postnrEntries.get(a.postnr);
159     if (weekEntry == null) {
160     weekEntry = new WeekEntry();
161     postnrEntries.put(a.postnr, weekEntry);
162     }
163 torben 2876
164 torben 2953
165    
166 torben 2838 if (a.state == AddressState.CREATED) {
167     weekEntry.incrementCreated();
168     } else {
169     weekEntry.man.updateFromState(a.stateMan);
170     weekEntry.tir.updateFromState(a.stateTir);
171     weekEntry.ons.updateFromState(a.stateOns);
172     weekEntry.tor.updateFromState(a.stateTor);
173     weekEntry.fre.updateFromState(a.stateFre);
174     weekEntry.lor.updateFromState(a.stateLor);
175     weekEntry.son.updateFromState(a.stateSon);
176 torben 2953
177 torben 2838 }
178     }
179     for (WeekEntry w : postnrEntries.values()) {
180     total.incrementFromOther(w);
181     }
182 torben 2953
183 torben 2838 StringBuilder sb = new StringBuilder();
184 torben 2953
185 torben 2838 sb.append("<h1>Indlæsnings rapport for " + source.getDistributor() + " </h1>");
186     sb.append("Filnavn: ").append( source.getFilename() ).append("<br>");
187     sb.append("Tidsforbrug: ").append( timer.getElapsed() ).append("ms. <br>");
188     sb.append("Skriv til DB: ").append( writeBack ).append("<br>");
189     sb.append("Antal entries: " ).append( entryCount ).append("<br>");
190 torben 2868 sb.append("Antal afviste: " ).append( addrManager.getRejectedCount() ).append("<br>");
191 torben 2924 sb.append("Antal dubletter: " ).append( duplicateCount ).append("<br>");
192 torben 2876 sb.append("Antal fejlede inserts: ").append(createFailCount).append("<br>");
193 torben 2865 sb.append("Filen indeholder data til: ");
194     if (ugedage.mandag) sb.append("Mandag, ");
195     if (ugedage.tirsdag) sb.append("Tirsdag, ");
196     if (ugedage.onsdag) sb.append("Onsdag, ");
197     if (ugedage.torsdag) sb.append("Torsdag, ");
198     if (ugedage.fredag) sb.append("Fredag, ");
199     if (ugedage.lordag) sb.append("Lørdag, ");
200     if (ugedage.sondag) sb.append("Søndag, ");
201 torben 3085 sb.append("<br><br>");
202 torben 2953
203 torben 2838 sb.append("<h2>Data Mangler</h2>\n");
204 torben 3085 sb.append("Mangler kommune/vejkode: ").append(antalUdenKommunekode).append("<br>");
205     sb.append("Mangler gadeid: ").append(antalUdenGadeid).append("<br>");
206     sb.append("Mangler DBK bane: ").append(antalUdenDBKBane).append("<br>");
207     sb.append("Mangler latitude/longitude: ").append(antalUdenLatitude).append("<br>");
208     sb.append("<br>");
209 torben 2838
210 torben 2953
211    
212 torben 2838 sb.append("<h2>Ændrings oversigt</h2>\n");
213     sb.append("<table>");
214 torben 2953
215 torben 2838 sb.append("<tr><td>&nbsp;</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>");
216     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>");
217     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>");
218     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>");
219     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>");
220 torben 2873 sb.append("</table>");
221 torben 2953
222 torben 2873 sb.append("<br>");
223     sb.append("<h2>Nye/Ukendte gader</h2>");
224     sb.append("<table>");
225     sb.append("<tr><th>Gadeid</th><th>Vejnavn</th></tr>");
226     for( Entry<Integer,String> unknownStreet : addrManager.getUnknownStreets().entrySet() ) {
227     sb.append("<tr><td>").append(unknownStreet.getKey()).append("</td><td>").append(unknownStreet.getValue()).append("</td></tr>");
228     }
229     sb.append("</table>");
230 torben 2953
231    
232 torben 2838 sb.append("</table><br>\n");
233 torben 2953
234 torben 2838 return sb.toString();
235     }
236 torben 3044
237     public String getAttachement() {
238     StringBuilder sb = new StringBuilder();
239     sb.append("Status;Gadeid;Post;Vejnavn;Husnr;Litra\r\n");
240    
241 torben 3058 ArrayList<Address> attachmentData = new ArrayList<Address>();
242 torben 3044 for (Address a : addressList) {
243 torben 3058 if (a.state == AddressState.CLOSED || a.state == AddressState.CREATED || a.state == AddressState.OPENED) {
244     attachmentData.add(a);
245     }
246     }
247    
248 torben 3059 Collections.sort(attachmentData, new AttachmentComparator());
249 torben 3058
250     for (Address a : attachmentData) {
251 torben 3044 switch (a.state) {
252     case CLOSED:
253 torben 3058 sb.append("Closed;");
254 torben 3044 break;
255     case CREATED:
256 torben 3058 sb.append("Created;");
257 torben 3044 break;
258     case OPENED:
259 torben 3058 sb.append("Opened;");
260 torben 3044 break;
261     default:
262     break;
263     }
264 torben 3058
265     //tilføj resten af dataene
266     sb.append( a.gadeid + ";" + a.postnr + ";" + a.vejnavn + ";" + a.husnr + ";" + a.husnrbogstav + "\r\n");
267 torben 3044 }
268    
269     return sb.toString();
270     }
271 torben 3058
272 torben 3060
273 torben 3061 /*
274     * Serializable is to silence findbugs
275     */
276     static class AttachmentComparator implements Comparator<Address>, Serializable {
277     private static final long serialVersionUID = 1L;
278 torben 3058
279     @Override
280     public int compare(Address a1, Address a2) {
281    
282    
283     //Brug apache commons lang3
284     return new CompareToBuilder()
285     .append(a1.state, a2.state)
286     .append(a1.postnr, a2.postnr)
287     .append(a1.vejnavn, a2.vejnavn)
288     .append(a1.husnr, a2.husnr)
289     .append(a1.husnrbogstav, a2.husnrbogstav)
290     .toComparison();
291     }
292    
293     }
294 torben 2838 }

  ViewVC Help
Powered by ViewVC 1.1.20