Search in sources :

Example 1 with CatalogEntry

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);
}
Also used : Condition(org.jooq.Condition) AV_TSV_DQU(usace.cwms.db.jooq.codegen.tables.AV_TSV_DQU) Arrays(java.util.Arrays) Connection(java.sql.Connection) DSL(org.jooq.impl.DSL) Date(java.util.Date) ZonedDateTime(java.time.ZonedDateTime) CwmsDbTs(usace.cwms.db.dao.ifc.ts.CwmsDbTs) DSL.field(org.jooq.impl.DSL.field) Table(org.jooq.Table) AV_TS_GRP_ASSGN(usace.cwms.db.jooq.codegen.tables.AV_TS_GRP_ASSGN) DSL.condition(org.jooq.impl.DSL.condition) Condition(org.jooq.Condition) SQL(org.jooq.SQL) NotFoundException(cwms.radar.api.NotFoundException) BigDecimal(java.math.BigDecimal) Record3(org.jooq.Record3) Record1(org.jooq.Record1) Duration(java.time.Duration) JoinType(javax.persistence.criteria.JoinType) DSLContext(org.jooq.DSLContext) BigInteger(java.math.BigInteger) ZoneOffset(java.time.ZoneOffset) Record8(org.jooq.Record8) CatalogEntry(cwms.radar.data.dto.catalog.CatalogEntry) TimeSeries(cwms.radar.data.dto.TimeSeries) TsvId(cwms.radar.data.dto.TsvId) Timestamp(java.sql.Timestamp) Collection(java.util.Collection) DSL.partitionBy(org.jooq.impl.DSL.partitionBy) Field(org.jooq.Field) Instant(java.time.Instant) Logger(java.util.logging.Logger) Result(org.jooq.Result) Collectors(java.util.stream.Collectors) JAXBException(javax.xml.bind.JAXBException) ZoneId(java.time.ZoneId) CwmsDbServiceLookup(usace.cwms.db.dao.util.services.CwmsDbServiceLookup) List(java.util.List) Optional(java.util.Optional) NotNull(org.jetbrains.annotations.NotNull) SelectSelectStep(org.jooq.SelectSelectStep) CWMS_LOC_PACKAGE(usace.cwms.db.jooq.codegen.packages.CWMS_LOC_PACKAGE) TimeseriesCatalogEntry(cwms.radar.data.dto.catalog.TimeseriesCatalogEntry) DSL.count(org.jooq.impl.DSL.count) CWMS_TS_PACKAGE(usace.cwms.db.jooq.codegen.packages.CWMS_TS_PACKAGE) org.jooq.impl(org.jooq.impl) LocalDateTime(java.time.LocalDateTime) HashMap(java.util.HashMap) CWMS_UTIL_PACKAGE(usace.cwms.db.jooq.codegen.packages.CWMS_UTIL_PACKAGE) ParamType(org.jooq.conf.ParamType) TemporalAccessor(java.time.temporal.TemporalAccessor) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) SelectConditionStep(org.jooq.SelectConditionStep) AV_LOC(usace.cwms.db.jooq.codegen.tables.AV_LOC) SQLException(java.sql.SQLException) TsvDquId(cwms.radar.data.dto.TsvDquId) JAXBContext(javax.xml.bind.JAXBContext) SelectHavingStep(org.jooq.SelectHavingStep) AV_TS_EXTENTS_UTC(usace.cwms.db.jooq.codegen.tables.AV_TS_EXTENTS_UTC.AV_TS_EXTENTS_UTC) RecentValue(cwms.radar.data.dto.RecentValue) Unmarshaller(javax.xml.bind.Unmarshaller) Record(org.jooq.Record) VerticalDatumInfo(cwms.radar.data.dto.VerticalDatumInfo) DSL.asterisk(org.jooq.impl.DSL.asterisk) Iterator(java.util.Iterator) CwmsDTOPaginated(cwms.radar.data.dto.CwmsDTOPaginated) AV_TSV(usace.cwms.db.jooq.codegen.tables.AV_TSV) TimeSeriesExtents(cwms.radar.data.dto.TimeSeriesExtents) StringReader(java.io.StringReader) CWMS_ROUNDING_PACKAGE(usace.cwms.db.jooq.codegen.packages.CWMS_ROUNDING_PACKAGE) Catalog(cwms.radar.data.dto.Catalog) Tsv(cwms.radar.data.dto.Tsv) DSL.max(org.jooq.impl.DSL.max) DateTimeFormatter(java.time.format.DateTimeFormatter) AV_LOC2(usace.cwms.db.jooq.codegen.tables.AV_LOC2) TsvDqu(cwms.radar.data.dto.TsvDqu) DateUtils(cwms.radar.helpers.DateUtils) SelectQuery(org.jooq.SelectQuery) AV_CWMS_TS_ID2(usace.cwms.db.jooq.codegen.tables.AV_CWMS_TS_ID2.AV_CWMS_TS_ID2) Collections(java.util.Collections) AV_CWMS_TS_ID2(usace.cwms.db.jooq.codegen.tables.AV_CWMS_TS_ID2) HashMap(java.util.HashMap) Catalog(cwms.radar.data.dto.Catalog) TimeseriesCatalogEntry(cwms.radar.data.dto.catalog.TimeseriesCatalogEntry) TimeSeriesExtents(cwms.radar.data.dto.TimeSeriesExtents) Record1(org.jooq.Record1)

