4343
4444import cwms .cda .api .enums .Nation ;
4545import cwms .cda .api .enums .Unit ;
46+ import cwms .cda .api .errors .AlreadyExists ;
4647import cwms .cda .api .errors .NotFoundException ;
48+ import cwms .cda .data .dao .location .kind .LocationUtil ;
4749import cwms .cda .data .dto .Catalog ;
4850import cwms .cda .data .dto .Location ;
4951import cwms .cda .data .dto .catalog .CatalogEntry ;
5860import java .util .List ;
5961import java .util .Map ;
6062import java .util .Objects ;
63+ import java .util .Optional ;
6164import java .util .Set ;
6265import java .util .logging .Level ;
6366import java .util .logging .Logger ;
8285import usace .cwms .db .dao .util .services .CwmsDbServiceLookup ;
8386import usace .cwms .db .jooq .codegen .packages .CWMS_LOC_PACKAGE ;
8487import usace .cwms .db .jooq .codegen .tables .AV_LOC2 ;
88+ import usace .cwms .db .jooq .codegen .udt .records .LOCATION_OBJ_T ;
8589
8690
8791public class LocationsDaoImpl extends JooqDao <Location > implements LocationsDao {
@@ -214,28 +218,50 @@ public void deleteLocation(String locationName, String officeId, boolean cascade
214218 });
215219 }
216220
221+ /**
222+ * @deprecated Use {@link #storeLocation(Location, boolean)} instead.
223+ */
224+ @ Deprecated
217225 @ Override
218226 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 {
219232 location .validate ();
220233 try {
221234 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+ }
239265 });
240266 } catch (DataAccessException ex ) {
241267 throw new IOException ("Failed to store Location" , ex );
@@ -525,8 +551,10 @@ private static LocationCatalogEntry buildCatalogEntry(usace.cwms.db.jooq.codegen
525551 .timeZone (loc .getTIME_ZONE_NAME ())
526552 .latitude (loc .getLATITUDE () != null ? loc .getLATITUDE ().doubleValue () : null )
527553 .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 )
530558 .horizontalDatum (loc .getHORIZONTAL_DATUM ())
531559 .elevation (loc .getELEVATION ())
532560 .unit (loc .getUNIT_ID ())
@@ -541,4 +569,35 @@ private static LocationCatalogEntry buildCatalogEntry(usace.cwms.db.jooq.codegen
541569 .build ();
542570 }
543571
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+ }
544603}
0 commit comments