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 14, 2024
1 parent d2ba6a0 commit 5036281
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 41 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
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

import cwms.cda.api.enums.Nation;
import cwms.cda.data.dao.DeleteRule;
import cwms.cda.data.dao.JooqDao.DeleteMethod;
import cwms.cda.data.dao.LocationsDaoImpl;
import cwms.cda.data.dao.LookupTypeDao;
import cwms.cda.data.dao.project.ProjectDao;
Expand All @@ -51,7 +52,6 @@
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.testcontainers.shaded.org.apache.commons.io.IOUtils;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
Expand All @@ -77,6 +77,10 @@ class WaterSupplyAccountingControllerIT extends DataApiTestIT {
private static final String ASCENDING = "ascending";
private static final WaterUserContract CONTRACT;
private static final LookupType testTransferType;
private static final LookupType testContractType;
private static final Location pump1;
private static final Location pump3;

static {
try (InputStream accountStream = WaterSupplyAccounting.class
.getResourceAsStream("/cwms/cda/api/pump_accounting.json");
Expand All @@ -90,6 +94,11 @@ class WaterSupplyAccountingControllerIT extends DataApiTestIT {
WATER_SUPPLY_ACCOUNTING = Formats.parseContent(new ContentType(Formats.JSONV1),
accountingJson, WaterSupplyAccounting.class);
testTransferType = WATER_SUPPLY_ACCOUNTING.getPumpAccounting().get(0).getTransferType();
testContractType = CONTRACT.getContractType();
pump1 = buildTestLocation(WATER_SUPPLY_ACCOUNTING.getPumpAccounting().get(0).getPumpLocation().getName(),
"PUMP");
pump3 = buildTestLocation(WATER_SUPPLY_ACCOUNTING.getPumpAccounting().get(1).getPumpLocation().getName(),
"PUMP");
} catch (Exception e) {
throw new RuntimeException(e);
}
Expand All @@ -101,20 +110,8 @@ static void setup() throws Exception {
// create water user
// create water user contract

Location contractLocation = new Location.Builder(CONTRACT.getContractId().getOfficeId(),
CONTRACT.getContractId().getName()).withLocationKind("PROJECT")
.withTimeZoneName(ZoneId.of("UTC"))
.withHorizontalDatum("WGS84").withLongitude(78.0).withLatitude(67.9).withVerticalDatum("WGS84")
.withLongName("TEST CONTRACT LOCATION").withActive(true).withMapLabel("LABEL").withNation(Nation.US)
.withElevation(456.7).withElevationUnits("m").withPublishedLongitude(78.9).withPublishedLatitude(45.3)
.withLocationType("PROJECT").withDescription("TEST PROJECT").build();
Location parentLocation = new Location.Builder(CONTRACT.getWaterUser().getProjectId().getOfficeId(),
CONTRACT.getWaterUser().getProjectId().getName()).withLocationKind("PROJECT")
.withTimeZoneName(ZoneId.of("UTC")).withHorizontalDatum("WGS84")
.withLongitude(38.0).withLatitude(56.5).withVerticalDatum("WGS84")
.withLongName("TEST CONTRACT LOCATION").withActive(true).withMapLabel("LABEL").withNation(Nation.US)
.withElevation(456.7).withElevationUnits("m").withPublishedLongitude(78.9).withPublishedLatitude(45.3)
.withLocationType("PROJECT").withDescription("TEST PROJECT").build();
Location contractLocation = buildTestLocation(CONTRACT.getContractId().getName(), "PROJECT");
Location parentLocation = buildTestLocation(CONTRACT.getWaterUser().getProjectId().getName(), "PROJECT");

Project project = new Project.Builder().withLocation(parentLocation)
.withFederalCost(BigDecimal.valueOf(123456789))
Expand All @@ -134,11 +131,15 @@ static void setup() throws Exception {
try {
lookupTypeDao.storeLookupType("AT_PHYSICAL_TRANSFER_TYPE","PHYS_TRANS_TYPE",
testTransferType);
lookupTypeDao.storeLookupType("AT_WS_CONTRACT_TYPE","WS_CONTRACT_TYPE",
testContractType);
locationsDao.storeLocation(contractLocation);
locationsDao.storeLocation(parentLocation);
locationsDao.storeLocation(pump1);
locationsDao.storeLocation(pump3);
projectDao.store(project, true);
waterContractDao.storeWaterUser(waterUser, true);
waterContractDao.storeWaterContract(CONTRACT, true, false);
waterContractDao.storeWaterContract(CONTRACT, true, true);
} catch (IOException e) {
throw new RuntimeException(e);
}
Expand Down Expand Up @@ -167,10 +168,16 @@ static void cleanup() throws Exception {
LocationsDaoImpl locationsDao = new LocationsDaoImpl(ctx);
LookupTypeDao lookupTypeDao = new LookupTypeDao(ctx);
ProjectDao projectDao = new ProjectDao(ctx);
WaterContractDao waterContractDao = new WaterContractDao(ctx);
waterContractDao.deleteWaterContract(CONTRACT, DeleteMethod.DELETE_ALL);
lookupTypeDao.deleteLookupType("AT_PHYSICAL_TRANSFER_TYPE", "PHYS_TRANS_TYPE",
OFFICE_ID, testTransferType.getDisplayValue());
lookupTypeDao.deleteLookupType("AT_WS_CONTRACT_TYPE", "WS_CONTRACT_TYPE",
OFFICE_ID, testContractType.getDisplayValue());
projectDao.delete(CONTRACT.getOfficeId(), CONTRACT.getWaterUser().getProjectId().getName(),
DeleteRule.DELETE_ALL);
locationsDao.deleteLocation(pump1.getName(), pump1.getOfficeId(), true);
locationsDao.deleteLocation(pump3.getName(), pump3.getOfficeId(), true);
locationsDao.deleteLocation(contractLocation.getName(), contractLocation.getOfficeId(), true);
locationsDao.deleteLocation(parentLocation.getName(), parentLocation.getOfficeId(), true);
}, CwmsDataApiSetupCallback.getWebUser());
Expand Down Expand Up @@ -290,4 +297,13 @@ void testRetrieveNotFoundOutsideTimeWindow() throws Exception {
.body(is("[]"))
;
}

private static Location buildTestLocation(String name, String locationKind) {
return new Location.Builder(OFFICE_ID, name).withLocationKind(locationKind)
.withTimeZoneName(ZoneId.of("UTC"))
.withHorizontalDatum("NAD84").withLongitude(-121.73).withLatitude(38.56).withVerticalDatum("WGS84")
.withLongName("TEST CONTRACT LOCATION").withActive(true).withMapLabel("LABEL").withNation(Nation.US)
.withElevation(456.7).withElevationUnits("m").withPublishedLongitude(-121.73).withPublishedLatitude(38.56)
.withLocationType(locationKind).withDescription("TEST PROJECT").build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ void testWaterSupplyAccountingSerializationRoundTripFromFile() throws Exception
.withContractName("Test Contract")
.withPumpAccounting(buildPumpAccounting()).build();
InputStream resource = this.getClass().getResourceAsStream(
"/cwms/cda/data/dto/watersupply/water_supply_accounting.json");
"/cwms/cda/data/dto/watersupply/pump_accounting.json");
assertNotNull(resource);
String serialized = IOUtils.toString(resource, StandardCharsets.UTF_8);
WaterSupplyAccounting deserialized = Formats.parseContent(Formats.parseHeader(Formats.JSONV1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
{
"pump-location": {
"office-id": "SWT",
"name": "PUMP1"
"name": "SACRAMENTO-PUMP1"
},
"transfer-type": {
"office-id": "SWT",
Expand Down

0 comments on commit 5036281

Please sign in to comment.