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 |
|
} |
70 |
|
|
71 |
|
return bb.clone();//never return the original / cached object |
72 |
|
} |
73 |
|
|
74 |
|
private BoundingBox getBoundingboxFromDb(String postnr) throws SQLException { |
75 |
|
String minPostnr = postnr.replace('x', '0'); |
76 |
|
String maxPostnr = postnr.replace('x', '9'); |
77 |
|
|
78 |
String sql = |
String sql = |
79 |
"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 " + |
97 |
|
|
98 |
return bbox; |
return bbox; |
99 |
} |
} |
100 |
|
|
101 |
|
|
102 |
|
|
103 |
public Queue<Adresse> hentIkkedaekkedeAdresser(String postnr) throws SQLException { |
public Queue<Adresse> hentIkkedaekkedeAdresser(String postnr) throws SQLException { |
104 |
|
|
105 |
String minPostnr = postnr.replace("x", "0"); |
String minPostnr = postnr.replace('x', '0'); |
106 |
String maxPostnr = postnr.replace("x", "9"); |
String maxPostnr = postnr.replace('x', '9'); |
107 |
|
|
108 |
ConcurrentLinkedQueue<Adresse> queue = new ConcurrentLinkedQueue<Adresse>(); |
ConcurrentLinkedQueue<Adresse> queue = new ConcurrentLinkedQueue<Adresse>(); |
109 |
|
|
139 |
*/ |
*/ |
140 |
|
|
141 |
|
|
142 |
String sql = "SELECT concat(left(postnr,3),'x') as postnr2 " + |
String sql = "SELECT rpad(left(postnr,?),'4', 'x') as postnr2 " + |
143 |
"FROM fulddaekning.adressetabel " + |
"FROM fulddaekning.adressetabel " + |
144 |
"WHERE postnr BETWEEN ? AND ? " + |
"WHERE postnr BETWEEN ? AND ? " + |
145 |
"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 |
146 |
|
"AND (postnr NOT BETWEEN 3900 and 3999) " + //Skip alle groenlandske postnumre |
147 |
"GROUP BY postnr2 " + |
"GROUP BY postnr2 " + |
148 |
"ORDER by postnr2 "; |
"ORDER by postnr2 "; |
149 |
|
|
151 |
|
|
152 |
PreparedStatement stmt = conn.prepareStatement(sql); |
PreparedStatement stmt = conn.prepareStatement(sql); |
153 |
//stmt.setString(1, Lookup.distributor ); |
//stmt.setString(1, Lookup.distributor ); |
154 |
stmt.setInt(1, consts.getMinPostnr()); |
|
155 |
stmt.setInt(2, consts.getMaxPostnr()); |
stmt.setInt(1, consts.getPostnrGroup() ); |
156 |
|
|
157 |
|
stmt.setInt(2, consts.getMinPostnr()); |
158 |
|
stmt.setInt(3, consts.getMaxPostnr()); |
159 |
ResultSet res = stmt.executeQuery(); |
ResultSet res = stmt.executeQuery(); |
160 |
|
|
161 |
while (res.next()) { |
while (res.next()) { |
189 |
stmt.setDouble(2, bbox.latitudeMax); |
stmt.setDouble(2, bbox.latitudeMax); |
190 |
stmt.setDouble(3, bbox.longitudeMin); |
stmt.setDouble(3, bbox.longitudeMin); |
191 |
stmt.setDouble(4, bbox.longitudeMax); |
stmt.setDouble(4, bbox.longitudeMax); |
192 |
stmt.setString(5, Lookup.distributor); |
stmt.setString(5, LookupMain.distributor); |
193 |
|
|
194 |
List<Adresse> list = hentAdresseListe( stmt ); |
List<Adresse> list = hentAdresseListe( stmt ); |
195 |
return list.toArray( new Adresse[ list.size() ] ); |
return list.toArray( new Adresse[ list.size() ] ); |
211 |
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); |
212 |
stmt.setFetchSize(Integer.MIN_VALUE); |
stmt.setFetchSize(Integer.MIN_VALUE); |
213 |
|
|
214 |
stmt.setString(1, Lookup.distributor); |
stmt.setString(1, LookupMain.distributor); |
215 |
|
|
216 |
List<Adresse> list = hentAdresseListe( stmt ); |
List<Adresse> list = hentAdresseListe( stmt ); |
217 |
return list.toArray( new Adresse[ list.size() ] ); |
return list.toArray( new Adresse[ list.size() ] ); |