Skip to content

Commit

Permalink
Merge pull request IQSS#9213 from IQSS/8944-metadatablocks
Browse files Browse the repository at this point in the history
IQSS#8944 - Extend 'metadatablocks/{block_id}' endpoint JSON output
  • Loading branch information
kcondon authored Dec 9, 2022
2 parents 120adc1 + ae83c27 commit d9da8c5
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 7 deletions.
5 changes: 5 additions & 0 deletions doc/release-notes/8944-metadatablocks.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
The API endpoint `/api/metadatablocks/{block_id}` has been extended to include the following fields:

- `controlledVocabularyValues` - All possible values for fields with a controlled vocabulary. For example, the values "Agricultural Sciences", "Arts and Humanities", etc. for the "Subject" field.
- `isControlledVocabulary`: Whether or not this field has a controlled vocabulary.
- `multiple`: Whether or not the field supports multiple values.
8 changes: 6 additions & 2 deletions doc/sphinx-guides/source/admin/metadatacustomization.rst
Original file line number Diff line number Diff line change
Expand Up @@ -386,12 +386,16 @@ Metadata Block Setup

Now that you understand the TSV format used for metadata blocks, the next step is to attempt to make improvements to existing metadata blocks or create entirely new metadata blocks. For either task, you should have a Dataverse Software development environment set up for testing where you can drop the database frequently while you make edits to TSV files. Once you have tested your TSV files, you should consider making a pull request to contribute your improvement back to the community.

.. _exploring-metadata-blocks:

Exploring Metadata Blocks
~~~~~~~~~~~~~~~~~~~~~~~~~

In addition to studying the TSV files themselves you might find the following highly experimental and subject-to-change API endpoints useful to understand the metadata blocks that have already been loaded into your Dataverse installation:
In addition to studying the TSV files themselves you will probably find the :ref:`metadata-blocks-api` API helpful in getting a structured dump of metadata blocks in JSON format.

There are also a few older, highly experimental, and subject-to-change API endpoints under the "admin" API documented below but the public API above is preferred.

You can get a dump of metadata fields (yes, the output is odd, please open a issue) like this:
You can get a dump of metadata fields like this:

``curl http://localhost:8080/api/admin/datasetfield``

