11package cwms .cda .api ;
22
33import static com .codahale .metrics .MetricRegistry .name ;
4- import static cwms .cda .api .Controllers .*;
4+ import static cwms .cda .api .Controllers .BEGIN ;
5+ import static cwms .cda .api .Controllers .CREATE ;
6+ import static cwms .cda .api .Controllers .CREATE_AS_LRTS ;
7+ import static cwms .cda .api .Controllers .CURSOR ;
8+ import static cwms .cda .api .Controllers .DATUM ;
9+ import static cwms .cda .api .Controllers .DELETE ;
10+ import static cwms .cda .api .Controllers .END ;
11+ import static cwms .cda .api .Controllers .END_TIME_INCLUSIVE ;
12+ import static cwms .cda .api .Controllers .FORMAT ;
13+ import static cwms .cda .api .Controllers .GET_ALL ;
14+ import static cwms .cda .api .Controllers .GET_ONE ;
15+ import static cwms .cda .api .Controllers .INCLUDE_ENTRY_DATE ;
16+ import static cwms .cda .api .Controllers .MAX_VERSION ;
17+ import static cwms .cda .api .Controllers .NAME ;
18+ import static cwms .cda .api .Controllers .NOT_SUPPORTED_YET ;
19+ import static cwms .cda .api .Controllers .OFFICE ;
20+ import static cwms .cda .api .Controllers .OVERRIDE_PROTECTION ;
21+ import static cwms .cda .api .Controllers .PAGE ;
22+ import static cwms .cda .api .Controllers .PAGE_SIZE ;
23+ import static cwms .cda .api .Controllers .RESULTS ;
24+ import static cwms .cda .api .Controllers .SIZE ;
25+ import static cwms .cda .api .Controllers .START_TIME_INCLUSIVE ;
26+ import static cwms .cda .api .Controllers .STATUS_200 ;
27+ import static cwms .cda .api .Controllers .STATUS_400 ;
28+ import static cwms .cda .api .Controllers .STATUS_404 ;
29+ import static cwms .cda .api .Controllers .STATUS_501 ;
30+ import static cwms .cda .api .Controllers .STORE_RULE ;
31+ import static cwms .cda .api .Controllers .TIMESERIES ;
32+ import static cwms .cda .api .Controllers .TIMEZONE ;
33+ import static cwms .cda .api .Controllers .TIME_FORMAT_DESC ;
34+ import static cwms .cda .api .Controllers .UNIT ;
35+ import static cwms .cda .api .Controllers .UNITS ;
36+ import static cwms .cda .api .Controllers .UPDATE ;
37+ import static cwms .cda .api .Controllers .VERSION ;
38+ import static cwms .cda .api .Controllers .VERSION_DATE ;
39+ import static cwms .cda .api .Controllers .addDeprecatedContentTypeWarning ;
40+ import static cwms .cda .api .Controllers .queryParamAsClass ;
41+ import static cwms .cda .api .Controllers .queryParamAsZdt ;
42+ import static cwms .cda .api .Controllers .requiredParam ;
43+ import static cwms .cda .api .Controllers .requiredZdt ;
544
645import com .codahale .metrics .Histogram ;
746import com .codahale .metrics .MetricRegistry ;
847import com .codahale .metrics .Timer ;
948import cwms .cda .api .enums .UnitSystem ;
1049import cwms .cda .api .errors .CdaError ;
50+ import cwms .cda .api .errors .NotFoundException ;
1151import cwms .cda .data .dao .JooqDao ;
1252import cwms .cda .data .dao .StoreRule ;
1353import cwms .cda .data .dao .TimeSeriesDao ;
1454import cwms .cda .data .dao .TimeSeriesDaoImpl ;
1555import cwms .cda .data .dao .TimeSeriesDeleteOptions ;
1656import cwms .cda .data .dao .TimeSeriesRequestParameters ;
57+ import cwms .cda .data .dao .TimeSeriesVerticalDatumConverter ;
58+ import cwms .cda .data .dao .VerticalDatum ;
1759import cwms .cda .data .dto .TimeSeries ;
1860import cwms .cda .formatters .ContentType ;
1961import cwms .cda .formatters .Formats ;
@@ -118,6 +160,7 @@ public TimeSeriesController(MetricRegistry metrics) {
118160
119161 static {
120162 JavalinValidation .register (StoreRule .class , StoreRule ::getStoreRule );
163+ JavalinValidation .register (VerticalDatum .class , VerticalDatum ::getVerticalDatum );
121164 }
122165
123166 private Timer .Context markAndTime (String subject ) {
@@ -147,7 +190,16 @@ private Timer.Context markAndTime(String subject) {
147190 @ OpenApiParam (name = STORE_RULE , type = StoreRule .class , description = STORE_RULE_DESC ),
148191 @ OpenApiParam (name = OVERRIDE_PROTECTION , type = Boolean .class , description = "A flag "
149192 + "to ignore the protected data quality when storing data. 'True' or 'False'"
150- + ", default is " + TimeSeriesDaoImpl .OVERRIDE_PROTECTION )
193+ + ", default is " + TimeSeriesDaoImpl .OVERRIDE_PROTECTION ),
194+ @ OpenApiParam (name = DATUM , type = VerticalDatum .class , description = "If the provided "
195+ + "time-series includes an explicit vertical-datum-info attribute "
196+ + "then it is assumed that the data is in the datum specified by the vertical-datum-info. "
197+ + "If the input timeseries does not include vertical-datum-info and "
198+ + "this parameter is not provided it is assumed that the data is in the as-stored "
199+ + "datum and no conversion is necessary. "
200+ + "If the input timeseries does not include vertical-datum-info and "
201+ + "this parameter is provided it is assumed that the data is in the Datum named by the argument "
202+ + "and should be converted to the as-stored datum before being saved." )
151203 },
152204 method = HttpMethod .POST ,
153205 path = "/timeseries" ,
@@ -162,12 +214,18 @@ public void create(@NotNull Context ctx) {
162214 boolean overrideProtection = ctx .queryParamAsClass (OVERRIDE_PROTECTION , Boolean .class )
163215 .getOrDefault (TimeSeriesDaoImpl .OVERRIDE_PROTECTION );
164216
217+ VerticalDatum vd = ctx .queryParamAsClass (DATUM , VerticalDatum .class )
218+ .getOrDefault (null );
219+
165220 try (final Timer .Context ignored = markAndTime (CREATE )) {
166221 DSLContext dsl = getDslContext (ctx );
167222
168223 TimeSeriesDao dao = getTimeSeriesDao (dsl );
169224 TimeSeries timeSeries = deserializeTimeSeries (ctx );
170- dao .create (timeSeries , createAsLrts , storeRule , overrideProtection );
225+
226+ vd = TimeSeriesVerticalDatumConverter .getVerticalDatum (timeSeries ).orElse (vd );
227+
228+ dao .create (timeSeries , createAsLrts , storeRule , overrideProtection , vd );
171229 ctx .status (HttpServletResponse .SC_OK );
172230 } catch (DataAccessException | IOException ex ) {
173231 CdaError re = new CdaError ("Internal Error" );
@@ -435,11 +493,6 @@ public void getAll(@NotNull Context ctx) {
435493
436494 if (version != null && version .equals ("2" )) {
437495
438- if (datum != null ) {
439- throw new IllegalArgumentException (String .format ("Datum is not supported for:%s and %s" ,
440- Formats .JSONV2 , Formats .XMLV2 ));
441- }
442-
443496 String office = requiredParam (ctx , OFFICE );
444497 TimeSeriesRequestParameters requestParameters = new TimeSeriesRequestParameters .Builder ()
445498 .withNames (names )
@@ -453,6 +506,12 @@ public void getAll(@NotNull Context ctx) {
453506 .build ();
454507 TimeSeries ts = dao .getTimeseries (cursor , pageSize , requestParameters );
455508
509+ if (datum != null ) { //this will be null for non-elevation ts
510+ // user has requested a specific vertical datum
511+ VerticalDatum vd = VerticalDatum .valueOf (datum ); // the users request
512+ ts = TimeSeriesVerticalDatumConverter .convertToVerticalDatum (ts , vd );
513+ }
514+
456515 results = Formats .format (contentType , ts );
457516
458517 ctx .status (HttpServletResponse .SC_OK );
@@ -482,6 +541,11 @@ public void getAll(@NotNull Context ctx) {
482541 }
483542 addDeprecatedContentTypeWarning (ctx , contentType );
484543 requestResultSize .update (results .length ());
544+ } catch (NotFoundException e ) {
545+ CdaError re = new CdaError ("Not found." );
546+ logger .log (Level .WARNING , re .toString (), e );
547+ ctx .status (HttpServletResponse .SC_NOT_FOUND );
548+ ctx .json (re );
485549 } catch (IllegalArgumentException ex ) {
486550 CdaError re = new CdaError ("Invalid arguments supplied" );
487551 logger .log (Level .SEVERE , re .toString (), ex );
@@ -542,7 +606,16 @@ public void getOne(@NotNull Context ctx, @NotNull String id) {
542606 @ OpenApiParam (name = CREATE_AS_LRTS , type = Boolean .class , description = "" ),
543607 @ OpenApiParam (name = STORE_RULE , type = StoreRule .class , description = STORE_RULE_DESC ),
544608 @ OpenApiParam (name = OVERRIDE_PROTECTION , type = Boolean .class , description =
545- "A flag to ignore the protected data quality when storing data. \" 'true' or 'false'\" " )
609+ "A flag to ignore the protected data quality when storing data. \" 'true' or 'false'\" " ),
610+ @ OpenApiParam (name = DATUM , type = VerticalDatum .class , description = "If the provided "
611+ + "time-series includes an explicit vertical-datum-info attribute "
612+ + "then it is assumed that the data is in the datum specified by the vertical-datum-info. "
613+ + "If the input timeseries does not include vertical-datum-info and "
614+ + "this parameter is not provided it is assumed that the data is in the as-stored "
615+ + "datum and no conversion is necessary. "
616+ + "If the input timeseries does not include vertical-datum-info and "
617+ + "this parameter is provided it is assumed that the data is in the Datum named by the argument "
618+ + "and should be converted to the as-stored datum before being saved." )
546619 },
547620 method = HttpMethod .PATCH ,
548621 path = "/timeseries/{timeseries}" ,
@@ -563,7 +636,11 @@ public void update(@NotNull Context ctx, @NotNull String id) {
563636 boolean overrideProtection = ctx .queryParamAsClass (OVERRIDE_PROTECTION , Boolean .class )
564637 .getOrDefault (TimeSeriesDaoImpl .OVERRIDE_PROTECTION );
565638
566- dao .store (timeSeries , createAsLrts , storeRule , overrideProtection );
639+ VerticalDatum vd = ctx .queryParamAsClass (DATUM , VerticalDatum .class )
640+ .getOrDefault (null );
641+ vd = TimeSeriesVerticalDatumConverter .getVerticalDatum (timeSeries ).orElse (vd );
642+
643+ dao .store (timeSeries , createAsLrts , storeRule , overrideProtection , vd );
567644
568645 ctx .status (HttpServletResponse .SC_OK );
569646 } catch (DataAccessException | IOException ex ) {
0 commit comments