Skip to content

Commit 4f5a6f1

Browse files
rohaan-rmaRohaan Sandhu
andauthored
Add StatusResponse class to handle successful response messages (#1167)
Fixes #1112 - [x] Added StatusResponse class -> office-id, message and identifier String fields - [x] Added basic serialization/deserialization unit tests - [x] Add office-id back, thus make StatusResponse extend CwmsDTO (all use cases of StatusResponse have an office) - [x] Changing HTTP status codes 204 -> 200 to allow for a response body containing simple message and information of what was deleted. Update tests and OpenApi docs too. - [x] TurbineController - [x] LevelsController - [x] LocationController - [x] EmbankmentController - [x] StreamLocationController - [x] StreamController - [x] BasinController - [x] LookupTypeController - [x] WaterContractController - [x] WaterUserController - [x] LockController - [x] PropertyController - [x] MeasurementController - [x] OutletController Saving the last 5 tasks for a separate PR (This one is too long already) - [x] VirtualOutletController - [x] AccountingCreateController - [x] RatingController - [x] TurbineChangesController - [x] StreamReachController --------- Co-authored-by: Rohaan Sandhu <rohsan4845@geiconsultants.com>
1 parent 2894994 commit 4f5a6f1

41 files changed

Lines changed: 682 additions & 175 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

cwms-data-api/src/main/java/cwms/cda/api/BasinController.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import cwms.cda.data.dao.JooqDao;
4646
import cwms.cda.data.dao.basinconnectivity.BasinDao;
4747
import cwms.cda.data.dto.CwmsId;
48+
import cwms.cda.data.dto.StatusResponse;
4849
import cwms.cda.data.dto.basinconnectivity.Basin;
4950
import cwms.cda.formatters.ContentType;
5051
import cwms.cda.formatters.Formats;
@@ -251,7 +252,8 @@ public void update(@NotNull Context ctx, @NotNull String name) {
251252
.withOfficeId(officeId)
252253
.build();
253254
basinDao.renameBasin(oldLoc, newLoc);
254-
ctx.status(HttpServletResponse.SC_OK).json("Updated Location");
255+
StatusResponse re = new StatusResponse(officeId, "Updated Location", newBasinId);
256+
ctx.status(HttpServletResponse.SC_OK).json(re);
255257
}
256258

257259
@OpenApi(
@@ -276,21 +278,23 @@ public void create(@NotNull Context ctx) {
276278
String newBasinId = basin.getBasinId().getName();
277279
cwms.cda.data.dao.basin.BasinDao basinDao = new cwms.cda.data.dao.basin.BasinDao(dsl);
278280
basinDao.storeBasin(basin);
279-
ctx.status(HttpServletResponse.SC_CREATED).json(newBasinId + " Created");
281+
StatusResponse re = new StatusResponse(basin.getBasinId().getOfficeId(),
282+
"Basin successfully stored to CWMS.", newBasinId);
283+
ctx.status(HttpServletResponse.SC_CREATED).json(re);
280284
}
281285

282286
@OpenApi(
283287
queryParams = {
284288
@OpenApiParam(name = OFFICE, required = true, description = "Specifies the"
285-
+ " owning office of the basin to be renamed."),
289+
+ " owning office of the basin to be deleted."),
286290
@OpenApiParam(name = METHOD, required = true, description = "Specifies the delete method used.",
287291
type = JooqDao.DeleteMethod.class)
288292
},
289293
pathParams = {
290294
@OpenApiParam(name = NAME, description = "Specifies the name of "
291295
+ "the basin to be deleted.")
292296
},
293-
description = "Renames CWMS Basin",
297+
description = "Deletes CWMS Basin",
294298
tags = {TAG}
295299
)
296300
@Override
@@ -303,6 +307,7 @@ public void delete(@NotNull Context ctx, @NotNull String name) {
303307
.withOfficeId(ctx.queryParam(OFFICE))
304308
.build();
305309
basinDao.deleteBasin(basinId, deleteMethod.getRule());
306-
ctx.status(HttpServletResponse.SC_NO_CONTENT).json(basinId.getName() + " Deleted");
310+
StatusResponse re = new StatusResponse(basinId.getOfficeId(), "Deleted CWMS Basin", basinId.getName());
311+
ctx.status(HttpServletResponse.SC_OK).json(re);
307312
}
308313
}

