use of cwms.radar.data.dto.catalog.CatalogEntry in project cwms-radar-api by USACE.
the class TimeSeriesDaoImpl method getTimeSeriesCatalog.
@Override
public Catalog getTimeSeriesCatalog(String page, int pageSize, Optional<String> office, String idLike, String locCategoryLike, String locGroupLike, String tsCategoryLike, String tsGroupLike) {
int total = 0;
String tsCursor = "*";
if (page == null || page.isEmpty()) {
Condition condition = AV_CWMS_TS_ID2.CWMS_TS_ID.upper().likeRegex(idLike.toUpperCase()).and(AV_CWMS_TS_ID2.ALIASED_ITEM.isNull());
if (office.isPresent()) {
condition = condition.and(AV_CWMS_TS_ID2.DB_OFFICE_ID.upper().eq(office.get().toUpperCase()));
}
if (locCategoryLike != null) {
condition.and(AV_CWMS_TS_ID2.LOC_ALIAS_CATEGORY.upper().likeRegex(locCategoryLike.toUpperCase()));
}
if (locGroupLike != null) {
condition.and(AV_CWMS_TS_ID2.LOC_ALIAS_GROUP.upper().likeRegex(locGroupLike.toUpperCase()));
}
if (tsCategoryLike != null) {
condition.and(AV_CWMS_TS_ID2.TS_ALIAS_CATEGORY.upper().likeRegex(tsCategoryLike.toUpperCase()));
}
if (tsGroupLike != null) {
condition.and(AV_CWMS_TS_ID2.TS_ALIAS_GROUP.upper().likeRegex(tsGroupLike.toUpperCase()));
}
SelectConditionStep<Record1<Integer>> count = dsl.select(count(asterisk())).from(AV_CWMS_TS_ID2).where(condition);
total = count.fetchOne().value1();
} else {
logger.fine("getting non-default page");
// get totally from page
String[] parts = CwmsDTOPaginated.decodeCursor(page, "|||");
logger.fine("decoded cursor: " + String.join("|||", parts));
for (String p : parts) {
logger.finest(p);
}
if (parts.length > 1) {
tsCursor = parts[0].split("/")[1];
total = Integer.parseInt(parts[1]);
}
}
SelectQuery<?> primaryDataQuery = dsl.selectQuery();
primaryDataQuery.addSelect(AV_CWMS_TS_ID2.DB_OFFICE_ID);
primaryDataQuery.addSelect(AV_CWMS_TS_ID2.CWMS_TS_ID);
primaryDataQuery.addSelect(AV_CWMS_TS_ID2.TS_CODE);
primaryDataQuery.addSelect(AV_CWMS_TS_ID2.UNIT_ID);
primaryDataQuery.addSelect(AV_CWMS_TS_ID2.INTERVAL_ID);
primaryDataQuery.addSelect(AV_CWMS_TS_ID2.INTERVAL_UTC_OFFSET);
if (this.getDbVersion() >= Dao.CWMS_21_1_1) {
primaryDataQuery.addSelect(AV_CWMS_TS_ID2.TIME_ZONE_ID);
}
primaryDataQuery.addFrom(AV_CWMS_TS_ID2);
primaryDataQuery.addConditions(AV_CWMS_TS_ID2.ALIASED_ITEM.isNull());
// add the regexp_like clause.
primaryDataQuery.addConditions(AV_CWMS_TS_ID2.CWMS_TS_ID.upper().likeRegex(idLike.toUpperCase()));
if (office.isPresent()) {
primaryDataQuery.addConditions(AV_CWMS_TS_ID2.DB_OFFICE_ID.upper().eq(office.get().toUpperCase()));
}
if (locCategoryLike != null) {
primaryDataQuery.addConditions(AV_CWMS_TS_ID2.LOC_ALIAS_CATEGORY.upper().likeRegex(locCategoryLike.toUpperCase()));
}
if (locGroupLike != null) {
primaryDataQuery.addConditions(AV_CWMS_TS_ID2.LOC_ALIAS_GROUP.upper().likeRegex(locGroupLike.toUpperCase()));
}
if (tsCategoryLike != null) {
primaryDataQuery.addConditions(AV_CWMS_TS_ID2.TS_ALIAS_CATEGORY.upper().likeRegex(tsCategoryLike.toUpperCase()));
}
if (tsGroupLike != null) {
primaryDataQuery.addConditions(AV_CWMS_TS_ID2.TS_ALIAS_GROUP.upper().likeRegex(tsGroupLike.toUpperCase()));
}
primaryDataQuery.addConditions(AV_CWMS_TS_ID2.CWMS_TS_ID.upper().gt(tsCursor));
primaryDataQuery.addOrderBy(AV_CWMS_TS_ID2.CWMS_TS_ID);
Table<?> dataTable = primaryDataQuery.asTable("data");
// query.addConditions(field("rownum").lessOrEqual(pageSize));
// query.addConditions(condition("rownum < 500"));
SelectQuery<?> limitQuery = dsl.selectQuery();
// limitQuery.addSelect(field("rownum"));
limitQuery.addSelect(dataTable.fields());
// .limit(pageSize);
limitQuery.addFrom(dataTable);
limitQuery.addConditions(field("rownum").lessOrEqual(pageSize));
Table<?> limitTable = limitQuery.asTable("limiter");
SelectQuery<?> overallQuery = dsl.selectQuery();
overallQuery.addSelect(limitTable.fields());
overallQuery.addSelect(AV_TS_EXTENTS_UTC.VERSION_TIME);
overallQuery.addSelect(AV_TS_EXTENTS_UTC.EARLIEST_TIME);
overallQuery.addSelect(AV_TS_EXTENTS_UTC.LATEST_TIME);
overallQuery.addFrom(limitTable);
overallQuery.addJoin(AV_TS_EXTENTS_UTC, org.jooq.JoinType.LEFT_OUTER_JOIN, condition("\"CWMS_20\".\"AV_TS_EXTENTS_UTC\".\"TS_CODE\" = " + field("\"limiter\".\"TS_CODE\"")));
logger.info(() -> overallQuery.getSQL(ParamType.INLINED));
Result<?> result = overallQuery.fetch();
HashMap<String, TimeseriesCatalogEntry.Builder> tsIdExtentMap = new HashMap<>();
result.forEach(row -> {
String tsId = row.get(AV_CWMS_TS_ID2.CWMS_TS_ID);
if (!tsIdExtentMap.containsKey(tsId)) {
TimeseriesCatalogEntry.Builder builder = new TimeseriesCatalogEntry.Builder().officeId(row.get(AV_CWMS_TS_ID2.DB_OFFICE_ID)).cwmsTsId(row.get(AV_CWMS_TS_ID2.CWMS_TS_ID)).units(row.get(AV_CWMS_TS_ID2.UNIT_ID)).interval(row.get(AV_CWMS_TS_ID2.INTERVAL_ID)).intervalOffset(row.get(AV_CWMS_TS_ID2.INTERVAL_UTC_OFFSET));
if (this.getDbVersion() > Dao.CWMS_21_1_1) {
builder.timeZone(row.get("TIME_ZONE_ID", String.class));
}
tsIdExtentMap.put(tsId, builder);
}
if (row.get(AV_TS_EXTENTS_UTC.EARLIEST_TIME) != null) {
// tsIdExtentMap.get(tsId)
TimeSeriesExtents extents = new TimeSeriesExtents(row.get(AV_TS_EXTENTS_UTC.VERSION_TIME), row.get(AV_TS_EXTENTS_UTC.EARLIEST_TIME), row.get(AV_TS_EXTENTS_UTC.LATEST_TIME));
tsIdExtentMap.get(tsId).withExtent(extents);
}
});
List<? extends CatalogEntry> entries = tsIdExtentMap.entrySet().stream().sorted((left, right) -> left.getKey().compareTo(right.getKey())).map(e -> {
return e.getValue().build();
}).collect(Collectors.toList());
return new Catalog(tsCursor, total, pageSize, entries);
}
use of cwms.radar.data.dto.catalog.CatalogEntry in project cwms-radar-api by USACE.
the class LocationsDaoImpl method getLocationCatalog.
@Override
public Catalog getLocationCatalog(String cursor, int pageSize, String unitSystem, Optional<String> office, String idLike, String categoryLike, String groupLike) {
int total = 0;
String locCursor = "*";
if (cursor == null || cursor.isEmpty()) {
Condition condition = buildCatalogWhere(unitSystem, office, idLike, categoryLike, groupLike);
SelectConditionStep<Record1<Integer>> count = dsl.select(count(asterisk())).from(AV_LOC).innerJoin(AV_LOC_GRP_ASSGN).on(AV_LOC.LOCATION_ID.eq(AV_LOC_GRP_ASSGN.LOCATION_ID)).where(condition);
total = count.fetchOne().value1().intValue();
} else {
// get totally from page
String[] parts = CwmsDTOPaginated.decodeCursor(cursor, "|||");
if (parts.length > 1) {
locCursor = parts[0].split("\\/")[1];
total = Integer.parseInt(parts[1]);
}
}
Condition condition = buildCatalogWhere(unitSystem, office, idLike, categoryLike, groupLike).and(AV_LOC.LOCATION_ID.upper().greaterThan(locCursor));
SelectConditionStep<Record1<String>> tmp = dsl.select(AV_LOC.LOCATION_ID).from(AV_LOC).innerJoin(AV_LOC_GRP_ASSGN).on(AV_LOC.LOCATION_ID.eq(AV_LOC_GRP_ASSGN.LOCATION_ID)).where(condition);
Table<?> forLimit = tmp.orderBy(AV_LOC.LOCATION_ID).limit(pageSize).asTable();
SelectConditionStep<Record> query = dsl.select(AV_LOC.asterisk(), AV_LOC_GRP_ASSGN.asterisk()).from(AV_LOC).innerJoin(forLimit).on(forLimit.field(AV_LOC.LOCATION_ID).eq(AV_LOC.LOCATION_ID)).leftJoin(AV_LOC_GRP_ASSGN).on(AV_LOC_GRP_ASSGN.LOCATION_ID.eq(AV_LOC.LOCATION_ID)).where(condition);
query.orderBy(AV_LOC.LOCATION_ID);
// logger.info( () -> query.getSQL(ParamType.INLINED));
HashMap<usace.cwms.db.jooq.codegen.tables.records.AV_LOC, ArrayList<usace.cwms.db.jooq.codegen.tables.records.AV_LOC_ALIAS>> theMap = new HashMap<>();
query.fetch().forEach(row -> {
usace.cwms.db.jooq.codegen.tables.records.AV_LOC loc = row.into(AV_LOC);
if (!theMap.containsKey(loc)) {
theMap.put(loc, new ArrayList<>());
}
usace.cwms.db.jooq.codegen.tables.records.AV_LOC_ALIAS alias = row.into(AV_LOC_ALIAS);
usace.cwms.db.jooq.codegen.tables.records.AV_LOC_GRP_ASSGN group = row.into(AV_LOC_GRP_ASSGN);
if (group.getALIAS_ID() != null) {
theMap.get(loc).add(alias);
}
});
List<? extends CatalogEntry> entries = theMap.entrySet().stream().sorted((left, right) -> (left.getKey().getLOCATION_ID().compareTo(right.getKey().getBASE_LOCATION_ID()))).map(e -> new LocationCatalogEntry(e.getKey().getDB_OFFICE_ID(), e.getKey().getLOCATION_ID(), e.getKey().getNEAREST_CITY(), e.getKey().getPUBLIC_NAME(), e.getKey().getLONG_NAME(), e.getKey().getDESCRIPTION(), e.getKey().getLOCATION_KIND_ID(), e.getKey().getLOCATION_TYPE(), e.getKey().getTIME_ZONE_NAME(), e.getKey().getLATITUDE() != null ? e.getKey().getLATITUDE().doubleValue() : null, e.getKey().getLONGITUDE() != null ? e.getKey().getLONGITUDE().doubleValue() : null, e.getKey().getPUBLISHED_LATITUDE() != null ? e.getKey().getPUBLISHED_LATITUDE().doubleValue() : null, e.getKey().getPUBLISHED_LONGITUDE() != null ? e.getKey().getPUBLISHED_LONGITUDE().doubleValue() : null, e.getKey().getHORIZONTAL_DATUM(), e.getKey().getELEVATION(), e.getKey().getUNIT_ID(), e.getKey().getVERTICAL_DATUM(), e.getKey().getNATION_ID(), e.getKey().getSTATE_INITIAL(), e.getKey().getCOUNTY_NAME(), e.getKey().getBOUNDING_OFFICE_ID(), e.getKey().getMAP_LABEL(), e.getKey().getACTIVE_FLAG().equalsIgnoreCase("T"), e.getValue().stream().map(a -> new LocationAlias(a.getCATEGORY_ID() + "-" + a.getGROUP_ID(), a.getALIAS_ID())).collect(Collectors.toList()))).collect(Collectors.toList());
return new Catalog(locCursor, total, pageSize, entries);
}
Aggregations