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; |
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 ); |
59 |
|
|
60 |
logger.info("Executing: " + sql); |
logger.info("Executing: " + sql); |
61 |
conn.createStatement().executeUpdate(sql); |
conn.createStatement().executeUpdate(sql); |
62 |
} |
} |
63 |
|
|
64 |
public BoundingBox getBoundingbox(String postnr) throws SQLException { |
public BoundingBox getBoundingbox(String postnr) throws SQLException { |
65 |
String minPostnr = postnr.replace("x", "0"); |
BoundingBox bb = bbCache.get(postnr); |
66 |
String maxPostnr = postnr.replace("x", "9"); |
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 |
|
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 " + |
99 |
|
|
100 |
return bbox; |
return bbox; |
101 |
} |
} |
102 |
|
|
103 |
|
|
104 |
|
|
105 |
public Queue<Adresse> hentIkkedaekkedeAdresser(String postnr) throws SQLException { |
public Queue<Adresse> hentIkkedaekkedeAdresser(String postnr) throws SQLException { |
106 |
|
|
107 |
String minPostnr = postnr.replace("x", "0"); |
String minPostnr = postnr.replace('x', '0'); |
108 |
String maxPostnr = postnr.replace("x", "9"); |
String maxPostnr = postnr.replace('x', '9'); |
109 |
|
|
110 |
ConcurrentLinkedQueue<Adresse> queue = new ConcurrentLinkedQueue<Adresse>(); |
ConcurrentLinkedQueue<Adresse> queue = new ConcurrentLinkedQueue<Adresse>(); |
111 |
|
|
141 |
*/ |
*/ |
142 |
|
|
143 |
|
|
144 |
String sql = "SELECT concat(left(postnr,3),'x') as postnr2 " + |
String sql = "SELECT rpad(left(postnr,?),'4', 'x') as postnr2 " + |
145 |
"FROM fulddaekning.adressetabel " + |
"FROM fulddaekning.adressetabel " + |
146 |
"WHERE postnr BETWEEN ? AND ? " + |
"WHERE postnr BETWEEN ? AND ? " + |
147 |
"AND rute is null " + // Træk kun liste på postnumre hvor der er ikke-dækkede adresser |
"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 " + |
"GROUP BY postnr2 " + |
150 |
"ORDER by postnr2 "; |
"ORDER by postnr2 "; |
151 |
|
|
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()) { |
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() ] ); |
213 |
PreparedStatement stmt = conn.prepareStatement(sql, java.sql.ResultSet.TYPE_FORWARD_ONLY, java.sql.ResultSet.CONCUR_READ_ONLY); |
PreparedStatement stmt = conn.prepareStatement(sql, java.sql.ResultSet.TYPE_FORWARD_ONLY, java.sql.ResultSet.CONCUR_READ_ONLY); |
214 |
stmt.setFetchSize(Integer.MIN_VALUE); |
stmt.setFetchSize(Integer.MIN_VALUE); |
215 |
|
|
216 |
stmt.setString(1, Lookup.distributor); |
stmt.setString(1, LookupMain.distributor); |
217 |
|
|
218 |
List<Adresse> list = hentAdresseListe( stmt ); |
List<Adresse> list = hentAdresseListe( stmt ); |
219 |
return list.toArray( new Adresse[ list.size() ] ); |
return list.toArray( new Adresse[ list.size() ] ); |
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"); |
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"); |
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 |
|
|