cwms-data-api/src/main/java/cwms/cda/api/EmbankmentController.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import com.codahale.metrics.Timer;
3030
import cwms.cda.data.dao.JooqDao;
3131
import cwms.cda.data.dao.location.kind.EmbankmentDao;
32+
import cwms.cda.data.dto.StatusResponse;
3233
import cwms.cda.data.dto.location.kind.Embankment;
3334
import cwms.cda.formatters.ContentType;
3435
import cwms.cda.formatters.Formats;
@@ -167,7 +168,9 @@ public void create(Context ctx) {
167168
DSLContext dsl = getDslContext(ctx);
168169
EmbankmentDao dao = new EmbankmentDao(dsl);
169170
dao.storeEmbankment(embankment, failIfExists);
170-
ctx.status(HttpServletResponse.SC_CREATED).json("Created Embankment");
171+
StatusResponse re = new StatusResponse(embankment.getLocation().getOfficeId(),
172+
"Embankment successfully stored to CWMS", embankment.getLocation().getName());
173+
ctx.status(HttpServletResponse.SC_CREATED).json(re);
171174
}
172175

173176
}
@@ -197,7 +200,8 @@ public void update(@NotNull Context ctx, @NotNull String name) {
197200
DSLContext dsl = getDslContext(ctx);
198201
EmbankmentDao dao = new EmbankmentDao(dsl);
199202
dao.renameEmbankment(office, name, newName);
200-
ctx.status(HttpServletResponse.SC_OK).json("Renamed Embankment");
203+
StatusResponse re = new StatusResponse(office, "Embankment successfully renamed in CWMS", newName);
204+
ctx.status(HttpServletResponse.SC_OK).json(re);
201205
}
202206
}
203207

@@ -217,7 +221,7 @@ public void update(@NotNull Context ctx, @NotNull String name) {
217221
method = HttpMethod.DELETE,
218222
tags = {TAG},
219223
responses = {
220-
@OpenApiResponse(status = STATUS_204, description = "Embankment successfully deleted from CWMS."),
224+
@OpenApiResponse(status = STATUS_200, description = "Embankment successfully deleted from CWMS."),
221225
@OpenApiResponse(status = STATUS_404, description = "Based on the combination of "
222226
+ "inputs provided the embankment was not found.")
223227
}
@@ -231,7 +235,8 @@ public void delete(@NotNull Context ctx, @NotNull String name) {
231235
DSLContext dsl = getDslContext(ctx);
232236
EmbankmentDao dao = new EmbankmentDao(dsl);
233237
dao.deleteEmbankment(name, office, deleteMethod.getRule());
234-
ctx.status(HttpServletResponse.SC_NO_CONTENT).json(name + " Deleted");
238+
StatusResponse re = new StatusResponse(office, "Embankment successfully deleted from CWMS", name);
239+
ctx.status(HttpServletResponse.SC_OK).json(re);
235240
}
236241
}
237242
}

cwms-data-api/src/main/java/cwms/cda/api/LevelsController.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import cwms.cda.api.enums.UnitSystem;
4444
import cwms.cda.data.dao.LocationLevelsDao;
4545
import cwms.cda.data.dao.LocationLevelsDaoImpl;
46+
import cwms.cda.data.dto.StatusResponse;
4647
import cwms.cda.data.dto.locationlevel.ConstantLocationLevel;
4748
import cwms.cda.data.dto.locationlevel.LocationLevel;
4849
import cwms.cda.data.dto.locationlevel.LocationLevels;
@@ -119,7 +120,8 @@ public void create(@NotNull Context ctx) {
119120
DSLContext dsl = getDslContext(ctx);
120121
LocationLevelsDao levelsDao = getLevelsDao(dsl);
121122
levelsDao.storeLocationLevel(level);
122-
ctx.status(HttpServletResponse.SC_CREATED).json("Created Location Level");
123+
StatusResponse re = new StatusResponse(level.getOfficeId(),"Created Location Level", level.getLocationLevelId());
124+
ctx.status(HttpServletResponse.SC_CREATED).json(re);
123125
} catch (IOException e) {
124126
throw new IllegalArgumentException("Unable to parse the request body", e);
125127
}
@@ -185,7 +187,8 @@ public void delete(@NotNull Context ctx, @NotNull String levelId) {
185187
? DateUtils.parseUserDate(dateString, timezone) : null;
186188
LocationLevelsDao levelsDao = getLevelsDao(dsl);
187189
levelsDao.deleteLocationLevel(levelId, unmarshalledDateTime, office, cascadeDelete);
188-
ctx.status(HttpServletResponse.SC_OK).json(levelId + " Deleted");
190+
StatusResponse re = new StatusResponse(office,"CWMS Location Level Deleted", levelId);
191+
ctx.status(HttpServletResponse.SC_OK).json(re);
189192
}
190193
}
191194

