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; |
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, orgAdresse,orgGadeid,orgHusnr,orgHusnrBogstav,orgRute,id,postnr,adresse,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 |
|
if (isIncremental == true) { |
76 |
|
return; |
77 |
|
} |
78 |
|
|
79 |
try (Statement stmt = conn.createStatement()) { |
try (Statement stmt = conn.createStatement()) { |
80 |
logger.info("Dropping old result table (if exists)"); |
logger.info("Dropping old result table (if exists)"); |
81 |
String sql = "DROP TABLE IF EXISTS fulddaekning.afstand_anden_rute_ny"; |
String sql = "DROP TABLE IF EXISTS fulddaekning.afstand_anden_rute_ny"; |
94 |
throw new RuntimeException("Can not rename tables in test mode"); |
throw new RuntimeException("Can not rename tables in test mode"); |
95 |
} |
} |
96 |
|
|
97 |
|
if (isIncremental) { |
98 |
|
return; |
99 |
|
} |
100 |
|
|
101 |
Constants consts = Constants.getInstance(); |
Constants consts = Constants.getInstance(); |
102 |
|
|
103 |
try (Statement stmt = conn.createStatement()) { |
try (Statement stmt = conn.createStatement()) { |
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.adressetabel 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 |
} |
} |
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.adressetabel 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 = ? "; |
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 |
229 |
|
|
230 |
try (PreparedStatement stmt = conn.prepareStatement(sql, java.sql.ResultSet.TYPE_FORWARD_ONLY, java.sql.ResultSet.CONCUR_READ_ONLY)) { |
try (PreparedStatement stmt = conn.prepareStatement(sql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY)) { |
231 |
stmt.setFetchSize(Integer.MIN_VALUE); |
stmt.setFetchSize(Integer.MIN_VALUE); |
232 |
|
|
233 |
stmt.setString(1, distributor); |
stmt.setString(1, distributor); |
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; |
346 |
|
|
347 |
while (res.next()) { |
while (res.next()) { |
348 |
|
|
349 |
|
|
350 |
double latitude = res.getDouble(7); |
double latitude = res.getDouble(7); |
351 |
double longitude = res.getDouble(8); |
double longitude = res.getDouble(8); |
352 |
|
|
364 |
adr.ruteMandag = ruteCache.getInstance( res.getString(9) ); |
adr.ruteMandag = ruteCache.getInstance( res.getString(9) ); |
365 |
adr.ho = res.getShort(10); |
adr.ho = res.getShort(10); |
366 |
|
|
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); |
list.add(adr); |
373 |
|
|
374 |
if (consts.doCheckHO() == true && adr.ho == 0) { |
if (consts.doCheckHO() == true && adr.ho == 0) { |
375 |
System.out.println( "Mangler HO" ); |
logger.info( "Mangler HO: " + adr ); |
|
System.out.println( adr ); |
|
376 |
throw new RuntimeException("Mangler HO: "+ adr); |
throw new RuntimeException("Mangler HO: "+ adr); |
377 |
} |
} |
378 |
|
|