Skip to content

Commit

Permalink
Added location group office parameter to LocationGroup getAll
Browse files Browse the repository at this point in the history
  • Loading branch information
zack-rma committed Nov 20, 2024
1 parent 4de9156 commit 0bf06ba
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,10 @@ private Timer.Context markAndTime(String subject) {
return Controllers.markAndTime(metrics, getClass().getName(), subject);
}

@OpenApi(queryParams = {
@OpenApi(
queryParams = {
@OpenApiParam(name = OFFICE, description = "Specifies the owning office of the "
+ "location group(s) whose data is to be included in the response. If this "
+ "locations assigned to the group whose data is to be included in the response. If this "
+ "field is not specified, matching location groups information from all "
+ "offices shall be returned."),
@OpenApiParam(name = INCLUDE_ASSIGNED, type = Boolean.class, description = "Include"
Expand All @@ -87,7 +88,9 @@ private Timer.Context markAndTime(String subject) {
+ "matching against the location category id"),
@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.")
+ "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."),
},
responses = {
@OpenApiResponse(status = STATUS_200,
Expand All @@ -96,16 +99,18 @@ private Timer.Context markAndTime(String subject) {
@OpenApiContent(isArray = true, from = CsvV1LocationGroup.class, type = Formats.CSV)
})
},
description = "Returns CWMS Location Groups Data", tags = {TAG})
description = "Returns CWMS Location Groups Data", tags = {TAG}
)
@Override
public void getAll(@NotNull Context ctx) {

try (final Timer.Context ignored = markAndTime(GET_ALL)) {
DSLContext dsl = getDslContext(ctx);
LocationGroupDao cdm = new LocationGroupDao(dsl);

String groupOfficeId = requiredParam(ctx, OFFICE);
String officeId = requiredParam(ctx, OFFICE);
String categoryOfficeId = requiredParam(ctx, CATEGORY_OFFICE_ID);
String groupOfficeId = ctx.queryParam(GROUP_OFFICE_ID);

boolean includeAssigned = queryParamAsClass(ctx, new String[]{INCLUDE_ASSIGNED},
Boolean.class, false, metrics, name(LocationGroupController.class.getName(),
Expand All @@ -114,7 +119,7 @@ Boolean.class, false, metrics, name(LocationGroupController.class.getName(),
String locCategoryLike = queryParamAsClass(ctx, new String[]{LOCATION_CATEGORY_LIKE},
String.class, null, metrics, name(LocationGroupController.class.getName(), GET_ALL));

List<LocationGroup> grps = cdm.getLocationGroups(groupOfficeId, categoryOfficeId,
List<LocationGroup> grps = cdm.getLocationGroups(officeId, groupOfficeId, categoryOfficeId,
includeAssigned, locCategoryLike);

if (!grps.isEmpty()) {
Expand Down Expand Up @@ -163,7 +168,8 @@ Boolean.class, false, metrics, name(LocationGroupController.class.getName(),
@OpenApiContent(type = Formats.GEOJSON)
})
},
description = "Retrieves requested Location Group", tags = {TAG})
description = "Retrieves requested Location Group", tags = {TAG}
)
@Override
public void getOne(@NotNull Context ctx, @NotNull String groupId) {

Expand Down Expand Up @@ -300,9 +306,8 @@ public void update(@NotNull Context ctx, @NotNull String oldGroupId) {
+ "location category of the location group to be deleted"),
@OpenApiParam(name = OFFICE, required = true, description = "Specifies the "
+ "owning office of the location group to be deleted"),
@OpenApiParam(name = CASCADE_DELETE, type = Boolean.class,
description = "Specifies whether to specifies whether to unassign any "
+ "location assignments. Default: false"),
@OpenApiParam(name = CASCADE_DELETE, type = Boolean.class, description = "Specifies whether "
+ "to unassign any location assignments. Default: false"),
},
method = HttpMethod.DELETE,
tags = {TAG}
Expand Down
162 changes: 93 additions & 69 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 @@ -80,7 +80,7 @@ public LocationGroupDao(DSLContext dsl) {
* @return An optional location group.
*/
public Optional<LocationGroup> getLocationGroup(@NotNull String officeId, @NotNull String categoryId,
@NotNull String groupId) {
@NotNull String groupId) {
AV_LOC_GRP_ASSGN alga = AV_LOC_GRP_ASSGN.AV_LOC_GRP_ASSGN;
AV_LOC_CAT_GRP alcg = AV_LOC_CAT_GRP.AV_LOC_CAT_GRP;

Expand All @@ -100,40 +100,40 @@ public Optional<LocationGroup> getLocationGroup(@NotNull String officeId, @NotNu
}

List<Pair<LocationGroup, AssignedLocation>> assignments = dsl.select(
alcg.CAT_DB_OFFICE_ID,
alcg.LOC_CATEGORY_ID,
alcg.LOC_CATEGORY_DESC,
alcg.GRP_DB_OFFICE_ID,
alcg.LOC_GROUP_ID,
alcg.LOC_GROUP_DESC,
alcg.LOC_GROUP_ATTRIBUTE,
alcg.SHARED_LOC_ALIAS_ID,
alcg.SHARED_REF_LOCATION_ID,
alga.DB_OFFICE_ID,
alga.LOCATION_ID,
alga.ALIAS_ID,
alga.ATTRIBUTE,
alga.REF_LOCATION_ID)
.from(alcg).leftJoin(alga)
.on(alcg.LOC_CATEGORY_ID.eq(alga.CATEGORY_ID)
.and(alcg.LOC_GROUP_ID.eq(alga.GROUP_ID)))
.where(alcg.LOC_CATEGORY_ID.eq(categoryId)
.and(alcg.LOC_GROUP_ID.eq(groupId))
.and(alcg.GRP_DB_OFFICE_ID.in(CWMS, officeId))
.and(alcg.CAT_DB_OFFICE_ID.in(CWMS, officeId))
.and(assignmentOffice)
)
.orderBy(alga.ATTRIBUTE).fetchSize(1000).fetch(mapper);
alcg.CAT_DB_OFFICE_ID,
alcg.LOC_CATEGORY_ID,
alcg.LOC_CATEGORY_DESC,
alcg.GRP_DB_OFFICE_ID,
alcg.LOC_GROUP_ID,
alcg.LOC_GROUP_DESC,
alcg.LOC_GROUP_ATTRIBUTE,
alcg.SHARED_LOC_ALIAS_ID,
alcg.SHARED_REF_LOCATION_ID,
alga.DB_OFFICE_ID,
alga.LOCATION_ID,
alga.ALIAS_ID,
alga.ATTRIBUTE,
alga.REF_LOCATION_ID)
.from(alcg).leftJoin(alga)
.on(alcg.LOC_CATEGORY_ID.eq(alga.CATEGORY_ID)
.and(alcg.LOC_GROUP_ID.eq(alga.GROUP_ID)))
.where(alcg.LOC_CATEGORY_ID.eq(categoryId)
.and(alcg.LOC_GROUP_ID.eq(groupId))
.and(alcg.GRP_DB_OFFICE_ID.in(CWMS, officeId))
.and(alcg.CAT_DB_OFFICE_ID.in(CWMS, officeId))
.and(assignmentOffice)
)
.orderBy(alga.ATTRIBUTE).fetchSize(1000).fetch(mapper);

// Might want to verify that all the groups in the list are the same?
LocationGroup locGroup =
assignments.stream().map(Pair::component1).findFirst().orElse(null);

if (locGroup != null) {
List<AssignedLocation> assignedLocations = assignments.stream()
.map(Pair::component2)
.filter(Objects::nonNull)
.collect(toList());
.map(Pair::component2)
.filter(Objects::nonNull)
.collect(toList());
locGroup = new LocationGroup(locGroup, assignedLocations);
}
return Optional.ofNullable(locGroup);
Expand All @@ -157,7 +157,7 @@ private AssignedLocation buildAssignedLocation(Record resultRecord) {
}

private LocationGroup buildLocationGroup(Record resultRecord,
LocationCategory locationCategory) {
LocationCategory locationCategory) {
AV_LOC_CAT_GRP alcg = AV_LOC_CAT_GRP.AV_LOC_CAT_GRP;

String groupId = resultRecord.get(alcg.LOC_GROUP_ID);
Expand Down Expand Up @@ -186,7 +186,7 @@ private LocationCategory buildLocationCategory(Record resultRecord) {
* @return A list of all location groups.
*/
public List<LocationGroup> getLocationGroups() {
return getLocationGroups(null, null, null);
return getLocationGroups(null, null, null, null);
}

/**
Expand All @@ -195,7 +195,11 @@ public List<LocationGroup> getLocationGroups() {
* @return A list of all location groups for the given office.
*/
public List<LocationGroup> getLocationGroups(String officeId) {
return getLocationGroups(officeId, null, null);
return getLocationGroups(officeId, null, null, null);
}

public List<LocationGroup> getLocationGroups(String officeId, String categoryOfficeId, String locCategoryLike) {
return getLocationGroups(officeId, null, categoryOfficeId, locCategoryLike);
}

/**
Expand All @@ -205,11 +209,11 @@ public List<LocationGroup> getLocationGroups(String officeId) {
* @param locCategoryLike A regex to use to filter the location categories. May be null.
* @return A list of all location groups for the given office and category.
*/
public List<LocationGroup> getLocationGroups(@Nullable String officeId,
String categoryOfficeId, boolean includeAssigned,
@Nullable String locCategoryLike) {
public List<LocationGroup> getLocationGroups(@Nullable String officeId, String groupOfficeId,
String categoryOfficeId, boolean includeAssigned,
@Nullable String locCategoryLike) {
if (includeAssigned) {
return getLocationGroups(officeId, categoryOfficeId, locCategoryLike);
return getLocationGroups(officeId, groupOfficeId, categoryOfficeId, locCategoryLike);
} else {
return getGroupsWithoutAssignedLocations(officeId, categoryOfficeId, locCategoryLike);
}
Expand All @@ -218,16 +222,15 @@ 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 groupOfficeId 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.
*/

public List<LocationGroup> getLocationGroups(String groupOfficeId, String categoryOfficeId, String locCategoryLike,
String sharedRefLocLike) {
AV_LOC_GRP_ASSGN alga = AV_LOC_GRP_ASSGN.AV_LOC_GRP_ASSGN;
AV_LOC_CAT_GRP alcg = AV_LOC_CAT_GRP.AV_LOC_CAT_GRP;
public List<LocationGroup> getLocationGroups(String officeId, String groupOfficeId, String categoryOfficeId,
String locCategoryLike, String sharedRefLocLike) {

final RecordMapper<Record, Pair<LocationGroup, AssignedLocation>> mapper = grpRecord -> {
LocationCategory category = buildLocationCategory(grpRecord);
Expand All @@ -238,6 +241,9 @@ public List<LocationGroup> getLocationGroups(String groupOfficeId, String catego
return new Pair<>(group, loc);
};

AV_LOC_GRP_ASSGN alga = AV_LOC_GRP_ASSGN.AV_LOC_GRP_ASSGN;
AV_LOC_CAT_GRP alcg = AV_LOC_CAT_GRP.AV_LOC_CAT_GRP;

Condition condition = noCondition();
if (locCategoryLike != null && !locCategoryLike.isEmpty()) {
condition = caseInsensitiveLikeRegex(alcg.LOC_CATEGORY_ID, locCategoryLike);
Expand Down Expand Up @@ -273,10 +279,21 @@ public List<LocationGroup> getLocationGroups(String groupOfficeId, String catego
.on(alcg.LOC_CATEGORY_ID.eq(alga.CATEGORY_ID)
.and(alcg.LOC_GROUP_ID.eq(alga.GROUP_ID)));

if (groupOfficeId != null) {
connectBy = onStep.where(DSL.upper(alcg.GRP_DB_OFFICE_ID).eq(groupOfficeId.toUpperCase()).and(condition));
if (officeId != null) {
if (groupOfficeId != null) {
connectBy = onStep.where(DSL.upper(alcg.GRP_DB_OFFICE_ID).eq(groupOfficeId.toUpperCase())
.and(DSL.upper(alga.DB_OFFICE_ID).eq(officeId.toUpperCase())).and(condition));
} else {
connectBy = onStep.where(DSL.upper(alga.DB_OFFICE_ID).eq(officeId.toUpperCase()).and(condition));
}
} else {
connectBy = onStep.where(condition);
if (groupOfficeId != null) {
connectBy = onStep.where(DSL.upper(alcg.GRP_DB_OFFICE_ID).eq(groupOfficeId.toUpperCase())
.and(condition));
} else {
connectBy = onStep.where(condition);
}

}


Expand All @@ -301,12 +318,13 @@ public List<LocationGroup> getLocationGroups(String groupOfficeId, String catego

/**
* Get all location groups for a given office and category.
* @param officeId The group office id to use for the query.
* @param groupOfficeId The group office id to use for the query.
* @param categoryOfficeId The category office id to use for the query.
* @param locCategoryLike A regex to use to filter the location categories. May be null.
* @return A list of all location groups for the given office and category.
*/
public List<LocationGroup> getLocationGroups(String groupOfficeId,
public List<LocationGroup> getLocationGroups(String officeId, String groupOfficeId,
String categoryOfficeId, String locCategoryLike) {
AV_LOC_GRP_ASSGN alga = AV_LOC_GRP_ASSGN.AV_LOC_GRP_ASSGN;
AV_LOC_CAT_GRP alcg = AV_LOC_CAT_GRP.AV_LOC_CAT_GRP;
Expand Down Expand Up @@ -339,8 +357,8 @@ public List<LocationGroup> getLocationGroups(String groupOfficeId,
alga.ATTRIBUTE,
alga.REF_LOCATION_ID)
.from(alcg).leftJoin(alga)
.on(alcg.LOC_CATEGORY_ID.eq(alga.CATEGORY_ID)
.and(alcg.LOC_GROUP_ID.eq(alga.GROUP_ID)));
.on(alcg.LOC_CATEGORY_ID.eq(alga.CATEGORY_ID)
.and(alcg.LOC_GROUP_ID.eq(alga.GROUP_ID)));


Condition condition = noCondition();
Expand All @@ -352,18 +370,24 @@ public List<LocationGroup> getLocationGroups(String groupOfficeId,
condition = condition.and(alcg.CAT_DB_OFFICE_ID.eq(categoryOfficeId.toUpperCase()));
}

if (groupOfficeId != null) {
if (CWMS.equalsIgnoreCase(groupOfficeId)) {
if (officeId != null) {
if (CWMS.equalsIgnoreCase(officeId)) {
connectBy = onStep.where(alcg.CAT_DB_OFFICE_ID.eq(CWMS)
.and(alcg.GRP_DB_OFFICE_ID.eq(CWMS))
.and(condition)
);
} else {
connectBy = onStep.where(alcg.CAT_DB_OFFICE_ID.in(CWMS, groupOfficeId)
.and(alcg.GRP_DB_OFFICE_ID.in(CWMS, groupOfficeId))
.and(alga.DB_OFFICE_ID.isNull().or(alga.DB_OFFICE_ID.eq(groupOfficeId)))
.and(condition)
);
if (groupOfficeId != null) {
connectBy = onStep.where(alcg.CAT_DB_OFFICE_ID.in(CWMS, officeId)
.and(alcg.GRP_DB_OFFICE_ID.in(CWMS, groupOfficeId))
.and(alga.DB_OFFICE_ID.isNull().or(alga.DB_OFFICE_ID.eq(officeId)))
.and(condition)
);
} else {
connectBy = onStep.where(alcg.CAT_DB_OFFICE_ID.in(CWMS, officeId)
.and(alga.DB_OFFICE_ID.isNull().or(alga.DB_OFFICE_ID.eq(officeId)))
.and(condition));
}
}
} else {
connectBy = onStep.where(alcg.LOC_GROUP_ID.isNotNull());
Expand Down Expand Up @@ -449,23 +473,23 @@ public Feature buildFeatureFromAvLocRecordWithLocGroup(Record avLocRecord) {
}

public FeatureCollection buildFeatureCollectionForLocationGroup(String locationOfficeId, String groupOfficeId,
String categoryOfficeId,
String categoryId,
String groupId, String units) {
String categoryOfficeId,
String categoryId,
String groupId, String units) {
AV_LOC_GRP_ASSGN alga = AV_LOC_GRP_ASSGN.AV_LOC_GRP_ASSGN;
AV_LOC al = AV_LOC.AV_LOC;

SelectSeekStep1<Record, BigDecimal> select = dsl.select(al.asterisk(), alga.CATEGORY_ID,
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(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))))
.orderBy(alga.ATTRIBUTE);
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(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))))
.orderBy(alga.ATTRIBUTE);

List<Feature> features =
select.stream()
Expand Down Expand Up @@ -513,7 +537,7 @@ public void create(LocationGroup group) {
private static LOC_ALIAS_TYPE3 convertToLocAliasType(AssignedLocation a) {
BigDecimal attribute = toBigDecimal(a.getAttribute());
return new LOC_ALIAS_TYPE3(a.getLocationId(),
attribute, a.getAliasId(), a.getRefLocationId());
attribute, a.getAliasId(), a.getRefLocationId());
}

/**
Expand Down Expand Up @@ -557,10 +581,10 @@ public void assignLocs(DSLContext dslContext, LocationGroup group, String office
List<AssignedLocation> assignedLocations = group.getAssignedLocations();
if (assignedLocations != null) {
List<LOC_ALIAS_TYPE3> collect = assignedLocations.stream()
.map(LocationGroupDao::convertToLocAliasType)
.collect(toList());
.map(LocationGroupDao::convertToLocAliasType)
.collect(toList());
LOC_ALIAS_ARRAY3 assignedLocs = new LOC_ALIAS_ARRAY3(collect);
LocationCategory cat = group.getLocationCategory();
LocationCategory cat = group.getLocationCategory();
CWMS_LOC_PACKAGE.call_ASSIGN_LOC_GROUPS3(dslContext.configuration(),
cat.getId(), group.getId(), assignedLocs, office);
}
Expand Down
Loading

0 comments on commit 0bf06ba

Please sign in to comment.