Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -356,8 +356,9 @@ public Set<Long> getCvocFieldSet() {
/**
* Adds information about the external vocabulary term being used in this DatasetField to the ExternalVocabularyValue table if it doesn't already exist.
* @param df - the primitive/parent compound field containing a newly saved value
* @param dataset - the dataset containing this field, used for dataset-level parameter substitution in retrieval-uri
*/
public void registerExternalVocabValues(DatasetField df) {
public void registerExternalVocabValues(DatasetField df, Dataset dataset) {
DatasetFieldType dft = df.getDatasetFieldType();
logger.fine("Registering for field: " + dft.getName());
JsonObject cvocEntry = getCVocConf(true).get(dft.getId());
Expand All @@ -367,7 +368,7 @@ public void registerExternalVocabValues(DatasetField df) {
siblingsDatasetFields = df.getParentDatasetFieldCompoundValue().getChildDatasetFields();
}
for (DatasetFieldValue dfv : df.getDatasetFieldValues()) {
registerExternalTerm(cvocEntry, dfv.getValue(), siblingsDatasetFields);
registerExternalTerm(cvocEntry, dfv.getValue(), siblingsDatasetFields, dataset);
}
} else {
if (df.getDatasetFieldType().isCompound()) {
Expand All @@ -376,7 +377,7 @@ public void registerExternalVocabValues(DatasetField df) {
for (DatasetField cdf : cv.getChildDatasetFields()) {
logger.fine("Found term uri field type id: " + cdf.getDatasetFieldType().getId());
if (cdf.getDatasetFieldType().equals(termdft)) {
registerExternalTerm(cvocEntry, cdf.getValue(), cv.getChildDatasetFields());
registerExternalTerm(cvocEntry, cdf.getValue(), cv.getChildDatasetFields(), dataset);
}
}
}
Expand Down Expand Up @@ -514,8 +515,9 @@ public JsonObject getExternalVocabularyValue(String termUri) {
* @param cvocEntry - the configuration for the DatasetFieldType associated with this term
* @param term - the term uri as a string
* @param relatedDatasetFields - siblings or childs of the term
* @param dataset - the dataset containing the term, used for dataset-level parameter substitution
*/
public void registerExternalTerm(JsonObject cvocEntry, String term, List<DatasetField> relatedDatasetFields) {
public void registerExternalTerm(JsonObject cvocEntry, String term, List<DatasetField> relatedDatasetFields, Dataset dataset) {
String retrievalUri = cvocEntry.getString("retrieval-uri", null);
String termUriFieldName = cvocEntry.getString("term-uri-field");
String prefix = cvocEntry.getString("prefix", null);
Expand Down Expand Up @@ -571,6 +573,13 @@ public void registerExternalTerm(JsonObject cvocEntry, String term, List<Dataset
for (DatasetField f : relatedDatasetFields) {
retrievalUri = tryToReplaceRetrievalUriParam(retrievalUri, f.getDatasetFieldType().getName(), f.getValue());
}
// Dataset-level parameter substitution
if (dataset != null) {
retrievalUri = tryToReplaceRetrievalUriParam(retrievalUri, "datasetId", String.valueOf(dataset.getId()));
if (dataset.getGlobalId() != null) {
retrievalUri = tryToReplaceRetrievalUriParam(retrievalUri, "datasetPID", dataset.getGlobalId().asString());
}
}
} catch (InvalidParameterException e) {
logger.warning("InvalidParameterException in tryReplaceRetrievalUriParam : " + e.getMessage());
return;
Expand Down
16 changes: 11 additions & 5 deletions src/main/java/edu/harvard/iq/dataverse/api/LocalContexts.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ public class LocalContexts extends AbstractApiBean {
@EJB
PermissionServiceBean permissionService;

static final String LOCAL_CONTEXTS_INFO_NOT_FOUND = "LocalContexts information not found for this dataset identifier.";
static final String INVALID_LOCAL_CONTEXTS_RESPONSE = "Invalid response from Local Contexts API.";

@GET
@Path("/datasets/{id}")
@Produces(MediaType.APPLICATION_JSON)
Expand Down Expand Up @@ -117,6 +120,10 @@ public Response getDatasetLocalContexts(@Context ContainerRequestContext crc,
}
}

/**
* Returns a Local Contexts project only when the project response includes a DOI matching the specified dataset.
* This call is used by the CVoc script in dataset pages and by the back end.
*/
@GET
@Path("/datasets/{id}/{projectId}")
@Produces(MediaType.APPLICATION_JSON)
Expand Down Expand Up @@ -169,15 +176,14 @@ public Response searchLocalContexts(
}
}
// DOI mismatch, return 404
return error(Response.Status.NOT_FOUND,
"LocalContexts information not found for this dataset.");
return error(Response.Status.NOT_FOUND, LOCAL_CONTEXTS_INFO_NOT_FOUND);
} else {
// "publication_doi" key not found in the response, return 404
return error(Response.Status.NOT_FOUND, "Invalid response from Local Contexts API.");
return error(Response.Status.NOT_FOUND, INVALID_LOCAL_CONTEXTS_RESPONSE);
}
} else {
// "external_ids" key not found in the response, return 404
return error(Response.Status.NOT_FOUND, "Invalid response from Local Contexts API.");
return error(Response.Status.NOT_FOUND, INVALID_LOCAL_CONTEXTS_RESPONSE);
}

} else {
Expand All @@ -193,7 +199,7 @@ public Response searchLocalContexts(
return error(Response.Status.SERVICE_UNAVAILABLE, "Error contacting LocalContexts");
}
} catch (WrappedResponse ex) {
return ex.getResponse();
return error(Response.Status.NOT_FOUND, LOCAL_CONTEXTS_INFO_NOT_FOUND);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@

protected void registerFilePidsIfNeeded(Dataset theDataset, CommandContext ctxt, boolean b) throws CommandException {
// Register file PIDs if needed
PidProvider pidGenerator = ctxt.dvObjects().getEffectivePidGenerator(getDataset());

Check warning on line 232 in src/main/java/edu/harvard/iq/dataverse/engine/command/impl/AbstractDatasetCommand.java

View workflow job for this annotation

GitHub Actions / (Stable / JDK 21) Unit Tests

getEffectivePidGenerator(edu.harvard.iq.dataverse.DvObjectContainer) in edu.harvard.iq.dataverse.DvObjectServiceBean has been deprecated and marked for removal

Check warning on line 232 in src/main/java/edu/harvard/iq/dataverse/engine/command/impl/AbstractDatasetCommand.java

View workflow job for this annotation

GitHub Actions / (Stable / JDK 21) Integration Tests

getEffectivePidGenerator(edu.harvard.iq.dataverse.DvObjectContainer) in edu.harvard.iq.dataverse.DvObjectServiceBean has been deprecated and marked for removal

Check warning on line 232 in src/main/java/edu/harvard/iq/dataverse/engine/command/impl/AbstractDatasetCommand.java

View workflow job for this annotation

GitHub Actions / Publish App Image / Build & Test

getEffectivePidGenerator(edu.harvard.iq.dataverse.DvObjectContainer) in edu.harvard.iq.dataverse.DvObjectServiceBean has been deprecated and marked for removal

Check warning on line 232 in src/main/java/edu/harvard/iq/dataverse/engine/command/impl/AbstractDatasetCommand.java

View workflow job for this annotation

GitHub Actions / Dataverse Tests / Dataverse API Tests

getEffectivePidGenerator(edu.harvard.iq.dataverse.DvObjectContainer) in edu.harvard.iq.dataverse.DvObjectServiceBean has been deprecated and marked for removal
boolean shouldRegister = !pidGenerator.registerWhenPublished() &&
ctxt.systemConfig().isFilePIDsEnabledForCollection(getDataset().getOwner()) &&
pidGenerator.canCreatePidsLike(getDataset().getGlobalId());
Expand Down Expand Up @@ -315,7 +315,7 @@
for (DatasetField df : newVersion.getFlatDatasetFields()) {
logger.fine("Found id: " + df.getDatasetFieldType().getId());
if (ctxt.dsField().getCVocConf(true).containsKey(df.getDatasetFieldType().getId())) {
ctxt.dsField().registerExternalVocabValues(df);
ctxt.dsField().registerExternalVocabValues(df, getDataset());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.security.InvalidParameterException;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

public class DatasetFieldServiceBeanFilterResponseTest {

Expand Down Expand Up @@ -287,4 +289,73 @@ private JsonObject readObject(String pathname) throws IOException {
filterResponseMethod.setAccessible(true);
return (JsonObject) filterResponseMethod.invoke(datasetFieldServiceBean, cvocEntry, readObject, termURI);
}

@Test
void tryToReplaceRetrievalUriParam_replaces_datasetId() throws Exception {
Method method = DatasetFieldServiceBean.class.getDeclaredMethod("tryToReplaceRetrievalUriParam", String.class, String.class, String.class);
method.setAccessible(true);

String result = (String) method.invoke(datasetFieldServiceBean,
"https://example.com/api/datasets/{datasetId}/terms/{0}",
"datasetId", "123");

assertThat(result).isEqualTo("https://example.com/api/datasets/123/terms/{0}");
}

@Test
void tryToReplaceRetrievalUriParam_replaces_datasetPID() throws Exception {
Method method = DatasetFieldServiceBean.class.getDeclaredMethod("tryToReplaceRetrievalUriParam", String.class, String.class, String.class);
method.setAccessible(true);

String result = (String) method.invoke(datasetFieldServiceBean,
"https://example.com/api/datasets/:persistentId/terms/{0}?persistentId={datasetPID}",
"datasetPID", "doi:10.1234/abc");

assertThat(result).isEqualTo("https://example.com/api/datasets/:persistentId/terms/{0}?persistentId=doi:10.1234/abc");
}

@Test
void tryToReplaceRetrievalUriParam_skips_missing_param() throws Exception {
Method method = DatasetFieldServiceBean.class.getDeclaredMethod("tryToReplaceRetrievalUriParam", String.class, String.class, String.class);
method.setAccessible(true);

String uri = "https://example.com/api/terms/{0}";
String result = (String) method.invoke(datasetFieldServiceBean, uri, "datasetId", "123");

assertThat(result).isEqualTo("https://example.com/api/terms/{0}");
}

@Test
void tryToReplaceRetrievalUriParam_throws_on_blank_value() throws Exception {
Method method = DatasetFieldServiceBean.class.getDeclaredMethod("tryToReplaceRetrievalUriParam", String.class, String.class, String.class);
method.setAccessible(true);

assertThatThrownBy(() -> method.invoke(datasetFieldServiceBean,
"https://example.com/api/datasets/{datasetId}/terms/{0}",
"datasetId", ""))
.hasCauseInstanceOf(InvalidParameterException.class);
}

@Test
void tryToReplaceRetrievalUriParam_throws_on_null_value() throws Exception {
Method method = DatasetFieldServiceBean.class.getDeclaredMethod("tryToReplaceRetrievalUriParam", String.class, String.class, String.class);
method.setAccessible(true);

assertThatThrownBy(() -> method.invoke(datasetFieldServiceBean,
"https://example.com/api/datasets/{datasetId}/terms/{0}",
"datasetId", (String) null))
.hasCauseInstanceOf(InvalidParameterException.class);
}

@Test
void tryToReplaceRetrievalUriParam_encodes_url_for_datasetPersistentId() throws Exception {
Method method = DatasetFieldServiceBean.class.getDeclaredMethod("tryToReplaceRetrievalUriParam", String.class, String.class, String.class);
method.setAccessible(true);

String result = (String) method.invoke(datasetFieldServiceBean,
"https://example.com/api/datasets/:persistentId/terms/{0}?persistentId={encodeUrl:datasetPID}",
"datasetPID", "doi:10.1234/abc");

assertThat(result).isEqualTo("https://example.com/api/datasets/:persistentId/terms/{0}?persistentId=doi%3A10.1234%2Fabc");
}
}
Loading