Expand Down
33 changes: 29 additions & 4 deletions doc/sphinx-guides/source/api/native-api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3007,22 +3007,47 @@ The fully expanded example above (without environment variables) looks like this
curl https://demo.dataverse.org/api/info/apiTermsOfUse
.. _metadata-blocks-api:
Metadata Blocks
---------------
See also :ref:`exploring-metadata-blocks`.
Show Info About All Metadata Blocks
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|CORS| Lists brief info about all metadata blocks registered in the system::
|CORS| Lists brief info about all metadata blocks registered in the system.
.. code-block:: bash
export SERVER_URL=https://demo.dataverse.org
curl $SERVER_URL/api/metadatablocks
The fully expanded example above (without environment variables) looks like this:
.. code-block:: bash
GET http://$SERVER/api/metadatablocks
curl https://demo.dataverse.org/api/metadatablocks
Show Info About Single Metadata Block
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|CORS| Return data about the block whose ``identifier`` is passed. ``identifier`` can either be the block's id, or its name::
|CORS| Return data about the block whose ``identifier`` is passed, including allowed controlled vocabulary values. ``identifier`` can either be the block's database id, or its name (i.e. "citation").
.. code-block:: bash
export SERVER_URL=https://demo.dataverse.org
export IDENTIFIER=citation
curl $SERVER_URL/api/metadatablocks/$IDENTIFIER
The fully expanded example above (without environment variables) looks like this:
.. code-block:: bash
GET http://$SERVER/api/metadatablocks/$identifier
curl https://demo.dataverse.org/api/metadatablocks/citation
.. _Notifications:
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/edu/harvard/iq/dataverse/util/json/JsonPrinter.java
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,17 @@ public static JsonObjectBuilder json(DatasetFieldType fld) {
fieldsBld.add("type", fld.getFieldType().toString());
fieldsBld.add("watermark", fld.getWatermark());
fieldsBld.add("description", fld.getDescription());
fieldsBld.add("multiple", fld.isAllowMultiples());
fieldsBld.add("isControlledVocabulary", fld.isControlledVocabulary());
if (fld.isControlledVocabulary()) {
// If the field has a controlled vocabulary,
// add all values to the resulting JSON
JsonArrayBuilder jab = Json.createArrayBuilder();
for (ControlledVocabularyValue cvv : fld.getControlledVocabularyValues()) {
jab.add(cvv.getStrValue());
}
fieldsBld.add("controlledVocabularyValues", jab);
}
if (!fld.getChildDatasetFieldTypes().isEmpty()) {
JsonObjectBuilder subFieldsBld = jsonObjectBuilder();
for (DatasetFieldType subFld : fld.getChildDatasetFieldTypes()) {
Expand Down
26 changes: 26 additions & 0 deletions src/test/java/edu/harvard/iq/dataverse/api/MetadataBlocksIT.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package edu.harvard.iq.dataverse.api;

import com.jayway.restassured.RestAssured;
import com.jayway.restassured.response.Response;
import static javax.ws.rs.core.Response.Status.OK;
import org.hamcrest.CoreMatchers;
import org.junit.BeforeClass;
import org.junit.Test;

public class MetadataBlocksIT {

@BeforeClass
public static void setUpClass() {
RestAssured.baseURI = UtilIT.getRestAssuredBaseUri();
}

@Test
public void testGetCitationBlock() {
Response getCitationBlock = UtilIT.getMetadataBlock("citation");
getCitationBlock.prettyPrint();
getCitationBlock.then().assertThat()
.statusCode(OK.getStatusCode())
.body("data.fields.subject.controlledVocabularyValues[0]", CoreMatchers.is("Agricultural Sciences"));
}

}
5 changes: 5 additions & 0 deletions src/test/java/edu/harvard/iq/dataverse/api/UtilIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,11 @@ static Response setMetadataBlocks(String dataverseAlias, JsonArrayBuilder blocks
.post("/api/dataverses/" + dataverseAlias + "/metadatablocks");
}

static Response getMetadataBlock(String block) {
return given()
.get("/api/metadatablocks/" + block);
}

static private String getDatasetXml(String title, String author, String description) {
String nullLicense = null;
String nullRights = null;
Expand Down
2 changes: 1 addition & 1 deletion tests/integration-tests.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
DataversesIT,DatasetsIT,SwordIT,AdminIT,BuiltinUsersIT,UsersIT,UtilIT,ConfirmEmailIT,FileMetadataIT,FilesIT,SearchIT,InReviewWorkflowIT,HarvestingServerIT,HarvestingClientsIT,MoveIT,MakeDataCountApiIT,FileTypeDetectionIT,EditDDIIT,ExternalToolsIT,AccessIT,DuplicateFilesIT,DownloadFilesIT,LinkIT,DeleteUsersIT,DeactivateUsersIT,AuxiliaryFilesIT,InvalidCharactersIT,LicensesIT,NotificationsIT,BagIT
DataversesIT,DatasetsIT,SwordIT,AdminIT,BuiltinUsersIT,UsersIT,UtilIT,ConfirmEmailIT,FileMetadataIT,FilesIT,SearchIT,InReviewWorkflowIT,HarvestingServerIT,HarvestingClientsIT,MoveIT,MakeDataCountApiIT,FileTypeDetectionIT,EditDDIIT,ExternalToolsIT,AccessIT,DuplicateFilesIT,DownloadFilesIT,LinkIT,DeleteUsersIT,DeactivateUsersIT,AuxiliaryFilesIT,InvalidCharactersIT,LicensesIT,NotificationsIT,BagIT,MetadataBlocksIT

0 comments on commit d9da8c5

Please sign in to comment.