Search in sources :

Example 1 with Catalog

use of cwms.radar.data.dto.Catalog 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 Catalog

use of cwms.radar.data.dto.Catalog 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)

Example 3 with Catalog

use of cwms.radar.data.dto.Catalog in project cwms-radar-api by USACE.

the class TimeseriesCatalogEntryTest method test_xml_serialization_earliest.

@Test
void test_xml_serialization_earliest() {
    CatalogEntry entry = buildEntry();
    Catalog cat = new Catalog(null, 1, 10, new ArrayList<CatalogEntry>() {

        {
            add(entry);
        }
    });
    ContentType contentType = Formats.parseHeader(Formats.XML);
    String xml = Formats.format(contentType, cat);
    assertNotNull(xml);
    assertFalse(xml.isEmpty());
    XmlPath path = XmlPath.from(xml);
    assertThat(path.getString("catalog.entries.entry.@name"), equalTo("Barren-Lake.Elev.Inst.0.0.USGS-raw"));
    assertThat(path.getString("catalog.entries.entry.units"), equalTo("m"));
    assertThat(path.getInt("catalog.entries.entry.interval"), equalTo(0));
    assertThat(path.getLong("catalog.entries.entry.interval-offset"), equalTo(-2147483648L));
    assertThat(path.getString("catalog.entries.entry.time-zone"), equalTo("US/Central"));
    Object tmp = path.get("catalog.entries.entry.extents");
    System.out.println(tmp.toString());
    assertThat(path.getString("catalog.entries.entry.extents.extents.earliest-time"), equalTo("2017-07-27T05:00:00Z"));
    assertThat(path.getString("catalog.entries.entry.extents.extents.latest-time"), equalTo("2017-11-24T22:30:00Z"));
}
Also used : XmlPath(io.restassured.path.xml.XmlPath) ContentType(cwms.radar.formatters.ContentType) Catalog(cwms.radar.data.dto.Catalog) Test(org.junit.jupiter.api.Test)

Example 4 with Catalog

use of cwms.radar.data.dto.Catalog in project cwms-radar-api by USACE.

the class CatalogController method getOne.

