--- dao/DaoAdresseVedligehold/src/main/java/dk/daoas/adressevedligehold/dawa/DawaTask.java 2016/04/19 12:39:38 3004 +++ dao/DaoAdresseVedligehold/src/main/java/dk/daoas/adressevedligehold/dawa/DawaTask.java 2016/04/19 19:55:19 3008 @@ -2,6 +2,7 @@ import java.util.ArrayList; import java.util.List; +import java.util.Map; import java.util.Set; import java.util.TreeMap; import java.util.TreeSet; @@ -13,6 +14,7 @@ import dk.daoas.adressevedligehold.tasks.Task; import dk.daoas.adressevedligehold.tasks.TaskLogger; import dk.daoas.adressevedligehold.util.DeduplicateHelper; +import dk.daoas.adressevedligehold.util.TimingHelper; /* * Alle regioner: http://dawa.aws.dk/regioner/ @@ -43,21 +45,18 @@ @Override protected void taskRun() throws Exception { - - int kommunekode = 615; - String kommunenavn = "Horsens"; - - // stage 1 - hent alle data + TimingHelper timer = new TimingHelper(); + DawaWebservice dawa = new DawaWebservice(); - List
dawaListe = dawa.lookup(kommunekode); + TreeMap search = new TreeMap<>(); + // stage 1 - hent alle data List
adresser = db.getAllAdresses(); - // stage 2 opbyg søge struktur logger.info("Building search structure"); - TreeMap search = new TreeMap<>(); + for (Address addr : adresser) { String dav = getDAVid(addr); @@ -71,83 +70,132 @@ } logger.info("Build search structure done"); - // stage 3 søg - Set ukendteDAV = new TreeSet<>(); - Set dobbeltGadeidDAV = new TreeSet<>(); - List
nyeAdresser = new ArrayList<>(); - - for (Address dawaAddr : dawaListe) { - String dav = getDAVid(dawaAddr); - DawaGade gade = search.get(dav); - if (gade == null) { - ukendteDAV.add(dav + " " + dawaAddr.vejnavn); - continue; - } - - if (gade.gadeids.size() != 1) { - dobbeltGadeidDAV.add(dav + " " + dawaAddr.vejnavn); - } - - boolean fundet = false; - for (Address target : gade.adresser) { - if ( target.husnr == dawaAddr.husnr && target.husnrbogstav.equals(dawaAddr.husnrbogstav) ) { - fundet = true; - break; - } - } - - if (fundet == false) { - dawaAddr.gadeid = gade.gadeids.iterator().next(); - dawaAddr.state = AddressState.CREATED; - dawaAddr.kommentar = kommentarTemplate; - nyeAdresser.add(dawaAddr); - } - } - logger.info("Address Search done - starting save to DB"); + Map kommuner = dawa.kommunerIRegion(regionCode); - db.saveNewAddresses(nyeAdresser); + String subject = "DAWA indlæsning - " + regionName; - logger.info("Save to db done - sending report mail"); + StringBuilder headerSB = new StringBuilder(); + headerSB.append("

Indlæsnings rapport for " + regionName + "


"); - List
saveOK = new ArrayList<>(); - List
saveFail = new ArrayList<>(); - for (Address a : nyeAdresser) { - if (a.state == AddressState.CREATE_FAIL) { - saveFail.add(a); - } else { - saveOK.add(a); - } - } + int totalAntalNye = 0; + int totalAntalFejl = 0; + int totalAntalUkendt = 0; + int totalAntalDobbelt = 0; - String subject = "DAWA indlæsning done"; StringBuilder sb = new StringBuilder(); - sb.append("

Indlæsnings rapport for " + regionName + " / " + regionCode + "


"); - sb.append("

Kommune: " + kommunenavn + " / " + kommunekode + "


\n"); - sb.append("

Adresser ok:

\n"); - for (Address a: saveOK) { - sb.append(a.toString() + "
\n"); - } + for (Map.Entry entry : kommuner.entrySet()) { + int kommunekode = entry.getValue(); + String kommunenavn = entry.getKey(); + + logger.info("Behandler: " + kommunekode + " " + kommunenavn); - sb.append("

SQL INSERT fejl:

\n"); - for (Address a: saveFail) { - sb.append(a.toString() + "
\n"); + + List
dawaListe = dawa.adresserIKommune(kommunekode); + // stage 3 søg + Set ukendteDAV = new TreeSet<>(); + Set dobbeltGadeidDAV = new TreeSet<>(); + List
nyeAdresser = new ArrayList<>(); + + for (Address dawaAddr : dawaListe) { + String dav = getDAVid(dawaAddr); + DawaGade gade = search.get(dav); + if (gade == null) { + ukendteDAV.add(dav + " " + dawaAddr.vejnavn); + continue; + } + + if (gade.gadeids.size() != 1) { + dobbeltGadeidDAV.add(dav + " " + dawaAddr.vejnavn); + } + + boolean fundet = false; + for (Address target : gade.adresser) { + if ( target.husnr == dawaAddr.husnr && target.husnrbogstav.equals(dawaAddr.husnrbogstav) ) { + fundet = true; + break; + } + } + + if (fundet == false) { + dawaAddr.gadeid = gade.gadeids.iterator().next(); + dawaAddr.state = AddressState.CREATED; + dawaAddr.kommentar = kommentarTemplate; + nyeAdresser.add(dawaAddr); + } + } + logger.info("Address Search done - starting save to DB"); + + db.saveNewAddresses(nyeAdresser); + + logger.info("Save to db done - sending report mail"); + + List
saveOK = new ArrayList<>(); + List
saveFail = new ArrayList<>(); + for (Address a : nyeAdresser) { + if (a.state == AddressState.CREATE_FAIL) { + saveFail.add(a); + } else { + saveOK.add(a); + } + } + + totalAntalNye += saveOK.size(); + totalAntalFejl += saveFail.size(); + totalAntalUkendt += ukendteDAV.size(); + totalAntalDobbelt += dobbeltGadeidDAV.size(); + + + sb.append("

Kommune: " + kommunenavn + "

\n"); + sb.append("Antal Inserts: " + saveOK.size() + "
\n"); + sb.append("Antal insert fails: " + saveFail.size() + "
\n"); + sb.append("Antal ukendte dav: " + ukendteDAV.size() + "
\n"); + sb.append("Antal dobbelt gadeid dav: " + dobbeltGadeidDAV.size() + "
\n"); + + sb.append("

Adresser ok:

\n"); + addressTable(sb, saveOK); + + sb.append("

SQL INSERT fejl:

\n"); + addressTable(sb, saveFail); + + sb.append("

Ukendte DAV:

\n"); + for (String dav : ukendteDAV) { + sb.append(dav + "
\n"); + } + + sb.append("

Dobbelt gadeid DAV:

\n"); + for (String dav : dobbeltGadeidDAV) { + sb.append(dav + "
\n"); + } + } - sb.append("

Ukendte DAV:

\n"); - for (String dav : ukendteDAV) { - sb.append(dav + "
\n"); - } + headerSB.append("Antal Inserts: " + totalAntalNye + "
\n"); + headerSB.append("Antal insert fails: " + totalAntalFejl + "
\n"); + headerSB.append("Antal ukendte dav: " + totalAntalUkendt + "
\n"); + headerSB.append("Antal dobbelt gadeid dav: " + totalAntalDobbelt + "
\n"); + headerSB.append("Elapsed: " + timer.getElapsed() + " ms.
\n"); - sb.append("

Dobbelt gadeid DAV:

\n"); - for (String dav : dobbeltGadeidDAV) { - sb.append(dav + "
\n"); + MailSender.sendMail(subject, headerSB.toString() + sb.toString() ); + + } + + private void addressTable(StringBuilder sb, List
addr) { + sb.append("\n"); + sb.append(""); + for (Address a: addr) { + sb.append(""); + sb.append(""); + sb.append(""); + sb.append(""); + sb.append(""); + sb.append(""); + sb.append(""); + sb.append(""); } + sb.append("
GadeIDPostnrKom.kodeVejkodeVejnavnHusnrLitra
 ").append(a.gadeid).append("  ").append(a.postnr).append("  ").append(a.kommunekode).append("  ").append(a.vejkode).append("  ").append(a.vejnavn).append("  ").append(a.husnr).append("  ").append(a.husnrbogstav).append("

\n"); - MailSender.sendMail(subject, sb.toString() ); - - } String getDAVid(Address addr) {