@@ -411,7 +414,8 @@ public void update(@NotNull Context ctx, @NotNull String oldLevelId) {
411414
if (!oldLevelId.equals(newLevelId)) {
412415
//if name changed then delete location with old name
413416
levelsDao.renameLocationLevel(oldLevelId, newLevelId, officeId);
414-
ctx.status(HttpServletResponse.SC_OK).json("Renamed Location Level");
417+
StatusResponse re = new StatusResponse(officeId,"Renamed Location Level", newLevelId);
418+
ctx.status(HttpServletResponse.SC_OK).json(re);
415419
} else {
416420
String dateString = queryParamAsClass(ctx,
417421
new String[]{EFFECTIVE_DATE, DATE}, String.class, null, metrics,
@@ -432,7 +436,8 @@ public void update(@NotNull Context ctx, @NotNull String oldLevelId) {
432436
levelFromBody, unmarshalledDateTime);
433437

434438
levelsDao.storeLocationLevel(updatedLocationLevel);
435-
ctx.status(HttpServletResponse.SC_OK).json("Updated Location Level");
439+
StatusResponse re = new StatusResponse(officeId,"Updated Location Level", newLevelId);
440+
ctx.status(HttpServletResponse.SC_OK).json(re);
436441
}
437442
} catch (JsonProcessingException ex) {
438443
throw new FormattingException("Failed to format location level update request", ex);

cwms-data-api/src/main/java/cwms/cda/api/LocationController.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
import cwms.cda.data.dao.LocationsDao;
6363
import cwms.cda.data.dao.LocationsDaoImpl;
6464
import cwms.cda.data.dto.Location;
65+
import cwms.cda.data.dto.StatusResponse;
6566
import cwms.cda.formatters.ContentType;
6667
import cwms.cda.formatters.Formats;
6768
import cwms.cda.formatters.UnsupportedFormatException;
@@ -306,7 +307,8 @@ public void create(@NotNull Context ctx) {
306307
Location locationFromBody = Formats.parseContent(contentType, ctx.body(), Location.class);
307308
boolean failIfExists = ctx.queryParamAsClass(FAIL_IF_EXISTS, Boolean.class).getOrDefault(true);
308309
locationsDao.storeLocation(locationFromBody, failIfExists);
309-
ctx.status(HttpServletResponse.SC_CREATED).json("Created Location");
310+
StatusResponse re = new StatusResponse(locationFromBody.getOfficeId(),"Created Location", locationFromBody.getName());
311+
ctx.status(HttpServletResponse.SC_CREATED).json(re);
310312
} catch (IOException ex) {
311313
CdaError re = new CdaError("failed to process request");
312314
logger.log(Level.SEVERE, re.toString(), ex);
@@ -351,10 +353,12 @@ public void update(@NotNull Context ctx, @NotNull String locationId) {
351353
if (!updatedLocation.getName().equalsIgnoreCase(existingLocation.getName())) {
352354
//if name changed then delete location with old name
353355
locationsDao.renameLocation(locationId, updatedLocation);
354-
ctx.status(HttpServletResponse.SC_OK).json("Updated and renamed Location");
356+
ctx.status(HttpServletResponse.SC_OK).json(new StatusResponse(updatedLocation.getOfficeId(),
357+
"Updated and renamed Location", updatedLocation.getName()));
355358
} else {
356359
locationsDao.storeLocation(updatedLocation, false);
357-
ctx.status(HttpServletResponse.SC_OK).json("Updated Location");
360+
ctx.status(HttpServletResponse.SC_OK).json(new StatusResponse(updatedLocation.getOfficeId(),
361+
"Updated Location", updatedLocation.getName()));
358362
}
359363
} catch (NotFoundException e) {
360364
CdaError re = new CdaError("Not found.");
@@ -386,6 +390,7 @@ public void update(@NotNull Context ctx, @NotNull String locationId) {
386390
path = "/locations",
387391
tags = {"Locations"},
388392
responses = {
393+
@OpenApiResponse(status = STATUS_200, description = "Location successfully deleted from CWMS."),
389394
@OpenApiResponse(status = STATUS_404, description = "Based on the combination of "
390395
+ "inputs provided the location was not found.")
391396
}
@@ -400,7 +405,8 @@ public void delete(@NotNull Context ctx, @NotNull String locationId) {
400405
LocationsDao locationsDao = getLocationsDao(dsl);
401406
boolean cascadeDelete = ctx.queryParamAsClass(CASCADE_DELETE, Boolean.class).getOrDefault(false);
402407
locationsDao.deleteLocation(locationId, office, cascadeDelete);
403-
ctx.status(HttpServletResponse.SC_OK).json(locationId + " Deleted");
408+
StatusResponse re = new StatusResponse(office,"Deleted CWMS Location", locationId);
409+
ctx.status(HttpServletResponse.SC_OK).json(re);
404410
} catch (DataAccessException ex) {
405411
SQLException cause = ex.getCause(SQLException.class);
406412
if (cause != null && cause.getErrorCode() == 20031) {

cwms-data-api/src/main/java/cwms/cda/api/LookupTypeController.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import com.codahale.metrics.Timer;
3030
import cwms.cda.data.dao.LookupTypeDao;
3131
import cwms.cda.data.dto.LookupType;
32+
import cwms.cda.data.dto.StatusResponse;
3233
import cwms.cda.formatters.ContentType;
3334
import cwms.cda.formatters.Formats;
3435
import io.javalin.apibuilder.CrudHandler;
@@ -138,7 +139,9 @@ public void create(Context ctx) {
138139
DSLContext dsl = getDslContext(ctx);
139140
LookupTypeDao dao = new LookupTypeDao(dsl);
140141
dao.storeLookupType(category, prefix, lookupType);
141-
ctx.status(HttpServletResponse.SC_CREATED).json("Created Lookup Type");
142+
StatusResponse re = new StatusResponse(lookupType.getOfficeId(), "Lookup Type successfully stored to CWMS.",
143+
lookupType.getDisplayValue());
144+
ctx.status(HttpServletResponse.SC_CREATED).json(re);
142145
}
143146
}
144147

@@ -156,7 +159,7 @@ public void create(Context ctx) {
156159
method = HttpMethod.PATCH,
157160
tags = {TAG},
158161
responses = {
159-
@OpenApiResponse(status = STATUS_204, description = "Lookup Type successfully stored to CWMS.")
162+
@OpenApiResponse(status = STATUS_200, description = "Updated Lookup Type")
160163
}
161164
)
162165
@Override
@@ -170,7 +173,9 @@ public void update(Context ctx, String name) {
170173
DSLContext dsl = getDslContext(ctx);
171174
LookupTypeDao dao = new LookupTypeDao(dsl);
172175
dao.updateLookupType(category, prefix, lookupType);
173-
ctx.status(HttpServletResponse.SC_OK).json("Updated Lookup Type");
176+
StatusResponse re = new StatusResponse(lookupType.getOfficeId(), "Updated Lookup Type",
177+
lookupType.getDisplayValue());
178+
ctx.status(HttpServletResponse.SC_OK).json(re);
174179
}
175180
}
176181

@@ -184,7 +189,7 @@ public void update(Context ctx, String name) {
184189
method = HttpMethod.DELETE,
185190
tags = {TAG},
186191
responses = {
187-
@OpenApiResponse(status = STATUS_204, description = "Lookup Type successfully deleted from CWMS."),
192+
@OpenApiResponse(status = STATUS_200, description = "Lookup Type successfully deleted from CWMS."),
188193
@OpenApiResponse(status = STATUS_404, description = "Based on the combination of inputs provided the lookup type was not found.")
189194
}
190195
)
@@ -197,7 +202,8 @@ public void delete(Context ctx, @NotNull String displayValue) {
197202
DSLContext dsl = getDslContext(ctx);
198203
LookupTypeDao dao = new LookupTypeDao(dsl);
199204
dao.deleteLookupType(category, prefix, officeId, displayValue);
200-
ctx.status(HttpServletResponse.SC_NO_CONTENT).json(displayValue + " Deleted");
205+
StatusResponse re = new StatusResponse(officeId, "Lookup Type successfully deleted from CWMS.", displayValue);
206+
ctx.status(HttpServletResponse.SC_OK).json(re);
201207
}
202208
}
203209

cwms-data-api/src/main/java/cwms/cda/api/MeasurementController.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,16 @@
4949
import static cwms.cda.api.Controllers.OFFICE;
5050
import static cwms.cda.api.Controllers.OFFICE_MASK;
5151
import static cwms.cda.api.Controllers.QUALITY;
52+
import static cwms.cda.api.Controllers.STATUS_200;
53+
import static cwms.cda.api.Controllers.STATUS_404;
5254
import static cwms.cda.api.Controllers.TIMEZONE;
5355
import static cwms.cda.api.Controllers.UNIT_SYSTEM;
5456
import static cwms.cda.api.Controllers.queryParamAsDouble;
5557
import static cwms.cda.api.Controllers.queryParamAsInstant;
5658
import static cwms.cda.api.Controllers.requiredParam;
5759
import cwms.cda.api.enums.UnitSystem;
5860
import cwms.cda.data.dao.MeasurementDao;
61+
import cwms.cda.data.dto.StatusResponse;
5962
import cwms.cda.data.dto.measurement.Measurement;
6063
import cwms.cda.formatters.ContentType;
6164
import cwms.cda.formatters.Formats;
@@ -188,7 +191,7 @@ public void getOne(@NotNull Context ctx, @NotNull String locationId) {
188191
method = HttpMethod.POST,
189192
tags = {TAG},
190193
responses = {
191-
@OpenApiResponse(status = "204", description = "Measurement(s) successfully stored.")
194+
@OpenApiResponse(status = "201", description = "Measurement(s) successfully stored.")
192195
}
193196
)
194197
@Override
@@ -202,12 +205,8 @@ public void create(Context ctx) {
202205
DSLContext dsl = getDslContext(ctx);
203206
MeasurementDao dao = new MeasurementDao(dsl);
204207
dao.storeMeasurements(measurements, failIfExists);
205-
String statusMsg = "Created Measurement";
206-
if(measurements.size() > 1)
207-
{
208-
statusMsg += "s";
209-
}
210-
ctx.status(HttpServletResponse.SC_CREATED).json(statusMsg);
208+
StatusResponse re = new StatusResponse(measurements.get(0).getOfficeId(), "Measurement(s) successfully stored.");
209+
ctx.status(HttpServletResponse.SC_CREATED).json(re);
211210
}
212211
}
213212

@@ -245,8 +244,8 @@ public void update(@NotNull Context ctx, @NotNull String locationId) {
245244
method = HttpMethod.DELETE,
246245
tags = {TAG},
247246
responses = {
248-
@OpenApiResponse(status = "204", description = "Measurement successfully deleted."),
249-
@OpenApiResponse(status = "404", description = "Measurement not found.")
247+
@OpenApiResponse(status = STATUS_200, description = "Measurement successfully deleted."),
248+
@OpenApiResponse(status = STATUS_404, description = "Measurement not found.")
250249
}
251250
)
252251
@Override
@@ -260,7 +259,8 @@ public void delete(@NotNull Context ctx, @NotNull String locationId) {
260259
DSLContext dsl = getDslContext(ctx);
261260
MeasurementDao dao = new MeasurementDao(dsl);
262261
dao.deleteMeasurements(officeId, locationId, minDate, maxDate,minNum, maxNum);
263-
ctx.status(HttpServletResponse.SC_NO_CONTENT).json( "Measurements for " + locationId + " Deleted");
262+
StatusResponse re = new StatusResponse(officeId, "Measurement successfully deleted for specified location-id.", locationId);
263+
ctx.status(HttpServletResponse.SC_OK).json( re);
264264
}
265265
}
266266

0 commit comments

Comments
 (0)