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

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

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

revision 2926 by torben, Thu Feb 4 15:37:14 2016 UTC revision 2927 by torben, Fri Feb 5 11:01:48 2016 UTC
# Line 47  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, orgAdresse,orgGadeid,orgHusnr,orgHusnrBogstav,orgRute,id,postnr,adresse,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 79  public class Database { Line 90  public class Database {
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 111  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.adressetabel 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 "                                  "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 261  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 279  public class Database { Line 339  public class Database {
339    
340                          while (res.next()) {                          while (res.next()) {
341                                                                    
342                                    
343                                  double latitude = res.getDouble(7);                                  double latitude = res.getDouble(7);
344                                  double longitude = res.getDouble(8);                                  double longitude = res.getDouble(8);
345                    
# Line 296  public class Database { Line 357  public class Database {
357                                  adr.ruteMandag =  ruteCache.getInstance( res.getString(9) );                                  adr.ruteMandag =  ruteCache.getInstance( res.getString(9) );
358                                  adr.ho = res.getShort(10);                                  adr.ho = res.getShort(10);
359                    
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);                                  list.add(adr);
366                    
367                                  if (consts.doCheckHO() == true && adr.ho == 0) {                                  if (consts.doCheckHO() == true && adr.ho == 0) {

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

  ViewVC Help
Powered by ViewVC 1.1.20