43
43
44
44
import cwms .cda .api .enums .Nation ;
45
45
import cwms .cda .api .enums .Unit ;
46
+ import cwms .cda .api .errors .AlreadyExists ;
46
47
import cwms .cda .api .errors .NotFoundException ;
48
+ import cwms .cda .data .dao .location .kind .LocationUtil ;
47
49
import cwms .cda .data .dto .Catalog ;
48
50
import cwms .cda .data .dto .Location ;
49
51
import cwms .cda .data .dto .catalog .CatalogEntry ;
58
60
import java .util .List ;
59
61
import java .util .Map ;
60
62
import java .util .Objects ;
63
+ import java .util .Optional ;
61
64
import java .util .Set ;
62
65
import java .util .logging .Level ;
63
66
import java .util .logging .Logger ;
82
85
import usace .cwms .db .dao .util .services .CwmsDbServiceLookup ;
83
86
import usace .cwms .db .jooq .codegen .packages .CWMS_LOC_PACKAGE ;
84
87
import usace .cwms .db .jooq .codegen .tables .AV_LOC2 ;
88
+ import usace .cwms .db .jooq .codegen .udt .records .LOCATION_OBJ_T ;
85
89
86
90
87
91
public class LocationsDaoImpl extends JooqDao <Location > implements LocationsDao {
@@ -214,28 +218,50 @@ public void deleteLocation(String locationName, String officeId, boolean cascade
214
218
});
215
219
}
216
220
221
+ /**
222
+ * @deprecated Use {@link #storeLocation(Location, boolean)} instead.
223
+ */
224
+ @ Deprecated
217
225
@ Override
218
226
public void storeLocation (Location location ) throws IOException {
227
+ storeLocation (location , false );
228
+ }
229
+
230
+ @ Override
231
+ public void storeLocation (Location location , boolean failIfExists ) throws IOException {
219
232
location .validate ();
220
233
try {
221
234
connection (dsl , c -> {
222
- setOffice (c ,location );
223
- CwmsDbLoc locJooq = CwmsDbServiceLookup .buildCwmsDb (CwmsDbLoc .class , c );
224
- String elevationUnits = location .getElevationUnits () == null
225
- ? Unit .METER .getValue () : location .getElevationUnits ();
226
- locJooq .store (c , location .getOfficeId (), location .getName (),
227
- location .getStateInitial (), location .getCountyName (),
228
- location .getTimezoneName (), location .getLocationType (),
229
- location .getLatitude (), location .getLongitude (), location .getElevation (),
230
- elevationUnits , location .getVerticalDatum (),
231
- location .getHorizontalDatum (), location .getPublicName (),
232
- location .getLongName (),
233
- location .getDescription (), location .getActive (),
234
- location .getLocationKind (), location .getMapLabel (),
235
- location .getPublishedLatitude (),
236
- location .getPublishedLongitude (), location .getBoundingOfficeId (),
237
- location .getNation ().getName (), location .getNearestCity (), true );
238
-
235
+ setOffice (c , location );
236
+ LOCATION_OBJ_T locationObjT = getLocationObj (location );
237
+ Configuration configuration = getDslContext (c , location .getOfficeId ()).configuration ();
238
+ try {
239
+ CWMS_LOC_PACKAGE .call_STORE_LOCATION__2 (configuration , locationObjT ,
240
+ formatBool (failIfExists ));
241
+ } catch (DataAccessException e ) {
242
+ try {
243
+ String dbLocationName = CWMS_LOC_PACKAGE .call_GET_LOCATION_ID (
244
+ configuration , location .getName (), location .getBoundingOfficeId ());
245
+ if (dbLocationName != null && !dbLocationName .isEmpty ()) {
246
+ String message ;
247
+ if (dbLocationName .equalsIgnoreCase (location .getName ())) {
248
+ message = String .format ("The location with name: %s already exists in office: %s" ,
249
+ location .getName (), location .getBoundingOfficeId ());
250
+ } else {
251
+ message = String .format ("The location with alias: '%s' and proper name: "
252
+ + "'%s' already exists in office: '%s'." ,
253
+ location .getName (), dbLocationName , location .getBoundingOfficeId ());
254
+ }
255
+ throw new AlreadyExists (message , e );
256
+ } else {
257
+ throw wrapException (e );
258
+ }
259
+ } catch (DataAccessException | NotFoundException ignored ) {
260
+ // If we can't find the location by name, then it doesn't exist.
261
+ // We can throw the original exception.
262
+ throw wrapException (e );
263
+ }
264
+ }
239
265
});
240
266
} catch (DataAccessException ex ) {
241
267
throw new IOException ("Failed to store Location" , ex );
@@ -525,8 +551,10 @@ private static LocationCatalogEntry buildCatalogEntry(usace.cwms.db.jooq.codegen
525
551
.timeZone (loc .getTIME_ZONE_NAME ())
526
552
.latitude (loc .getLATITUDE () != null ? loc .getLATITUDE ().doubleValue () : null )
527
553
.longitude (loc .getLONGITUDE () != null ? loc .getLONGITUDE ().doubleValue () : null )
528
- .publishedLatitude (loc .getPUBLISHED_LATITUDE () != null ? loc .getPUBLISHED_LATITUDE ().doubleValue () : null )
529
- .publishedLongitude (loc .getPUBLISHED_LONGITUDE () != null ? loc .getPUBLISHED_LONGITUDE ().doubleValue () : null )
554
+ .publishedLatitude (loc .getPUBLISHED_LATITUDE () != null
555
+ ? loc .getPUBLISHED_LATITUDE ().doubleValue () : null )
556
+ .publishedLongitude (loc .getPUBLISHED_LONGITUDE () != null
557
+ ? loc .getPUBLISHED_LONGITUDE ().doubleValue () : null )
530
558
.horizontalDatum (loc .getHORIZONTAL_DATUM ())
531
559
.elevation (loc .getELEVATION ())
532
560
.unit (loc .getUNIT_ID ())
@@ -541,4 +569,35 @@ private static LocationCatalogEntry buildCatalogEntry(usace.cwms.db.jooq.codegen
541
569
.build ();
542
570
}
543
571
572
+ private static LOCATION_OBJ_T getLocationObj (Location location ) {
573
+ LOCATION_OBJ_T retval = null ;
574
+ if (location != null ) {
575
+ retval = new LOCATION_OBJ_T ();
576
+ String elevationUnits = location .getElevationUnits () == null
577
+ ? Unit .METER .getValue () : location .getElevationUnits ();
578
+ retval .setLOCATION_REF (LocationUtil .getLocationRef (location .getName (), location .getOfficeId ()));
579
+ retval .setLOCATION_KIND_ID (location .getLocationKind ());
580
+ retval .setTIME_ZONE_NAME (location .getTimezoneName ());
581
+ retval .setLATITUDE (toBigDecimal (location .getLatitude ()));
582
+ retval .setLONGITUDE (toBigDecimal (location .getLongitude ()));
583
+ retval .setHORIZONTAL_DATUM (location .getHorizontalDatum ());
584
+ retval .setACTIVE_FLAG (formatBool (location .getActive ()));
585
+ retval .setDESCRIPTION (location .getDescription ());
586
+ retval .setELEVATION (toBigDecimal (location .getElevation ()));
587
+ retval .setELEV_UNIT_ID (elevationUnits );
588
+ retval .setCOUNTY_NAME (location .getCountyName ());
589
+ retval .setBOUNDING_OFFICE_ID (location .getBoundingOfficeId ());
590
+ retval .setNATION_ID (Optional .ofNullable (location .getNation ()).map (Nation ::getName ).orElse (null ));
591
+ retval .setMAP_LABEL (location .getMapLabel ());
592
+ retval .setPUBLIC_NAME (location .getPublicName ());
593
+ retval .setPUBLISHED_LATITUDE (toBigDecimal (location .getPublishedLatitude ()));
594
+ retval .setPUBLISHED_LONGITUDE (toBigDecimal (location .getPublishedLongitude ()));
595
+ retval .setVERTICAL_DATUM (location .getVerticalDatum ());
596
+ retval .setLONG_NAME (location .getLongName ());
597
+ retval .setSTATE_INITIAL (location .getStateInitial ());
598
+ retval .setLOCATION_TYPE (location .getLocationType ());
599
+ retval .setNEAREST_CITY (location .getNearestCity ());
600
+ }
601
+ return retval ;
602
+ }
544
603
}
0 commit comments