Skip to content

Commit

Permalink
Account for changes to IcatUnits
Browse files Browse the repository at this point in the history
  • Loading branch information
patrick-austin committed Mar 22, 2024
1 parent b1a2514 commit d4bd8f9
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
import org.icatproject.core.entity.User;
import org.icatproject.core.manager.Rest;
import org.icatproject.utils.IcatUnits;
import org.icatproject.utils.IcatUnits.SystemValue;
import org.icatproject.utils.IcatUnits.Value;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -1081,17 +1081,15 @@ private void updateNestedEntityByQuery(OpensearchBulk bulk, long id, String inde
* @param rebuilder JsonObjectBuilder being used to create a new document
* with converted units.
* @param valueString Field name of the numeric value.
* @param numericValue Value to possibly be converted.
* @param numericalValue Value to possibly be converted.
*/
private void convertUnits(JsonObject document, JsonObjectBuilder rebuilder, String valueString,
Double numericValue) {
double numericalValue) {
String unitString = document.getString("type.units");
SystemValue systemValue = icatUnits.new SystemValue(numericValue, unitString);
if (systemValue.units != null) {
rebuilder.add("type.unitsSI", systemValue.units);
}
if (systemValue.value != null) {
rebuilder.add(valueString, systemValue.value);
Value value = icatUnits.convertValueToSiUnits(numericalValue, unitString);
if (value != null) {
rebuilder.add("type.unitsSI", value.units);
rebuilder.add(valueString + "SI", value.numericalValue);
}
}

Expand All @@ -1111,18 +1109,18 @@ private JsonObject convertDocumentUnits(JsonObject document) {
for (String key : document.keySet()) {
rebuilder.add(key, document.get(key));
}
Double numericValue = document.containsKey("numericValue")
? document.getJsonNumber("numericValue").doubleValue()
: null;
Double rangeBottom = document.containsKey("rangeBottom")
? document.getJsonNumber("rangeBottom").doubleValue()
: null;
Double rangeTop = document.containsKey("rangeTop")
? document.getJsonNumber("rangeTop").doubleValue()
: null;
convertUnits(document, rebuilder, "numericValueSI", numericValue);
convertUnits(document, rebuilder, "rangeBottomSI", rangeBottom);
convertUnits(document, rebuilder, "rangeTopSI", rangeTop);
if (document.containsKey("numericValue")) {
double numericValue = document.getJsonNumber("numericValue").doubleValue();
convertUnits(document, rebuilder, "numericValueSI", numericValue);
}
if (document.containsKey("rangeBottom")) {
double rangeBottom = document.getJsonNumber("rangeBottom").doubleValue();
convertUnits(document, rebuilder, "rangeBottomSI", rangeBottom);
}
if (document.containsKey("rangeTop")) {
double rangeTop = document.getJsonNumber("rangeTop").doubleValue();
convertUnits(document, rebuilder, "rangeTopSI", rangeTop);
}
document = rebuilder.build();
return document;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import org.icatproject.core.IcatException;
import org.icatproject.core.IcatException.IcatExceptionType;
import org.icatproject.utils.IcatUnits.SystemValue;
import org.icatproject.utils.IcatUnits.Value;

/**
* Utilities for building queries in Json understood by Opensearch.
Expand Down Expand Up @@ -425,12 +425,12 @@ private void parseRangeFilter(String field, List<JsonObject> queryObjectsList, J
JsonNumber to = nestedFilter.getJsonNumber("to");
String units = nestedFilter.getString("units", null);
if (units != null) {
SystemValue fromValue = opensearchApi.icatUnits.new SystemValue(from.doubleValue(), units);
SystemValue toValue = opensearchApi.icatUnits.new SystemValue(to.doubleValue(), units);
if (fromValue.value != null && toValue.value != null) {
Value fromValue = opensearchApi.icatUnits.convertValueToSiUnits(from.doubleValue(), units);
Value toValue = opensearchApi.icatUnits.convertValueToSiUnits(to.doubleValue(), units);
if (fromValue != null && toValue != null) {
// If we were able to parse the units, apply query to the SI value
String fieldSI = field + "." + nestedField + "SI";
queryObjectsList.add(buildDoubleRangeQuery(fieldSI, fromValue.value, toValue.value));
queryObjectsList.add(buildDoubleRangeQuery(fieldSI, fromValue.numericalValue, toValue.numericalValue));
} else {
// If units could not be parsed, make them part of the query on the raw data
queryObjectsList.add(buildRangeQuery(field + "." + nestedField, from, to));
Expand Down Expand Up @@ -460,13 +460,13 @@ private void parseExactFilter(String field, List<JsonObject> queryObjectsList, J
JsonNumber exact = nestedFilter.getJsonNumber("exact");
String units = nestedFilter.getString("units", null);
if (units != null) {
SystemValue exactValue = opensearchApi.icatUnits.new SystemValue(exact.doubleValue(), units);
if (exactValue.value != null) {
Value exactValue = opensearchApi.icatUnits.convertValueToSiUnits(exact.doubleValue(), units);
if (exactValue != null) {
// If we were able to parse the units, apply query to the SI value
JsonObject bottomQuery = buildDoubleRangeQuery(field + ".rangeBottomSI", null, exactValue.value);
JsonObject topQuery = buildDoubleRangeQuery(field + ".rangeTopSI", exactValue.value, null);
JsonObject bottomQuery = buildDoubleRangeQuery(field + ".rangeBottomSI", null, exactValue.numericalValue);
JsonObject topQuery = buildDoubleRangeQuery(field + ".rangeTopSI", exactValue.numericalValue, null);
JsonObject inRangeQuery = buildBoolQuery(Arrays.asList(bottomQuery, topQuery), null);
JsonObject exactQuery = buildTermQuery(field + "." + nestedField + "SI", exactValue.value);
JsonObject exactQuery = buildTermQuery(field + "." + nestedField + "SI", exactValue.numericalValue);
queryObjectsList.add(buildBoolQuery(null, Arrays.asList(inRangeQuery, exactQuery)));
} else {
// If units could not be parsed, make them part of the query on the raw data
Expand Down

0 comments on commit d4bd8f9

Please sign in to comment.