Skip to content

Commit

Permalink
Query Project children of a kind.
Browse files Browse the repository at this point in the history
  • Loading branch information
rma-rripken committed Jul 25, 2024
1 parent d209b68 commit a7874b2
Show file tree
Hide file tree
Showing 9 changed files with 1,082 additions and 7 deletions.
2 changes: 2 additions & 0 deletions cwms-data-api/src/main/java/cwms/cda/ApiServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +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.location.kind.VirtualOutletCreateController;
import cwms.cda.data.dao.JooqDao;
import cwms.cda.formatters.Formats;
Expand Down Expand Up @@ -519,6 +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));
cdaCrudCache(format("/projects/{%s}", Controllers.NAME),
new ProjectController(metrics), requiredRoles,5, TimeUnit.MINUTES);
cdaCrudCache(format("/properties/{%s}", Controllers.NAME),
Expand Down
8 changes: 1 addition & 7 deletions cwms-data-api/src/main/java/cwms/cda/api/Controllers.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,6 @@ public final class Controllers {
public static final String LOCATION_CATEGORY_LIKE = "location-category-like";
public static final String LOCATION_GROUP_LIKE = "location-group-like";




public static final String TIMESERIES_GROUP_LIKE = "timeseries-group-like";
public static final String ACCEPT = "Accept";
public static final String CLOB_ID = "clob-id";
Expand Down Expand Up @@ -145,7 +142,6 @@ public final class Controllers {
public static final String ANY_MASK = "*";
public static final String OFFICE_MASK = "office-mask";
public static final String ID_MASK = "id-mask";
public static final String LOCATION_MASK = "location-mask";
public static final String NAME_MASK = "name-mask";
public static final String BOTTOM_MASK = "bottom-mask";
public static final String TOP_MASK = "top-mask";
Expand All @@ -164,9 +160,6 @@ public final class Controllers {
public static final String STATUS_501 = "501";
public static final String STATUS_400 = "400";
public static final String TEXT_MASK = "text-mask";
public static final String DELETE_MODE = "delete-mode";
public static final String MIN_ATTRIBUTE = "min-attribute";
public static final String MAX_ATTRIBUTE = "max-attribute";
public static final String STANDARD_TEXT_ID_MASK = "standard-text-id-mask";
public static final String STANDARD_TEXT_ID = "standard-text-id";
public static final String STREAM_ID_MASK = "stream-id-mask";
Expand All @@ -189,6 +182,7 @@ public final class Controllers {
public static final String DEFAULT_VALUE = "default-value";
public static final String CATEGORY = "category";
public static final String PREFIX = "prefix";
public static final String PROJECT_LIKE = "project-like";

private static final String DEPRECATED_HEADER = "CWMS-DATA-Format-Deprecated";
private static final String DEPRECATED_TAB = "2024-11-01 TAB is not used often.";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*
* MIT License
*
* Copyright (c) 2024 Hydrologic Engineering Center
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package cwms.cda.api.project;

import static com.codahale.metrics.MetricRegistry.name;
import static cwms.cda.api.Controllers.GET_ALL;
import static cwms.cda.api.Controllers.LOCATION_KIND_LIKE;
import static cwms.cda.api.Controllers.OFFICE;
import static cwms.cda.api.Controllers.PROJECT_LIKE;
import static cwms.cda.api.Controllers.STATUS_200;
import static cwms.cda.api.Controllers.requiredParam;

import com.codahale.metrics.Histogram;
import com.codahale.metrics.MetricRegistry;
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.formatters.ContentType;
import cwms.cda.formatters.Formats;
import io.javalin.core.util.Header;
import io.javalin.http.Context;
import io.javalin.http.Handler;
import io.javalin.plugin.openapi.annotations.HttpMethod;
import io.javalin.plugin.openapi.annotations.OpenApi;
import io.javalin.plugin.openapi.annotations.OpenApiContent;
import io.javalin.plugin.openapi.annotations.OpenApiParam;
import io.javalin.plugin.openapi.annotations.OpenApiResponse;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.jetbrains.annotations.NotNull;


public class ProjectChildrenHandler implements Handler {
public static final String TAGS = "Projects";
public static final String PATH = "/projects/children";
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) {
this.metrics = metrics;
requestResultSize = this.metrics.histogram((name(ProjectChildrenHandler.class, Controllers.RESULTS,
Controllers.SIZE)));
}


@OpenApi(
description = "Get a list of project child locations",
queryParams = {
@OpenApiParam(name = OFFICE, required = true),
@OpenApiParam(name = PROJECT_LIKE, description = "Posix <a href=\"regexp.html\">regular expression</a> matching "
+ "against the project_id."),
@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:"
+ "{\"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)})
},
tags = {TAGS},
path = PATH,
method = HttpMethod.GET
)
@Override
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));
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);
String formatHeader = ctx.header(Header.ACCEPT);
ContentType contentType = Formats.parseHeader(formatHeader, ProjectChildren.class);
String result = Formats.format(contentType, children, ProjectChildren.class);
ctx.result(result).contentType(contentType.toString());
requestResultSize.update(result.length());
ctx.status(HttpServletResponse.SC_OK);
}
}


}
Loading

0 comments on commit a7874b2

Please sign in to comment.