Skip to content

Commit

Permalink
Updated LocationGroupDao and TimeSeriesGroupDao with group and catego…
Browse files Browse the repository at this point in the history
…ry office id parameters
  • Loading branch information
zack-rma committed Nov 13, 2024
1 parent 4ffb43f commit 835c5c1
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 53 deletions.
2 changes: 2 additions & 0 deletions cwms-data-api/src/main/java/cwms/cda/api/Controllers.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ public final class Controllers {
public static final String SIZE = "size";

public static final String OFFICE = "office";
public static final String CATEGORY_OFFICE_ID = "category-office-id";
public static final String GROUP_OFFICE_ID = "group-office-id";
public static final String UNIT = "unit";
public static final String COUNT = "count";
public static final String TIME = "time";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,15 +135,20 @@ Boolean.class, false, metrics, name(LocationGroupController.class.getName(),
@OpenApi(
pathParams = {
@OpenApiParam(name = GROUP_ID, required = true, description = "Specifies "
+ "the location_group whose data is to be included in the response")
+ "the location_group whose data is to be included in the response")
},
queryParams = {
@OpenApiParam(name = OFFICE, required = true, description = "Specifies the "
+ "owning office of the location group whose data is to be included "
+ "in the response."),
+ "owning office of the location group whose data is to be included "
+ "in the response."),
@OpenApiParam(name = GROUP_OFFICE_ID, required = true, description = "Specifies the "
+ "owning office of the location group whose data is to be included in the response."),
@OpenApiParam(name = CATEGORY_OFFICE_ID, required = true, description = "Specifies the "
+ "owning office of the category the location group belongs to "
+ "whose data is to be included in the response."),
@OpenApiParam(name = CATEGORY_ID, required = true, description = "Specifies"
+ " the category containing the location group whose data is to be "
+ "included in the response."),
+ " the category containing the location group whose data is to be "
+ "included in the response."),
},
responses = {
@OpenApiResponse(status = STATUS_200, content = {
Expand All @@ -161,14 +166,15 @@ public void getOne(@NotNull Context ctx, @NotNull String groupId) {
LocationGroupDao cdm = new LocationGroupDao(dsl);
String office = requiredParam(ctx, OFFICE);
String categoryId = requiredParam(ctx, CATEGORY_ID);

String groupOfficeId = requiredParam(ctx, GROUP_OFFICE_ID);
String categoryOfficeId = requiredParam(ctx, CATEGORY_OFFICE_ID);
String formatHeader = ctx.header(Header.ACCEPT);
String result;
ContentType contentType;
if (formatHeader != null && formatHeader.contains(Formats.GEOJSON)) {
contentType = new ContentType(Formats.GEOJSON);
FeatureCollection fc = cdm.buildFeatureCollectionForLocationGroup(office,
categoryId, groupId, "EN");
groupOfficeId, categoryOfficeId, categoryId, groupId, "EN");
ObjectMapper mapper = ctx.appAttribute("ObjectMapper");
result = mapper.writeValueAsString(fc);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ private Timer.Context markAndTime(String subject) {
+ " the assigned timeseries in the returned timeseries groups. (default: true)"),
@OpenApiParam(name = TIMESERIES_CATEGORY_LIKE, description = "Posix <a href=\"regexp.html\">regular expression</a> "
+ "matching against the timeseries category id"),
@OpenApiParam(name = CATEGORY_OFFICE_ID, description = "Specifies the owning office of the "
+ "timeseries group category"),
@OpenApiParam(name = GROUP_OFFICE_ID, description = "Specifies the owning office of the "
+ "timeseries group"),
@OpenApiParam(name = TIMESERIES_GROUP_LIKE, description = "Posix <a href=\"regexp.html\">regular expression</a> "
+ "matching against the timeseries group id")
},
Expand All @@ -102,6 +106,8 @@ public void getAll(@NotNull Context ctx) {

TimeSeriesGroupDao dao = new TimeSeriesGroupDao(dsl);
String office = ctx.queryParam(OFFICE);
String categoryOffice = ctx.queryParam(CATEGORY_OFFICE_ID);
String groupOffice = ctx.queryParam(GROUP_OFFICE_ID);

boolean includeAssigned = queryParamAsClass(ctx, new String[]{INCLUDE_ASSIGNED},
Boolean.class, true, metrics, name(TimeSeriesGroupController.class.getName(),
Expand All @@ -111,7 +117,8 @@ Boolean.class, true, metrics, name(TimeSeriesGroupController.class.getName(),
String tsGroupLike = queryParamAsClass(ctx, new String[]{TIMESERIES_GROUP_LIKE},
String.class, null, metrics, name(TimeSeriesGroupController.class.getName(), GET_ALL));

List<TimeSeriesGroup> grps = dao.getTimeSeriesGroups(office, includeAssigned, tsCategoryLike, tsGroupLike);
List<TimeSeriesGroup> grps = dao.getTimeSeriesGroups(office, categoryOffice, groupOffice,
includeAssigned, tsCategoryLike, tsGroupLike);
if (grps.isEmpty()) {
CdaError re = new CdaError("No data found for The provided office");
logger.info(() -> re + " for request " + ctx.fullUrl());
Expand Down Expand Up @@ -140,6 +147,10 @@ Boolean.class, true, metrics, name(TimeSeriesGroupController.class.getName(),
@OpenApiParam(name = OFFICE, required = true, description = "Specifies the "
+ "owning office of the timeseries group whose data is to be included"
+ " in the response."),
@OpenApiParam(name = CATEGORY_OFFICE_ID, description = "Specifies the owning office of the "
+ "timeseries group category"),
@OpenApiParam(name = GROUP_OFFICE_ID, description = "Specifies the owning office of the "
+ "timeseries group"),
@OpenApiParam(name = CATEGORY_ID, required = true, description = "Specifies"
+ " the category containing the timeseries group whose data is to be "
+ "included in the response."),
Expand All @@ -158,13 +169,15 @@ public void getOne(@NotNull Context ctx, @NotNull String groupId) {
TimeSeriesGroupDao dao = new TimeSeriesGroupDao(dsl);
String office = ctx.queryParam(OFFICE);
String categoryId = ctx.queryParam(CATEGORY_ID);
String groupOffice = ctx.queryParam(GROUP_OFFICE_ID);
String categoryOffice = ctx.queryParam(CATEGORY_OFFICE_ID);

String formatHeader = ctx.header(Header.ACCEPT);
ContentType contentType = Formats.parseHeader(formatHeader, TimeSeriesGroup.class);

TimeSeriesGroup group = null;
List<TimeSeriesGroup> timeSeriesGroups = dao.getTimeSeriesGroups(office, categoryId,
groupId);
List<TimeSeriesGroup> timeSeriesGroups = dao.getTimeSeriesGroups(office, groupOffice, categoryOffice,
categoryId, groupId);
if (timeSeriesGroups != null && !timeSeriesGroups.isEmpty()) {
if (timeSeriesGroups.size() == 1) {
group = timeSeriesGroups.get(0);
Expand Down
23 changes: 12 additions & 11 deletions cwms-data-api/src/main/java/cwms/cda/data/dao/LocationGroupDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -218,9 +218,9 @@ public List<LocationGroup> getLocationGroups(@Nullable String officeId,
/**
* Get all location groups for a given office and category,
* as well as a where clause to filter the shared_ref_location_id.
* @Param officeId The office id to use for the query.
* @Param locCategoryLike A regex to use to filter the location categories. May be null.
* @Param sharedRefLocLike A where clause to filter the shared_loc_alias_id. May be null.
* @param officeId The office id to use for the query.
* @param locCategoryLike A regex to use to filter the location categories. May be null.
* @param sharedRefLocLike A where clause to filter the shared_loc_alias_id. May be null.
* @return A list of all location groups for the given parameters.
*/

Expand Down Expand Up @@ -432,7 +432,8 @@ public Feature buildFeatureFromAvLocRecordWithLocGroup(Record avLocRecord) {
return feature;
}

public FeatureCollection buildFeatureCollectionForLocationGroup(String officeId,
public FeatureCollection buildFeatureCollectionForLocationGroup(String locationOfficeId, String groupOfficeId,
String categoryOfficeId,
String categoryId,
String groupId, String units) {
AV_LOC_GRP_ASSGN alga = AV_LOC_GRP_ASSGN.AV_LOC_GRP_ASSGN;
Expand All @@ -442,9 +443,9 @@ public FeatureCollection buildFeatureCollectionForLocationGroup(String officeId,
alga.GROUP_ID, alga.ATTRIBUTE, alga.ALIAS_ID, alga.SHARED_REF_LOCATION_ID,
alga.SHARED_ALIAS_ID)
.from(al).join(alga).on(al.LOCATION_ID.eq(alga.LOCATION_ID))
.where(alga.DB_OFFICE_ID.eq(officeId)
.and(alga.CATEGORY_OFFICE_ID.eq())
.and(alga.GROUP_OFFICE_ID.eq())
.where(alga.DB_OFFICE_ID.eq(locationOfficeId)
.and(alga.CATEGORY_OFFICE_ID.eq(categoryOfficeId))
.and(alga.GROUP_OFFICE_ID.eq(groupOfficeId))
.and(alga.CATEGORY_ID.eq(categoryId)
.and(alga.GROUP_ID.eq(groupId))
.and(al.UNIT_SYSTEM.eq(units))))
Expand Down Expand Up @@ -533,8 +534,8 @@ public void assignLocs(LocationGroup group, String office) {
/**
* Used when an appropriate context already exists to avoid opening a second connection.
* @param dslContext a dslContext that is assumed to be fully prepared for use in this operation
* @param group
* @param office
* @param group the location group to assign locations to
* @param office the office to use for the operation
*/
public void assignLocs(DSLContext dslContext, LocationGroup group, String office) {
List<AssignedLocation> assignedLocations = group.getAssignedLocations();
Expand All @@ -544,8 +545,8 @@ public void assignLocs(DSLContext dslContext, LocationGroup group, String office
.collect(toList());
LOC_ALIAS_ARRAY3 assignedLocs = new LOC_ALIAS_ARRAY3(collect);
LocationCategory cat = group.getLocationCategory();
CWMS_LOC_PACKAGE.call_ASSIGN_LOC_GROUPS3(dslContext.configuration(),
cat.getId(), group.getId(), assignedLocs, office);
CWMS_LOC_PACKAGE.call_ASSIGN_LOC_GROUPS3(dslContext.configuration(),
cat.getId(), group.getId(), assignedLocs, office);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,17 @@

package cwms.cda.data.dao;

import static java.util.stream.Collectors.toList;

import cwms.cda.data.dto.AssignedTimeSeries;
import cwms.cda.data.dto.TimeSeriesCategory;
import cwms.cda.data.dto.TimeSeriesGroup;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;
import kotlin.Pair;
import org.jetbrains.annotations.NotNull;
import org.jooq.*;
Expand All @@ -38,14 +46,6 @@
import usace.cwms.db.jooq.codegen.udt.records.TS_ALIAS_T;
import usace.cwms.db.jooq.codegen.udt.records.TS_ALIAS_TAB_T;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

import static java.util.stream.Collectors.toList;

public class TimeSeriesGroupDao extends JooqDao<TimeSeriesGroup> {
private static final Logger logger = Logger.getLogger(TimeSeriesGroupDao.class.getName());
Expand All @@ -56,19 +56,20 @@ public TimeSeriesGroupDao(DSLContext dsl) {
}

public List<TimeSeriesGroup> getTimeSeriesGroups() {
return getTimeSeriesGroups(null);
return getTimeSeriesGroups(null, null, null);
}

public List<TimeSeriesGroup> getTimeSeriesGroups(String officeId) {
public List<TimeSeriesGroup> getTimeSeriesGroups(String officeId, String groupOfficeId, String categoryOfficeId) {
Condition whereCond = DSL.noCondition();
if (officeId != null) {
whereCond = AV_TS_CAT_GRP.AV_TS_CAT_GRP.GRP_DB_OFFICE_ID.eq(officeId);
}

return getTimeSeriesGroupsWhere(whereCond);
return getTimeSeriesGroupsWhere(whereCond, categoryOfficeId, groupOfficeId);
}

public List<TimeSeriesGroup> getTimeSeriesGroups(String officeId, boolean includeAssigned, String tsCategoryLike, String tsGroupLike) {
public List<TimeSeriesGroup> getTimeSeriesGroups(String officeId, String groupOfficeId, String categoryOfficeId,
boolean includeAssigned, String tsCategoryLike, String tsGroupLike) {

Condition whereCond = DSL.noCondition();
if (officeId != null) {
Expand All @@ -85,22 +86,22 @@ public List<TimeSeriesGroup> getTimeSeriesGroups(String officeId, boolean includ
whereCond = whereCond.and(AV_TS_CAT_GRP.AV_TS_CAT_GRP.TS_GROUP_ID.isNotNull());
}

if(includeAssigned){
return getTimeSeriesGroupsWhere(whereCond);
if (includeAssigned) {
return getTimeSeriesGroupsWhere(whereCond, categoryOfficeId, groupOfficeId);
} else {
return getTimeSeriesGroupsWithoutAssigned(whereCond);
}

}


public List<TimeSeriesGroup> getTimeSeriesGroups(String officeId, String categoryId, String groupId) {
return getTimeSeriesGroupsWhere(buildWhereCondition(officeId, categoryId, groupId));
public List<TimeSeriesGroup> getTimeSeriesGroups(String officeId, String groupOfficeId, String categoryOfficeId,
String categoryId, String groupId) {
return getTimeSeriesGroupsWhere(buildWhereCondition(officeId, categoryId, groupId), categoryOfficeId, groupOfficeId);
}

@NotNull
private List<TimeSeriesGroup> getTimeSeriesGroupsWhere(Condition whereCond) {
List<TimeSeriesGroup> retval = new ArrayList<>();
private List<TimeSeriesGroup> getTimeSeriesGroupsWhere(Condition whereCond, String categoryOfficeId, String groupOfficeId) {
AV_TS_CAT_GRP catGrp = AV_TS_CAT_GRP.AV_TS_CAT_GRP;
AV_TS_GRP_ASSGN grpAssgn = AV_TS_GRP_ASSGN.AV_TS_GRP_ASSGN;

Expand All @@ -112,6 +113,15 @@ private List<TimeSeriesGroup> getTimeSeriesGroupsWhere(Condition whereCond) {
return new Pair<>(group, loc);
};

Condition whereCondGrpCat = DSL.noCondition();
if (categoryOfficeId != null) {
whereCondGrpCat = whereCondGrpCat.and(grpAssgn.CATEGORY_OFFICE_ID.eq(categoryOfficeId))
.and(grpAssgn.GROUP_OFFICE_ID.eq(groupOfficeId));
}
if (groupOfficeId != null) {
whereCondGrpCat = whereCondGrpCat.and(grpAssgn.GROUP_OFFICE_ID.eq(groupOfficeId));
}

SelectSeekStep1<?, BigDecimal> query = dsl.select(catGrp.CAT_DB_OFFICE_ID,
catGrp.TS_CATEGORY_ID, catGrp.TS_CATEGORY_DESC, catGrp.GRP_DB_OFFICE_ID,
catGrp.TS_GROUP_ID, catGrp.TS_GROUP_DESC, catGrp.SHARED_TS_ALIAS_ID,
Expand All @@ -122,8 +132,7 @@ private List<TimeSeriesGroup> getTimeSeriesGroupsWhere(Condition whereCond) {
.on(catGrp.TS_CATEGORY_ID.eq(grpAssgn.CATEGORY_ID)
.and(catGrp.TS_GROUP_ID.eq(grpAssgn.GROUP_ID)))
.where(whereCond)
.and(grpAssgn.CATEGORY_OFFICE_ID.eq())
.and(grpAssgn.GROUP_OFFICE_ID.eq())
.and(whereCondGrpCat)
.orderBy(grpAssgn.ATTRIBUTE);

logger.fine(() -> query.getSQL(ParamType.INLINED));
Expand All @@ -141,6 +150,7 @@ private List<TimeSeriesGroup> getTimeSeriesGroupsWhere(Condition whereCond) {
}
}

List<TimeSeriesGroup> retval = new ArrayList<>();
for (final Map.Entry<TimeSeriesGroup, List<AssignedTimeSeries>> entry : map.entrySet()) {
List<AssignedTimeSeries> assigned = entry.getValue();
retval.add(new TimeSeriesGroup(entry.getKey(), assigned));
Expand Down Expand Up @@ -243,22 +253,20 @@ public void delete(String categoryId, String groupId, String office) {
}

public void create(TimeSeriesGroup group, boolean failIfExists) {
connection(dsl, c-> {
connection(dsl, c -> {
Configuration configuration = getDslContext(c,group.getOfficeId()).configuration();
String categoryId = group.getTimeSeriesCategory().getId();
CWMS_TS_PACKAGE.call_STORE_TS_GROUP(configuration, categoryId,
group.getId(), group.getDescription(), formatBool(failIfExists),
"T", group.getSharedAliasId(),
group.getSharedRefTsId(), group.getOfficeId());
group.getId(), group.getDescription(), formatBool(failIfExists),
"T", group.getSharedAliasId(),
group.getSharedRefTsId(), group.getOfficeId());
assignTs(configuration,group, group.getOfficeId());
});

}

private void assignTs(Configuration configuration,TimeSeriesGroup group, String office) {
List<AssignedTimeSeries> assignedTimeSeries = group.getAssignedTimeSeries();
if(assignedTimeSeries != null)
{
if (assignedTimeSeries != null) {
List<TS_ALIAS_T> collect = assignedTimeSeries.stream()
.map(TimeSeriesGroupDao::convertToTsAliasType)
.collect(toList());
Expand All @@ -269,7 +277,7 @@ private void assignTs(Configuration configuration,TimeSeriesGroup group, String
}

public void assignTs(TimeSeriesGroup group, String office) {
connection(dsl, c->assignTs(getDslContext(c, office).configuration(),group, office));
connection(dsl, c -> assignTs(getDslContext(c, office).configuration(),group, office));
}

private static TS_ALIAS_T convertToTsAliasType(AssignedTimeSeries assignedTimeSeries) {
Expand All @@ -279,9 +287,9 @@ private static TS_ALIAS_T convertToTsAliasType(AssignedTimeSeries assignedTimeSe
}

public void renameTimeSeriesGroup(String oldGroupId, TimeSeriesGroup group) {
connection(dsl, c->
connection(dsl, c ->
CWMS_TS_PACKAGE.call_RENAME_TS_GROUP(
getDslContext(c,group.getOfficeId()).configuration(),
getDslContext(c, group.getOfficeId()).configuration(),
group.getTimeSeriesCategory().getId(), oldGroupId, group.getId(),
group.getOfficeId())
);
Expand Down
Loading

0 comments on commit 835c5c1

Please sign in to comment.