diff --git a/cwms-data-api/src/main/java/cwms/cda/data/dao/watersupply/WaterSupplyAccountingDao.java b/cwms-data-api/src/main/java/cwms/cda/data/dao/watersupply/WaterSupplyAccountingDao.java index 504d597f0..48b429d31 100644 --- a/cwms-data-api/src/main/java/cwms/cda/data/dao/watersupply/WaterSupplyAccountingDao.java +++ b/cwms-data-api/src/main/java/cwms/cda/data/dao/watersupply/WaterSupplyAccountingDao.java @@ -36,7 +36,6 @@ import java.time.Instant; import java.util.ArrayList; import java.util.List; -import java.util.TimeZone; import org.jooq.DSLContext; import org.jooq.impl.DSL; import usace.cwms.db.jooq.codegen.packages.CWMS_WATER_SUPPLY_PACKAGE; @@ -52,7 +51,6 @@ public WaterSupplyAccountingDao(DSLContext dsl) { } public void storeAccounting(WaterSupplyAccounting accounting) { - TimeZone timeZone = TimeZone.getTimeZone("UTC"); String volumeUnitId = null; String storeRule = Const.Delete_Insert; boolean overrideProtection = false; @@ -64,7 +62,7 @@ public void storeAccounting(WaterSupplyAccounting accounting) { WATER_USER_CONTRACT_REF_T contractRefT = WaterSupplyUtils .toContractRef(accounting.getWaterUser(), accounting.getContractName()); LOC_REF_TIME_WINDOW_TAB_T pumpTimeWindowTab = WaterSupplyUtils.toTimeWindowTabT(accounting); - String timeZoneId = timeZone == null ? null : timeZone.getID(); + String timeZoneId = "UTC"; String overrideProt = formatBool(overrideProtection); CWMS_WATER_SUPPLY_PACKAGE.call_STORE_ACCOUNTING_SET(DSL.using(c).configuration(), accountingTab, contractRefT, pumpTimeWindowTab, timeZoneId, volumeUnitId, storeRule, overrideProt); @@ -79,7 +77,7 @@ public List retrieveAccounting(String contractName, Water WATER_USER_CONTRACT_REF_T contractRefT = WaterSupplyUtils.toContractRef(waterUser, contractName); Timestamp startTimestamp = Timestamp.from(startTime); Timestamp endTimestamp = Timestamp.from(endTime); - String timeZoneId = null; + String timeZoneId = "UTC"; String startInclusiveFlag = formatBool(startInclusive); String endInclusiveFlag = formatBool(endInclusive); String ascendingFlagStr = formatBool(ascendingFlag); diff --git a/cwms-data-api/src/main/java/cwms/cda/data/dao/watersupply/WaterSupplyUtils.java b/cwms-data-api/src/main/java/cwms/cda/data/dao/watersupply/WaterSupplyUtils.java index c62f3199d..7e73a4e1b 100644 --- a/cwms-data-api/src/main/java/cwms/cda/data/dao/watersupply/WaterSupplyUtils.java +++ b/cwms-data-api/src/main/java/cwms/cda/data/dao/watersupply/WaterSupplyUtils.java @@ -30,7 +30,6 @@ 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.AccountingKey; import cwms.cda.data.dto.watersupply.PumpLocation; import cwms.cda.data.dto.watersupply.PumpTransfer; import cwms.cda.data.dto.watersupply.PumpType; @@ -48,7 +47,9 @@ import java.util.TreeMap; import java.util.logging.Level; import java.util.logging.Logger; +import org.jetbrains.annotations.NotNull; import org.jooq.impl.DSL; +import usace.cwms.db.jooq.codegen.udt.records.LOCATION_REF_T; import usace.cwms.db.jooq.codegen.udt.records.LOC_REF_TIME_WINDOW_OBJ_T; import usace.cwms.db.jooq.codegen.udt.records.LOC_REF_TIME_WINDOW_TAB_T; import usace.cwms.db.jooq.codegen.udt.records.LOOKUP_TYPE_OBJ_T; @@ -61,7 +62,7 @@ import usace.cwms.db.jooq.codegen.udt.records.WAT_USR_CONTRACT_ACCT_TAB_T; -final class WaterSupplyUtils { +public final class WaterSupplyUtils { private static final Logger LOGGER = Logger.getLogger(WaterSupplyUtils.class.getName()); private WaterSupplyUtils() { @@ -170,6 +171,10 @@ public static WATER_USER_CONTRACT_TAB_T toWaterUserContractTs(WaterUserContract public static WAT_USR_CONTRACT_ACCT_TAB_T toWaterUserContractAcctTs(WaterSupplyAccounting accounting) { List watUsrContractAcctObjTList = new ArrayList<>(); + LOCATION_REF_T pumpIn = LocationUtil.getLocationRef(accounting.getPumpLocations().getPumpIn()); + LOCATION_REF_T pumpOut = LocationUtil.getLocationRef(accounting.getPumpLocations().getPumpOut()); + LOCATION_REF_T pumpBelow = LocationUtil.getLocationRef(accounting.getPumpLocations().getPumpBelow()); + for (Map.Entry> entry : accounting.getPumpAccounting().entrySet()) { for (PumpTransfer transfer : entry.getValue()) { WAT_USR_CONTRACT_ACCT_OBJ_T watUsrContractAcctObjT = new WAT_USR_CONTRACT_ACCT_OBJ_T(); @@ -186,20 +191,18 @@ public static WAT_USR_CONTRACT_ACCT_TAB_T toWaterUserContractAcctTs(WaterSupplyA watUsrContractAcctObjT.setPHYSICAL_TRANSFER_TYPE(transferType); switch (transfer.getPumpType()) { case IN: - watUsrContractAcctObjT.setPUMP_LOCATION_REF(LocationUtil - .getLocationRef(accounting.getPumpLocations().getPumpIn())); + watUsrContractAcctObjT.setPUMP_LOCATION_REF(pumpIn); break; case OUT: - watUsrContractAcctObjT.setPUMP_LOCATION_REF(LocationUtil - .getLocationRef(accounting.getPumpLocations().getPumpOut())); + watUsrContractAcctObjT.setPUMP_LOCATION_REF(pumpOut); break; case BELOW: - watUsrContractAcctObjT.setPUMP_LOCATION_REF(LocationUtil - .getLocationRef(accounting.getPumpLocations().getPumpBelow())); + watUsrContractAcctObjT.setPUMP_LOCATION_REF(pumpBelow); break; default: LOGGER.log(Level.WARNING, "Invalid pump type"); - break; + throw new IllegalArgumentException( + String.format("Invalid pump type for mapping to DB object: %s", transfer.getPumpType())); } watUsrContractAcctObjT.setTRANSFER_START_DATETIME(Timestamp.from(entry.getKey())); watUsrContractAcctObjTList.add(watUsrContractAcctObjT); @@ -210,21 +213,22 @@ public static WAT_USR_CONTRACT_ACCT_TAB_T toWaterUserContractAcctTs(WaterSupplyA public static LOC_REF_TIME_WINDOW_TAB_T toTimeWindowTabT(WaterSupplyAccounting accounting) { List timeWindowList = new ArrayList<>(); + LOCATION_REF_T pumpIn = LocationUtil.getLocationRef(accounting.getPumpLocations().getPumpIn()); + LOCATION_REF_T pumpOut = LocationUtil.getLocationRef(accounting.getPumpLocations().getPumpOut()); + LOCATION_REF_T pumpBelow = LocationUtil.getLocationRef(accounting.getPumpLocations().getPumpBelow()); + for (Map.Entry> entry : accounting.getPumpAccounting().entrySet()) { for (PumpTransfer transfer : entry.getValue()) { LOC_REF_TIME_WINDOW_OBJ_T timeWindow = new LOC_REF_TIME_WINDOW_OBJ_T(); switch (transfer.getPumpType()) { case IN: - timeWindow.setLOCATION_REF(LocationUtil - .getLocationRef(accounting.getPumpLocations().getPumpIn())); + timeWindow.setLOCATION_REF(pumpIn); break; case OUT: - timeWindow.setLOCATION_REF(LocationUtil - .getLocationRef(accounting.getPumpLocations().getPumpOut())); + timeWindow.setLOCATION_REF(pumpOut); break; case BELOW: - timeWindow.setLOCATION_REF(LocationUtil - .getLocationRef(accounting.getPumpLocations().getPumpBelow())); + timeWindow.setLOCATION_REF(pumpBelow); break; default: LOGGER.log(Level.WARNING, "Invalid pump type"); @@ -244,15 +248,16 @@ public static List toWaterSupplyAccountingList(Connection List waterSupplyAccounting = new ArrayList<>(); Map cacheMap = new TreeMap<>(); + for (WAT_USR_CONTRACT_ACCT_OBJ_T watUsrContractAcctObjT : watUsrContractAcctTabT) { + WATER_USER_CONTRACT_REF_T watUsrContractRef = watUsrContractAcctObjT.getWATER_USER_CONTRACT_REF(); AccountingKey key = new AccountingKey.Builder() - .withContractName(watUsrContractAcctObjT.getWATER_USER_CONTRACT_REF().getCONTRACT_NAME()) + .withContractName(watUsrContractRef.getCONTRACT_NAME()) .withWaterUser(new WaterUser.Builder() - .withWaterRight(watUsrContractAcctObjT.getWATER_USER_CONTRACT_REF().getWATER_USER().getWATER_RIGHT()) - .withEntityName(watUsrContractAcctObjT.getWATER_USER_CONTRACT_REF().getWATER_USER().getENTITY_NAME()) - .withProjectId(CwmsId.buildCwmsId(watUsrContractAcctObjT - .getWATER_USER_CONTRACT_REF().getWATER_USER().getPROJECT_LOCATION_REF().getOFFICE_ID(), - watUsrContractAcctObjT.getWATER_USER_CONTRACT_REF().getWATER_USER().getPROJECT_LOCATION_REF().call_GET_LOCATION_ID())) + .withWaterRight(watUsrContractRef.getWATER_USER().getWATER_RIGHT()) + .withEntityName(watUsrContractRef.getWATER_USER().getENTITY_NAME()) + .withProjectId(CwmsId.buildCwmsId(watUsrContractRef.getWATER_USER().getPROJECT_LOCATION_REF().getOFFICE_ID(), + watUsrContractRef.getWATER_USER().getPROJECT_LOCATION_REF().call_GET_LOCATION_ID())) .build()) .build(); if (cacheMap.containsKey(key)) { @@ -270,13 +275,14 @@ public static List toWaterSupplyAccountingList(Connection private static WaterSupplyAccounting createAccounting(Connection c, WAT_USR_CONTRACT_ACCT_OBJ_T acctObjT) { WaterContractDao waterContractDao = new WaterContractDao(DSL.using(c)); + WATER_USER_OBJ_T waterUserObjT = acctObjT.getWATER_USER_CONTRACT_REF().getWATER_USER(); WaterUserContract waterUserContract = waterContractDao.getWaterContract( acctObjT.getWATER_USER_CONTRACT_REF().getCONTRACT_NAME(), new CwmsId.Builder() - .withOfficeId(acctObjT.getWATER_USER_CONTRACT_REF().getWATER_USER().getPROJECT_LOCATION_REF().getOFFICE_ID()) - .withName(acctObjT.getWATER_USER_CONTRACT_REF().getWATER_USER().getPROJECT_LOCATION_REF().call_GET_LOCATION_ID()) + .withOfficeId(waterUserObjT.getPROJECT_LOCATION_REF().getOFFICE_ID()) + .withName(waterUserObjT.getPROJECT_LOCATION_REF().call_GET_LOCATION_ID()) .build(), - acctObjT.getWATER_USER_CONTRACT_REF().getWATER_USER().getENTITY_NAME()); + waterUserObjT.getENTITY_NAME()); Map> pumpAccounting = new TreeMap<>(); String pumpLocation = acctObjT.getPUMP_LOCATION_REF().call_GET_LOCATION_ID(); String pumpOffice = acctObjT.getPUMP_LOCATION_REF().getOFFICE_ID(); @@ -299,7 +305,7 @@ private static WaterSupplyAccounting createAccounting(Connection c, WAT_USR_CONT } return new WaterSupplyAccounting.Builder() .withContractName(acctObjT.getWATER_USER_CONTRACT_REF().getCONTRACT_NAME()) - .withWaterUser(toWaterUser(acctObjT.getWATER_USER_CONTRACT_REF().getWATER_USER())) + .withWaterUser(toWaterUser(waterUserObjT)) .withPumpLocations(new PumpLocation.Builder() .withPumpIn(CwmsId.buildCwmsId(waterUserContract.getPumpInLocation().getPumpLocation().getOfficeId(), waterUserContract.getPumpInLocation().getPumpLocation().getName())) @@ -336,4 +342,77 @@ private static void addTransfer(WAT_USR_CONTRACT_ACCT_OBJ_T acctObjTs, WaterSupp accounting.getPumpAccounting().put(acctObjTs.getTRANSFER_START_DATETIME().toInstant(), Collections.singletonList(transfer)); } + + static class AccountingKey implements Comparable { + private final WaterUser waterUser; + private final String contractName; + + private AccountingKey(Builder builder) { + this.waterUser = builder.waterUser; + this.contractName = builder.contractName; + } + + public WaterUser getWaterUser() { + return waterUser; + } + + public String getContractName() { + return contractName; + } + + @Override + public int hashCode() { + return waterUser.hashCode() + contractName.hashCode(); + } + + @Override + public boolean equals(Object obj) { + if (obj == this) { + return true; + } + if (!(obj instanceof AccountingKey)) { + return false; + } + AccountingKey key = (AccountingKey) obj; + return compareWaterUser(waterUser, key.getWaterUser()) && key.getContractName().equals(contractName); + } + + @Override + public int compareTo(@NotNull AccountingKey key) { + if (key == this) { + return 0; + } + int i = contractName.compareTo(key.getContractName()); + if (i == 0) { + i = compareWaterUser(waterUser, key.getWaterUser()) ? 0 : 1; + } + return i; + } + + private boolean compareWaterUser(WaterUser waterUser1, WaterUser waterUser2) { + return waterUser1.getEntityName().equals(waterUser2.getEntityName()) + && waterUser1.getProjectId().getName().equals(waterUser2.getProjectId().getName()) + && waterUser1.getProjectId().getOfficeId().equals(waterUser2.getProjectId().getOfficeId()) + && waterUser1.getWaterRight().equals(waterUser2.getWaterRight()); + } + + private static final class Builder { + private WaterUser waterUser; + private String contractName; + + public Builder withWaterUser(WaterUser waterUser) { + this.waterUser = waterUser; + return this; + } + + public Builder withContractName(String contractName) { + this.contractName = contractName; + return this; + } + + public AccountingKey build() { + return new AccountingKey(this); + } + } + } } diff --git a/cwms-data-api/src/main/java/cwms/cda/data/dto/watersupply/AccountingKey.java b/cwms-data-api/src/main/java/cwms/cda/data/dto/watersupply/AccountingKey.java deleted file mode 100644 index e3b480619..000000000 --- a/cwms-data-api/src/main/java/cwms/cda/data/dto/watersupply/AccountingKey.java +++ /dev/null @@ -1,102 +0,0 @@ -/* - * - * 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.dto.watersupply; - -import org.jetbrains.annotations.NotNull; - -public class AccountingKey implements Comparable { - private final WaterUser waterUser; - private final String contractName; - - private AccountingKey(Builder builder) { - this.waterUser = builder.waterUser; - this.contractName = builder.contractName; - } - - public WaterUser getWaterUser() { - return waterUser; - } - - public String getContractName() { - return contractName; - } - - @Override - public int hashCode() { - return waterUser.hashCode() + contractName.hashCode(); - } - - @Override - public boolean equals(Object obj) { - if (obj == this) { - return true; - } - if (!(obj instanceof AccountingKey)) { - return false; - } - AccountingKey key = (AccountingKey) obj; - return compareWaterUser(waterUser, key.getWaterUser()) && key.getContractName().equals(contractName); - } - - @Override - public int compareTo(@NotNull AccountingKey key) { - if (key == this) { - return 0; - } - int i = contractName.compareTo(key.getContractName()); - if (i == 0) { - i = compareWaterUser(waterUser, key.getWaterUser()) ? 0 : 1; - } - return i; - } - - private boolean compareWaterUser(WaterUser waterUser1, WaterUser waterUser2) { - return waterUser1.getEntityName().equals(waterUser2.getEntityName()) - && waterUser1.getProjectId().getName().equals(waterUser2.getProjectId().getName()) - && waterUser1.getProjectId().getOfficeId().equals(waterUser2.getProjectId().getOfficeId()) - && waterUser1.getWaterRight().equals(waterUser2.getWaterRight()); - } - - public static final class Builder { - private WaterUser waterUser; - private String contractName; - - public Builder withWaterUser(WaterUser waterUser) { - this.waterUser = waterUser; - return this; - } - - public Builder withContractName(String contractName) { - this.contractName = contractName; - return this; - } - - public AccountingKey build() { - return new AccountingKey(this); - } - } -}