Skip to content

Commit

Permalink
Issue 886 - Updated LocationGroup and TimeSeriesGroup patch (#900)
Browse files Browse the repository at this point in the history
Issue #886 - Updated LocationGroup and TimeSeriesGroup patch office
handling to allow for assigning non-CWMS TS/locations to a CWMS-owned
group
  • Loading branch information
zack-rma authored Oct 17, 2024
1 parent 8e3dcfd commit 336d4df
Show file tree
Hide file tree
Showing 12 changed files with 931 additions and 93 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 @@ -197,6 +197,8 @@ public final class Controllers {
public static final String ALLOW = "allow";
public static final String SOURCE_ID = "source-id";

public static final String CWMS_OFFICE = "CWMS";

private static final String DEPRECATED_HEADER = "CWMS-DATA-Format-Deprecated";
private static final String DEPRECATED_TAB = "2024-11-01 TAB is not used often.";
private static final String DEPRECATED_CSV = "2024-11-01 CSV is not used often.";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,8 @@ public void create(@NotNull Context ctx) {
}

@OpenApi(
description = "Update existing LocationGroup",
description = "Update existing LocationGroup. Allows for renaming group, assigning new locations, "
+ "and unassigning all locations from the group.",
requestBody = @OpenApiRequestBody(
content = {
@OpenApiContent(from = LocationGroup.class, type = Formats.JSON)
Expand All @@ -235,31 +236,32 @@ public void create(@NotNull Context ctx) {
+ "unassign all existing locations before assigning new locations specified in the content body "
+ "Default: false"),
@OpenApiParam(name = OFFICE, required = true, description = "Specifies the "
+ "owning office of the location group to be updated"),
+ "office of the user making the request. This is the office that the location, group, and category "
+ "belong to. If the group and/or category belong to the CWMS office, this only identifies the location."),
},
method = HttpMethod.PATCH,
tags = {TAG}
)
@Override
public void update(@NotNull Context ctx, String oldGroupId) {
public void update(@NotNull Context ctx, @NotNull String oldGroupId) {

try (Timer.Context ignored = markAndTime(CREATE)) {
DSLContext dsl = getDslContext(ctx);

String formatHeader = ctx.req.getContentType();
String body = ctx.body();
String office = requiredParam(ctx, OFFICE);
ContentType contentType = Formats.parseHeader(formatHeader, LocationGroup.class);
LocationGroup deserialize = Formats.parseContent(contentType, body, LocationGroup.class);
boolean replaceAssignedLocs = ctx.queryParamAsClass(REPLACE_ASSIGNED_LOCS,
Boolean.class).getOrDefault(false);
LocationGroupDao locationGroupDao = new LocationGroupDao(dsl);
if (!oldGroupId.equals(deserialize.getId())) {
if (!office.equalsIgnoreCase(CWMS_OFFICE) && !oldGroupId.equals(deserialize.getId())) {
locationGroupDao.renameLocationGroup(oldGroupId, deserialize);
}
if (replaceAssignedLocs) {
locationGroupDao.unassignAllLocs(deserialize);
locationGroupDao.unassignAllLocs(deserialize, office);
}
locationGroupDao.assignLocs(deserialize);
locationGroupDao.assignLocs(deserialize, office);
ctx.status(HttpServletResponse.SC_OK);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,8 @@ public void create(@NotNull Context ctx) {
}

@OpenApi(
description = "Update existing TimeSeriesGroup",
description = "Update existing TimeSeriesGroup. Allows for renaming of the group, "
+ "assigning new time series, and unassigning all time series from the group.",
requestBody = @OpenApiRequestBody(
content = {
@OpenApiContent(from = TimeSeriesGroup.class, type = Formats.JSON)
Expand All @@ -239,31 +240,31 @@ public void create(@NotNull Context ctx) {
+ "unassign all existing time series before assigning new time series specified in the content body "
+ "Default: false"),
@OpenApiParam(name = OFFICE, required = true, description = "Specifies the "
+ "owning office of the time series group to be updated"),
+ "office of the user making the request. This is the office that the timeseries, group, and category "
+ "belong to. If the group and/or category belong to the CWMS office, this only identifies the timeseries."),
},
method = HttpMethod.PATCH,
tags = {TAG}
)
@Override
public void update(@NotNull Context ctx, String oldGroupId) {

public void update(@NotNull Context ctx, @NotNull String oldGroupId) {
try (Timer.Context ignored = markAndTime(CREATE)) {
DSLContext dsl = getDslContext(ctx);

String formatHeader = ctx.req.getContentType();
String body = ctx.body();
String office = requiredParam(ctx, OFFICE);
ContentType contentType = Formats.parseHeader(formatHeader, TimeSeriesGroup.class);
TimeSeriesGroup deserialize = Formats.parseContent(contentType, body, TimeSeriesGroup.class);
boolean replaceAssignedTs = ctx.queryParamAsClass(REPLACE_ASSIGNED_TS, Boolean.class)
.getOrDefault(false);
TimeSeriesGroupDao timeSeriesGroupDao = new TimeSeriesGroupDao(dsl);
if (!oldGroupId.equals(deserialize.getId())) {
if (!office.equalsIgnoreCase(CWMS_OFFICE) && !oldGroupId.equals(deserialize.getId())) {
timeSeriesGroupDao.renameTimeSeriesGroup(oldGroupId, deserialize);
}
if (replaceAssignedTs) {
timeSeriesGroupDao.unassignAllTs(deserialize);
timeSeriesGroupDao.unassignAllTs(deserialize, office);
}
timeSeriesGroupDao.assignTs(deserialize);
timeSeriesGroupDao.assignTs(deserialize, office);
ctx.status(HttpServletResponse.SC_OK);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -478,15 +478,15 @@ public void delete(String categoryId, String groupId, boolean cascadeDelete, Str
* @param group The location group to create.
*/
public void create(LocationGroup group) {
String office = group.getOfficeId();
String office = group.getOfficeId();
String categoryId = group.getLocationCategory().getId();

connection(dsl, conn -> {
DSLContext dslContext = getDslContext(conn, office);
CWMS_LOC_PACKAGE.call_CREATE_LOC_GROUP2(dslContext.configuration(), categoryId,
group.getId(), group.getDescription(), office, group.getSharedLocAliasId(),
group.getId(), group.getDescription(), group.getOfficeId(), group.getSharedLocAliasId(),
group.getSharedRefLocationId());
assignLocs(group);
assignLocs(group, office);
});
}

Expand All @@ -512,8 +512,7 @@ public void renameLocationGroup(String oldGroupId, LocationGroup newGroup) {
});
}

public void unassignAllLocs(LocationGroup group) {
String office = group.getOfficeId();
public void unassignAllLocs(LocationGroup group, String office) {
LocationCategory cat = group.getLocationCategory();
connection(dsl, conn -> {
DSLContext dslContext = getDslContext(conn, office);
Expand All @@ -522,15 +521,14 @@ public void unassignAllLocs(LocationGroup group) {
});
}

public void assignLocs(LocationGroup group) {
public void assignLocs(LocationGroup group, String office) {
List<AssignedLocation> assignedLocations = group.getAssignedLocations();
if (assignedLocations != null) {
List<LOC_ALIAS_TYPE3> collect = assignedLocations.stream()
.map(LocationGroupDao::convertToLocAliasType)
.collect(toList());
LOC_ALIAS_ARRAY3 assignedLocs = new LOC_ALIAS_ARRAY3(collect);

String office = group.getOfficeId();
LocationCategory cat = group.getLocationCategory();
connection(dsl, conn -> {
DSLContext dslContext = getDslContext(conn, office);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,12 +253,12 @@ public void create(TimeSeriesGroup group, boolean failIfExists) {
group.getId(), group.getDescription(), formatBool(failIfExists),
"T", group.getSharedAliasId(),
group.getSharedRefTsId(), group.getOfficeId());
assignTs(configuration,group);
assignTs(configuration,group, group.getOfficeId());
});

}

private void assignTs(Configuration configuration,TimeSeriesGroup group) {
private void assignTs(Configuration configuration,TimeSeriesGroup group, String office) {
List<AssignedTimeSeries> assignedTimeSeries = group.getAssignedTimeSeries();
if(assignedTimeSeries != null)
{
Expand All @@ -267,12 +267,12 @@ private void assignTs(Configuration configuration,TimeSeriesGroup group) {
.collect(toList());
TS_ALIAS_TAB_T assignedLocs = new TS_ALIAS_TAB_T(collect);
CWMS_TS_PACKAGE.call_ASSIGN_TS_GROUPS(configuration, group.getTimeSeriesCategory().getId(),
group.getId(), assignedLocs, group.getOfficeId());
group.getId(), assignedLocs, office);
}
}

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

private static TS_ALIAS_T convertToTsAliasType(AssignedTimeSeries assignedTimeSeries) {
Expand All @@ -290,12 +290,12 @@ public void renameTimeSeriesGroup(String oldGroupId, TimeSeriesGroup group) {
);
}

public void unassignAllTs(TimeSeriesGroup group) {
public void unassignAllTs(TimeSeriesGroup group, String officeId) {
connection(dsl, c ->
CWMS_TS_PACKAGE.call_UNASSIGN_TS_GROUP(
getDslContext(c,group.getOfficeId()).configuration(),
getDslContext(c,officeId).configuration(),
group.getTimeSeriesCategory().getId(), group.getId(),
null, "T", group.getOfficeId())
null, "T", officeId)
);
}

Expand Down
Loading

0 comments on commit 336d4df

Please sign in to comment.