Skip to content

Commit

Permalink
Renamed children to childLocations
Browse files Browse the repository at this point in the history
  • Loading branch information
rma-rripken committed Jul 25, 2024
1 parent 6d195d0 commit 842ad77
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 91 deletions.
4 changes: 2 additions & 2 deletions cwms-data-api/src/main/java/cwms/cda/ApiServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@
import cwms.cda.api.errors.JsonFieldsException;
import cwms.cda.api.errors.NotFoundException;
import cwms.cda.api.errors.RequiredQueryParameterException;
import cwms.cda.api.project.ProjectChildrenHandler;
import cwms.cda.api.project.ProjectChildLocationHandler;
import cwms.cda.api.location.kind.VirtualOutletCreateController;
import cwms.cda.data.dao.JooqDao;
import cwms.cda.formatters.Formats;
Expand Down Expand Up @@ -520,7 +520,7 @@ protected void configureRoutes() {
cdaCrudCache(virtualOutletPath, new VirtualOutletController(metrics), requiredRoles, 1, TimeUnit.DAYS);
post(virtualOutletCreatePath, new VirtualOutletCreateController(metrics));

get("/projects/children/", new ProjectChildrenHandler(metrics));
get("/projects/child-locations/", new ProjectChildLocationHandler(metrics));
cdaCrudCache(format("/projects/{%s}", Controllers.NAME),
new ProjectController(metrics), requiredRoles,5, TimeUnit.MINUTES);
cdaCrudCache(format("/properties/{%s}", Controllers.NAME),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
import com.codahale.metrics.Timer;
import cwms.cda.api.Controllers;
import cwms.cda.data.dao.JooqDao;
import cwms.cda.data.dao.project.ProjectChildrenDao;
import cwms.cda.data.dto.project.ProjectChildren;
import cwms.cda.data.dao.project.ProjectChildLocationDao;
import cwms.cda.data.dto.project.ProjectChildLocations;
import cwms.cda.formatters.ContentType;
import cwms.cda.formatters.Formats;
import io.javalin.core.util.Header;
Expand All @@ -54,19 +54,19 @@
import org.jetbrains.annotations.NotNull;


public class ProjectChildrenHandler implements Handler {
public class ProjectChildLocationHandler implements Handler {
public static final String TAGS = "Projects";
public static final String PATH = "/projects/children";
public static final String PATH = "/projects/child-locations/";
private final MetricRegistry metrics;
private final Histogram requestResultSize;

private Timer.Context markAndTime(String subject) {
return Controllers.markAndTime(metrics, getClass().getName(), subject);
}

public ProjectChildrenHandler(MetricRegistry metrics) {
public ProjectChildLocationHandler(MetricRegistry metrics) {
this.metrics = metrics;
requestResultSize = this.metrics.histogram((name(ProjectChildrenHandler.class, Controllers.RESULTS,
requestResultSize = this.metrics.histogram((name(ProjectChildLocationHandler.class, Controllers.RESULTS,
Controllers.SIZE)));
}

Expand All @@ -80,14 +80,14 @@ public ProjectChildrenHandler(MetricRegistry metrics) {
@OpenApiParam(name = LOCATION_KIND_LIKE, description = "Posix <a "
+ "href=\"regexp.html\">regular expression</a> matching against the "
+ "location kind. The pattern will be matched against "
+ "the valid location-kinds for Project Children:"
+ "the valid location-kinds for Project child locations:"
+ "{\"EMBANKMENT\", \"TURBINE\", \"OUTLET\", \"LOCK\", \"GATE\"}. "
+ "Multiple kinds can be matched by using Regular Expression "
+ "OR clauses. For example: \"(TURBINE|OUTLET)\"")
},
responses = {
@OpenApiResponse(status = STATUS_200, content = {
@OpenApiContent(type = Formats.JSON, from = ProjectChildren.class, isArray = true)})
@OpenApiContent(type = Formats.JSON, from = ProjectChildLocations.class, isArray = true)})
},
tags = {TAGS},
path = PATH,
Expand All @@ -97,14 +97,14 @@ public ProjectChildrenHandler(MetricRegistry metrics) {
public void handle(@NotNull Context ctx) throws Exception {
String office = requiredParam(ctx, OFFICE);
try (Timer.Context ignored = markAndTime(GET_ALL)) {
ProjectChildrenDao lockDao = new ProjectChildrenDao(JooqDao.getDslContext(ctx));
ProjectChildLocationDao lockDao = new ProjectChildLocationDao(JooqDao.getDslContext(ctx));
String projLike = ctx.queryParamAsClass(PROJECT_LIKE, String.class).getOrDefault(null);
String kindLike = ctx.queryParamAsClass(LOCATION_KIND_LIKE, String.class).getOrDefault(null);

List<ProjectChildren> children = lockDao.children(office, projLike, kindLike);
List<ProjectChildLocations> childLocations = lockDao.retrieveProjectChildLocations(office, projLike, kindLike);
String formatHeader = ctx.header(Header.ACCEPT);
ContentType contentType = Formats.parseHeader(formatHeader, ProjectChildren.class);
String result = Formats.format(contentType, children, ProjectChildren.class);
ContentType contentType = Formats.parseHeader(formatHeader, ProjectChildLocations.class);
String result = Formats.format(contentType, childLocations, ProjectChildLocations.class);
ctx.result(result).contentType(contentType.toString());
requestResultSize.update(result.length());
ctx.status(HttpServletResponse.SC_OK);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

import cwms.cda.data.dao.JooqDao;
import cwms.cda.data.dto.CwmsId;
import cwms.cda.data.dto.project.ProjectChildren;
import cwms.cda.data.dto.project.ProjectChildLocations;

import java.util.*;
import java.util.stream.Collectors;
Expand All @@ -37,29 +37,29 @@
import usace.cwms.db.jooq.codegen.tables.AV_OUTLET;
import usace.cwms.db.jooq.codegen.tables.AV_TURBINE;

public class ProjectChildrenDao extends JooqDao<ProjectChildren> {
public class ProjectChildLocationDao extends JooqDao<ProjectChildLocations> {

public ProjectChildrenDao(DSLContext dsl) {
public ProjectChildLocationDao(DSLContext dsl) {
super(dsl);
}


public List<ProjectChildren> children(String office, String projLike, String kindRegex) {
return children(office, projLike, ProjectKind.getMatchingKinds(kindRegex));
public List<ProjectChildLocations> retrieveProjectChildLocations(String office, String projLike, String kindRegex) {
return retrieveProjectChildLocations(office, projLike, ProjectKind.getMatchingKinds(kindRegex));
}

private List<ProjectChildren> children(String office, String projLike, Set<ProjectKind> kinds) {
private List<ProjectChildLocations> retrieveProjectChildLocations(String office, String projLike, Set<ProjectKind> kinds) {

Map<String, ProjectChildren.Builder> builderMap = new LinkedHashMap<>(); // proj-id->
Map<String, ProjectChildLocations.Builder> builderMap = new LinkedHashMap<>(); // proj-id->

for (ProjectKind kind : kinds) {
Map<String, List<CwmsId>> locsOfKind = getChildrenOfKind(office, projLike, kind);
Map<String, List<CwmsId>> locsOfKind = getChildLocationsOfKind(office, projLike, kind);
if (locsOfKind != null) {
for (Map.Entry<String, List<CwmsId>> entry : locsOfKind.entrySet()) {
String projId = entry.getKey();
List<CwmsId> locs = entry.getValue();
ProjectChildren.Builder builder = builderMap.computeIfAbsent(projId, k ->
new ProjectChildren.Builder()
ProjectChildLocations.Builder builder = builderMap.computeIfAbsent(projId, k ->
new ProjectChildLocations.Builder()
.withProject(new CwmsId.Builder()
.withOfficeId(office)
.withName(projId)
Expand All @@ -83,38 +83,35 @@ private List<ProjectChildren> children(String office, String projLike, Set<Proje
default:
break;
}

}
}
}

return builderMap.values().stream()
.map(ProjectChildren.Builder::build)
.map(ProjectChildLocations.Builder::build)
.collect(Collectors.toList());

}

@Nullable
private Map<String, List<CwmsId>> getChildrenOfKind(String office, String projLike, ProjectKind kind) {
private Map<String, List<CwmsId>> getChildLocationsOfKind(String office, String projRegex, ProjectKind kind) {
switch (kind) {

case EMBANKMENT:
return getEmbankmentChildren(office, projLike);
return getEmbankmentChildLocations(office, projRegex);
case TURBINE:
return getTurbineChildren(office, projLike);
return getTurbineChildLocations(office, projRegex);
case OUTLET:
return getOutletChildren(office, projLike);
return getOutletChildLocations(office, projRegex);
case LOCK:
return getLockChildren(office, projLike);
return getLockChildLocations(office, projRegex);
case GATE:
return getGateChildren(office, projLike);
return getGateChildLocations(office, projRegex);
default:
return null;
}

}

private Map<String, List<CwmsId>> getGateChildren(String office, @Nullable String projLike) {
private Map<String, List<CwmsId>> getGateChildLocations(String office, @Nullable String projRegex) {
Map<String, List<CwmsId>> retval = new LinkedHashMap<>();
// AV_GATE is apparently not used.
AV_OUTLET view = AV_OUTLET.AV_OUTLET;
Expand All @@ -123,7 +120,7 @@ private Map<String, List<CwmsId>> getGateChildren(String office, @Nullable Strin
.where(view.OFFICE_ID.eq(office)
.and(view.OPENING_UNIT_EN.isNotNull().or(view.OPENING_UNIT_SI.isNotNull()))
)
.and(caseInsensitiveLikeRegexNullTrue(view.PROJECT_ID, projLike))
.and(caseInsensitiveLikeRegexNullTrue(view.PROJECT_ID, projRegex))
.orderBy(view.OFFICE_ID, view.PROJECT_ID, view.OUTLET_ID)
.forEach(row -> {
String projId = row.get(view.PROJECT_ID);
Expand All @@ -137,13 +134,13 @@ private Map<String, List<CwmsId>> getGateChildren(String office, @Nullable Strin
return retval;
}

private Map<String, List<CwmsId>> getLockChildren(String office, String projLike) {
private Map<String, List<CwmsId>> getLockChildLocations(String office, @Nullable String projRegex) {
Map<String, List<CwmsId>> retval = new LinkedHashMap<>();
AV_LOCK view = AV_LOCK.AV_LOCK;
dsl.selectDistinct(view.DB_OFFICE_ID, view.LOCK_ID, view.PROJECT_ID)
.from(view)
.where(view.DB_OFFICE_ID.eq(office))
.and(caseInsensitiveLikeRegexNullTrue(view.PROJECT_ID, projLike))
.and(caseInsensitiveLikeRegexNullTrue(view.PROJECT_ID, projRegex))
.orderBy(view.DB_OFFICE_ID, view.PROJECT_ID, view.LOCK_ID)
.forEach(row -> {
String projId = row.get(view.PROJECT_ID);
Expand All @@ -157,13 +154,13 @@ private Map<String, List<CwmsId>> getLockChildren(String office, String projLike
return retval;
}

private Map<String, List<CwmsId>> getOutletChildren(String office, String projLike) {
private Map<String, List<CwmsId>> getOutletChildLocations(String office, @Nullable String projRegex) {
Map<String, List<CwmsId>> retval = new LinkedHashMap<>();
AV_OUTLET view = AV_OUTLET.AV_OUTLET;
dsl.selectDistinct(view.OFFICE_ID, view.OUTLET_ID, view.PROJECT_ID)
.from(view)
.where(view.OFFICE_ID.eq(office))
.and(caseInsensitiveLikeRegexNullTrue(view.PROJECT_ID, projLike))
.and(caseInsensitiveLikeRegexNullTrue(view.PROJECT_ID, projRegex))
.orderBy(view.OFFICE_ID, view.PROJECT_ID, view.OUTLET_ID)
.forEach(row -> {
String projId = row.get(view.PROJECT_ID);
Expand All @@ -177,13 +174,13 @@ private Map<String, List<CwmsId>> getOutletChildren(String office, String projLi
return retval;
}

private Map<String, List<CwmsId>> getTurbineChildren(String office, String projLike) {
private Map<String, List<CwmsId>> getTurbineChildLocations(String office, @Nullable String projRegex) {
Map<String, List<CwmsId>> retval = new LinkedHashMap<>();
AV_TURBINE view = AV_TURBINE.AV_TURBINE;
dsl.selectDistinct(view.OFFICE_ID, view.TURBINE_ID, view.PROJECT_ID)
.from(view)
.where(view.OFFICE_ID.eq(office))
.and(caseInsensitiveLikeRegexNullTrue(view.PROJECT_ID, projLike))
.and(caseInsensitiveLikeRegexNullTrue(view.PROJECT_ID, projRegex))
.orderBy(view.OFFICE_ID, view.PROJECT_ID, view.TURBINE_ID)
.forEach(row -> {
String projId = row.get(view.PROJECT_ID);
Expand All @@ -197,14 +194,14 @@ private Map<String, List<CwmsId>> getTurbineChildren(String office, String projL
return retval;
}

private Map<String, List<CwmsId>> getEmbankmentChildren(String office, String projLike) {
private Map<String, List<CwmsId>> getEmbankmentChildLocations(String office, @Nullable String projRegex) {
Map<String, List<CwmsId>> retval = new LinkedHashMap<>();
AV_EMBANKMENT view = AV_EMBANKMENT.AV_EMBANKMENT;
dsl.selectDistinct(view.OFFICE_ID, view.EMBANKMENT_LOCATION_ID, view.PROJECT_ID)
.from(view)
.where(view.OFFICE_ID.eq(office)
.and(view.UNIT_SYSTEM.eq("SI")))
.and(caseInsensitiveLikeRegexNullTrue(view.PROJECT_ID, projLike))
.and(caseInsensitiveLikeRegexNullTrue(view.PROJECT_ID, projRegex))
.orderBy(view.OFFICE_ID, view.PROJECT_ID, view.EMBANKMENT_LOCATION_ID)
.forEach(row -> {
String projId = row.get(view.PROJECT_ID);
Expand All @@ -218,5 +215,4 @@ private Map<String, List<CwmsId>> getEmbankmentChildren(String office, String pr
return retval;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@
import org.jetbrains.annotations.Nullable;

/**
* This class holds a project and lists of the project children by kind.
* This class holds a project and lists of the project child locations by kind.
*/
@JsonDeserialize(builder = ProjectChildren.Builder.class)
@JsonDeserialize(builder = ProjectChildLocations.Builder.class)
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonNaming(PropertyNamingStrategies.KebabCaseStrategy.class)
@FormattableWith(contentType = Formats.JSON, formatter = JsonV2.class)
public class ProjectChildren implements CwmsDTOBase {
@FormattableWith(contentType = Formats.JSONV2, aliases = {Formats.JSON}, formatter = JsonV2.class)
public class ProjectChildLocations implements CwmsDTOBase {

private final CwmsId project;

Expand All @@ -55,7 +55,7 @@ public class ProjectChildren implements CwmsDTOBase {
private final List<CwmsId> turbines;
private final List<CwmsId> gates;

private ProjectChildren(Builder builder) {
private ProjectChildLocations(Builder builder) {
this.project = builder.project;
this.embankments = builder.embankments;
this.locks = builder.locks;
Expand Down Expand Up @@ -142,8 +142,8 @@ private static List<CwmsId> wrapList(@Nullable List<CwmsId> embankments) {
return retval;
}

public ProjectChildren build() {
return new ProjectChildren(this);
public ProjectChildLocations build() {
return new ProjectChildLocations(this);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
import org.junit.jupiter.api.Test;

@Tag("integration")
public class ProjectChildrenHandlerIT extends DataApiTestIT {
public class ProjectChildLocationHandlerIT extends DataApiTestIT {

public static final String OFFICE = "SPK";

Expand Down Expand Up @@ -121,7 +121,7 @@ void test_get_embankment() throws Exception {
.when()
.redirects().follow(true)
.redirects().max(3)
.get("/projects/children/")
.get("/projects/child-locations/")
.then()
.log().ifValidationFails(LogDetail.ALL, true)
.assertThat()
Expand Down
Loading

0 comments on commit 842ad77

Please sign in to comment.