/[projects]/dao/DaoAdresseVedligehold/src/main/java/dk/daoas/adressevedligehold/afstandandenrute/DatabaseRouteDistance.java
ViewVC logotype

Diff of /dao/DaoAdresseVedligehold/src/main/java/dk/daoas/adressevedligehold/afstandandenrute/DatabaseRouteDistance.java

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

revision 2879 by torben, Sat Jan 30 14:12:51 2016 UTC revision 2927 by torben, Fri Feb 5 11:01:48 2016 UTC
# Line 14  import java.util.Queue; Line 14  import java.util.Queue;
14  import java.util.Set;  import java.util.Set;
15  import java.util.TreeSet;  import java.util.TreeSet;
16  import java.util.concurrent.ConcurrentLinkedQueue;  import java.util.concurrent.ConcurrentLinkedQueue;
 import java.util.logging.Logger;  
17    
18  import dk.daoas.adressevedligehold.beans.Address;  import dk.daoas.adressevedligehold.beans.Address;
19    import dk.daoas.adressevedligehold.tasks.TaskLogger;
20  import dk.daoas.adressevedligehold.util.DeduplicateHelper;  import dk.daoas.adressevedligehold.util.DeduplicateHelper;
21    import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
22    
23    
24    
25  public class Database {  public class Database {
26          Logger logger = Logger.getLogger(Database.class.getName());          private TaskLogger logger = TaskLogger.getInstance();
27    
28          int batchCount = 0;          int batchCount = 0;
29    
# Line 46  public class Database { Line 47  public class Database {
47                    
48          //Map<Short, List<Address>> ikkeDaekkedePrPost = new HashMap<Short, List<Address>>();          //Map<Short, List<Address>> ikkeDaekkedePrPost = new HashMap<Short, List<Address>>();
49                    
50            boolean isIncremental;
51                    
52          private HashMap<Short,BoundingBox> bbCache = new HashMap<Short,BoundingBox>();          private HashMap<Short,BoundingBox> bbCache = new HashMap<Short,BoundingBox>();
53    
54          public Database(Connection conn)  throws SQLException,IOException {          public Database(Connection conn, boolean isIncremental)  throws SQLException,IOException {
55                  this.conn = conn;                  this.conn = conn;
56                    
57                    String newExt = "";
58                    if (isIncremental == false) {
59                            newExt = "_ny";
60                    }
61    
62                  String sql = "INSERT INTO fulddaekning.afstand_anden_rute_ny (orgId,orgPostnr, orgAddress,orgGadeid,orgHusnr,orgHusnrBogstav,orgRute,id,postnr,Address,gadeid,husnr,husnrbogstav,rute,afstand,`timestamp`) "+                  String sql = "INSERT INTO fulddaekning.afstand_anden_rute" + newExt + " (orgId,orgPostnr, orgAdresse,orgGadeid,orgHusnr,orgHusnrBogstav,orgRute,id,postnr,adresse,gadeid,husnr,husnrbogstav,rute,afstand,`timestamp`) "+
63                                  "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?, now() )";                                  "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?, now() )";
64    
65                  saveStmt = conn.prepareStatement(sql);                            saveStmt = conn.prepareStatement(sql);          
66                    this.isIncremental = isIncremental;
67    
68          }          }
69    
70          public void resetResultTable() throws SQLException {          public void resetResultTable() throws SQLException {
71                    if (isIncremental == true) {
72                            return;
73                    }
74                    
75                  try (Statement stmt = conn.createStatement()) {                  try (Statement stmt = conn.createStatement()) {
76                          logger.info("Dropping old result table (if exists)");                          logger.info("Dropping old result table (if exists)");
77                          String sql = "DROP TABLE IF EXISTS fulddaekning.afstand_anden_rute_ny";                          String sql = "DROP TABLE IF EXISTS fulddaekning.afstand_anden_rute_ny";
# Line 72  public class Database { Line 84  public class Database {
84                  }                  }
85          }                }      
86                    
87            @SuppressFBWarnings("SQL_NONCONSTANT_STRING_PASSED_TO_EXECUTE")
88          public void renameResultTables() throws SQLException {          public void renameResultTables() throws SQLException {
89                  if (AfstandAndenRuteTask.test_mode == true) {                  if (AfstandAndenRuteTask.test_mode == true) {
90                          throw new RuntimeException("Can not rename tables in test mode");                          throw new RuntimeException("Can not rename tables in test mode");
91                  }                  }
92                                    
93                    if (isIncremental) {
94                            return;
95                    }
96                    
97                  Constants consts = Constants.getInstance();                  Constants consts = Constants.getInstance();
98                                    
99                  try (Statement stmt = conn.createStatement()) {                  try (Statement stmt = conn.createStatement()) {
# Line 99  public class Database { Line 116  public class Database {
116                    
117          public BoundingBox getBoundingbox(short postnr)  {          public BoundingBox getBoundingbox(short postnr)  {
118                  BoundingBox bb = bbCache.get(postnr);                  BoundingBox bb = bbCache.get(postnr);
119                  return bb.clone();//never return the original / cached object                  return new BoundingBox(bb); //never return the original / cached object
120          }          }
121                    
122                    
# Line 109  public class Database { Line 126  public class Database {
126    
127    
128          public Queue<Address> hentAlleIkkedaekkedeAdresser(int minPostnr, int maxPostnr)  throws SQLException {          public Queue<Address> hentAlleIkkedaekkedeAdresser(int minPostnr, int maxPostnr)  throws SQLException {
129                    String placeHolder1 = "%INCREMENTAL1%";
130                    String placeHolder2 = "%INCREMENTAL2%";
131                    
132                    Constants consts = Constants.getInstance();
133                                    
134                  logger.info("Henter alle IKKE-daekkede Addressr");                  logger.info("Henter alle IKKE-daekkede Addressr");
135    
136                  String sql = "SELECT id,a.postnr,vejnavn,gadeid,husnr,husnrbogstav,latitude,longitude,ruteMa,p.distributor as ho " +                  String sql = "SELECT a.id,a.postnr,a.vejnavn,a.gadeid,a.husnr,a.husnrbogstav,latitude,longitude,ruteMa,p.distributor as ho " +
137                                  "FROM fulddaekning.Addresstabel a " +                                  "FROM fulddaekning.adressetabel a " +
138                                  "LEFT JOIN bogleveringer.postnummerdistributor p on (a.postnr=p.postnr) " +                                  "LEFT JOIN bogleveringer.postnummerdistributor p on (a.postnr=p.postnr) " +
139                                    placeHolder1 +
140                                  "WHERE ruteMa IS NULL " +  //Ingen dækning                                  "WHERE ruteMa IS NULL " +  //Ingen dækning
141                                  "AND a.postnr BETWEEN ? AND ? " +                                  "AND a.postnr BETWEEN ? AND ? " +
142                                  "AND latitude IS NOT NULL " +                                  "AND latitude IS NOT NULL " +
143                                  "AND longitude IS NOT NULL " +                                  "AND longitude IS NOT NULL " +
144                                  "AND gadeid IS NOT NULL " +                                  "AND a.gadeid IS NOT NULL " +
145                                  "AND (a.distributor IS NULL OR a.distributor<>'LUKKET') "                                  "AND (a.distributor IS NULL OR a.distributor<>'LUKKET') " +
146                                    placeHolder2 +
147                                    "ORDER BY gadeid "
148                                  ;                                                ;              
149                                    
150                    
151                    if (isIncremental) {                    
152                            sql = sql.replace(placeHolder1, "LEFT JOIN fulddaekning.afstand_anden_rute" + consts.getTableExtension() +" afstand ON (a.id = afstand.orgId) " );
153                            sql = sql.replace(placeHolder2, "AND afstand.id IS NULL " );
154                    } else {
155                            sql = sql.replace(placeHolder1, "");
156                            sql = sql.replace(placeHolder2, "");
157                    }
158                    
159                  if (AfstandAndenRuteTask.test_mode == true) {                  if (AfstandAndenRuteTask.test_mode == true) {
160                          sql = sql + " LIMIT 100 ";                          sql = sql + " LIMIT 100 ";
161                  }                  }
# Line 176  public class Database { Line 209  public class Database {
209                                    
210                  if ( alleAddressr == null ) {                  if ( alleAddressr == null ) {
211                          String sql = "SELECT id,a.postnr,vejnavn,gadeid,husnr,husnrbogstav,latitude,longitude,ruteMa,p.distributor as ho " +                          String sql = "SELECT id,a.postnr,vejnavn,gadeid,husnr,husnrbogstav,latitude,longitude,ruteMa,p.distributor as ho " +
212                                          "FROM fulddaekning.Addresstabel a " +                                          "FROM fulddaekning.adressetabel a " +
213                                          "LEFT JOIN bogleveringer.postnummerdistributor p on (a.postnr=p.postnr) " +                                          "LEFT JOIN bogleveringer.postnummerdistributor p on (a.postnr=p.postnr) " +
214                                          "WHERE ruteMa IS NOT NULL " +                                          "WHERE ruteMa IS NOT NULL " +
215                                          "AND latitude IS NOT NULL " +                                          "AND latitude IS NOT NULL " +
# Line 258  public class Database { Line 291  public class Database {
291                  //saveStmt.close();                      //saveStmt.close();    
292          }          }
293                    
294            
295            @SuppressFBWarnings("SQL_NONCONSTANT_STRING_PASSED_TO_EXECUTE")
296            public void prepareIncrementalSearch() throws SQLException {
297                    
298                    logger.info("prepareIncrementalSearch() ");
299                    
300                    Constants consts = Constants.getInstance();
301                    
302                    String sql1 = "UPDATE fulddaekning.afstand_anden_rute" + consts.getTableExtension() + " afstand " +
303                                             "JOIN fulddaekning.adressetabel addr ON (afstand.id = addr.id) " +
304                                             "SET afstand.id = 0 " +
305                                             "WHERE addr.rutema is null "
306                                             ;
307                    
308                    String sql2 = "DELETE FROM fulddaekning.afstand_anden_rute" + consts.getTableExtension() + " " +
309                                              "WHERE id=0 "
310                                              ;
311                    
312                    
313                    try (Statement stmt = conn.createStatement()) {
314                            
315                            int rows = stmt.executeUpdate(sql1);
316                            logger.info(sql1 + "\n updated rows: " + rows);
317                            
318                            rows = stmt.executeUpdate(sql2);
319                            logger.info(sql2 + "\n deleted rows: " + rows);
320                            
321                    }
322            }
323            
324          public synchronized void saveBatch() throws SQLException{          public synchronized void saveBatch() throws SQLException{
325                  saveStmt.executeBatch();                  saveStmt.executeBatch();
326                  batchCount = 0;                  batchCount = 0;
# Line 271  public class Database { Line 334  public class Database {
334                  Constants consts = Constants.getInstance();                  Constants consts = Constants.getInstance();
335    
336                  //logger.info("Starting query");                  //logger.info("Starting query");
337                  ResultSet res = stmt.executeQuery();                  try ( ResultSet res = stmt.executeQuery() ) {
338                  //logger.info("Starting exec query done");                  
   
                 while (res.next()) {  
                           
                         double latitude = res.getDouble(7);  
                         double longitude = res.getDouble(8);  
339    
340                                                    while (res.next()) {
341                          Address adr = new Address(latitude,longitude);                                  
342                                    
343                          adr.id = res.getInt(1);                                  double latitude = res.getDouble(7);
344                          adr.postnr = res.getShort(2);                                  double longitude = res.getDouble(8);
345                          adr.vejnavn = vejnavnCache.getInstance( res.getString(3) );          
346                          adr.gadeid = res.getInt(4);                                  
347                          adr.husnr = res.getShort(5);                                  Address adr = new Address(latitude,longitude);
348                          adr.husnrbogstav = husnrbogstavCache.getInstance( res.getString(6) );          
349                          //adr.latitude = res.getDouble(7);                                  adr.id = res.getInt(1);
350                          //adr.longitude = res.getDouble(8);                                  adr.postnr = res.getShort(2);
351                          adr.ruteMandag =  ruteCache.getInstance( res.getString(9) );                                  adr.vejnavn = vejnavnCache.getInstance( res.getString(3) );
352                          adr.ho = res.getShort(10);                                  adr.gadeid = res.getInt(4);
353                                    adr.husnr = res.getShort(5);
354                          list.add(adr);                                  adr.husnrbogstav = husnrbogstavCache.getInstance( res.getString(6) );
355                                    //adr.latitude = res.getDouble(7);
356                          if (consts.doCheckHO() == true && adr.ho == 0) {                                  //adr.longitude = res.getDouble(8);
357                                  System.out.println( "Mangler HO" );                                  adr.ruteMandag =  ruteCache.getInstance( res.getString(9) );
358                                  System.out.println( adr );                                  adr.ho = res.getShort(10);
359                                  System.exit(0);          
360                                    
361                                    if (consts.validatePostnr(adr.postnr) == false) {//delegate to Constants implementations to validate whether we should look at this address
362                                            continue;
363                                    }
364                                    
365                                    list.add(adr);
366            
367                                    if (consts.doCheckHO() == true && adr.ho == 0) {
368                                            logger.info( "Mangler HO: " + adr );
369                                            throw new RuntimeException("Mangler HO: "+ adr);
370                                    }
371            
372                                    //logger.info( "Adress:" + adr);
373                          }                          }
374            
375                          //logger.info( "Adress:" + adr);                          res.close();
376                            stmt.close();
377            
378                            return list;
379                  }                  }
   
                 res.close();  
                 stmt.close();  
   
                 return list;  
380          }          }
381                                    
382  }  }

Legend:
Removed from v.2879  
changed lines
  Added in v.2927

  ViewVC Help
Powered by ViewVC 1.1.20