@OpenApi(queryParams = { @OpenApiParam(name = "page", description = "This end point can return a lot of data, this identifies where in the request you are."), @OpenApiParam(name = "pageSize", type = Integer.class, description = "How many entires per page returned. Default 500."), @OpenApiParam(name = "unitSystem", type = UnitSystem.class, description = UnitSystem.DESCRIPTION), @OpenApiParam(name = "office", description = "3-4 letter office name representing the district you want to isolate data to."), @OpenApiParam(name = "like", description = "Posix regular expression matching against the id"), @OpenApiParam(name = "timeseriesCategoryLike", description = "Posix regular expression matching against the timeseries category id"), @OpenApiParam(name = "timeseriesGroupLike", description = "Posix regular expression matching against the timeseries group id"), @OpenApiParam(name = "locationCategoryLike", description = "Posix regular expression matching against the location category id"), @OpenApiParam(name = "locationGroupLike", description = "Posix regular expression matching against the location group id") }, pathParams = { @OpenApiParam(name = "dataSet", required = false, type = CatalogableEndpoint.class, description = "A list of what data? E.g. Timeseries, Locations, Ratings, etc") }, responses = { @OpenApiResponse(status = "200", description = "A list of elements the data set you've selected.", content = { @OpenApiContent(from = Catalog.class, type = Formats.JSONV2), @OpenApiContent(from = Catalog.class, type = Formats.XML) }) }, tags = { TAG })
@Override
public void getOne(Context ctx, String dataSet) {
    getOneRequest.mark();
    try (final Timer.Context timeContext = getOneRequestTime.time();
        DSLContext dsl = JooqDao.getDslContext(ctx)) {
        String valDataSet = ((PolicyFactory) ctx.appAttribute("PolicyFactory")).sanitize(dataSet);
        String cursor = ctx.queryParamAsClass("cursor", String.class).getOrDefault(ctx.queryParamAsClass("page", String.class).getOrDefault(""));
        int pageSize = ctx.queryParamAsClass("pageSize", Integer.class).getOrDefault(ctx.queryParamAsClass("pagesize", Integer.class).getOrDefault(defaultPageSize));
        String unitSystem = ctx.queryParamAsClass("unitSystem", String.class).getOrDefault(UnitSystem.SI.getValue());
        Optional<String> office = Optional.ofNullable(ctx.queryParamAsClass("office", String.class).allowNullable().check(Office::validOfficeCanNull, "Invalid office provided").get());
        String like = ctx.queryParamAsClass("like", String.class).getOrDefault(".*");
        String tsCategoryLike = ctx.queryParamAsClass("timeseriesCategoryLike", String.class).getOrDefault(null);
        String tsGroupLike = ctx.queryParamAsClass("timeseriesGroupLike", String.class).getOrDefault(null);
        String locCategoryLike = ctx.queryParamAsClass("locationCategoryLike", String.class).getOrDefault(null);
        String locGroupLike = ctx.queryParamAsClass("locationGroupLike", String.class).getOrDefault(null);
        String acceptHeader = ctx.header("Accept");
        ContentType contentType = Formats.parseHeaderAndQueryParm(acceptHeader, null);
        Catalog cat = null;
        if ("timeseries".equalsIgnoreCase(valDataSet)) {
            TimeSeriesDao tsDao = new TimeSeriesDaoImpl(dsl);
            cat = tsDao.getTimeSeriesCatalog(cursor, pageSize, office, like, locCategoryLike, locGroupLike, tsCategoryLike, tsGroupLike);
        } else if ("locations".equalsIgnoreCase(valDataSet)) {
            LocationsDao dao = new LocationsDaoImpl(dsl);
            cat = dao.getLocationCatalog(cursor, pageSize, unitSystem, office, like, locCategoryLike, locGroupLike);
        }
        if (cat != null) {
            String data = Formats.format(contentType, cat);
            ctx.result(data).contentType(contentType.toString());
            requestResultSize.update(data.length());
        } else {
            final RadarError re = new RadarError("Cannot create catalog of requested information");
            logger.info(() -> re.toString() + "with url:" + ctx.fullUrl());
            ctx.json(re).status(HttpCode.NOT_FOUND);
        }
    }
}
Also used : Office(cwms.radar.data.dto.Office) PolicyFactory(org.owasp.html.PolicyFactory) ContentType(cwms.radar.formatters.ContentType) DSLContext(org.jooq.DSLContext) LocationsDao(cwms.radar.data.dao.LocationsDao) Catalog(cwms.radar.data.dto.Catalog) LocationsDaoImpl(cwms.radar.data.dao.LocationsDaoImpl) RadarError(cwms.radar.api.errors.RadarError) Timer(com.codahale.metrics.Timer) TimeSeriesDaoImpl(cwms.radar.data.dao.TimeSeriesDaoImpl) TimeSeriesDao(cwms.radar.data.dao.TimeSeriesDao) OpenApi(io.javalin.plugin.openapi.annotations.OpenApi)

Example 5 with Catalog

use of cwms.radar.data.dto.Catalog in project cwms-radar-api by USACE.

the class TimeseriesCatalogEntryTest method test_json_serialization_earliest.

@Test
void test_json_serialization_earliest() {
    CatalogEntry entry = buildEntry();
    Catalog cat = new Catalog(null, 1, 10, new ArrayList<CatalogEntry>() {

        {
            add(entry);
        }
    });
    ContentType contentType = Formats.parseHeader(Formats.JSONV2);
    String json = Formats.format(contentType, cat);
    assertNotNull(json);
    assertFalse(json.isEmpty());
    JsonPath path = JsonPath.from(json);
    assertThat(path.getString("entries[0].name"), equalTo("Barren-Lake.Elev.Inst.0.0.USGS-raw"));
    assertThat(path.getString("entries[0].units"), equalTo("m"));
    assertThat(path.getInt("entries[0].interval"), equalTo(0));
    assertThat(path.getLong("entries[0].interval-offset"), equalTo(-2147483648L));
    assertThat(path.getString("entries[0].time-zone"), equalTo("US/Central"));
    assertThat(path.getString("entries[0].extents[0].earliest-time"), equalTo("2017-07-27T05:00:00Z"));
    assertThat(path.getString("entries[0].extents[0].latest-time"), equalTo("2017-11-24T22:30:00Z"));
}
Also used : ContentType(cwms.radar.formatters.ContentType) JsonPath(io.restassured.path.json.JsonPath) Catalog(cwms.radar.data.dto.Catalog) Test(org.junit.jupiter.api.Test)

Aggregations

Catalog (cwms.radar.data.dto.Catalog)5 ContentType (cwms.radar.formatters.ContentType)3 DSLContext (org.jooq.DSLContext)3 NotFoundException (cwms.radar.api.NotFoundException)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 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 Timer (com.codahale.metrics.Timer)1 Nation (cwms.radar.api.enums.Nation)1