15 |
import dk.daoas.adressevedligehold.beans.Address; |
import dk.daoas.adressevedligehold.beans.Address; |
16 |
import dk.daoas.adressevedligehold.beans.Address.AddressState; |
import dk.daoas.adressevedligehold.beans.Address.AddressState; |
17 |
import dk.daoas.adressevedligehold.db.DatabaseLayerImplementation; |
import dk.daoas.adressevedligehold.db.DatabaseLayerImplementation; |
18 |
|
import dk.daoas.adressevedligehold.tasks.TaskLogger; |
19 |
import dk.daoas.adressevedligehold.util.DeduplicateHelper; |
import dk.daoas.adressevedligehold.util.DeduplicateHelper; |
20 |
import dk.daoas.adressevedligehold.util.MiscUtils; |
import dk.daoas.adressevedligehold.util.MiscUtils; |
21 |
import dk.daoas.adressevedligehold.util.TimingHelper; |
import dk.daoas.adressevedligehold.util.TimingHelper; |
29 |
|
|
30 |
public class AddressManager { |
public class AddressManager { |
31 |
|
|
32 |
|
private TaskLogger logger = TaskLogger.getInstance(); |
33 |
|
|
34 |
|
private int duplicateCount; |
35 |
|
|
36 |
List<Address> addressList; |
List<Address> addressList; |
37 |
|
|
38 |
Map<Integer, TreeMap<Short, ArrayList<Address>> > searchStructure; |
Map<Integer, TreeMap<Short, ArrayList<Address>> > searchStructure; |
39 |
|
|
40 |
ArrayList<AddressSourceEntry> afvisteEntries = new ArrayList<AddressSourceEntry>(); |
ArrayList<AddressSourceEntry> rejectedEntries = new ArrayList<AddressSourceEntry>(); |
41 |
|
|
42 |
|
Map<Integer,String> unknownStreets = new TreeMap<Integer, String>(); |
43 |
|
|
44 |
Map<Short,Short> dbkBaneMap = new TreeMap<Short,Short>(); |
Map<Short,Short> dbkBaneMap = new TreeMap<Short,Short>(); |
45 |
|
|
88 |
list.trimToSize(); |
list.trimToSize(); |
89 |
} |
} |
90 |
|
|
91 |
System.out.println("AddressManager ready, elapsed " + timer.getElapsed() + "ms"); |
logger.info("AddressManager ready, elapsed " + timer.getElapsed() + "ms"); |
92 |
|
|
93 |
|
|
94 |
} |
} |
185 |
continue; |
continue; |
186 |
} |
} |
187 |
|
|
188 |
System.out.println(addr.state + ": " + addr.toStringExtended() ); |
logger.info(addr.state + ": " + addr.toStringExtended() ); |
189 |
System.out.println(addr.getRuteString() ); |
logger.info(addr.getRuteString() ); |
190 |
|
|
191 |
if (count++ > 50) |
if (count++ > 50) |
192 |
return; |
return; |
194 |
} |
} |
195 |
|
|
196 |
public void writeBackChanges() { |
public void writeBackChanges() { |
197 |
System.out.println("Writing back changes"); |
logger.info("Writing back changes"); |
198 |
TimingHelper timer = new TimingHelper(); |
TimingHelper timer = new TimingHelper(); |
199 |
|
|
200 |
List<Address> updates = new ArrayList<Address>(1024*1024); |
List<Address> updates = new ArrayList<Address>(1024*1024); |
212 |
DatabaseLayerImplementation db = new DatabaseLayerImplementation(); |
DatabaseLayerImplementation db = new DatabaseLayerImplementation(); |
213 |
|
|
214 |
db.updateAddresses(updates); |
db.updateAddresses(updates); |
215 |
|
db.saveNewAddresses(inserts); |
216 |
|
|
217 |
|
|
218 |
|
|
219 |
System.out.println("Writeback done: elapsed " + timer.getElapsed() + "ms."); |
logger.info("Writeback done: elapsed " + timer.getElapsed() + "ms."); |
220 |
} |
} |
221 |
|
|
222 |
|
|
231 |
private void visitSingle(AddressSourceEntry entry) { |
private void visitSingle(AddressSourceEntry entry) { |
232 |
TreeMap<Short, ArrayList<Address>> gade = searchStructure.get( entry.gadeid ); |
TreeMap<Short, ArrayList<Address>> gade = searchStructure.get( entry.gadeid ); |
233 |
if (gade == null) { |
if (gade == null) { |
234 |
|
if (! unknownStreets.containsKey(entry.gadeid)) { |
235 |
|
unknownStreets.put(entry.gadeid, entry.vejnavn); |
236 |
|
} |
237 |
|
|
238 |
createFromEntry(entry); // if we get here there was no match - so we need to create it |
createFromEntry(entry); // if we get here there was no match - so we need to create it |
239 |
return; |
return; |
240 |
} |
} |
244 |
createFromEntry(entry); // if we get here there was no match - so we need to create it |
createFromEntry(entry); // if we get here there was no match - so we need to create it |
245 |
return; |
return; |
246 |
} |
} |
247 |
|
boolean found = false; |
248 |
for (Address addr : litraList) { |
for (Address addr : litraList) { |
249 |
if (addr.husnrbogstav.equals(entry.litra) ) { |
if (addr.husnrbogstav.equals(entry.litra) ) { |
250 |
updateAddress(addr, entry); |
updateAddress(addr, entry); |
251 |
return; //Done for now - no need to look at the rest of the list |
found = true; // 1 visit should be enough but as long as there's duplicates on gadeid+husnr+litra we will visit them all |
252 |
|
//when the issue with duplicates is resolved this should be reverted to a return or break |
253 |
} |
} |
254 |
} |
} |
255 |
|
if (found == false) { |
256 |
createFromEntry(entry); // if we get here there was no match - so we need to create it |
createFromEntry(entry); // if we get here there was no match - so we need to create it |
257 |
|
} |
258 |
} |
} |
259 |
|
|
260 |
private void visitRange(AddressSourceEntry entry) { |
private void visitRange(AddressSourceEntry entry) { |
261 |
|
|
262 |
TreeMap<Short, ArrayList<Address>> gade = searchStructure.get( entry.gadeid ); |
TreeMap<Short, ArrayList<Address>> gade = searchStructure.get( entry.gadeid ); |
263 |
if (gade == null) { |
if (gade == null) { |
264 |
System.out.println("[Range] Ukendt gadeID " + entry); |
if (! unknownStreets.containsKey(entry.gadeid)) { |
265 |
|
unknownStreets.put(entry.gadeid, entry.vejnavn); |
266 |
|
} |
267 |
|
|
268 |
|
logger.info("[Range] Ukendt gadeID " + entry); |
269 |
return; |
return; |
270 |
} |
} |
271 |
|
|
304 |
|
|
305 |
private void updateAddress(Address addr, AddressSourceEntry entry) { |
private void updateAddress(Address addr, AddressSourceEntry entry) { |
306 |
if ( addr.distributor != null && addr.distributor.equals(entry.distributor) == false) { |
if ( addr.distributor != null && addr.distributor.equals(entry.distributor) == false) { |
307 |
afvisteEntries.add(entry); |
rejectedEntries.add(entry); |
308 |
System.out.println("Afviser " + entry); |
logger.info("Afviser " + entry); |
309 |
return; |
return; |
310 |
} |
} |
311 |
|
|
327 |
} |
} |
328 |
} |
} |
329 |
} else { |
} else { |
330 |
System.out.println( "Double visit monday " + addr); |
duplicateCount++; |
331 |
|
logger.info( "Double visit monday " + addr); |
332 |
|
return; // if the entry is duplicate on one day that it covers - then it will be as well on all the othters |
333 |
} |
} |
334 |
|
|
335 |
} |
} |
351 |
} |
} |
352 |
} |
} |
353 |
} else { |
} else { |
354 |
System.out.println( "Double visit tuesday " + addr); |
duplicateCount++; |
355 |
|
logger.info( "Double visit tuesday " + addr); |
356 |
|
return; // if the entry is duplicate on one day that it covers - then it will be as well on all the othters |
357 |
} |
} |
358 |
} |
} |
359 |
|
|
373 |
} |
} |
374 |
} |
} |
375 |
} else { |
} else { |
376 |
System.out.println( "Double visit wednesday " + addr); |
duplicateCount++; |
377 |
|
logger.info( "Double visit wednesday " + addr); |
378 |
|
return; // if the entry is duplicate on one day that it covers - then it will be as well on all the othters |
379 |
} |
} |
380 |
} |
} |
381 |
|
|
397 |
} |
} |
398 |
} |
} |
399 |
} else { |
} else { |
400 |
System.out.println( "Double visit thursday " + addr); |
duplicateCount++; |
401 |
|
logger.info( "Double visit thursday " + addr); |
402 |
|
return; // if the entry is duplicate on one day that it covers - then it will be as well on all the othters |
403 |
} |
} |
404 |
} |
} |
405 |
|
|
420 |
} |
} |
421 |
} |
} |
422 |
} else { |
} else { |
423 |
System.out.println( "Double visit friday " + addr); |
duplicateCount++; |
424 |
|
logger.info( "Double visit friday " + addr); |
425 |
|
return; // if the entry is duplicate on one day that it covers - then it will be as well on all the othters |
426 |
} |
} |
427 |
} |
} |
428 |
|
|
444 |
} |
} |
445 |
} |
} |
446 |
} else { |
} else { |
447 |
System.out.println( "Double visit saturday " + addr); |
duplicateCount++; |
448 |
|
logger.info( "Double visit saturday " + addr); |
449 |
|
return; // if the entry is duplicate on one day that it covers - then it will be as well on all the othters |
450 |
} |
} |
451 |
} |
} |
452 |
|
|
467 |
} |
} |
468 |
} |
} |
469 |
} else { |
} else { |
470 |
System.out.println( "Double visit sunday " + addr); |
duplicateCount++; |
471 |
|
logger.info( "Double visit sunday " + addr); |
472 |
|
return; // if the entry is duplicate on one day that it covers - then it will be as well on all the othters |
473 |
} |
} |
474 |
} |
} |
475 |
|
|
476 |
|
|
477 |
|
|
478 |
addr.visited = true; |
//addr.visited = true; |
479 |
if (addr.state == AddressState.NOT_CHANGED) { |
if (addr.state == AddressState.NOT_CHANGED) { |
480 |
if (addr.distributor == null) { |
if (addr.distributor == null) { |
481 |
addr.state = AddressState.OPENED; |
addr.state = AddressState.OPENED; |
497 |
|
|
498 |
|
|
499 |
private void createFromEntry(AddressSourceEntry entry) { |
private void createFromEntry(AddressSourceEntry entry) { |
500 |
|
if (entry.husnr >= 1000) { //husnumre i DK må kun være på 3 cifre |
501 |
|
return; |
502 |
|
} |
503 |
|
|
504 |
if (entry.husnr == 999) { |
if (entry.husnr == 999) { |
505 |
return; |
return; |
506 |
} |
} |
507 |
|
|
508 |
|
if (entry.litra.length() > 1) { //litra må kun være på 1 tegn |
509 |
|
return; |
510 |
|
} |
511 |
|
if (entry.litra.length() == 1) { |
512 |
|
char litra = entry.litra.charAt(0); |
513 |
|
if ( Character.isLetter(litra) == false ) { |
514 |
|
return; |
515 |
|
} |
516 |
|
|
517 |
|
} |
518 |
|
|
519 |
if (entry.kommunekode == 0 || entry.vejkode == 0) { |
if (entry.kommunekode == 0 || entry.vejkode == 0) { |
520 |
return; |
return; |
521 |
} |
} |
522 |
|
if (entry.kommunekode < 100) { |
523 |
|
return; |
524 |
|
} |
525 |
|
|
526 |
|
if (entry.gadeid == 0) { |
527 |
|
return; |
528 |
|
} |
529 |
|
|
530 |
|
String gadeidStr = Integer.toString(entry.gadeid); |
531 |
|
if (gadeidStr.length() != 9) { |
532 |
|
return; |
533 |
|
} |
534 |
|
if (Short.parseShort(gadeidStr.substring(0, 4)) != entry.postnr) { |
535 |
|
return; //gadeid / postnr mismatch |
536 |
|
} |
537 |
|
|
538 |
|
|
539 |
|
|
540 |
System.out.println("Opretter adresse ud fra " + entry); |
logger.info("Opretter adresse ud fra " + entry); |
541 |
|
|
542 |
Address a = new Address(); |
Address a = new Address(); |
543 |
a.state = AddressState.CREATED; |
a.state = AddressState.CREATED; |
550 |
a.husnr = entry.husnr; |
a.husnr = entry.husnr; |
551 |
a.husnrbogstav = entry.litra; |
a.husnrbogstav = entry.litra; |
552 |
a.postnr = entry.postnr; |
a.postnr = entry.postnr; |
553 |
a.visited = true; |
//a.visited = true; |
554 |
|
|
555 |
if (entry.ugedage.mandag) { |
if (entry.ugedage.mandag) { |
556 |
a.ruteMandag = entry.rute; |
a.ruteMandag = entry.rute; |
649 |
break; |
break; |
650 |
case "NS": |
case "NS": |
651 |
bane = 204; |
bane = 204; |
652 |
break; |
break; |
653 |
|
default: |
654 |
|
throw new RuntimeException("Ukendt distributor: " + a.distributor); //Silence findBugs |
655 |
} |
} |
656 |
|
|
657 |
if (bane == null) { |
if (bane == null) { |
658 |
throw new RuntimeException("Ukendt bane for postnr" + a.postnr); |
throw new RuntimeException("Ukendt bane for postnr: " + a.postnr); |
659 |
} |
} |
660 |
|
|
661 |
if (a.dbkBane != bane) { |
if (a.dbkBane != bane) { |
670 |
return Collections.unmodifiableList(addressList); |
return Collections.unmodifiableList(addressList); |
671 |
} |
} |
672 |
|
|
673 |
|
public Map<Integer,String> getUnknownStreets() { |
674 |
|
return Collections.unmodifiableMap( unknownStreets ); |
675 |
|
} |
676 |
|
|
677 |
|
public int getDuplicateCount() { |
678 |
|
return duplicateCount; |
679 |
|
} |
680 |
|
|
681 |
public int getAfvistAntal() { |
public int getRejectedCount() { |
682 |
return afvisteEntries.size(); |
return rejectedEntries.size(); |
683 |
} |
} |
684 |
|
|
685 |
} |
} |