Skip to content

Commit

Permalink
Updated controllers to match DAO changes - DB bug for pump retrieval
Browse files Browse the repository at this point in the history
  • Loading branch information
zack-rma committed Aug 13, 2024
1 parent a55295b commit 620625b
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 654 deletions.
8 changes: 8 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 @@ -118,6 +118,8 @@
import cwms.cda.api.project.ProjectPublishStatusUpdate;
import cwms.cda.api.project.RemoveAllLockRevokerRights;
import cwms.cda.api.project.UpdateLockRevokerRights;
import cwms.cda.api.watersupply.AccountingCatalogController;
import cwms.cda.api.watersupply.AccountingCreateController;
import cwms.cda.api.watersupply.WaterContractCatalogController;
import cwms.cda.api.watersupply.WaterContractController;
import cwms.cda.api.watersupply.WaterContractCreateController;
Expand Down Expand Up @@ -544,6 +546,8 @@ protected void configureRoutes() {
addWaterUserHandlers(format("/projects/{%s}/{%s}/water-user", OFFICE, PROJECT_ID), requiredRoles);
addWaterContractHandlers(format("/projects/{%s}/{%s}/water-user/{%s}/contracts", OFFICE, PROJECT_ID,
WATER_USER), requiredRoles);
addAccountingHandlers(format("/projects/{%s}/{%s}/water-user/{%s}"
+ "/contracts/{%s}/accounting", OFFICE, PROJECT_ID, WATER_USER, CONTRACT_NAME), requiredRoles);
delete(format("/projects/{%s}/{%s}/water-user/{%s}/contracts/{%s}/pumps/{%s}", OFFICE, PROJECT_ID,
WATER_USER, CONTRACT_NAME, NAME), new WaterPumpDisassociateController(metrics), requiredRoles);
addWaterContractTypeHandlers(format("/projects/{%s}/contract-types", OFFICE), requiredRoles);
Expand Down Expand Up @@ -578,6 +582,10 @@ protected void configureRoutes() {
addProjectLockRightsHandlers("/project-lock-rights/{project-id}", requiredRoles);
}

private void addAccountingHandlers(String path, RouteRole[] requiredRoles) {
get(path, new AccountingCatalogController(metrics));
post(path, new AccountingCreateController(metrics), requiredRoles);
}

private void addProjectLocksHandlers(String path, RouteRole[] requiredRoles) {
String pathWithoutResource = path.replace(getResourceId(path), "");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@

package cwms.cda.api.watersupply;

import static cwms.cda.api.Controllers.CONTRACT_NAME;
import static cwms.cda.api.Controllers.GET_ALL;
import static cwms.cda.api.Controllers.OFFICE;
import static cwms.cda.api.Controllers.PROJECT_ID;
import static cwms.cda.api.Controllers.STATUS_200;
import static cwms.cda.api.Controllers.STATUS_404;
import static cwms.cda.api.Controllers.STATUS_501;
import static cwms.cda.api.Controllers.WATER_USER;
import static cwms.cda.data.dao.JooqDao.getDslContext;

import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.Timer;
import cwms.cda.api.Controllers;
Expand All @@ -47,22 +57,18 @@
import io.javalin.plugin.openapi.annotations.OpenApiContent;
import io.javalin.plugin.openapi.annotations.OpenApiParam;
import io.javalin.plugin.openapi.annotations.OpenApiResponse;
import org.jetbrains.annotations.NotNull;
import org.jooq.DSLContext;

import javax.servlet.http.HttpServletResponse;
import java.time.Instant;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletResponse;
import org.jetbrains.annotations.NotNull;
import org.jooq.DSLContext;

import static cwms.cda.api.Controllers.*;
import static cwms.cda.data.dao.JooqDao.getDslContext;

public class AccountingCatalogController implements Handler {
private final Logger LOGGER = Logger.getLogger(AccountingCatalogController.class.getName());
private static final String TAG = "Pump Accounting";
private static final String CONTRACT_ID = "contract-id";
private static final String START_TIME = "start";
private static final String START_INCLUSIVE = "start-inclusive";
private static final String END_INCLUSIVE = "end-inclusive";
Expand Down Expand Up @@ -104,7 +110,7 @@ protected WaterSupplyAccountingDao getWaterSupplyAccountingDao(DSLContext dsl) {
required = true),
@OpenApiParam(name = WATER_USER, description = "The water user the pump accounting is "
+ "associated with.", required = true),
@OpenApiParam(name = CONTRACT_ID, description = "The name of the contract associated with "
@OpenApiParam(name = CONTRACT_NAME, description = "The name of the contract associated with "
+ "the pump accounting.", required = true),
@OpenApiParam(name = PROJECT_ID, description = "The project ID the pump accounting is "
+ "associated with.", required = true)
Expand All @@ -122,7 +128,7 @@ protected WaterSupplyAccountingDao getWaterSupplyAccountingDao(DSLContext dsl) {
@OpenApiResponse(status = STATUS_501, description = "Requested format is not implemented")
},
description = "Get pump accounting entries associated with a water supply contract.",
path = "/projects/{office}/water-user/{water-user}/contracts/{contract-id}/accounting",
path = "/projects/{office}/water-user/{water-user}/contracts/{contract-name}/accounting",
method = HttpMethod.GET,
tags = {TAG}
)
Expand All @@ -132,7 +138,7 @@ public void handle(Context ctx) {
try (Timer.Context ignored = markAndTime(GET_ALL)) {
final String office = ctx.pathParam(OFFICE);
final String waterUserName = ctx.pathParam(WATER_USER);
final String contractId = ctx.pathParam(CONTRACT_ID);
final String contractId = ctx.pathParam(CONTRACT_NAME);
final String locationId = ctx.pathParam(PROJECT_ID);
final String startTime = ctx.queryParam(START_TIME) == null
? "1800-01-01T00:00:00Z" : ctx.queryParam(START_TIME);
Expand All @@ -145,7 +151,6 @@ public void handle(Context ctx) {
|| Boolean.parseBoolean(ctx.queryParam(ASCENDING));
final int rowLimit = ctx.queryParam(ROW_LIMIT) != null ? Integer.parseInt(ctx.queryParam(ROW_LIMIT)) : 0;
DSLContext dsl = getDslContext(ctx);
String result;
Instant startInstant = DateUtils.parseUserDate(startTime, "UTC").toInstant();
Instant endInstant = DateUtils.parseUserDate(endTime, "UTC").toInstant();
String formatHeader = ctx.header(Header.ACCEPT) != null ? ctx.header(Header.ACCEPT) : Formats.JSONV1;
Expand All @@ -155,11 +160,12 @@ public void handle(Context ctx) {

WaterContractDao contractDao = new WaterContractDao(dsl);
WaterUser waterUser = contractDao.getWaterUser(projectLocation, waterUserName);
List<WaterUserContract> contract = contractDao.getAllWaterContracts(projectLocation, waterUser.getEntityName());
List<WaterUserContract> contract = contractDao.getAllWaterContracts(projectLocation,
waterUser.getEntityName());

if (waterUser.getEntityName() == null) {
CdaError error = new CdaError("Unable to retrieve accounting - no water user found for the" +
" provided parameters.");
CdaError error = new CdaError("Unable to retrieve accounting - no water user found for the"
+ " provided parameters.");
LOGGER.log(Level.SEVERE, "Error retrieving water pump accounting - no water user found.");
ctx.status(HttpServletResponse.SC_NOT_FOUND).json(error);
return;
Expand All @@ -174,8 +180,8 @@ public void handle(Context ctx) {
}

if (!contractExists) {
CdaError error = new CdaError("Unable to retrieve accounting - no matching contract found for the" +
" provided parameters.");
CdaError error = new CdaError("Unable to retrieve accounting - no matching contract found for the"
+ " provided parameters.");
LOGGER.log(Level.SEVERE, "Error retrieving water pump accounting - no contract found.");
ctx.status(HttpServletResponse.SC_NOT_FOUND).json(error);
return;
Expand All @@ -186,7 +192,7 @@ public void handle(Context ctx) {
projectLocation, null, startInstant, endInstant, startInclusive, endInclusive,
ascending, rowLimit);

result = Formats.format(contentType, accounting, WaterSupplyAccounting.class);
String result = Formats.format(contentType, accounting, WaterSupplyAccounting.class);
ctx.result(result);
ctx.status(HttpServletResponse.SC_OK);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@

package cwms.cda.api.watersupply;

import static cwms.cda.api.Controllers.*;
import static cwms.cda.api.Controllers.CONTRACT_NAME;
import static cwms.cda.api.Controllers.CREATE;
import static cwms.cda.api.Controllers.OFFICE;
import static cwms.cda.api.Controllers.STATUS_204;
import static cwms.cda.api.Controllers.STATUS_501;
import static cwms.cda.api.Controllers.WATER_USER;
import static cwms.cda.data.dao.JooqDao.getDslContext;

import com.codahale.metrics.MetricRegistry;
Expand Down Expand Up @@ -55,7 +60,6 @@

public class AccountingCreateController implements Handler {
private static final String TAG = "Pump Accounting";
private static final String CONTRACT_ID = "contract-id";
private final MetricRegistry metrics;

private Timer.Context markAndTime(String subject) {
Expand All @@ -82,23 +86,23 @@ protected WaterSupplyAccountingDao getWaterSupplyAccountingDao(DSLContext dsl) {
required = true),
@OpenApiParam(name = WATER_USER, description = "The water user the accounting is associated with.",
required = true),
@OpenApiParam(name = CONTRACT_ID, description = "The name of the contract associated with the accounting.",
required = true),
@OpenApiParam(name = CONTRACT_NAME, description = "The name of the contract associated with the "
+ "accounting.", required = true),
},
responses = {
@OpenApiResponse(status = STATUS_204, description = "The pump accounting entry was created."),
@OpenApiResponse(status = STATUS_501, description = "Requested format is not implemented")
},
description = "Create a new pump accounting entry associated with a water supply contract.",
path = "/projects/{office}/water-user/{water-user}/contracts/{contract-id}/accounting",
path = "/projects/{office}/water-user/{water-user}/contracts/{contract-name}/accounting",
method = HttpMethod.POST,
tags = {TAG}
)

@Override
public void handle(@NotNull Context ctx) {
try (Timer.Context ignored = markAndTime(CREATE)) {
final String contractId = ctx.pathParam(CONTRACT_ID);
final String contractId = ctx.pathParam(CONTRACT_NAME);
final String office = ctx.pathParam(OFFICE);
DSLContext dsl = getDslContext(ctx);
String formatHeader = ctx.header(Header.ACCEPT) != null ? ctx.header(Header.ACCEPT) : Formats.JSONV1;
Expand Down

This file was deleted.

Loading

0 comments on commit 620625b

Please sign in to comment.