diff --git a/cwms-data-api/src/main/java/cwms/cda/data/dao/LocationLevelsDaoImpl.java b/cwms-data-api/src/main/java/cwms/cda/data/dao/LocationLevelsDaoImpl.java index 53844a255..fff8d9b7e 100644 --- a/cwms-data-api/src/main/java/cwms/cda/data/dao/LocationLevelsDaoImpl.java +++ b/cwms-data-api/src/main/java/cwms/cda/data/dao/LocationLevelsDaoImpl.java @@ -99,6 +99,7 @@ import org.jooq.Result; import org.jooq.Select; import org.jooq.SelectLimitPercentAfterOffsetStep; +import org.jooq.SelectSeekStep5; import org.jooq.Table; import org.jooq.conf.ParamType; import org.jooq.exception.DataAccessException; @@ -135,6 +136,12 @@ public class LocationLevelsDaoImpl extends JooqDao implements Loc private static final AV_LOCATION_LEVEL_VALUES values = AV_LOCATION_LEVEL_VALUES; private static final usace.cwms.db.jooq.codegen.tables.AV_LOC_ALIAS aliasView = AV_LOC_ALIAS; + private static final String LOCATION_LEVEL_CODE = "LOCATION_LEVEL_CODE"; + private static final String LOCATION_LEVEL_ID = "LOCATION_LEVEL_ID"; + private static final String LOCATION_LEVEL_DATE = "LOCATION_LEVEL_DATE"; + private static final String OFFICE_ID = "OFFICE_ID"; + private static final String TIME_OFFSET = "TIME_OFFSET"; + private static final String CALENDAR_OFFSET = "CALENDAR_OFFSET"; private static final String TABLE_ALIAS1 = "T1"; private static final String TABLE_ALIAS2 = "T2"; private static final String ALIASED_ATTRIBUTE_ID = TABLE_ALIAS1 + ".ATTRIBUTE_ID"; @@ -188,69 +195,78 @@ public LocationLevels getLocationLevels(String cursor, int pageSize, } if (supportsNewView()) { - Condition whereCondition = ref.LOCATION_LEVEL_ID.isNotNull(); + Condition whereCondition = field(LOCATION_LEVEL_ID, String.class).isNotNull(); if (office != null && !office.isEmpty()) { - whereCondition = whereCondition.and(ref.OFFICE_ID.eq(office.toUpperCase())); + whereCondition = whereCondition.and(field(OFFICE_ID, String.class).eq(office.toUpperCase())); } if (levelIdMask != null && !levelIdMask.isEmpty()) { whereCondition = whereCondition.and( - JooqDao.caseInsensitiveLikeRegex(ref.LOCATION_LEVEL_ID, levelIdMask)); + JooqDao.caseInsensitiveLikeRegex(field(LOCATION_LEVEL_ID, String.class), levelIdMask)); } if (beginZdt != null) { whereCondition = - whereCondition.and(ref.LOCATION_LEVEL_DATE.greaterOrEqual(Timestamp.from(beginZdt.toInstant()))); + whereCondition.and(field(LOCATION_LEVEL_DATE, Timestamp.class).greaterOrEqual(Timestamp.from(beginZdt.toInstant()))); } if (endZdt != null) { whereCondition = - whereCondition.and(ref.LOCATION_LEVEL_DATE.lessThan(Timestamp.from(endZdt.toInstant()))); + whereCondition.and(field(LOCATION_LEVEL_DATE, Timestamp.class).lessThan(Timestamp.from(endZdt.toInstant()))); } Map builderMap = new LinkedHashMap<>(); - SelectLimitPercentAfterOffsetStep query; + SelectSeekStep5 query; + + var pagedRef = dsl.select(ref.LOCATION_LEVEL_CODE, ref.LOCATION_ID, + ref.LOCATION_LEVEL_ID, ref.ATTRIBUTE_ID, ref.OFFICE_ID, ref.LOCATION_LEVEL_DATE, ref.LOCATION_CODE, + ref.PARAMETER_ID, ref.PARAMETER_TYPE_ID, ref.DURATION_ID, ref.SPECIFIED_LEVEL_ID, ref.EXPIRATION_DATE, + ref.LOCATION_LEVEL_COMMENT) + .from(ref) + .where(whereCondition) + .orderBy(ref.OFFICE_ID, ref.LOCATION_ID, ref.PARAMETER_ID, ref.PARAMETER_TYPE_ID, ref.DURATION_ID, ref.SPECIFIED_LEVEL_ID, ref.LOCATION_LEVEL_DATE) + .offset(offset) + .limit(pageSize); if (includeAliases) { - query = dsl.select(asterisk()).from(dsl.selectDistinct(LOCATION_ALIAS_FIELDS_NEW_VIEW) - .from(ref) - .join(values) - .on(ref.LOCATION_LEVEL_CODE.eq(values.LOCATION_LEVEL_CODE.cast(Long.class))) + List> selectFields = new ArrayList<>(); + selectFields.addAll(LOCATION_ALIAS_FIELDS_NEW_VIEW); + selectFields.addAll(List.of(pagedRef.fields())); + selectFields.remove(pagedRef.field("LOCATION_ID", String.class)); + query = dsl.select(asterisk()).from(dsl.select(selectFields) + .from(pagedRef) + .leftJoin(values) + .on(pagedRef.field(LOCATION_LEVEL_CODE, Long.class).eq(values.LOCATION_LEVEL_CODE.cast(Long.class))) .leftJoin(aliasView) - .on(ref.OFFICE_ID.eq(aliasView.DB_OFFICE_ID)) - .and(ref.LOCATION_ID.eq(aliasView.LOCATION_ID)) - .and(ref.LOCATION_CODE.eq(aliasView.LOCATION_CODE.cast(Long.class))) - .where(whereCondition)) - .orderBy(field("OFFICE_ID"), field("LOCATION_LEVEL_ID"), - field("LOCATION_LEVEL_DATE"), field("CALENDAR_OFFSET"), field("TIME_OFFSET") - ) - .offset(offset) - .limit(pageSize); + .on(aliasView.DB_OFFICE_ID.eq(pagedRef.field(OFFICE_ID, String.class))) + .and(aliasView.LOCATION_ID.eq(pagedRef.field("LOCATION_ID", String.class))) + .and(aliasView.LOCATION_CODE.eq(pagedRef.field("LOCATION_CODE", BigDecimal.class)))) + .orderBy(field(OFFICE_ID), field(LOCATION_LEVEL_ID), + field(LOCATION_LEVEL_DATE), field(CALENDAR_OFFSET), field(TIME_OFFSET) + ); } else { - query = dsl.selectDistinct(LOCATION_LEVEL_FIELDS_NEW_VIEW) - .from(ref) - .join(values) - .on(ref.LOCATION_LEVEL_CODE.eq(values.LOCATION_LEVEL_CODE.cast(Long.class))) - .where(whereCondition) - .orderBy(DSL.upper(ref.OFFICE_ID), DSL.upper(ref.LOCATION_LEVEL_ID), - ref.LOCATION_LEVEL_DATE, values.CALENDAR_OFFSET, values.TIME_OFFSET - ) - .offset(offset) - .limit(pageSize); + List> selectFields = new ArrayList<>(); + selectFields.addAll(LOCATION_LEVEL_FIELDS_NEW_VIEW); + selectFields.addAll(List.of(pagedRef.fields())); + query = dsl.select(selectFields) + .from(pagedRef) + .leftJoin(values) + .on(pagedRef.field(LOCATION_LEVEL_CODE, Long.class).eq(values.LOCATION_LEVEL_CODE.cast(Long.class))) + .orderBy(DSL.upper(pagedRef.field(OFFICE_ID, String.class)), + DSL.upper(pagedRef.field(LOCATION_LEVEL_ID, String.class)), + pagedRef.field(LOCATION_LEVEL_DATE), values.CALENDAR_OFFSET, values.TIME_OFFSET + ); } if (!totalSet) { - total = dsl.fetchCount(dsl.selectDistinct(ref.OFFICE_ID, ref.LOCATION_LEVEL_ID, ref.LOCATION_LEVEL_DATE, - values.CALENDAR_OFFSET, values.TIME_OFFSET) + total = dsl.fetchCount(dsl.select(ref.OFFICE_ID, ref.LOCATION_LEVEL_ID, ref.LOCATION_LEVEL_DATE) .from(ref) - .fullOuterJoin(values) - .on(ref.LOCATION_LEVEL_CODE.eq(values.LOCATION_LEVEL_CODE.cast(Long.class))) .where(whereCondition) ); } - final SelectLimitPercentAfterOffsetStep queryFinal = query; + final var queryFinal = query; logger.atFine().log("getLocationLevels query: %s", lazy(() -> queryFinal.getSQL(ParamType.INLINED))); @@ -355,7 +371,7 @@ public LocationLevels getLocationLevels(String cursor, int pageSize, .and(mapping.getLocationLevelId(TABLE_ALIAS1, null).eq(virtView.LOCATION_LEVEL_ID))) .where(whereCondition).asTable(TABLE_ALIAS2)) .orderBy(mapping.getOfficeId(null, null), mapping.getLocationLevelId(null, null), - field("LEVEL_DATE"), field("CALENDAR_OFFSET"), field("TIME_OFFSET"), + field("LEVEL_DATE"), field(CALENDAR_OFFSET), field(TIME_OFFSET), mapping.getLocationLevelId(TABLE_ALIAS2, null), field("EFFECTIVE_DATE_UTC") ) .offset(offset) @@ -377,8 +393,7 @@ public LocationLevels getLocationLevels(String cursor, int pageSize, if (!totalSet) { total = dsl.fetchCount(dsl.selectDistinct(view.OFFICE_ID, view.LOCATION_LEVEL_ID, view.LEVEL_DATE, - view.CALENDAR_OFFSET, view.TIME_OFFSET, virtView.OFFICE_ID, virtView.LOCATION_LEVEL_ID, - virtView.EFFECTIVE_DATE_UTC) + virtView.OFFICE_ID, virtView.LOCATION_LEVEL_ID, virtView.EFFECTIVE_DATE_UTC) .from(view) .fullOuterJoin(virtView) .on(view.LOCATION_LEVEL_CODE.eq(virtView.LOCATION_LEVEL_CODE)) @@ -1104,7 +1119,7 @@ private void parseLevelsNewView(Record r, Map getOfficeId(String prefix, Table table) { - return table.field(DSL.name(prefix, "OFFICE_ID"), String.class); + return table.field(DSL.name(prefix, OFFICE_ID), String.class); } @Override public Field getLocationLevelId(String prefix, Table table) { - return table.field(DSL.name(prefix, "LOCATION_LEVEL_ID"), String.class); + return table.field(DSL.name(prefix, LOCATION_LEVEL_ID), String.class); } @Override @@ -1534,19 +1531,19 @@ public Field getUnitSystem() { private static final class AliasedLocationFieldMapping implements LocationFieldMapping { @Override public Field getOfficeId(String prefix, Table table) { - String field = prefix != null ? prefix + ".OFFICE_ID" : "OFFICE_ID"; + String field = prefix != null ? prefix + ".OFFICE_ID" : OFFICE_ID; return field(field, String.class); } @Override public Field getLocationLevelId(String prefix, Table table) { - String field = prefix != null ? prefix + ".LOCATION_LEVEL_ID" : "LOCATION_LEVEL_ID"; + String field = prefix != null ? prefix + ".LOCATION_LEVEL_ID" : LOCATION_LEVEL_ID; return field(field, String.class); } @Override public Field getLocationLevelCode() { - return field(String.format("%s.%s", TABLE_ALIAS1, "LOCATION_LEVEL_CODE"), Long.class); + return field(String.format("%s.%s", TABLE_ALIAS1, LOCATION_LEVEL_CODE), Long.class); } @Override @@ -1863,12 +1860,12 @@ public Field getInterpolate() { @Override public Field getCalendarOffset() { - return DSL.field(DSL.name(TABLE_ALIAS2, "CALENDAR_OFFSET"), String.class); + return DSL.field(DSL.name(TABLE_ALIAS2, CALENDAR_OFFSET), String.class); } @Override public Field getTimeOffset() { - return DSL.field(DSL.name(TABLE_ALIAS2, "TIME_OFFSET"), String.class); + return DSL.field(DSL.name(TABLE_ALIAS2, TIME_OFFSET), String.class); } @Override diff --git a/cwms-data-api/src/test/java/cwms/cda/api/LevelsControllerTestIT.java b/cwms-data-api/src/test/java/cwms/cda/api/LevelsControllerTestIT.java index 5b1374184..c725013cb 100644 --- a/cwms-data-api/src/test/java/cwms/cda/api/LevelsControllerTestIT.java +++ b/cwms-data-api/src/test/java/cwms/cda/api/LevelsControllerTestIT.java @@ -1946,7 +1946,7 @@ void testRetrieveAllSeasonalLevel() throws Exception { .body("levels.size()", is(1)) .body("levels[0].expiration-date", equalTo(levelDate.plusYears(50).toInstant().toString())) .body("levels[0].seasonal-values.size()", is(numValues)) - .body("total", is(numValues)); + .body("total", is(1)); } @Test