4747import java .util .Map ;
4848import java .util .TreeMap ;
4949import com .google .common .flogger .FluentLogger ;
50+ import mil .army .usace .hec .metadata .DataSetIllegalArgumentException ;
51+ import mil .army .usace .hec .metadata .Parameter ;
52+ import mil .army .usace .hec .metadata .UnitUtil ;
53+ import mil .army .usace .hec .metadata .UnitsConversionException ;
5054import org .jetbrains .annotations .NotNull ;
5155import org .jooq .impl .DSL ;
5256import usace .cwms .db .jooq .codegen .udt .records .LOCATION_REF_T ;
6266import usace .cwms .db .jooq .codegen .udt .records .WAT_USR_CONTRACT_ACCT_TAB_T ;
6367
6468
65- final class WaterSupplyUtils {
69+ public final class WaterSupplyUtils {
6670 private static final FluentLogger LOGGER = FluentLogger .forEnclosingClass ();
6771
6872 private WaterSupplyUtils () {
6973 throw new IllegalStateException ("Utility class" );
7074 }
7175
76+ /**
77+ * Converts all PumpTransfer flow values to SI units and returns a new WaterSupplyAccounting instance.
78+ * The SI flow units are determined using metadata for the FLOW parameter. If any conversion fails,
79+ * an IllegalArgumentException is thrown wrapping the UnitsConversionException. If the SI units
80+ * cannot be determined due to metadata issues, a DataSetIllegalArgumentException may be thrown.
81+ *
82+ * @param accounting the input WaterSupplyAccounting, possibly containing non-SI flow units
83+ * @return a new WaterSupplyAccounting with all flows in SI units
84+ * @throws DataSetIllegalArgumentException if SI flow units cannot be determined
85+ * @throws IllegalArgumentException if a units conversion fails
86+ */
87+ public static WaterSupplyAccounting convertAccountingFlowsToSi (WaterSupplyAccounting accounting )
88+ throws DataSetIllegalArgumentException , IllegalArgumentException {
89+ String siFlowUnits = Parameter .getParameter (Parameter .PARAMID_FLOW )
90+ .getUnitsStringForSystem (UnitUtil .SI_ID );
91+
92+ Map <Instant , List <PumpTransfer >> converted = new java .util .TreeMap <>();
93+ for (Map .Entry <Instant , List <PumpTransfer >> entry : accounting .getPumpAccounting ().entrySet ()) {
94+ List <PumpTransfer > transformed = new java .util .ArrayList <>();
95+ for (PumpTransfer pt : entry .getValue ()) {
96+ String fromUnits = pt .getFlowUnit ();
97+ if (fromUnits != null && !fromUnits .equalsIgnoreCase (siFlowUnits )) {
98+ try {
99+ double siValue = UnitUtil .convertUnits (pt .getFlow (), fromUnits , siFlowUnits );
100+ transformed .add (new PumpTransfer (pt .getPumpType (), pt .getTransferTypeDisplay (), siValue ,
101+ siFlowUnits , pt .getComment ()));
102+ } catch (UnitsConversionException e ) {
103+ throw new IllegalArgumentException (e .getMessage (), e );
104+ }
105+ } else {
106+ transformed .add (pt );
107+ }
108+ }
109+ converted .put (entry .getKey (), transformed );
110+ }
111+ return new WaterSupplyAccounting .Builder ()
112+ .withWaterUser (accounting .getWaterUser ())
113+ .withContractName (accounting .getContractName ())
114+ .withPumpLocations (accounting .getPumpLocations ())
115+ .withPumpAccounting (converted )
116+ .build ();
117+ }
118+
72119 static WaterUserContract toWaterContract (WATER_USER_CONTRACT_OBJ_T contract ) {
73120 Instant effectiveDate = null ;
74121 if (contract .getWS_CONTRACT_EFFECTIVE_DATE () != null ) {
@@ -251,7 +298,7 @@ static LOC_REF_TIME_WINDOW_TAB_T toTimeWindowTabT(WaterSupplyAccounting accounti
251298 }
252299
253300 static List <WaterSupplyAccounting > toWaterSupplyAccountingList (Connection c , WAT_USR_CONTRACT_ACCT_TAB_T
254- watUsrContractAcctTabT ) {
301+ watUsrContractAcctTabT , String flowUnits ) {
255302
256303 List <WaterSupplyAccounting > waterSupplyAccounting = new ArrayList <>();
257304 Map <AccountingKey , WaterSupplyAccounting > cacheMap = new TreeMap <>();
@@ -269,9 +316,9 @@ static List<WaterSupplyAccounting> toWaterSupplyAccountingList(Connection c, WAT
269316 .build ();
270317 if (cacheMap .containsKey (key )) {
271318 WaterSupplyAccounting accounting = cacheMap .get (key );
272- addTransfer (watUsrContractAcctObjT , accounting );
319+ addTransfer (watUsrContractAcctObjT , accounting , flowUnits );
273320 } else {
274- cacheMap .put (key , createAccounting (c , watUsrContractAcctObjT ));
321+ cacheMap .put (key , createAccounting (c , watUsrContractAcctObjT , flowUnits ));
275322 }
276323 }
277324 for (Map .Entry <AccountingKey , WaterSupplyAccounting > entry : cacheMap .entrySet ()) {
@@ -280,7 +327,7 @@ static List<WaterSupplyAccounting> toWaterSupplyAccountingList(Connection c, WAT
280327 return waterSupplyAccounting ;
281328 }
282329
283- private static WaterSupplyAccounting createAccounting (Connection c , WAT_USR_CONTRACT_ACCT_OBJ_T acctObjT ) {
330+ private static WaterSupplyAccounting createAccounting (Connection c , WAT_USR_CONTRACT_ACCT_OBJ_T acctObjT , String flowUnits ) {
284331 WaterContractDao waterContractDao = new WaterContractDao (DSL .using (c ));
285332 WATER_USER_OBJ_T waterUserObjT = acctObjT .getWATER_USER_CONTRACT_REF ().getWATER_USER ();
286333 WaterUserContract waterUserContract = waterContractDao .getWaterContract (
@@ -307,13 +354,13 @@ private static WaterSupplyAccounting createAccounting(Connection c, WAT_USR_CONT
307354 PumpTransfer transfer = null ;
308355 if (pumpIn != null && pumpIn .getName ().equalsIgnoreCase (pumpLocation )
309356 && pumpIn .getOfficeId ().equalsIgnoreCase (pumpOffice )) {
310- transfer = new PumpTransfer (PumpType .IN , transferDisplay , flow , remarks );
357+ transfer = new PumpTransfer (PumpType .IN , transferDisplay , flow , flowUnits , remarks );
311358 } else if (pumpOut != null && pumpOut .getName ().equalsIgnoreCase (pumpLocation )
312359 && pumpOut .getOfficeId ().equalsIgnoreCase (pumpOffice )) {
313- transfer = new PumpTransfer (PumpType .OUT , transferDisplay , flow , remarks );
360+ transfer = new PumpTransfer (PumpType .OUT , transferDisplay , flow , flowUnits , remarks );
314361 } else if (pumpBelow != null && pumpBelow .getName ().equalsIgnoreCase (pumpLocation )
315362 && pumpBelow .getOfficeId ().equalsIgnoreCase (pumpOffice )) {
316- transfer = new PumpTransfer (PumpType .BELOW , transferDisplay , flow , remarks );
363+ transfer = new PumpTransfer (PumpType .BELOW , transferDisplay , flow , flowUnits , remarks );
317364 }
318365 if (transfer != null ) {
319366 pumpAccounting .put (transferStart , Collections .singletonList (transfer ));
@@ -330,7 +377,7 @@ private static WaterSupplyAccounting createAccounting(Connection c, WAT_USR_CONT
330377 .build ();
331378 }
332379
333- private static void addTransfer (WAT_USR_CONTRACT_ACCT_OBJ_T acctObjTs , WaterSupplyAccounting accounting ) {
380+ private static void addTransfer (WAT_USR_CONTRACT_ACCT_OBJ_T acctObjTs , WaterSupplyAccounting accounting , String flowUnits ) {
334381 PumpTransfer transfer = null ;
335382 String transferDisplay = acctObjTs .getPHYSICAL_TRANSFER_TYPE ().getDISPLAY_VALUE ();
336383 String accountingRemarks = acctObjTs .getACCOUNTING_REMARKS ();
@@ -343,14 +390,14 @@ private static void addTransfer(WAT_USR_CONTRACT_ACCT_OBJ_T acctObjTs, WaterSupp
343390
344391 if (pumpIn != null && pumpIn .getName ().equalsIgnoreCase (locationId )
345392 && pumpIn .getOfficeId ().equalsIgnoreCase (officeId )) {
346- transfer = new PumpTransfer (PumpType .IN , transferDisplay , acctObjTs .getPUMP_FLOW (), accountingRemarks );
393+ transfer = new PumpTransfer (PumpType .IN , transferDisplay , acctObjTs .getPUMP_FLOW (), flowUnits , accountingRemarks );
347394 } else if (pumpOut != null && pumpOut .getName ().equalsIgnoreCase (locationId )
348395 && pumpOut .getOfficeId ().equalsIgnoreCase (officeId )) {
349- transfer = new PumpTransfer (PumpType .OUT , transferDisplay , acctObjTs .getPUMP_FLOW (), accountingRemarks );
396+ transfer = new PumpTransfer (PumpType .OUT , transferDisplay , acctObjTs .getPUMP_FLOW (), flowUnits , accountingRemarks );
350397 } else if (pumpBelow != null && pumpBelow .getName ().equalsIgnoreCase (locationId )
351398 && pumpBelow .getOfficeId ().equalsIgnoreCase (officeId )) {
352399 transfer = new PumpTransfer (PumpType .BELOW , transferDisplay ,
353- acctObjTs .getPUMP_FLOW (), accountingRemarks );
400+ acctObjTs .getPUMP_FLOW (), flowUnits , accountingRemarks );
354401 }
355402 if (accounting .getPumpAccounting ().get (transferStart ) != null ) {
356403 List <PumpTransfer > transfers = new ArrayList <>(accounting .getPumpAccounting ().get (transferStart ));
0 commit comments