4040import java .time .Instant ;
4141import java .time .ZoneId ;
4242import java .time .ZonedDateTime ;
43- import java .time .temporal .ChronoUnit ;
4443import java .util .ArrayList ;
4544import java .util .Arrays ;
4645import java .util .Calendar ;
6665import org .jetbrains .annotations .NotNull ;
6766import org .jooq .Condition ;
6867import org .jooq .DSLContext ;
68+ import org .jooq .Field ;
6969import org .jooq .Record ;
70+ import org .jooq .Record1 ;
71+ import org .jooq .Record2 ;
7072import org .jooq .ResultQuery ;
73+ import org .jooq .SelectConditionStep ;
74+ import org .jooq .SelectForUpdateStep ;
7175import org .jooq .conf .ParamType ;
76+ import org .jooq .impl .DSL ;
77+ import static org .jooq .impl .DSL .field ;
7278import usace .cwms .db .dao .util .OracleTypeMap ;
7379import usace .cwms .db .jooq .codegen .packages .CWMS_RATING_PACKAGE ;
7480import usace .cwms .db .jooq .codegen .tables .AV_RATING ;
@@ -420,7 +426,13 @@ public void create(String xml, boolean failIfExists) {
420426 );
421427 }
422428
429+ /**
430+ * Retrieve effective dates for specs matching the specIdMask and officeIdMask within the given date range.
431+ * NOTE: This makes a separate query to get the list of non-aliased spec ids for the officeIdMask, so that aliased specs can be skipped.
432+ */
423433 public RatingEffectiveDatesMap retrieveSpecEffectiveDates (String officeIdMask , String specIdMask , Instant begin , Instant end ) {
434+ //set of non-alias spec ids used to filter out aliased specs
435+ Set <String > ratingIdsNoAliases = getRatingIds (officeIdMask , "*" , false );
424436 return connectionResult (dsl , conn -> {
425437 //office->spec->dates
426438 NavigableMap <String , NavigableMap <String , NavigableSet <Instant >>> specDateMap = new TreeMap <>();
@@ -429,6 +441,9 @@ public RatingEffectiveDatesMap retrieveSpecEffectiveDates(String officeIdMask, S
429441 while (rs .next ()) {
430442 String officeId = rs .getString (OFFICE_ID );
431443 String specId = rs .getString (SPECIFICATION_ID );
444+ if (!ratingIdsNoAliases .contains (specId )) { // skip aliased specs based on queried list of rating ids not including aliases
445+ continue ;
446+ }
432447 Timestamp timestamp = rs .getTimestamp (EFFECTIVE_DATE , GMT_CALENDAR );
433448 Instant date = timestamp .toInstant ();
434449 NavigableSet <Instant > dateList = specDateMap .computeIfAbsent (officeId , k -> new TreeMap <>())
@@ -490,4 +505,40 @@ private ResultSet catRatings(Connection conn, String officeIdMask, String specId
490505
491506 return output ;
492507 }
508+
509+ private Set <String > getRatingIds (String office , String templateIdMask , boolean includeAliases ) {
510+ return connectionResult (dsl , conn ->
511+ {
512+ AV_RATING_SPEC specView = AV_RATING_SPEC .AV_RATING_SPEC ;
513+ Condition condition = DSL .noCondition ();
514+
515+ if (office != null ) {
516+ condition = condition .and (specView .OFFICE_ID .eq (office ));
517+ }
518+
519+ if (templateIdMask != null ) {
520+ Condition ratingIdLike = JooqDao .caseInsensitiveLikeRegex (specView .RATING_ID ,
521+ templateIdMask );
522+ condition = condition .and (ratingIdLike );
523+ }
524+
525+ if (!includeAliases ) {
526+ condition = condition .and (specView .ALIASED_ITEM .isNull ());
527+ }
528+
529+ Field <String > idField = field ("RATING_ID" , String .class );
530+
531+ SelectConditionStep <Record2 <String , String >> ratingStep = DSL .using (conn ).select (
532+ specView .OFFICE_ID ,
533+ specView .RATING_ID .as (idField ))
534+ .from (specView )
535+ .where (condition );
536+
537+ SelectForUpdateStep <Record1 <String >> query = DSL .using (conn ).selectDistinct (idField )
538+ .from (ratingStep )
539+ .orderBy (idField .asc ());
540+ return new LinkedHashSet <>(query .fetch (idField ));
541+ });
542+
543+ }
493544}
0 commit comments