Skip to content

Commit

Permalink
CWDB-225 - Updated to de-support update/PATCH for measurements.
Browse files Browse the repository at this point in the history
  • Loading branch information
rma-bryson committed Oct 17, 2024
1 parent 4f75f87 commit 87859c1
Show file tree
Hide file tree
Showing 8 changed files with 3 additions and 593 deletions.
2 changes: 0 additions & 2 deletions cwms-data-api/src/main/java/cwms/cda/ApiServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import static cwms.cda.api.Controllers.OFFICE;
import static cwms.cda.api.Controllers.PROJECT_ID;
import static cwms.cda.api.Controllers.WATER_USER;
import cwms.cda.api.MeasurementPatchController;
import static io.javalin.apibuilder.ApiBuilder.crud;
import static io.javalin.apibuilder.ApiBuilder.delete;
import static io.javalin.apibuilder.ApiBuilder.get;
Expand Down Expand Up @@ -542,7 +541,6 @@ protected void configureRoutes() {
cdaCrudCache(format("/stream-reaches/{%s}", NAME),
new StreamReachController(metrics), requiredRoles,1, TimeUnit.DAYS);
String measurements = "/measurements/";
patch(measurements, new MeasurementPatchController(metrics));
cdaCrudCache(format(measurements + "{%s}", LOCATION_ID),
new cwms.cda.api.MeasurementController(metrics), requiredRoles,5, TimeUnit.MINUTES);
cdaCrudCache("/blobs/{blob-id}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ public void create(Context ctx) {
@Override
public void update(@NotNull Context ctx, @NotNull String locationId) {
try (final Timer.Context ignored = markAndTime(GET_ONE)) {
throw new UnsupportedOperationException("Not supported with required location Id");
throw new UnsupportedOperationException(NOT_SUPPORTED_YET);
}
}

Expand Down

This file was deleted.

89 changes: 0 additions & 89 deletions cwms-data-api/src/main/java/cwms/cda/data/dao/MeasurementDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@
import java.util.ArrayList;
import java.util.Date;
import java.util.TimeZone;
import java.util.stream.Collectors;
import mil.army.usace.hec.metadata.location.LocationTemplate;
import org.jooq.DSLContext;

Expand All @@ -71,13 +70,11 @@
import org.jooq.impl.DSL;
import usace.cwms.db.dao.util.OracleTypeMap;
import usace.cwms.db.jooq.codegen.packages.CWMS_STREAM_PACKAGE;
import static usace.cwms.db.jooq.codegen.tables.AV_STREAMFLOW_MEAS.AV_STREAMFLOW_MEAS;
import usace.cwms.db.jooq.codegen.udt.records.STREAMFLOW_MEAS2_T;
import usace.cwms.db.jooq.codegen.udt.records.STREAMFLOW_MEAS2_TAB_T;

public final class MeasurementDao extends JooqDao<Measurement> {
static final XmlMapper XML_MAPPER = buildXmlMapper();
public static final String IGNORE_EXISTING_CHECK_FOR_BULK_UPDATE_PROPERTY = "measurement.ignoreExistingCheckForBulkUpdate";

public MeasurementDao(DSLContext dsl) {
super(dsl);
Expand Down Expand Up @@ -151,63 +148,6 @@ private void storeMeasurementsJooq(Connection conn, List<Measurement> measuremen
}
}

/**
* Updates an existing measurement
* @param measurement - the measurement to update
*/
public void updateMeasurement(Measurement measurement) {
connection(dsl, conn -> {
setOffice(conn, measurement.getOfficeId());
verifyMeasurementExists(conn, measurement);
storeMeasurementJooq(conn, measurement, false);
});
}

/**
* Updates a list of existing measurements
* @param measurements - the measurements to update
*/
public void updateMeasurements(List<Measurement> measurements)
{
connection(dsl, conn -> {
if(!measurements.isEmpty()) {
List<List<Measurement>> measurementsByOffice = new ArrayList<>(measurements.stream()
.collect(Collectors.groupingBy(Measurement::getOfficeId))
.values());
for (List<Measurement> measurementsList : measurementsByOffice) {
Measurement measurement = measurements.get(0);
String officeId = measurement.getOfficeId();
setOffice(conn, officeId);
//group measurementsList by locationId
List<List<Measurement>> measurementsByLocation = new ArrayList<>(measurementsList.stream()
.collect(Collectors.groupingBy(Measurement::getLocationId))
.values());
for (List<Measurement> locationMeasurements : measurementsByLocation) {
String locationId = locationMeasurements.get(0).getLocationId();
verifyMeasurementsExists(conn, officeId, locationId, locationMeasurements);
storeMeasurementsJooq(conn, locationMeasurements, false);
}
}
}
});
}

// Helper method to retrieve existing measurement numbers from the database
private List<String> getExistingMeasurementNumbers(Connection conn, String officeId, String locationId, List<String> measurementNumbers) {
usace.cwms.db.jooq.codegen.tables.AV_STREAMFLOW_MEAS view = AV_STREAMFLOW_MEAS;
return getDslContext(conn, officeId)
.selectDistinct(view.LOCATION_ID, view.OFFICE_ID, view.MEAS_NUMBER)
.from(view)
.where(view.LOCATION_ID.eq(locationId)
.and(view.OFFICE_ID.eq(officeId))
.and(view.MEAS_NUMBER.in(measurementNumbers)))
.groupBy(view.LOCATION_ID, view.OFFICE_ID, view.MEAS_NUMBER)
.fetch()
.stream()
.map(r -> r.get(view.MEAS_NUMBER))
.collect(Collectors.toList());
}

/**
* Delete a measurement
*
Expand All @@ -229,14 +169,6 @@ public void deleteMeasurements(String officeId, String locationId, Instant minDa
});
}

private void verifyMeasurementExists(Connection conn, Measurement measurement) {
List<Measurement> measurements = retrieveMeasurementsJooq(conn, measurement.getOfficeId(), measurement.getLocationId(), UnitSystem.EN.toString(),
null, null, null, null, measurement.getNumber(), measurement.getNumber(), null, null, null, null, OracleTypeMap.GMT_TIME_ZONE);
if (measurements.isEmpty() || measurements.stream().noneMatch(lt -> lt.getNumber().equals(measurement.getNumber()))) {
throw new NotFoundException("Could not find measurement.");
}
}

private void verifyMeasurementsExists(Connection conn, String officeId, String locationId, String minNum, String maxNum) {
List<Measurement> measurements = retrieveMeasurementsJooq(conn, officeId, locationId, UnitSystem.EN.toString(),
null, null, null, null, minNum, maxNum, null, null, null, null, OracleTypeMap.GMT_TIME_ZONE);
Expand All @@ -245,27 +177,6 @@ private void verifyMeasurementsExists(Connection conn, String officeId, String l
}
}

private void verifyMeasurementsExists(Connection conn, String officeId, String locationId, List<Measurement> locationMeasurements) {
List<String> measurementNumbers = locationMeasurements.stream()
.map(Measurement::getNumber)
.collect(Collectors.toList());

// Retrieve existing measurements from the database
List<String> missingNumbers = new ArrayList<>();
if(!Boolean.getBoolean(IGNORE_EXISTING_CHECK_FOR_BULK_UPDATE_PROPERTY))
{
List<String> existingNumbers = getExistingMeasurementNumbers(conn, officeId, locationId, measurementNumbers);
missingNumbers = new ArrayList<>(measurementNumbers);
// Find missing numbers
missingNumbers.removeAll(existingNumbers);
}

if (!missingNumbers.isEmpty()) {
throw new NotFoundException("Could not find measurements " + String.join(",", missingNumbers) +
" for " + locationId + " in office " + officeId + ".");
}
}

static String toDbXml(List<Measurement> measurements) throws JsonProcessingException {
MeasurementsXmlDto xmlDto = convertMeasurementsToXmlDto(measurements);
return XML_MAPPER.writeValueAsString(xmlDto);
Expand Down
Loading

0 comments on commit 87859c1

Please sign in to comment.