Skip to content

Commit

Permalink
Remove duplicate results from Nahima before choosing best one
Browse files Browse the repository at this point in the history
  • Loading branch information
GenieTim committed Dec 12, 2023
1 parent e53fee6 commit efe8949
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 6 deletions.
32 changes: 27 additions & 5 deletions src/main/java/edu/harvard/mcz/imagecapture/data/NahimaManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@
import edu.harvard.mcz.imagecapture.exceptions.SkipSpecimenException;
import edu.harvard.mcz.imagecapture.ui.dialog.ChooseFromJArrayDialog;
import edu.harvard.mcz.imagecapture.ui.dialog.VerifyJSONDialog;
import edu.harvard.mcz.imagecapture.utility.AbstractRestClient;
import edu.harvard.mcz.imagecapture.utility.FileUtility;
import edu.harvard.mcz.imagecapture.utility.ListUtility;
import edu.harvard.mcz.imagecapture.utility.NullHandlingUtility;
import edu.harvard.mcz.imagecapture.utility.*;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
Expand Down Expand Up @@ -466,6 +463,31 @@ public JSONObject resolveOrCreate(String name, String objectType, String mask, J
return results;
}

/**
* Remove duplicate entries (ignoring underscores in JSON Object' keys) from a JSONArray
*
* @param results
* @return
*/
public JSONArray removeDuplicates(JSONArray results) {
JSONArray uniqueResults = new JSONArray();
for (int i = 0; i < results.length(); i++) {
JSONObject entry = results.getJSONObject(i);
boolean found = false;
// check that this is not present yet
for (int j = 0; j < uniqueResults.length(); j++) {
JSONObject compareTo = uniqueResults.getJSONObject(j);
if (JSONUtility.areEqualIgnoringUnderscore(entry, compareTo)) {
found = true;
}
}
if (!found) {
uniqueResults.put(entry);
}
}
return uniqueResults;
}

/**
* Search an object in Nahima by a string.
* Ask the user whether to create it if it is not found,
Expand All @@ -492,7 +514,7 @@ public JSONObject resolveOrCreateInteractive(String name, String objectType, Str
((results != null && results.has("code")) ? results.getString("code") : "Unknown"));
}

JSONArray foundObjects = (JSONArray) results.get("objects");
JSONArray foundObjects = removeDuplicates(results.getJSONArray("objects"));
if (foundObjects.length() == 1) {
// TODO: check compliance, does it really match well? We use "must", so let's hope so, but the resolution could still go wrong.
return (JSONObject) foundObjects.get(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ protected String postRequest(String url, String data, Map<String, String> header
.uri(URI.create(url))
.setHeader("User-Agent", "DataShot " + ImageCaptureApp.getAppVersion()) // add request header
.setHeader("Content-Type", "application/x-www-form-urlencoded")
.timeout(Duration.ofMinutes(5));
.timeout(Duration.ofMinutes(3));

if (headers != null) {
for (Map.Entry<String, String> entry : headers.entrySet()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package edu.harvard.mcz.imagecapture.utility;

import org.jetbrains.annotations.NotNull;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.Iterator;

public class JSONUtility {

public static boolean areEqualIgnoringUnderscore(JSONArray a1, JSONArray a2) {
if (a1.length() != a2.length()) {
return false;
}
for (int i = 0; i < a1.length(); i++) {
Object obj1 = a1.get(i);
Object obj2 = a2.get(i);
if (!objectsAreEqual(obj1, obj2)) {
return false;
}
}
return true;
}

public static boolean areEqualIgnoringUnderscore(JSONObject obj1, JSONObject obj2) {
return areEqualIgnoringUnderscore(obj1, obj2, false);
}

private static boolean areEqualIgnoringUnderscore(@NotNull JSONObject obj1, JSONObject obj2, boolean inverted) {
Iterator<String> keys = obj1.keys();
while (keys.hasNext()) {
String key = keys.next();
if (key.startsWith("_")) {
continue;
}
if (!obj2.has(key)) {
return false;
}
Object val1 = obj1.get(key);
Object val2 = obj2.get(key);
if (!objectsAreEqual(val1, val2)) {
return false;
}
}

if (inverted) {
return true;
} else {
return areEqualIgnoringUnderscore(obj2, obj1, true);
}
}

private static boolean objectsAreEqual(Object val1, Object val2) {
if (val1 instanceof JSONObject && val2 instanceof JSONObject) {
return areEqualIgnoringUnderscore((JSONObject) val1, (JSONObject) val2);
} else if (val1 instanceof JSONArray && val2 instanceof JSONArray) {
return areEqualIgnoringUnderscore((JSONArray) val1, (JSONArray) val2);
} //else {
return val1.equals(val2);
}
}

0 comments on commit efe8949

Please sign in to comment.