Skip to content

Commit e6af7be

Browse files
authored
Merge pull request #11793 from QualitativeDataRepository/CVoc-improvement
CVoc retrieval filtering enhancement
2 parents 40bf831 + 81aa56b commit e6af7be

File tree

3 files changed

+71
-2
lines changed

3 files changed

+71
-2
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This version of Dataverse includes extensions of the Dataverse External Vocabulary mechanism (https://guides.dataverse.org/en/latest/admin/metadatacustomization.html#using-external-vocabulary-services) that improve Dataverse's ability to include metadata about vocabulary terms and external identifiers such as ORCID and ROR in it's metadata exports. More information on how to configure external vocabulary scripts to use this functionality can be found at https://github.com/gdcc/dataverse-external-vocab-support/blob/main/docs/readme.md and in the examples in the https://github.com/gdcc/dataverse-external-vocab-support repository.

src/main/java/edu/harvard/iq/dataverse/DatasetFieldServiceBean.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -764,13 +764,24 @@ Object processPathSegment(int index, String[] pathParts, JsonValue curPath, Stri
764764
JsonValue val = jo.get(keyVal[0]);
765765
if (val != null) {
766766
if (val.getValueType().equals(ValueType.STRING)) {
767+
//Match a string value
767768
if (((JsonString) val).getString().equals(expected)) {
768769
logger.fine("Found: " + jo);
769770
curPath = jo;
770771
return processPathSegment(index + 1, pathParts, curPath, termUri);
771772
}
772-
} else {
773-
logger.warning("Expected a string value for " + keyVal[0] + " but found: " + val.getValueType());
773+
} else if (val.getValueType() == JsonValue.ValueType.ARRAY) {
774+
// Match one string in an array
775+
JsonArray jsonArray = (JsonArray) val;
776+
for (JsonValue arrayVal : jsonArray) {
777+
if (arrayVal.getValueType() == JsonValue.ValueType.STRING) {
778+
if (((JsonString) arrayVal).getString().equals(expected)) {
779+
logger.fine("Found match in array: " + jo.toString());
780+
curPath = jo;
781+
return processPathSegment(index + 1, pathParts, curPath, termUri);
782+
}
783+
}
784+
}
774785
}
775786
}
776787
}

src/test/java/edu/harvard/iq/dataverse/DatasetFieldServiceBeanTest.java

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package edu.harvard.iq.dataverse;
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertNotNull;
5+
import static org.junit.jupiter.api.Assertions.assertNull;
6+
import static org.junit.jupiter.api.Assertions.assertTrue;
47

58
import java.io.File;
69
import java.io.IOException;
@@ -17,7 +20,10 @@
1720

1821
import edu.harvard.iq.dataverse.settings.SettingsServiceBean;
1922
import jakarta.json.Json;
23+
import jakarta.json.JsonArray;
24+
import jakarta.json.JsonArrayBuilder;
2025
import jakarta.json.JsonObject;
26+
import jakarta.json.JsonObjectBuilder;
2127

2228
public class DatasetFieldServiceBeanTest {
2329

@@ -152,6 +158,57 @@ void getIndexableStringsByTermUriOrcid() throws IOException {
152158
assertEquals(Collections.emptySet(), result);
153159
}
154160

161+
@Test
162+
public void testProcessPathSegmentWithArrayStringMatching() {
163+
// Create a DatasetFieldServiceBean instance
164+
DatasetFieldServiceBean bean = new DatasetFieldServiceBean();
165+
166+
String termUri = "http://example.org/term/123";
167+
168+
// Create a JSON structure with an array containing string values
169+
JsonArrayBuilder tagsArrayBuilder = Json.createArrayBuilder();
170+
JsonObject testObject = Json.createObjectBuilder().add("tag", Json.createArrayBuilder().add("research").add("science")).add("name", "one").build();
171+
tagsArrayBuilder.add(testObject);
172+
JsonObject testObject2 = Json.createObjectBuilder().add("tag", "art").add("name", "two").build();
173+
tagsArrayBuilder.add(testObject2);
174+
JsonObject testObject3 = Json.createObjectBuilder().add("tag", termUri).add("name", "three").build();
175+
tagsArrayBuilder.add(testObject3);
176+
JsonArray tags = tagsArrayBuilder.build();
177+
178+
// Test case 1: Match a string in an array of strings
179+
String[] pathParts = { "tag=research", "name" };
180+
Object result = bean.processPathSegment(0, pathParts, tags, termUri);
181+
182+
// The method should return the test object when a match is found
183+
// Result should not be null when a match is found
184+
assertNotNull(result);
185+
assertEquals("one", result, "Result should be the original test object");
186+
187+
// Test case 2: Match a string
188+
pathParts[0] = "tag=art";
189+
result = bean.processPathSegment(0, pathParts, tags, termUri);
190+
191+
// The method should return the test object when a match is found
192+
// Result should not be null when a match is found
193+
assertNotNull(result);
194+
assertEquals("two", result, "Result should be the original test object");
195+
196+
// Test case 3: No match in the array
197+
String[] noMatchPathParts = { "tags=nonexistent" };
198+
Object noMatchResult = bean.processPathSegment(0, noMatchPathParts, testObject, termUri);
199+
200+
// The method should return null when no match is found
201+
assertNull(noMatchResult, "Result should be null when no match is found");
202+
203+
// Test case 4: Match with @id substitution
204+
205+
String[] idPathParts = { "tag=@id", "name" };
206+
Object idResult = bean.processPathSegment(0, idPathParts, tags, termUri);
207+
208+
assertNotNull(idResult, "Result should not be null when matching with @id");
209+
assertEquals("three", idResult, "Result should be the original test object");
210+
}
211+
155212
/**
156213
* Prepare unit tests with mock methods.
157214
*

0 commit comments

Comments
 (0)