Skip to content

Commit

Permalink
handle recursion in link command
Browse files Browse the repository at this point in the history
  • Loading branch information
pdurbin committed Oct 28, 2024
1 parent ca62d99 commit 6e8f573
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@
import edu.harvard.iq.dataverse.engine.command.CommandContext;
import edu.harvard.iq.dataverse.engine.command.DataverseRequest;
import edu.harvard.iq.dataverse.engine.command.exception.CommandException;
import java.util.ArrayList;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.Logger;
import java.util.stream.Stream;

/**
Expand All @@ -23,6 +25,8 @@
// no annotations here, since permissions are dynamically decided
public class ListMetadataBlocksCommand extends AbstractCommand<List<MetadataBlock>> {

private static final Logger logger = Logger.getLogger(ListMetadataBlocksCommand.class.getCanonicalName());

private final Dataverse dataverse;
private final boolean onlyDisplayedOnCreate;
private final DatasetType datasetType;
Expand All @@ -36,35 +40,27 @@ public ListMetadataBlocksCommand(DataverseRequest request, Dataverse dataverse,

@Override
public List<MetadataBlock> execute(CommandContext ctxt) throws CommandException {
System.out.println("got to execute...");
if (onlyDisplayedOnCreate) {
return listMetadataBlocksDisplayedOnCreate(ctxt, dataverse);
}
return dataverse.getMetadataBlocks();
List<MetadataBlock> orig = dataverse.getMetadataBlocks();
List<MetadataBlock> extraFromDatasetTypes = new ArrayList<>();
if (datasetType != null) {
extraFromDatasetTypes = datasetType.getMetadataBlocks();
}
return Stream.concat(orig.stream(), extraFromDatasetTypes.stream()).toList();
}

private List<MetadataBlock> listMetadataBlocksDisplayedOnCreate(CommandContext ctxt, Dataverse dataverse) {
System.out.println("got to listMetadataBlocksDisplayedOnCreate...");
// TODO keep all this isMetadataBlockRoot and is root collection business?
// if (dataverse.isMetadataBlockRoot() || dataverse.getOwner() == null) {
// return ctxt.metadataBlocks().listMetadataBlocksDisplayedOnCreate(dataverse);
// }
// return listMetadataBlocksDisplayedOnCreate(ctxt, dataverse.getOwner());
List<MetadataBlock> metadataBlocks = ctxt.metadataBlocks().listMetadataBlocksDisplayedOnCreate(dataverse);
if (datasetType == null) {
System.out.println("no dataset type, returning normal list");
return metadataBlocks;
} else {
// Add the metadata blocks based on the dataset type
System.out.println("yes dataset type, returning extra");
List<MetadataBlock> extra = datasetType.getMetadataBlocks();
System.out.println("size of extra: " + extra.size());
for (MetadataBlock metadataBlock : extra) {
System.out.println("name: " + metadataBlock.getDisplayName());
if (dataverse.isMetadataBlockRoot() || dataverse.getOwner() == null) {
List<MetadataBlock> orig = ctxt.metadataBlocks().listMetadataBlocksDisplayedOnCreate(dataverse);
List<MetadataBlock> extraFromDatasetTypes = new ArrayList<>();
if (datasetType != null) {
extraFromDatasetTypes = datasetType.getMetadataBlocks();
}
return Stream.concat(metadataBlocks.stream(), extra.stream()).toList();
return Stream.concat(orig.stream(), extraFromDatasetTypes.stream()).toList();
}

return listMetadataBlocksDisplayedOnCreate(ctxt, dataverse.getOwner());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import java.util.List;

// inspired by UpdateDataverseMetadataBlocksCommand
@RequiredPermissions({})
@RequiredPermissions({}) //TODO add auth!
public class UpdateDatasetTypeLinksWithMetadataBlocks extends AbstractVoidCommand {
// rename to "to"? UpdateDatasetTypeLinksToMetadataBlocks

Expand Down
63 changes: 52 additions & 11 deletions src/test/java/edu/harvard/iq/dataverse/api/DatasetTypesIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasItemInArray;
import static org.junit.jupiter.api.Assertions.assertEquals;
Expand Down Expand Up @@ -281,11 +282,21 @@ public void testUpdateDatasetTypeLinksWithMetadataBlocks() {
String apiToken = UtilIT.getApiTokenFromResponse(createUser);
UtilIT.setSuperuserStatus(username, true).then().assertThat().statusCode(OK.getStatusCode());

Response getCitationBlock = UtilIT.getMetadataBlock("citation");
getCitationBlock.prettyPrint();
getCitationBlock.then().assertThat()
System.out.println("listing root collection blocks with display on create: only citation");
Response listBlocks = UtilIT.listMetadataBlocks(":root", true, false, apiToken);
listBlocks.prettyPrint();
listBlocks.then().assertThat()
.statusCode(OK.getStatusCode())
.body("data[0].name", is("citation"))
.body("data[1].name", nullValue());

System.out.println("listing root collection blocks without display on create: only citation");
listBlocks = UtilIT.listMetadataBlocks(":root", false, false, apiToken);
listBlocks.prettyPrint();
listBlocks.then().assertThat()
.statusCode(OK.getStatusCode())
.body("data.associatedDatasetTypes[0]", CoreMatchers.nullValue());
.body("data[0].name", is("citation"))
.body("data[1].name", nullValue());

//Avoid all-numeric names (which are not allowed)
String randomName = "zzz" + UUID.randomUUID().toString().substring(0, 8);
Expand Down Expand Up @@ -320,21 +331,52 @@ public void testUpdateDatasetTypeLinksWithMetadataBlocks() {
getTypeById = UtilIT.getDatasetType(typeId.toString());
getTypeById.prettyPrint();
getTypeById.then().assertThat()
.statusCode(OK.getStatusCode());
// .statusCode(OK.getStatusCode())
// .body("data.linkedMetadataBlocks.after[0]", CoreMatchers.is("geospatial"));
.statusCode(OK.getStatusCode())
.body("data.linkedMetadataBlocks[0]", CoreMatchers.is("geospatial"));

System.out.println("listing root collection blocks with display on create");
listBlocks = UtilIT.listMetadataBlocks(":root", true, false, randomName, apiToken);
listBlocks.prettyPrint();
listBlocks.then().assertThat()
.statusCode(OK.getStatusCode())
.body("data[0].name", is("citation"))
.body("data[1].name", is("geospatial"))
.body("data[2].name", nullValue());

System.out.println("listing root collection blocks without display on create");
listBlocks = UtilIT.listMetadataBlocks(":root", false, false, randomName, apiToken);
listBlocks.prettyPrint();
listBlocks.then().assertThat()
.statusCode(OK.getStatusCode())
.body("data[0].name", is("citation"))
.body("data[1].name", is("geospatial"))
.body("data[2].name", nullValue());

Response createDataverse = UtilIT.createRandomDataverse(apiToken);
createDataverse.then().assertThat().statusCode(CREATED.getStatusCode());

String dataverseAlias = UtilIT.getAliasFromResponse(createDataverse);
Integer dataverseId = UtilIT.getDataverseIdFromResponse(createDataverse);

Response listBlocks = UtilIT.listMetadataBlocks(dataverseAlias, true, false, randomName, apiToken);
UtilIT.publishDataverseViaNativeApi(dataverseAlias, apiToken).then().assertThat().statusCode(OK.getStatusCode());

System.out.println("listing " + dataverseAlias + "collection blocks with display on create using dataset type " + randomName);
listBlocks = UtilIT.listMetadataBlocks(dataverseAlias, true, false, randomName, apiToken);
listBlocks.prettyPrint();
listBlocks.then().assertThat()
.statusCode(OK.getStatusCode())
.body("data[0].name", is("citation"))
.body("data[1].name", is("geospatial"))
.body("data[2].name", nullValue());

System.out.println("listing " + dataverseAlias + "collection blocks without display on create using dataset type " + randomName);
listBlocks = UtilIT.listMetadataBlocks(dataverseAlias, false, false, randomName, apiToken);
listBlocks.prettyPrint();
listBlocks.then().assertThat()
.statusCode(OK.getStatusCode())
.body("data[0].name", is("geospatial"));
.body("data[0].name", is("citation"))
.body("data[1].name", is("geospatial"))
.body("data[2].name", nullValue());

// We send an empty array to mean "delete or clear all"
String emptyJsonArray = "[]";
Expand All @@ -347,8 +389,7 @@ public void testUpdateDatasetTypeLinksWithMetadataBlocks() {
listBlocks = UtilIT.listMetadataBlocks(dataverseAlias, true, false, randomName, apiToken);
listBlocks.prettyPrint();
listBlocks.then().assertThat()
.statusCode(OK.getStatusCode())
.body("data[0].name", CoreMatchers.nullValue());
.body("data[0].name", is("citation"));
}

}

0 comments on commit 6e8f573

Please sign in to comment.