Skip to content

Commit

Permalink
Merge branch 'develop' into feature/cto-137-project-sub-location
Browse files Browse the repository at this point in the history
  • Loading branch information
rma-rripken authored Aug 5, 2024
2 parents 5912c77 + 6df27a6 commit d8af3d7
Show file tree
Hide file tree
Showing 28 changed files with 1,810 additions and 65 deletions.
61 changes: 26 additions & 35 deletions cwms-data-api/src/main/java/cwms/cda/api/BasinController.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,25 @@

package cwms.cda.api;

import static cwms.cda.api.Controllers.METHOD;
import static cwms.cda.api.Controllers.STATUS_200;
import static cwms.cda.api.Controllers.STATUS_204;
import static cwms.cda.api.Controllers.STATUS_404;
import static cwms.cda.api.Controllers.STATUS_501;
import static cwms.cda.api.Controllers.DELETE_MODE;
import static cwms.cda.api.Controllers.GET_ALL;
import static cwms.cda.api.Controllers.GET_ONE;
import static cwms.cda.api.Controllers.NAME;
import static cwms.cda.api.Controllers.OFFICE;
import static cwms.cda.api.Controllers.UNIT;
import static cwms.cda.api.Controllers.requiredParam;
import static cwms.cda.api.Controllers.requiredParamAs;
import static cwms.cda.data.dao.JooqDao.getDslContext;

