Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Iqss/9150 handle fundreg reqs for ext cvv #9402

Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Support searching through all objs in array
Made method recursive. The new =* syntax allows navigating to all
children in an array, which is required for handling the crossref funder
registry output to extract labels in other languages
  • Loading branch information
qqmyers committed Feb 21, 2023
commit 6bf71a205bcfe9746d8182789641af0582673480
95 changes: 64 additions & 31 deletions src/main/java/edu/harvard/iq/dataverse/DatasetFieldServiceBean.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@
import javax.inject.Named;
import javax.json.Json;
import javax.json.JsonArray;
import javax.json.JsonArrayBuilder;
import javax.json.JsonException;
import javax.json.JsonObject;
import javax.json.JsonObjectBuilder;
import javax.json.JsonReader;
import javax.json.JsonString;
import javax.json.JsonValue;
import javax.json.JsonValue.ValueType;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.NonUniqueResultException;
Expand Down Expand Up @@ -542,37 +544,7 @@ private JsonObject filterResponse(JsonObject cvocEntry, JsonObject readObject, S
String[] pathParts = param.split("/");
logger.fine("PP: " + String.join(", ", pathParts));
JsonValue curPath = readObject;
for (int j = 0; j < pathParts.length - 1; j++) {
if (pathParts[j].contains("=")) {
JsonArray arr = ((JsonArray) curPath);
for (int k = 0; k < arr.size(); k++) {
String[] keyVal = pathParts[j].split("=");
logger.fine("Looking for object where " + keyVal[0] + " is " + keyVal[1]);
JsonObject jo = arr.getJsonObject(k);
String val = jo.getString(keyVal[0]);
String expected = keyVal[1];
if (expected.equals("@id")) {
expected = termUri;
}
if (val.equals(expected)) {
logger.fine("Found: " + jo.toString());
curPath = jo;
break;
}
}
} else {
curPath = ((JsonObject) curPath).get(pathParts[j]);
logger.fine("Found next Path object " + curPath.toString());
}
}
JsonValue jv = ((JsonObject) curPath).get(pathParts[pathParts.length - 1]);
if (jv.getValueType().equals(JsonValue.ValueType.STRING)) {
vals.add(i, ((JsonString) jv).getString());
} else if (jv.getValueType().equals(JsonValue.ValueType.ARRAY)) {
vals.add(i, jv);
} else if (jv.getValueType().equals(JsonValue.ValueType.OBJECT)) {
vals.add(i, jv);
}
vals.add(i, processPathSegment(0, pathParts, curPath, termUri));
logger.fine("Added param value: " + i + ": " + vals.get(i));
} else {
logger.fine("Param is: " + param);
Expand Down Expand Up @@ -615,6 +587,7 @@ private JsonObject filterResponse(JsonObject cvocEntry, JsonObject readObject, S
} catch (Exception e) {
logger.warning("External Vocabulary: " + termUri + " - Failed to find value for " + filterKey + ": "
+ e.getMessage());
e.printStackTrace();
}
}
}
Expand All @@ -628,6 +601,66 @@ private JsonObject filterResponse(JsonObject cvocEntry, JsonObject readObject, S
}
}

Object processPathSegment(int index, String[] pathParts, JsonValue curPath, String termUri) {
if (index < pathParts.length - 1) {
if (pathParts[index].contains("=")) {
JsonArray arr = ((JsonArray) curPath);
String[] keyVal = pathParts[index].split("=");
logger.fine("Looking for object where " + keyVal[0] + " is " + keyVal[1]);
String expected = keyVal[1];

if (!expected.equals("*")) {
if (expected.equals("@id")) {
expected = termUri;
}
for (int k = 0; k < arr.size(); k++) {
JsonObject jo = arr.getJsonObject(k);
String val = jo.getString(keyVal[0]);
if (val.equals(expected)) {
logger.fine("Found: " + jo.toString());
curPath = jo;
return processPathSegment(index + 1, pathParts, curPath, termUri);
}
}
} else {
JsonArrayBuilder parts = Json.createArrayBuilder();
for (JsonValue subPath : arr) {
if (subPath instanceof JsonObject) {
JsonValue nextValue = ((JsonObject) subPath).get(keyVal[0]);
Object obj = processPathSegment(index + 1, pathParts, nextValue, termUri);
if (obj instanceof String) {
parts.add((String) obj);
} else {
parts.add((JsonValue) obj);
}
}
}
return parts.build();
}

} else {
curPath = ((JsonObject) curPath).get(pathParts[index]);
logger.fine("Found next Path object " + curPath.toString());
return processPathSegment(index + 1, pathParts, curPath, termUri);
}
} else {
logger.fine("Last segment: " + curPath.toString());
logger.fine("Looking for : " + pathParts[index]);
JsonValue jv = ((JsonObject) curPath).get(pathParts[index]);
ValueType type =jv.getValueType();
if (type.equals(JsonValue.ValueType.STRING)) {
return ((JsonString) jv).getString();
} else if (jv.getValueType().equals(JsonValue.ValueType.ARRAY)) {
return jv;
} else if (jv.getValueType().equals(JsonValue.ValueType.OBJECT)) {
return jv;
}
}

return null;

}

/**
* Supports validation of externally controlled values. If the value is a URI it
* must be in the namespace (start with) one of the uriSpace values of an
Expand Down