Example 2 with CatalogEntry

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);
}
Also used : java.util(java.util) LocationCatalogEntry(cwms.radar.data.dto.catalog.LocationCatalogEntry) DSL(org.jooq.impl.DSL) Nation(cwms.radar.api.enums.Nation) Table(org.jooq.Table) Feature(org.geojson.Feature) Condition(org.jooq.Condition) NotFoundException(cwms.radar.api.NotFoundException) SelectConditionStep(org.jooq.SelectConditionStep) BigDecimal(java.math.BigDecimal) Unit(cwms.radar.api.enums.Unit) FeatureCollection(org.geojson.FeatureCollection) Record1(org.jooq.Record1) DSLContext(org.jooq.DSLContext) CatalogEntry(cwms.radar.data.dto.catalog.CatalogEntry) AV_LOC_GRP_ASSGN(usace.cwms.db.jooq.codegen.tables.AV_LOC_GRP_ASSGN.AV_LOC_GRP_ASSGN) Record(org.jooq.Record) DataAccessException(org.jooq.exception.DataAccessException) RequiredFieldException(cwms.radar.api.errors.RequiredFieldException) AV_LOC(usace.cwms.db.jooq.codegen.tables.AV_LOC.AV_LOC) CwmsDTOPaginated(cwms.radar.data.dto.CwmsDTOPaginated) CwmsDbLoc(usace.cwms.db.dao.ifc.loc.CwmsDbLoc) Location(cwms.radar.data.dto.Location) IOException(java.io.IOException) Logger(java.util.logging.Logger) Collectors(java.util.stream.Collectors) ZoneId(java.time.ZoneId) CwmsDbServiceLookup(usace.cwms.db.dao.util.services.CwmsDbServiceLookup) AV_LOC_ALIAS(usace.cwms.db.jooq.codegen.tables.AV_LOC_ALIAS.AV_LOC_ALIAS) Catalog(cwms.radar.data.dto.Catalog) Point(org.geojson.Point) LocationAlias(cwms.radar.data.dto.catalog.LocationAlias) CWMS_LOC_PACKAGE(usace.cwms.db.jooq.codegen.packages.CWMS_LOC_PACKAGE) AV_LOC(usace.cwms.db.jooq.codegen.tables.AV_LOC.AV_LOC) Record(org.jooq.Record) Record1(org.jooq.Record1) Condition(org.jooq.Condition) LocationAlias(cwms.radar.data.dto.catalog.LocationAlias) Point(org.geojson.Point) Catalog(cwms.radar.data.dto.Catalog) LocationCatalogEntry(cwms.radar.data.dto.catalog.LocationCatalogEntry)

Aggregations

NotFoundException (cwms.radar.api.NotFoundException)2 Catalog (cwms.radar.data.dto.Catalog)2 CwmsDTOPaginated (cwms.radar.data.dto.CwmsDTOPaginated)2 CatalogEntry (cwms.radar.data.dto.catalog.CatalogEntry)2 BigDecimal (java.math.BigDecimal)2 ZoneId (java.time.ZoneId)2 Logger (java.util.logging.Logger)2 Collectors (java.util.stream.Collectors)2 Condition (org.jooq.Condition)2 DSLContext (org.jooq.DSLContext)2 Record (org.jooq.Record)2 Record1 (org.jooq.Record1)2 SelectConditionStep (org.jooq.SelectConditionStep)2 Table (org.jooq.Table)2 DSL (org.jooq.impl.DSL)2 CwmsDbServiceLookup (usace.cwms.db.dao.util.services.CwmsDbServiceLookup)2 CWMS_LOC_PACKAGE (usace.cwms.db.jooq.codegen.packages.CWMS_LOC_PACKAGE)2 Nation (cwms.radar.api.enums.Nation)1 Unit (cwms.radar.api.enums.Unit)1 RequiredFieldException (cwms.radar.api.errors.RequiredFieldException)1