treatmentservice: add method to check if record with pump id exists.

This commit is contained in:
Johannes Mockenhaupt 2018-07-01 17:18:42 +02:00
parent 780a4dda36
commit f12db81f96
No known key found for this signature in database
GPG key ID: 9E1EA6AF7BBBB0D1

View file

@ -248,12 +248,8 @@ public class TreatmentService extends OrmLiteBaseService<DatabaseHelper> {
if (treatment.source == Source.PUMP) {
// check for changed from pump change in NS
QueryBuilder<Treatment, Long> queryBuilder = getDao().queryBuilder();
Where where = queryBuilder.where();
where.eq("pumpId", treatment.pumpId);
PreparedQuery<Treatment> preparedQuery = queryBuilder.prepare();
List<Treatment> trList = getDao().query(preparedQuery);
if (trList.size() > 0) {
Treatment existingTreatment = getPumpRecordById(treatment.pumpId);
if (existingTreatment != null) {
// do nothing, pump history record cannot be changed
log.debug("TREATMENT: Pump record already found in database: " + treatment.toString());
return false;
@ -322,6 +318,26 @@ public class TreatmentService extends OrmLiteBaseService<DatabaseHelper> {
return false;
}
/** Returns the record for the given id, null if none, throws RuntimeException
* if multiple records with the same pump id exist. */
@Nullable
public Treatment getPumpRecordById(long pumpId) {
try {
QueryBuilder<Treatment, Long> queryBuilder = getDao().queryBuilder();
Where where = queryBuilder.where();
where.eq("pumpId", pumpId);
PreparedQuery<Treatment> preparedQuery = queryBuilder.prepare();
List<Treatment> result = getDao().query(preparedQuery);
switch (result.size()) {
case 0: return null;
case 1: return result.get(1);
default: throw new RuntimeException("Multiple records with the same pump id found: " + result.toString());
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
public void deleteNS(JSONObject json) {
String _id = JsonHelper.safeGetString(json, "_id");
if (_id != null && !_id.isEmpty())