22
33import edu .suffolk .litlab .efsp .db .DatabaseCreator ;
44import edu .suffolk .litlab .efsp .ecfcodes .tyler .CodeDatabase ;
5+ import edu .suffolk .litlab .efsp .ecfcodes .tyler .CodeTableConstants .UnsupportedTableException ;
56import edu .suffolk .litlab .efsp .server .ecf4 .Ecf4Helper ;
67import edu .suffolk .litlab .efsp .server .ecf4 .SoapClientChooser ;
78import edu .suffolk .litlab .efsp .server .utils .HeaderSigner ;
2526import java .net .URL ;
2627import java .sql .SQLException ;
2728import java .sql .Savepoint ;
28- import java .time .Clock ;
2929import java .time .Duration ;
3030import java .time .Instant ;
31+ import java .util .HashMap ;
3132import java .util .HashSet ;
3233import java .util .List ;
3334import java .util .Map ;
3435import java .util .Map .Entry ;
3536import java .util .Optional ;
36- import java .util .Set ;
3737import java .util .concurrent .ConcurrentHashMap ;
3838import java .util .function .Function ;
3939import java .util .function .Supplier ;
@@ -176,19 +176,17 @@ public static InputStream getCodesZip(String toRead, String authHeader) throws I
176176 */
177177 private boolean downloadAndProcessZip (
178178 String toRead , String signedTime , Function <InputStream , Boolean > process ) {
179- Instant startTable = Instant .now (Clock . systemUTC () );
179+ Instant startTable = Instant .now ();
180180 try (InputStream urlStream = getCodesZip (toRead , signedTime )) {
181181 // Write out the zip file
182- downloadDuration =
183- downloadDuration .plus (Duration .between (startTable , Instant .now (Clock .systemUTC ())));
182+ downloadDuration = downloadDuration .plus (Duration .between (startTable , Instant .now ()));
184183
185184 ZipInputStream zip = new ZipInputStream (urlStream );
186185 zip .getNextEntry ();
187186
188- Instant updateTableLoc = Instant .now (Clock . systemUTC () );
187+ Instant updateTableLoc = Instant .now ();
189188 boolean success = process .apply (zip );
190- updateDuration =
191- updateDuration .plus (Duration .between (updateTableLoc , Instant .now (Clock .systemUTC ())));
189+ updateDuration = updateDuration .plus (Duration .between (updateTableLoc , Instant .now ()));
192190 zip .close ();
193191 return success ;
194192 } catch (IOException ex ) {
@@ -234,6 +232,8 @@ private boolean downloadSystemTables(String baseUrl, CodeDatabaseAPI cd, HeaderS
234232 }
235233
236234 for (Map .Entry <String , String > urlSuffix : codeUrls .entrySet ()) {
235+ // Let SQL exceptions through here; table names are hard coded, so if they break
236+ // we need to know.
237237 cd .deleteFromTable (urlSuffix .getKey ());
238238 final Function <InputStream , Boolean > process =
239239 (is ) -> {
@@ -348,7 +348,7 @@ private boolean downloadCourtTables(
348348 throws JAXBException , IOException , SQLException {
349349 MDC .put (MDCWrappers .SESSION_ID , location );
350350 log .info ("Doing updates for: {}, tables: {}" , location , tables );
351- Instant downloadStart = Instant .now (Clock . systemUTC () );
351+ Instant downloadStart = Instant .now ();
352352 // TODO(brycew-later): check that the effective date is later than today
353353 // JAXBElement<?> obj = ccl.getEffectiveDate().getDateRepresentation();
354354 Map <String , String > urlMap =
@@ -383,12 +383,12 @@ private boolean downloadCourtTables(
383383 }
384384 Map <String , DownloadedCodes > downloaded =
385385 streamDownload (signedTime .get (), location , toDownload .parallel (), tables );
386- var downloadInc = Duration .between (downloadStart , Instant .now (Clock . systemUTC () ));
386+ var downloadInc = Duration .between (downloadStart , Instant .now ());
387387 downloadDuration = downloadDuration .plus (downloadInc );
388388 log .info (
389389 "Location: {}: Downloads took: {} (total: {})" , location , downloadInc , downloadDuration );
390390
391- Instant updateStart = Instant .now (Clock . systemUTC () );
391+ Instant updateStart = Instant .now ();
392392 for (DownloadedCodes down : downloaded .values ()) {
393393 MDC .put (MDCWrappers .REQUEST_ID , down .tableName ());
394394 try {
@@ -400,7 +400,7 @@ private boolean downloadCourtTables(
400400 down .input ().close ();
401401 }
402402 }
403- var updateInc = Duration .between (updateStart , Instant .now (Clock . systemUTC () ));
403+ var updateInc = Duration .between (updateStart , Instant .now ());
404404 updateDuration = updateDuration .plus (updateInc );
405405
406406 cd .commit ();
@@ -410,6 +410,45 @@ private boolean downloadCourtTables(
410410 return true ;
411411 }
412412
413+ /**
414+ * Creates all of the tables to update. If it can't create them, it removes them from the list.
415+ */
416+ private Map <String , List <String >> makeOrRemoveUnsupportedTables (
417+ Map <String , List <String >> versionsToUpdate , CodeDatabaseAPI cd ) throws SQLException {
418+ var allTables = new HashSet <String >();
419+ log .info ("Making tables (if any are absent)" );
420+ for (var tables : versionsToUpdate .values ()) {
421+ allTables .addAll (tables );
422+ }
423+
424+ var brokenTables = new HashSet <String >();
425+ for (String table : allTables ) {
426+ try {
427+ cd .createTableIfAbsent (table );
428+ } catch (UnsupportedTableException ex ) {
429+ log .warn ("Ignoring table {} from Tyler's report (not in our allow list)" , table );
430+ brokenTables .add (table );
431+ }
432+ }
433+
434+ var toReturn = new HashMap <String , List <String >>();
435+ for (var courtAndTables : versionsToUpdate .entrySet ()) {
436+ String courtLocation = courtAndTables .getKey ();
437+ if (courtLocation .isBlank ()) {
438+ log .warn ("Ignoring tables with an empty court!" );
439+ continue ;
440+ }
441+ List <String > tables = courtAndTables .getValue ();
442+ tables .removeAll (brokenTables );
443+ if (tables .isEmpty ()) {
444+ continue ;
445+ }
446+ toReturn .put (courtLocation , tables );
447+ }
448+
449+ return toReturn ;
450+ }
451+
413452 /** Returns true if successful, false if not successful */
414453 public boolean updateAll (String baseUrl , FilingReviewMDEPort filingPort , CodeDatabaseAPI cd )
415454 throws SQLException , IOException , JAXBException {
@@ -424,49 +463,36 @@ public boolean updateAll(String baseUrl, FilingReviewMDEPort filingPort, CodeDat
424463
425464 // Drop each of tables that need to be updated
426465 Savepoint sp = cd .setSavepoint ("court update savepoint" );
427- Map <String , List <String >> versionsToUpdate = cd .getVersionsToUpdate ();
428- Instant startDel = Instant .now (Clock .systemUTC ());
429- Set <String > allTables = new HashSet <>();
430- int n = 0 ;
431- log .info ("Making tables if absent" );
432- for (var tables : versionsToUpdate .values ()) {
433- n += tables .size ();
434- allTables .addAll (tables );
435- }
436- for (String table : allTables ) {
437- cd .createTableIfAbsent (table );
438- }
439- log .info ("Removing {} court entries, over {} queries" , versionsToUpdate .size (), n );
466+ Map <String , List <String >> rawVersionsToUpdate = cd .getVersionsToUpdate ();
467+ Map <String , List <String >> versionsToUpdate =
468+ makeOrRemoveUnsupportedTables (rawVersionsToUpdate , cd );
469+ Instant startDel = Instant .now ();
470+ log .info (
471+ "Removing {} court entries, over {} queries" ,
472+ versionsToUpdate .size (),
473+ versionsToUpdate .values ().stream ().map (List ::size ).reduce (0 , (a , b ) -> a + b ));
440474 for (Entry <String , List <String >> courtAndTables : versionsToUpdate .entrySet ()) {
441475 final String courtLocation = courtAndTables .getKey ();
442- if (courtLocation .isBlank ()) {
443- log .warn ("Ignoring tables with an empty court!" );
444- continue ;
445- }
446476 List <String > tables = courtAndTables .getValue ();
447477 log .debug (
448478 "In {}, removing entries for court {} for tables: {}" ,
449479 cd .getDomain (),
450480 courtLocation ,
451481 tables );
452482 for (String table : tables ) {
453- Instant deleteFromTable = Instant .now (Clock . systemUTC () );
483+ Instant delTime = Instant .now ();
454484
455- // No longer checking for false here -- see PR.
456485 // Will ignore tables that don't exist.
457486 cd .deleteFromTable (table , courtLocation );
458487
459- updateDuration =
460- updateDuration .plus (Duration .between (deleteFromTable , Instant .now (Clock .systemUTC ())));
488+ updateDuration = updateDuration .plus (Duration .between (delTime , Instant .now ()));
461489 }
462490 }
463- log .info (
464- "Took {} to remove existing tables" ,
465- Duration .between (startDel , Instant .now (Clock .systemUTC ())));
466- Instant startPolicy = Instant .now (Clock .systemUTC ());
491+ log .info ("Took {} to remove existing tables" , Duration .between (startDel , Instant .now ()));
492+ Instant startPolicy = Instant .now ();
467493 Map <String , CourtPolicyResponseMessageType > policies =
468494 streamPolicies (versionsToUpdate .keySet ().stream ().parallel (), cd .getDomain (), filingPort );
469- var soapInc = Duration .between (startPolicy , Instant .now (Clock . systemUTC () ));
495+ var soapInc = Duration .between (startPolicy , Instant .now ());
470496 soapDuration = soapDuration .plus (soapInc );
471497 log .info ("Soaps took: {} (total: {})" , soapInc , soapDuration );
472498
@@ -517,10 +543,10 @@ public boolean replaceSome(
517543 }
518544 // Remove the "0" or top level court, which doesn't usually have individual court tables
519545 locs .remove ("0" );
520- Instant startPolicy = Instant .now (Clock . systemUTC () );
546+ Instant startPolicy = Instant .now ();
521547 Map <String , CourtPolicyResponseMessageType > policies =
522548 streamPolicies (locs .stream (), cd .getDomain (), filingPort );
523- soapDuration = soapDuration .plus (Duration .between (startPolicy , Instant .now (Clock . systemUTC () )));
549+ soapDuration = soapDuration .plus (Duration .between (startPolicy , Instant .now ()));
524550 log .info ("Soaps: {}" , soapDuration );
525551 for (var policy : policies .entrySet ()) {
526552 final String location = policy .getKey ();
0 commit comments