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

Diff of /dao/DaoAdresseVedligehold/src/main/java/dk/daoas/adressevedligehold/coveragefileupload/AddressManager.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 2862 by torben, Thu Jan 28 11:07:07 2016 UTC revision 2924 by torben, Fri Feb 5 08:01:18 2016 UTC
# Line 15  import dk.daoas.adressevedligehold.Addre Line 15  import dk.daoas.adressevedligehold.Addre
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;
# Line 28  import dk.daoas.adressevedligehold.util. Line 29  import dk.daoas.adressevedligehold.util.
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                    
# Line 82  public class AddressManager { Line 88  public class AddressManager {
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          }          }
# Line 179  public class AddressManager { Line 185  public class AddressManager {
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;
# Line 188  public class AddressManager { Line 194  public class AddressManager {
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);
# Line 206  public class AddressManager { Line 212  public class AddressManager {
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                    
# Line 224  public class AddressManager { Line 231  public class AddressManager {
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                  }                  }
# Line 233  public class AddressManager { Line 244  public class AddressManager {
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                                    
# Line 287  public class AddressManager { Line 304  public class AddressManager {
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                                    
# Line 310  public class AddressManager { Line 327  public class AddressManager {
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                  }                  }
# Line 332  public class AddressManager { Line 351  public class AddressManager {
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    
# Line 352  public class AddressManager { Line 373  public class AddressManager {
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    
# Line 374  public class AddressManager { Line 397  public class AddressManager {
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    
# Line 395  public class AddressManager { Line 420  public class AddressManager {
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    
# Line 417  public class AddressManager { Line 444  public class AddressManager {
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    
# Line 438  public class AddressManager { Line 467  public class AddressManager {
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                                    
# Line 466  public class AddressManager { Line 497  public class AddressManager {
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) {                  if (entry.kommunekode < 100) {
523                          return;                          return;
524                  }                  }
525                  if (Short.parseShort(Integer.toString(entry.gadeid).substring(0, 4)) != entry.postnr) {                  
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                          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;
# Line 593  public class AddressManager { Line 649  public class AddressManager {
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) {
# Line 612  public class AddressManager { Line 670  public class AddressManager {
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  }  }

Legend:
Removed from v.2862  
changed lines
  Added in v.2924

  ViewVC Help
Powered by ViewVC 1.1.20