import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.Timer;
import cwms.cda.api.enums.UnitSystem;
import cwms.cda.api.errors.CdaError;
import cwms.cda.data.dao.JooqDao;
import cwms.cda.data.dao.basinconnectivity.BasinDao;
import cwms.cda.data.dto.CwmsId;
import cwms.cda.data.dto.basinconnectivity.Basin;
Expand Down Expand Up @@ -184,6 +187,7 @@ public void getOne(@NotNull Context ctx, @NotNull String name) {

try (final Timer.Context ignored = markAndTime(GET_ONE)) {
DSLContext dsl = getDslContext(ctx);

String units =
ctx.queryParamAsClass(UNIT, String.class).getOrDefault(UnitSystem.EN.value());
String office = ctx.queryParam(OFFICE);
Expand All @@ -206,8 +210,7 @@ public void getOne(@NotNull Context ctx, @NotNull String name) {
.withOfficeId(office)
.build();
cwms.cda.data.dto.basin.Basin basin = basinDao.getBasin(basinId, units);
result = Formats.format(contentType, Collections.singletonList(basin),
cwms.cda.data.dto.basin.Basin.class);
result = Formats.format(contentType, basin);
ctx.result(result);
ctx.status(HttpServletResponse.SC_OK);
}
Expand All @@ -224,6 +227,8 @@ public void getOne(@NotNull Context ctx, @NotNull String name) {
@OpenApiParam(name = NAME, required = true, description = "Specifies the new name for the basin.")
},
pathParams = {
@OpenApiParam(name = OFFICE, required = true, description = "Specifies the owning office of "
+ "the basin to be renamed."),
@OpenApiParam(name = NAME, description = "Specifies the name of "
+ "the basin to be renamed.")
},
Expand All @@ -233,35 +238,26 @@ public void getOne(@NotNull Context ctx, @NotNull String name) {
@OpenApiResponse(status = STATUS_501, description = "Requested format is not "
+ "implemented")
},
method = HttpMethod.PATCH,
description = "Renames CWMS Basin",
tags = {TAG}
)
@Override
public void update(@NotNull Context ctx, @NotNull String name) {
DSLContext dsl = getDslContext(ctx);
String formatHeader = ctx.header(Header.ACCEPT) != null ? ctx.header(Header.ACCEPT) :
Formats.JSONV1;
ContentType contentType = Formats.parseHeader(formatHeader, Basin.class);
ctx.contentType(contentType.toString());

cwms.cda.data.dto.basin.Basin basin = Formats.parseContent(contentType, ctx.body(),
cwms.cda.data.dto.basin.Basin.class);

if (contentType.getType().equals(Formats.NAMED_PGJSON)) {
ctx.status(HttpCode.NOT_IMPLEMENTED).json(CdaError.notImplemented());
} else {
cwms.cda.data.dao.basin.BasinDao basinDao = new cwms.cda.data.dao.basin.BasinDao(dsl);
CwmsId oldLoc = new CwmsId.Builder()
.withName(name)
.withOfficeId(basin.getBasinId().getOfficeId())
.build();
CwmsId newLoc = new CwmsId.Builder()
.withName(ctx.queryParam(NAME))
.withOfficeId(basin.getBasinId().getOfficeId())
.build();
basinDao.renameBasin(oldLoc, newLoc);
ctx.status(HttpServletResponse.SC_OK).json("Updated Location");
}
String officeId = requiredParam(ctx, OFFICE);
String newStreamId = requiredParam(ctx, NAME);
cwms.cda.data.dao.basin.BasinDao basinDao = new cwms.cda.data.dao.basin.BasinDao(dsl);
CwmsId oldLoc = new CwmsId.Builder()
.withName(name)
.withOfficeId(officeId)
.build();
CwmsId newLoc = new CwmsId.Builder()
.withName(newStreamId)
.withOfficeId(officeId)
.build();
basinDao.renameBasin(oldLoc, newLoc);
ctx.status(HttpServletResponse.SC_OK).json("Updated Location");
}

@OpenApi(
Expand Down Expand Up @@ -294,7 +290,8 @@ public void create(@NotNull Context ctx) {
queryParams = {
@OpenApiParam(name = OFFICE, required = true, description = "Specifies the"
+ " owning office of the basin to be renamed."),
@OpenApiParam(name = DELETE_MODE, required = true, description = "Specifies the delete method used.")
@OpenApiParam(name = METHOD, required = true, description = "Specifies the delete method used.",
type = JooqDao.DeleteMethod.class)
},
pathParams = {
@OpenApiParam(name = NAME, description = "Specifies the name of "
Expand All @@ -312,19 +309,13 @@ public void create(@NotNull Context ctx) {
@Override
public void delete(@NotNull Context ctx, @NotNull String name) {
DSLContext dsl = getDslContext(ctx);
String deleteMethod = ctx.queryParam(DELETE_MODE);
JooqDao.DeleteMethod deleteMethod = requiredParamAs(ctx, METHOD, JooqDao.DeleteMethod.class);
cwms.cda.data.dao.basin.BasinDao basinDao = new cwms.cda.data.dao.basin.BasinDao(dsl);
CwmsId basinId = new CwmsId.Builder()
.withName(name)
.withOfficeId(ctx.queryParam(OFFICE))
.build();
cwms.cda.data.dto.basin.Basin retBasin = basinDao.getBasin(basinId, "EN");
if (retBasin == null) {
CdaError error = new CdaError("No matching basin " + name);
ctx.status(HttpServletResponse.SC_NOT_FOUND).json(error);
return;
}
basinDao.deleteBasin(basinId, deleteMethod);
basinDao.deleteBasin(basinId, deleteMethod.getRule());
ctx.status(HttpServletResponse.SC_NO_CONTENT).json(basinId.getName() + " Deleted");
}
}
14 changes: 14 additions & 0 deletions cwms-data-api/src/main/java/cwms/cda/api/Controllers.java
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ public final class Controllers {
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 Down Expand Up @@ -340,6 +342,18 @@ public static String requiredParam(io.javalin.http.Context ctx, String name) {
return param;
}

/**
* Returns the first matching query param or throws RequiredQueryParameterException.
* @param ctx Request Context
* @param name Query parameter name
* @return value of the parameter
* @throws RequiredQueryParameterException if the parameter is not found
*/
public static <T> T requiredParamAs(io.javalin.http.Context ctx, String name, Class<T> type) {
return ctx.queryParamAsClass(name, type)
.getOrThrow(e -> new RequiredQueryParameterException(name));
}

@Nullable
public static ZonedDateTime queryParamAsZdt(Context ctx, String param, String timezone) {
ZonedDateTime beginZdt = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

package cwms.cda.data.dao.basin;

import cwms.cda.data.dao.DeleteRule;
import cwms.cda.data.dao.JooqDao;
import cwms.cda.data.dto.CwmsId;
import cwms.cda.data.dto.basin.Basin;
Expand Down Expand Up @@ -128,12 +129,13 @@ public void renameBasin(CwmsId oldBasin, CwmsId newBasin) {

}

public void deleteBasin(CwmsId basinId, String deleteAction) {
public void deleteBasin(CwmsId basinId, DeleteRule deleteAction) {
basinId.validate();

connection(dsl, c -> {
setOffice(c, basinId.getOfficeId());
CWMS_BASIN_PACKAGE.call_DELETE_BASIN(DSL.using(c).configuration(), basinId.getName(), deleteAction, basinId.getOfficeId());
CWMS_BASIN_PACKAGE.call_DELETE_BASIN(DSL.using(c).configuration(), basinId.getName(),
deleteAction.getRule(), basinId.getOfficeId());
});
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
/*
*
* 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.data.dao.watersupply;

import static java.util.stream.Collectors.toList;

import cwms.cda.api.errors.NotFoundException;
import cwms.cda.data.dao.JooqDao;
import cwms.cda.data.dao.location.kind.LocationUtil;
import cwms.cda.data.dto.CwmsId;
import cwms.cda.data.dto.LookupType;
import cwms.cda.data.dto.watersupply.PumpType;
import cwms.cda.data.dto.watersupply.WaterUser;
import cwms.cda.data.dto.watersupply.WaterUserContract;
import java.util.List;
import org.jooq.DSLContext;
import org.jooq.impl.DSL;
import usace.cwms.db.dao.util.OracleTypeMap;
import usace.cwms.db.jooq.codegen.packages.CWMS_WATER_SUPPLY_PACKAGE;
import usace.cwms.db.jooq.codegen.udt.records.LOCATION_REF_T;
import usace.cwms.db.jooq.codegen.udt.records.LOOKUP_TYPE_TAB_T;
import usace.cwms.db.jooq.codegen.udt.records.WATER_USER_CONTRACT_REF_T;
import usace.cwms.db.jooq.codegen.udt.records.WATER_USER_CONTRACT_TAB_T;
import usace.cwms.db.jooq.codegen.udt.records.WATER_USER_OBJ_T;
import usace.cwms.db.jooq.codegen.udt.records.WATER_USER_TAB_T;


public class WaterContractDao extends JooqDao<WaterUserContract> {
public WaterContractDao(DSLContext dsl) {
super(dsl);
}

public List<WaterUserContract> getAllWaterContracts(CwmsId projectLocation, String entityName) {
return connectionResult(dsl, c -> {
setOffice(c, projectLocation.getOfficeId());
LOCATION_REF_T projectLocationRef = LocationUtil.getLocationRef(projectLocation);
return CWMS_WATER_SUPPLY_PACKAGE.call_RETRIEVE_CONTRACTS(
DSL.using(c).configuration(), projectLocationRef, entityName)
.stream()
.map(WaterSupplyUtils::toWaterContract)
.collect(toList());
});
}

public WaterUserContract getWaterContract(String contractName, CwmsId projectLocation, String entityName) {
return connectionResult(dsl, c -> {
setOffice(c, projectLocation.getOfficeId());
LOCATION_REF_T projectLocationRef = LocationUtil.getLocationRef(projectLocation);
return CWMS_WATER_SUPPLY_PACKAGE.call_RETRIEVE_CONTRACTS(
DSL.using(c).configuration(), projectLocationRef, entityName)
.stream()
.map(WaterSupplyUtils::toWaterContract)
.filter(contract -> contract.getContractId().getName().equals(contractName))
.findAny()
.orElseThrow(() -> new NotFoundException("Water contract not found: " + contractName));
});
}

public List<LookupType> getAllWaterContractTypes(String officeId) {
return connectionResult(dsl, c -> {
setOffice(c, officeId);
return CWMS_WATER_SUPPLY_PACKAGE.call_GET_CONTRACT_TYPES(
DSL.using(c).configuration(), officeId)
.stream()
.map(LocationUtil::getLookupType)
.collect(toList());
});
}

public List<WaterUser> getAllWaterUsers(CwmsId projectLocation) {
return connectionResult(dsl, c -> {
setOffice(c, projectLocation.getOfficeId());
LOCATION_REF_T projectLocationRef = LocationUtil.getLocationRef(projectLocation);
return CWMS_WATER_SUPPLY_PACKAGE.call_RETRIEVE_WATER_USERS(
DSL.using(c).configuration(), projectLocationRef)
.stream()
.map(WaterSupplyUtils::toWaterUser)
.collect(toList());
});
}

public WaterUser getWaterUser(CwmsId projectLocation, String entityName) {
return connectionResult(dsl, c -> {
setOffice(c, projectLocation.getOfficeId());
LOCATION_REF_T projectLocationRef = LocationUtil.getLocationRef(projectLocation);
return CWMS_WATER_SUPPLY_PACKAGE.call_RETRIEVE_WATER_USERS(
DSL.using(c).configuration(), projectLocationRef)
.stream()
.map(WaterSupplyUtils::toWaterUser)
.filter(waterUser -> waterUser.getEntityName().equals(entityName))
.findAny()
.orElseThrow(() -> new NotFoundException("Water user not found: " + entityName));
});
}

public void storeWaterContract(WaterUserContract waterContract, boolean failIfExists, boolean ignoreNulls) {
connection(dsl, c -> {
setOffice(c, waterContract.getOfficeId());
String paramFailIfExists = OracleTypeMap.formatBool(failIfExists);
String paramIgnoreNulls = OracleTypeMap.formatBool(ignoreNulls);
WATER_USER_CONTRACT_TAB_T paramContracts = WaterSupplyUtils.toWaterUserContractTs(waterContract);
CWMS_WATER_SUPPLY_PACKAGE.call_STORE_CONTRACTS2(DSL.using(c).configuration(), paramContracts,
paramFailIfExists, paramIgnoreNulls);
});
}

public void renameWaterUser(String oldWaterUser, String newWaterUser, CwmsId projectLocation) {
connection(dsl, c -> {
setOffice(c, projectLocation.getOfficeId());
LOCATION_REF_T projectLocationRefT = LocationUtil.getLocationRef(projectLocation);
CWMS_WATER_SUPPLY_PACKAGE.call_RENAME_WATER_USER(DSL.using(c).configuration(), projectLocationRefT,
oldWaterUser, newWaterUser);
});
}

public void storeWaterUser(WaterUser waterUser, boolean failIfExists) {
connection(dsl, c -> {
setOffice(c, waterUser.getProjectId().getOfficeId());
WATER_USER_TAB_T waterUsers = WaterSupplyUtils.toWaterUserTs(waterUser);
String paramFailIfExists = OracleTypeMap.formatBool(failIfExists);
CWMS_WATER_SUPPLY_PACKAGE.call_STORE_WATER_USERS(DSL.using(c).configuration(),
waterUsers, paramFailIfExists);
});
}

public void renameWaterContract(WaterUser waterUser, String oldContractName,
String newContractName) {
connection(dsl, c -> {
setOffice(c, waterUser.getProjectId().getOfficeId());
WATER_USER_OBJ_T waterUserT = WaterSupplyUtils.toWaterUser(waterUser);
WATER_USER_CONTRACT_REF_T waterUserContract = new WATER_USER_CONTRACT_REF_T(waterUserT, oldContractName);
CWMS_WATER_SUPPLY_PACKAGE.call_RENAME_CONTRACT(DSL.using(c).configuration(), waterUserContract,
oldContractName, newContractName);
});
}

public void deleteWaterUser(CwmsId location, String entityName, String deleteAction) {
connection(dsl, c -> {
setOffice(c, location.getOfficeId());
LOCATION_REF_T projectLocationRef = LocationUtil.getLocationRef(location);
CWMS_WATER_SUPPLY_PACKAGE.call_DELETE_WATER_USER(DSL.using(c).configuration(), projectLocationRef,
entityName, deleteAction);
});
}

public void deleteWaterContract(WaterUserContract contract, String deleteAction) {
connection(dsl, c -> {
setOffice(c, contract.getOfficeId());
WATER_USER_OBJ_T waterUserT = WaterSupplyUtils.toWaterUser(contract.getWaterUser());
String contractName = contract.getContractId().getName();
WATER_USER_CONTRACT_REF_T waterUserContract = new WATER_USER_CONTRACT_REF_T(waterUserT, contractName);
CWMS_WATER_SUPPLY_PACKAGE.call_DELETE_CONTRACT(DSL.using(c).configuration(), waterUserContract,
deleteAction);
});
}

public void storeWaterContractTypes(List<LookupType> lookupTypes,
boolean failIfExists) {
connection(dsl, c -> {
setOffice(c, lookupTypes.get(0).getOfficeId());
LOOKUP_TYPE_TAB_T contractTypes = WaterSupplyUtils.toLookupTypeT(lookupTypes);
String paramFailIfExists = OracleTypeMap.formatBool(failIfExists);
CWMS_WATER_SUPPLY_PACKAGE.call_SET_CONTRACT_TYPES(DSL.using(c).configuration(),
contractTypes, paramFailIfExists);
});
}

public void removePumpFromContract(WaterUserContract contract, String pumpLocName,
PumpType pumpType, boolean deleteAccountingData) {
connection(dsl, c -> {
setOffice(c, contract.getOfficeId());
WATER_USER_CONTRACT_REF_T waterUserContractRefT = WaterSupplyUtils
.toWaterUserContractRefTs(contract);
String paramDeleteAccountingData = OracleTypeMap.formatBool(deleteAccountingData);
CWMS_WATER_SUPPLY_PACKAGE.call_DISASSOCIATE_PUMP(DSL.using(c).configuration(),
waterUserContractRefT, pumpLocName, pumpType.toString(), paramDeleteAccountingData);
});
}
}
Loading

0 comments on commit d8af3d7

Please sign in to comment.