5 |
import java.sql.PreparedStatement; |
import java.sql.PreparedStatement; |
6 |
import java.sql.ResultSet; |
import java.sql.ResultSet; |
7 |
import java.sql.SQLException; |
import java.sql.SQLException; |
8 |
|
import java.sql.Statement; |
9 |
import java.util.ArrayList; |
import java.util.ArrayList; |
10 |
import java.util.HashMap; |
import java.util.HashMap; |
11 |
import java.util.List; |
import java.util.List; |
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 DatabaseRouteDistance { |
26 |
Logger logger = Logger.getLogger(Database.class.getName()); |
private TaskLogger logger = TaskLogger.getInstance(); |
27 |
|
|
28 |
int batchCount = 0; |
int batchCount = 0; |
29 |
|
|
35 |
|
|
36 |
Map<Short,List<Address>> daekkedeAddressrHO = new HashMap<Short,List<Address>>(); |
Map<Short,List<Address>> daekkedeAddressrHO = new HashMap<Short,List<Address>>(); |
37 |
|
|
38 |
|
String weekdayField; |
39 |
|
|
40 |
|
|
41 |
|
|
42 |
|
|
49 |
|
|
50 |
//Map<Short, List<Address>> ikkeDaekkedePrPost = new HashMap<Short, List<Address>>(); |
//Map<Short, List<Address>> ikkeDaekkedePrPost = new HashMap<Short, List<Address>>(); |
51 |
|
|
52 |
|
boolean isIncremental; |
53 |
|
|
54 |
private HashMap<Short,BoundingBox> bbCache = new HashMap<Short,BoundingBox>(); |
private HashMap<Short,BoundingBox> bbCache = new HashMap<Short,BoundingBox>(); |
55 |
|
|
56 |
public Database(Connection conn) throws SQLException,IOException { |
public DatabaseRouteDistance(Connection conn, boolean isIncremental, String weekdayField) throws SQLException,IOException { |
57 |
this.conn = conn; |
this.conn = conn; |
58 |
|
|
59 |
|
String newExt = ""; |
60 |
|
if (isIncremental == false) { |
61 |
|
newExt = "_ny"; |
62 |
|
} |
63 |
|
|
64 |
|
this.weekdayField = weekdayField; |
65 |
|
|
66 |
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 IGNORE INTO fulddaekning.afstand_anden_rute" + newExt + " (orgId,orgPostnr, orgAdresse,orgGadeid,orgHusnr,orgHusnrBogstav,orgRute,id,postnr,adresse,gadeid,husnr,husnrbogstav,rute,afstand,`timestamp`) "+ |
67 |
"VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?, now() )"; |
"VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?, now() )"; |
68 |
|
|
69 |
saveStmt = conn.prepareStatement(sql); |
saveStmt = conn.prepareStatement(sql); |
70 |
|
this.isIncremental = isIncremental; |
71 |
|
|
72 |
} |
} |
73 |
|
|
74 |
public void resetResultTable() throws SQLException { |
public void resetResultTable() throws SQLException { |
75 |
logger.info("Dropping old result table (if exists)"); |
if (isIncremental == true) { |
76 |
String sql = "DROP TABLE IF EXISTS fulddaekning.afstand_anden_rute_ny"; |
return; |
77 |
conn.createStatement().executeUpdate(sql); |
} |
78 |
|
|
79 |
logger.info("Create new result table"); |
try (Statement stmt = conn.createStatement()) { |
80 |
sql = "CREATE TABLE fulddaekning.afstand_anden_rute_ny LIKE fulddaekning.afstand_anden_rute"; |
logger.info("Dropping old result table (if exists)"); |
81 |
conn.createStatement().executeUpdate(sql); |
String sql = "DROP TABLE IF EXISTS fulddaekning.afstand_anden_rute_ny"; |
82 |
|
stmt.executeUpdate(sql); |
83 |
|
|
84 |
|
logger.info("Create new result table"); |
85 |
|
sql = "CREATE TABLE fulddaekning.afstand_anden_rute_ny LIKE fulddaekning.afstand_anden_rute"; |
86 |
|
stmt.executeUpdate(sql); |
87 |
|
|
88 |
|
} |
89 |
} |
} |
90 |
|
|
91 |
|
@SuppressFBWarnings("SQL_NONCONSTANT_STRING_PASSED_TO_EXECUTE") |
92 |
public void renameResultTables() throws SQLException { |
public void renameResultTables() throws SQLException { |
93 |
if (AfstandAndenRuteTask.test_mode == true) { |
if (AfstandAndenRuteTask.test_mode == true) { |
94 |
throw new RuntimeException("Can not rename tables in test mode"); |
throw new RuntimeException("Can not rename tables in test mode"); |
95 |
} |
} |
|
|
|
|
Constants consts = Constants.getInstance(); |
|
|
String ext = consts.getTableExtension(); |
|
96 |
|
|
97 |
logger.info("Dropping old backup table (if exists)"); |
if (isIncremental) { |
98 |
String sql = "DROP TABLE IF EXISTS fulddaekning.afstand_anden_rute_old" + ext; |
return; |
99 |
conn.createStatement().executeUpdate(sql); |
} |
100 |
|
|
101 |
|
Constants consts = Constants.getInstance(); |
102 |
|
|
103 |
logger.info("Rename tables"); |
try (Statement stmt = conn.createStatement()) { |
104 |
String sql2 = "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; |
|
105 |
|
String ext = consts.getTableExtension(); |
106 |
|
|
107 |
|
logger.info("Dropping old backup table (if exists)"); |
108 |
|
String sql = "DROP TABLE IF EXISTS fulddaekning.afstand_anden_rute_old" + ext; |
109 |
|
stmt.executeUpdate(sql); |
110 |
|
|
111 |
|
logger.info("Rename tables"); |
112 |
|
String sql2 = "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; |
113 |
|
|
114 |
|
logger.info("Executing: " + sql2); |
115 |
|
stmt.executeUpdate(sql2); |
116 |
|
} |
117 |
|
|
118 |
logger.info("Executing: " + sql2); |
|
|
conn.createStatement().executeUpdate(sql2); |
|
119 |
} |
} |
120 |
|
|
121 |
public BoundingBox getBoundingbox(short postnr) { |
public BoundingBox getBoundingbox(short postnr) { |
122 |
BoundingBox bb = bbCache.get(postnr); |
BoundingBox bb = bbCache.get(postnr); |
123 |
return bb.clone();//never return the original / cached object |
return new BoundingBox(bb); //never return the original / cached object |
124 |
} |
} |
125 |
|
|
126 |
|
|
130 |
|
|
131 |
|
|
132 |
public Queue<Address> hentAlleIkkedaekkedeAdresser(int minPostnr, int maxPostnr) throws SQLException { |
public Queue<Address> hentAlleIkkedaekkedeAdresser(int minPostnr, int maxPostnr) throws SQLException { |
133 |
|
String placeHolder1 = "%INCREMENTAL1%"; |
134 |
|
String placeHolder2 = "%INCREMENTAL2%"; |
135 |
|
|
136 |
|
Constants consts = Constants.getInstance(); |
137 |
|
|
138 |
logger.info("Henter alle IKKE-daekkede Addressr"); |
logger.info("Henter alle IKKE-daekkede Addressr"); |
139 |
|
|
140 |
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 " + |
141 |
"FROM fulddaekning.Addresstabel a " + |
"FROM fulddaekning.adressetabel a " + |
142 |
"LEFT JOIN bogleveringer.postnummerdistributor p on (a.postnr=p.postnr) " + |
"LEFT JOIN bogleveringer.postnummerdistributor p on (a.postnr=p.postnr) " + |
143 |
"WHERE ruteMa IS NULL " + //Ingen dækning |
placeHolder1 + |
144 |
|
"WHERE " + weekdayField + " IS NULL " + //Ingen dækning |
145 |
"AND a.postnr BETWEEN ? AND ? " + |
"AND a.postnr BETWEEN ? AND ? " + |
146 |
"AND latitude IS NOT NULL " + |
"AND latitude IS NOT NULL " + |
147 |
"AND longitude IS NOT NULL " + |
"AND longitude IS NOT NULL " + |
148 |
"AND gadeid IS NOT NULL " + |
"AND a.gadeid IS NOT NULL " + |
149 |
"AND (a.distributor IS NULL OR a.distributor<>'LUKKET') " |
"AND (a.distributor IS NULL OR a.distributor<>'LUKKET') " + |
150 |
|
placeHolder2 + |
151 |
|
"ORDER BY gadeid " |
152 |
; |
; |
153 |
|
|
154 |
|
|
155 |
|
if (isIncremental) { |
156 |
|
sql = sql.replace(placeHolder1, "LEFT JOIN fulddaekning.afstand_anden_rute" + consts.getTableExtension() +" afstand ON (a.id = afstand.orgId) " ); |
157 |
|
sql = sql.replace(placeHolder2, "AND afstand.id IS NULL " ); |
158 |
|
} else { |
159 |
|
sql = sql.replace(placeHolder1, ""); |
160 |
|
sql = sql.replace(placeHolder2, ""); |
161 |
|
} |
162 |
|
|
163 |
if (AfstandAndenRuteTask.test_mode == true) { |
if (AfstandAndenRuteTask.test_mode == true) { |
164 |
sql = sql + " LIMIT 100 "; |
sql = sql + " LIMIT 100 "; |
165 |
} |
} |
166 |
|
|
167 |
PreparedStatement stmt = conn.prepareStatement(sql); |
try (PreparedStatement stmt = conn.prepareStatement(sql)) { |
|
stmt.setInt(1, minPostnr); |
|
|
stmt.setInt(2, maxPostnr); |
|
|
|
|
|
List<Address> list = hentAdresseListe( stmt ); |
|
|
alleIkkeDaekkede = list.toArray( new Address[ list.size() ] ); |
|
|
|
|
|
logger.info("Analyserer ikke-daekkede Addressr"); |
|
|
|
|
|
for (Address a : alleIkkeDaekkede) { |
|
168 |
|
|
169 |
|
|
170 |
|
stmt.setInt(1, minPostnr); |
171 |
|
stmt.setInt(2, maxPostnr); |
172 |
|
|
173 |
|
List<Address> list = hentAdresseListe( stmt ); |
174 |
|
alleIkkeDaekkede = list.toArray( new Address[ list.size() ] ); |
175 |
|
|
176 |
BoundingBox bbox; |
logger.info("Analyserer ikke-daekkede Addressr"); |
177 |
|
|
178 |
if (! postnumre.contains(a.postnr )) { |
for (Address a : alleIkkeDaekkede) { |
|
postnumre.add( a.postnr ); |
|
179 |
|
|
|
bbox = new BoundingBox(); |
|
180 |
|
|
181 |
bbCache.put( a.postnr, bbox); |
BoundingBox bbox; |
182 |
|
|
183 |
|
if (! postnumre.contains(a.postnr )) { |
184 |
|
postnumre.add( a.postnr ); |
185 |
|
|
186 |
|
bbox = new BoundingBox(); |
187 |
|
|
188 |
|
bbCache.put( a.postnr, bbox); |
189 |
|
|
190 |
|
} else { |
191 |
|
bbox = bbCache.get( a.postnr); |
192 |
|
} |
193 |
|
|
194 |
|
bbox.latitudeMax = Math.max(bbox.latitudeMax, a.latitude); |
195 |
|
bbox.latitudeMin = Math.min(bbox.latitudeMin, a.latitude); |
196 |
|
bbox.longitudeMax = Math.max(bbox.longitudeMax, a.longitude); |
197 |
|
bbox.longitudeMin = Math.min(bbox.longitudeMin, a.longitude); |
198 |
|
|
199 |
|
|
|
} else { |
|
|
bbox = bbCache.get( a.postnr); |
|
200 |
} |
} |
201 |
|
return new ConcurrentLinkedQueue<Address>( list ); |
|
bbox.latitudeMax = Math.max(bbox.latitudeMax, a.latitude); |
|
|
bbox.latitudeMin = Math.min(bbox.latitudeMin, a.latitude); |
|
|
bbox.longitudeMax = Math.max(bbox.longitudeMax, a.longitude); |
|
|
bbox.longitudeMin = Math.min(bbox.longitudeMin, a.longitude); |
|
|
|
|
|
|
|
202 |
} |
} |
|
return new ConcurrentLinkedQueue<Address>( list ); |
|
203 |
} |
} |
204 |
|
|
205 |
|
|
213 |
|
|
214 |
if ( alleAddressr == null ) { |
if ( alleAddressr == null ) { |
215 |
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 " + |
216 |
"FROM fulddaekning.Addresstabel a " + |
"FROM fulddaekning.adressetabel a " + |
217 |
"LEFT JOIN bogleveringer.postnummerdistributor p on (a.postnr=p.postnr) " + |
"LEFT JOIN bogleveringer.postnummerdistributor p on (a.postnr=p.postnr) " + |
218 |
"WHERE ruteMa IS NOT NULL " + |
"WHERE " + weekdayField + " IS NOT NULL " + |
219 |
"AND latitude IS NOT NULL " + |
"AND latitude IS NOT NULL " + |
220 |
"AND longitude IS NOT NULL " + |
"AND longitude IS NOT NULL " + |
221 |
"AND a.distributor = ? "; |
"AND a.distributor = ? "; |
225 |
} |
} |
226 |
|
|
227 |
// 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) |
228 |
// 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 |
|
PreparedStatement stmt = conn.prepareStatement(sql, java.sql.ResultSet.TYPE_FORWARD_ONLY, java.sql.ResultSet.CONCUR_READ_ONLY); |
|
|
stmt.setFetchSize(Integer.MIN_VALUE); |
|
|
|
|
|
stmt.setString(1, distributor); |
|
|
|
|
|
List<Address> list = hentAdresseListe( stmt ); |
|
229 |
|
|
230 |
alleAddressr = new ArrayList<Address>(); |
try (PreparedStatement stmt = conn.prepareStatement(sql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY)) { |
231 |
alleAddressr.addAll(list); |
stmt.setFetchSize(Integer.MIN_VALUE); |
232 |
|
|
233 |
for(Address addr : list) { |
stmt.setString(1, distributor); |
234 |
short ho = addr.ho; |
|
235 |
List<Address> hoListe = daekkedeAddressrHO.get(ho); |
List<Address> list = hentAdresseListe( stmt ); |
|
if ( hoListe == null) { |
|
|
hoListe = new ArrayList<Address>(); |
|
|
daekkedeAddressrHO.put(ho, hoListe); |
|
|
} |
|
236 |
|
|
237 |
hoListe.add(addr); |
alleAddressr = new ArrayList<Address>(); |
238 |
|
alleAddressr.addAll(list); |
239 |
|
|
240 |
|
for(Address addr : list) { |
241 |
|
short ho = addr.ho; |
242 |
|
List<Address> hoListe = daekkedeAddressrHO.get(ho); |
243 |
|
if ( hoListe == null) { |
244 |
|
hoListe = new ArrayList<Address>(); |
245 |
|
daekkedeAddressrHO.put(ho, hoListe); |
246 |
|
} |
247 |
|
|
248 |
|
hoListe.add(addr); |
249 |
|
|
250 |
|
} |
251 |
} |
} |
252 |
} |
} |
253 |
|
|
254 |
return alleAddressr.size(); |
return alleAddressr.size(); |
255 |
|
|
256 |
} |
} |
257 |
|
|
258 |
|
|
295 |
//saveStmt.close(); |
//saveStmt.close(); |
296 |
} |
} |
297 |
|
|
298 |
|
|
299 |
|
@SuppressFBWarnings("SQL_NONCONSTANT_STRING_PASSED_TO_EXECUTE") |
300 |
|
public void prepareIncrementalSearch() throws SQLException { |
301 |
|
if (isIncremental == false) { |
302 |
|
return; |
303 |
|
} |
304 |
|
|
305 |
|
logger.info("prepareIncrementalSearch() "); |
306 |
|
|
307 |
|
Constants consts = Constants.getInstance(); |
308 |
|
|
309 |
|
String sql1 = "UPDATE fulddaekning.afstand_anden_rute" + consts.getTableExtension() + " afstand " + |
310 |
|
"JOIN fulddaekning.adressetabel addr ON (afstand.id = addr.id) " + |
311 |
|
"SET afstand.id = 0 " + |
312 |
|
"WHERE ruteMa is null " |
313 |
|
; |
314 |
|
|
315 |
|
String sql2 = "DELETE FROM fulddaekning.afstand_anden_rute" + consts.getTableExtension() + " " + |
316 |
|
"WHERE id=0 " |
317 |
|
; |
318 |
|
|
319 |
|
|
320 |
|
try (Statement stmt = conn.createStatement()) { |
321 |
|
|
322 |
|
int rows = stmt.executeUpdate(sql1); |
323 |
|
logger.info(sql1 + "\n updated rows: " + rows); |
324 |
|
|
325 |
|
rows = stmt.executeUpdate(sql2); |
326 |
|
logger.info(sql2 + "\n deleted rows: " + rows); |
327 |
|
|
328 |
|
} |
329 |
|
} |
330 |
|
|
331 |
public synchronized void saveBatch() throws SQLException{ |
public synchronized void saveBatch() throws SQLException{ |
332 |
saveStmt.executeBatch(); |
saveStmt.executeBatch(); |
333 |
batchCount = 0; |
batchCount = 0; |
341 |
Constants consts = Constants.getInstance(); |
Constants consts = Constants.getInstance(); |
342 |
|
|
343 |
//logger.info("Starting query"); |
//logger.info("Starting query"); |
344 |
ResultSet res = stmt.executeQuery(); |
try ( ResultSet res = stmt.executeQuery() ) { |
345 |
//logger.info("Starting exec query done"); |
|
|
|
|
|
while (res.next()) { |
|
|
|
|
|
double latitude = res.getDouble(7); |
|
|
double longitude = res.getDouble(8); |
|
346 |
|
|
347 |
|
while (res.next()) { |
348 |
Address adr = new Address(latitude,longitude); |
|
349 |
|
|
350 |
adr.id = res.getInt(1); |
double latitude = res.getDouble(7); |
351 |
adr.postnr = res.getShort(2); |
double longitude = res.getDouble(8); |
352 |
adr.vejnavn = vejnavnCache.getInstance( res.getString(3) ); |
|
353 |
adr.gadeid = res.getInt(4); |
|
354 |
adr.husnr = res.getShort(5); |
Address adr = new Address(latitude,longitude); |
355 |
adr.husnrbogstav = husnrbogstavCache.getInstance( res.getString(6) ); |
|
356 |
//adr.latitude = res.getDouble(7); |
adr.id = res.getInt(1); |
357 |
//adr.longitude = res.getDouble(8); |
adr.postnr = res.getShort(2); |
358 |
adr.ruteMandag = ruteCache.getInstance( res.getString(9) ); |
adr.vejnavn = vejnavnCache.getInstance( res.getString(3) ); |
359 |
adr.ho = res.getShort(10); |
adr.gadeid = res.getInt(4); |
360 |
|
adr.husnr = res.getShort(5); |
361 |
list.add(adr); |
adr.husnrbogstav = husnrbogstavCache.getInstance( res.getString(6) ); |
362 |
|
//adr.latitude = res.getDouble(7); |
363 |
if (consts.doCheckHO() == true && adr.ho == 0) { |
//adr.longitude = res.getDouble(8); |
364 |
System.out.println( "Mangler HO" ); |
adr.ruteMandag = ruteCache.getInstance( res.getString(9) ); |
365 |
System.out.println( adr ); |
adr.ho = res.getShort(10); |
366 |
System.exit(0); |
|
367 |
|
|
368 |
|
if (consts.validatePostnr(adr.postnr) == false) {//delegate to Constants implementations to validate whether we should look at this address |
369 |
|
continue; |
370 |
|
} |
371 |
|
|
372 |
|
list.add(adr); |
373 |
|
|
374 |
|
if (consts.doCheckHO() == true && adr.ho == 0) { |
375 |
|
logger.info( "Mangler HO: " + adr ); |
376 |
|
throw new RuntimeException("Mangler HO: "+ adr); |
377 |
|
} |
378 |
|
|
379 |
|
//logger.info( "Adress:" + adr); |
380 |
} |
} |
381 |
|
|
382 |
//logger.info( "Adress:" + adr); |
res.close(); |
383 |
|
stmt.close(); |
384 |
|
|
385 |
|
return list; |
386 |
} |
} |
|
|
|
|
res.close(); |
|
|
stmt.close(); |
|
|
|
|
|
return list; |
|
387 |
} |
} |
388 |
|
|
389 |
} |
} |