/[projects]/dao/FuldDaekningWorker/src/main/java/dk/daoas/fulddaekning/Database.java
ViewVC logotype

Diff of /dao/FuldDaekningWorker/src/main/java/dk/daoas/fulddaekning/Database.java

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

revision 2220 by torben, Mon Sep 15 10:37:25 2014 UTC revision 2261 by torben, Mon Feb 9 14:39:37 2015 UTC
# Line 7  import java.sql.PreparedStatement; Line 7  import java.sql.PreparedStatement;
7  import java.sql.ResultSet;  import java.sql.ResultSet;
8  import java.sql.SQLException;  import java.sql.SQLException;
9  import java.util.ArrayList;  import java.util.ArrayList;
10    import java.util.HashMap;
11  import java.util.List;  import java.util.List;
12  import java.util.Properties;  import java.util.Properties;
13  import java.util.Queue;  import java.util.Queue;
# Line 22  public class Database { Line 23  public class Database {
23    
24          Connection conn;          Connection conn;
25          PreparedStatement saveStmt;          PreparedStatement saveStmt;
26            
27            private HashMap<String,BoundingBox> bbCache = new HashMap<String,BoundingBox>();
28    
29          public Database(SafeProperties conf)  throws SQLException,IOException {          public Database(SafeProperties conf)  throws SQLException,IOException {
30                  this.conn = getConnection( conf );                        this.conn = getConnection( conf );      
# Line 44  public class Database { Line 47  public class Database {
47          }                }      
48                    
49          public void renameResultTables() throws SQLException {          public void renameResultTables() throws SQLException {
50                    Constants consts = Constants.getInstance();
51                    String ext = consts.getTableExtension();
52                    
53                  logger.info("Dropping old backup table (if exists)");                  logger.info("Dropping old backup table (if exists)");
54                  String sql = "DROP TABLE IF EXISTS fulddaekning.afstand_anden_rute_old";                  String sql = "DROP TABLE IF EXISTS fulddaekning.afstand_anden_rute_old" + ext;
55                  conn.createStatement().executeUpdate(sql);                  conn.createStatement().executeUpdate(sql);
56                                    
57                  logger.info("Rename tables");                  logger.info("Rename tables");
58                  sql = "RENAME TABLE fulddaekning.afstand_anden_rute TO fulddaekning.afstand_anden_rute_old, fulddaekning.afstand_anden_rute_ny TO fulddaekning.afstand_anden_rute";                  sql = "RENAME TABLE fulddaekning.afstand_anden_rute" + ext + " TO fulddaekning.afstand_anden_rute_old" + ext + ", fulddaekning.afstand_anden_rute_ny TO fulddaekning.afstand_anden_rute" + ext;
59    
60                    logger.info("Executing: " + sql);
61                  conn.createStatement().executeUpdate(sql);                                conn.createStatement().executeUpdate(sql);              
62          }                }
63            
64            public BoundingBox getBoundingbox(String postnr) throws SQLException {
65                    BoundingBox bb = bbCache.get(postnr);
66                    if ( bb == null ) {
67                            bb = getBoundingboxFromDb(postnr);
68                            bbCache.put(postnr, bb);
69                    } else {
70                            logger.info("Serving BB from cache");
71                    }
72                    
73                    return bb.clone();//never return the original / cached object
74            }
75    
76          public BoundingBox getBoundingbox(int postnr) throws SQLException {          private BoundingBox getBoundingboxFromDb(String postnr) throws SQLException {
77                    String minPostnr = postnr.replace('x', '0');
78                    String maxPostnr = postnr.replace('x', '9');
79    
80                  String sql =                  String sql =
81                                  "SELECT max(latitude) latmax, min(latitude) latmin, max(longitude) lngmax,min(longitude) lngmin  " +                                  "SELECT max(latitude) latmax, min(latitude) latmin, max(longitude) lngmax,min(longitude) lngmin  " +
82                                  "FROM fulddaekning.adressetabel WHERE postnr=? and rute is null;";                                  "FROM fulddaekning.adressetabel WHERE postnr BETWEEN ? and ? and rute is null;";
83    
84                  PreparedStatement stmt = conn.prepareStatement(sql);                  PreparedStatement stmt = conn.prepareStatement(sql);
85                  stmt.setInt(1, postnr);                  stmt.setString(1, minPostnr);
86                    stmt.setString(2, maxPostnr);
87    
88                  ResultSet res = stmt.executeQuery();                  ResultSet res = stmt.executeQuery();
89                  res.next(); //query returnerer altid 1 række                  res.next(); //query returnerer altid 1 række
# Line 76  public class Database { Line 99  public class Database {
99    
100                  return bbox;                  return bbox;
101          }          }
102            
103            
104    
105          public Queue<Adresse> hentIkkedaekkedeAdresser(int postnr)  throws SQLException {          public Queue<Adresse> hentIkkedaekkedeAdresser(String postnr)  throws SQLException {
106                    
107                    String minPostnr = postnr.replace('x', '0');
108                    String maxPostnr = postnr.replace('x', '9');
109                    
110                  ConcurrentLinkedQueue<Adresse> queue = new ConcurrentLinkedQueue<Adresse>();                  ConcurrentLinkedQueue<Adresse> queue = new ConcurrentLinkedQueue<Adresse>();
111    
112                  String sql = "SELECT id,postnr,adresse,gadeid,husnr,husnrbogstav,latitude,longitude,rute " +                  String sql = "SELECT id,a.postnr,adresse,gadeid,husnr,husnrbogstav,latitude,longitude,rute,p.distributor as ho " +
113                                  "FROM fulddaekning.adressetabel " +                                  "FROM fulddaekning.adressetabel a " +
114                                    "LEFT JOIN bogleveringer.postnummerdistributor p on (a.postnr=p.postnr) " +
115                                  "WHERE rute IS NULL " +  //Ingen dækning                                  "WHERE rute IS NULL " +  //Ingen dækning
116                                  "AND postnr=?  " +                                  "AND a.postnr BETWEEN ? AND ? " +
117                                  "AND latitude IS NOT NULL " +                                  "AND latitude IS NOT NULL " +
118                                  "AND longitude IS NOT NULL " +                                  "AND longitude IS NOT NULL " +
119                                  "AND gadeid IS NOT NULL ";                                  "AND gadeid IS NOT NULL " +
120                                    "AND (a.distributor IS NULL OR a.distributor<>'LUKKET') ";              
121                  PreparedStatement stmt = conn.prepareStatement(sql);                  PreparedStatement stmt = conn.prepareStatement(sql);
122                  stmt.setInt(1, postnr);                  stmt.setString(1, minPostnr);
123                    stmt.setString(2, maxPostnr);
124    
125                  queue.addAll( hentAdresseListe( stmt ) );                  queue.addAll( hentAdresseListe( stmt ) );
126                  return queue;                  return queue;
127          }          }
128    
129          public List<Integer> hentPostnumre() throws SQLException {          public List<String> hentPostnumre() throws SQLException {
130                  ArrayList<Integer> list = new ArrayList<Integer>();                  ArrayList<String> list = new ArrayList<String>();
131                                    
132                  Constants consts = Constants.getInstance();                  Constants consts = Constants.getInstance();
133    
134                    /*
135                  String sql = "SELECT postnr " +                  String sql = "SELECT postnr " +
136                                           "FROM fulddaekning.adressetabel " +                                           "FROM fulddaekning.adressetabel " +
                                          //"WHERE distributor = ? and rute is not null " +  
137                                           "WHERE postnr BETWEEN ? AND ? " +                                           "WHERE postnr BETWEEN ? AND ? " +
138                                             "AND rute is null " + // Træk kun liste på postnumre hvor der er ikke-dækkede adresser
139                                           "GROUP BY postnr " +                                           "GROUP BY postnr " +
140                                           "ORDER by postnr";                                           "ORDER by postnr";
141                    */
142                    
143                    
144                    String sql = "SELECT rpad(left(postnr,?),'4', 'x') as postnr2 " +
145                                             "FROM fulddaekning.adressetabel " +
146                                             "WHERE postnr BETWEEN ? AND ? " +
147                                             "AND rute is null " + // Trae kun liste paa postnumre hvor der er ikke-daekede adresser
148                                             "AND (postnr NOT BETWEEN 3900 and 3999) " + //Skip alle groenlandske postnumre
149                                             "GROUP BY postnr2 " +
150                                             "ORDER by postnr2 ";
151                    
152                                            
153                                            
154                  PreparedStatement stmt = conn.prepareStatement(sql);                  PreparedStatement stmt = conn.prepareStatement(sql);
155                  //stmt.setString(1, Lookup.distributor );                  //stmt.setString(1, Lookup.distributor );
156                  stmt.setInt(1, consts.getMinPostnr());  
157                  stmt.setInt(2, consts.getMaxPostnr());                  stmt.setInt(1, consts.getPostnrGroup() );
158    
159                    stmt.setInt(2, consts.getMinPostnr());
160                    stmt.setInt(3, consts.getMaxPostnr());
161                  ResultSet res = stmt.executeQuery();                  ResultSet res = stmt.executeQuery();
162    
163                  while (res.next()) {                  while (res.next()) {
164                          int postnr = res.getInt("postnr");                          String postnr = res.getString("postnr2");
165                          list.add(postnr);                          list.add(postnr);
166                  }                  }
167                  res.close();                  res.close();
# Line 125  public class Database { Line 173  public class Database {
173          }          }
174    
175          public Adresse[] hentDaekkedeAdresser( BoundingBox bbox) throws SQLException {          public Adresse[] hentDaekkedeAdresser( BoundingBox bbox) throws SQLException {
176                  String sql = "SELECT id,postnr,adresse,gadeid,husnr,husnrbogstav,latitude,longitude,rute " +                  String sql = "SELECT id,a.postnr,adresse,gadeid,husnr,husnrbogstav,latitude,longitude,rute,p.distributor as ho " +
177                                  "FROM fulddaekning.adressetabel " +                                  "FROM fulddaekning.adressetabel a " +
178                                    "LEFT JOIN bogleveringer.postnummerdistributor p on (a.postnr=p.postnr) " +
179                                  "WHERE rute IS NOT NULL " +                                  "WHERE rute IS NOT NULL " +
180                                  "AND latitude BETWEEN ? AND ? " +                                  "AND latitude BETWEEN ? AND ? " +
181                                  "AND longitude BETWEEN ? AND ? " +                                  "AND longitude BETWEEN ? AND ? " +
182                                  "AND distributor = ? ";                                  "AND a.distributor = ? ";
183    
184                  // Forward only + concur_read_only + fetchsize tvinger driver til at hente en række af gangen (bedre performance ved store result sets)                  // Forward only + concur_read_only + fetchsize tvinger driver til at hente en række af gangen (bedre performance ved store result sets)
185                  // Se http://dev.mysql.com/doc/connector-j/en/connector-j-reference-implementation-notes.html                  // Se http://dev.mysql.com/doc/connector-j/en/connector-j-reference-implementation-notes.html
# Line 142  public class Database { Line 191  public class Database {
191                  stmt.setDouble(2, bbox.latitudeMax);                  stmt.setDouble(2, bbox.latitudeMax);
192                  stmt.setDouble(3, bbox.longitudeMin);                  stmt.setDouble(3, bbox.longitudeMin);
193                  stmt.setDouble(4, bbox.longitudeMax);                  stmt.setDouble(4, bbox.longitudeMax);
194                  stmt.setString(5, Lookup.distributor);                  stmt.setString(5, LookupMain.distributor);
195    
196                  List<Adresse> list = hentAdresseListe( stmt );                  List<Adresse> list = hentAdresseListe( stmt );
197                  return list.toArray( new Adresse[ list.size() ] );                  return list.toArray( new Adresse[ list.size() ] );
   
198          }          }
199            
200            
201            public Adresse[] hentAlleDaekkedeAdresser() throws SQLException {
202                    String sql = "SELECT id,a.postnr,adresse,gadeid,husnr,husnrbogstav,latitude,longitude,rute,p.distributor as ho " +
203                                    "FROM fulddaekning.adressetabel a " +
204                                    "LEFT JOIN bogleveringer.postnummerdistributor p on (a.postnr=p.postnr) " +
205                                    "WHERE rute IS NOT NULL " +
206                                    "AND latitude IS NOT NULL " +
207                                    "AND longitude IS NOT NULL " +
208                                    "AND a.distributor = ? ";
209    
210                    // Forward only + concur_read_only + fetchsize tvinger driver til at hente en række af gangen (bedre performance ved store result sets)
211                    // Se http://dev.mysql.com/doc/connector-j/en/connector-j-reference-implementation-notes.html
212                    //PreparedStatement stmt = conn.prepareStatement(sql);
213                    PreparedStatement stmt = conn.prepareStatement(sql, java.sql.ResultSet.TYPE_FORWARD_ONLY, java.sql.ResultSet.CONCUR_READ_ONLY);
214                    stmt.setFetchSize(Integer.MIN_VALUE);
215    
216                    stmt.setString(1, LookupMain.distributor);
217    
218                    List<Adresse> list = hentAdresseListe( stmt );
219                    return list.toArray( new Adresse[ list.size() ] );
220            }
221            
222            
223    
224          public synchronized void gemResultat(Adresse orgAdresse, Adresse bedsteAdresse, double bedsteAfstand) throws SQLException {          public synchronized void gemResultat(Adresse orgAdresse, Adresse bedsteAdresse, double bedsteAfstand) throws SQLException {
225                  /*String sql = "INSERT INTO fulddaekning.afstand_anden_rute_thn (orgId,orgPostnr, orgAdresse,orgGadeid,orgHusnr,orgHusnrBogstav,orgLatitude,orgLongitude,orgRute,id,postnr,adresse,gadeid,husnr,husnrbogstav,latitude,longitude,rute,afstand,`timestamp`) "+                  /*String sql = "INSERT INTO fulddaekning.afstand_anden_rute_thn (orgId,orgPostnr, orgAdresse,orgGadeid,orgHusnr,orgHusnrBogstav,orgLatitude,orgLongitude,orgRute,id,postnr,adresse,gadeid,husnr,husnrbogstav,latitude,longitude,rute,afstand,`timestamp`) "+
# Line 207  public class Database { Line 278  public class Database {
278    
279                  while (res.next()) {                  while (res.next()) {
280                          Adresse adr = new Adresse();                          Adresse adr = new Adresse();
281    
282                            /*
283                          adr.id = res.getInt("id");                          adr.id = res.getInt("id");
284                          adr.postnr = res.getInt("postnr");                          adr.postnr = res.getInt("postnr");
285                          adr.adresse = res.getString("adresse");                          adr.adresse = res.getString("adresse");
# Line 216  public class Database { Line 289  public class Database {
289                          adr.latitude = res.getDouble("latitude");                          adr.latitude = res.getDouble("latitude");
290                          adr.longitude = res.getDouble("longitude");                          adr.longitude = res.getDouble("longitude");
291                          adr.rute = res.getString("rute");                          adr.rute = res.getString("rute");
292                            adr.ho = res.getInt("ho");
293                            */
294            
295                            adr.id = res.getInt(1);
296                            adr.postnr = res.getInt(2);
297                            adr.adresse = res.getString(3);
298                            adr.gadeid = res.getInt(4);
299                            adr.husnr = res.getInt(5);
300                            adr.husnrbogstav = res.getString(6);
301                            adr.latitude = res.getDouble(7);
302                            adr.longitude = res.getDouble(8);
303                            adr.rute = res.getString(9);
304                            adr.ho = res.getInt(10);
305    
306                          list.add(adr);                          list.add(adr);
307    

Legend:
Removed from v.2220  
changed lines
  Added in v.2261

  ViewVC Help
Powered by ViewVC 1.1.20