Skip to content

Commit

Permalink
start passing datasetType in again (revert experiment)
Browse files Browse the repository at this point in the history
G Portas was interested in seeing the behavior of
ListMetadataBlocksCommand before all my hacks.

Now those hacks are back in.
  • Loading branch information
pdurbin committed Oct 30, 2024
1 parent 57a743f commit b2605d7
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -962,17 +962,18 @@ public List<DatasetFieldType> findAllInMetadataBlockAndDataverse(MetadataBlock m

criteriaQuery.select(datasetFieldTypeRoot).distinct(true);

if (true) {
System.out.println("returning early if true!!");
return em.createQuery(criteriaQuery).getResultList();
}
// if (true) {
// System.out.println("returning early if true!!");
// // This was the old, original return
// return em.createQuery(criteriaQuery).getResultList();
// }

System.out.println("got here 2 - non-root: " + dataverse.getAlias());
System.out.println("got here 2 - non-root collection: " + dataverse.getAlias());
List<DatasetFieldType> orig = em.createQuery(criteriaQuery).getResultList();
logger.info("orig size: " + orig.size());
for (DatasetFieldType datasetFieldType : orig) {
logger.info("from orig: " + datasetFieldType.getName());
}
// for (DatasetFieldType datasetFieldType : orig) {
// logger.info("from orig: " + datasetFieldType.getName());
// }

List<DatasetFieldType> extraFromDatasetTypes = new ArrayList<>();
DatasetType datasetType = datasetTypeService.getByName("software");
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/edu/harvard/iq/dataverse/api/Dataverses.java
Original file line number Diff line number Diff line change
Expand Up @@ -768,15 +768,16 @@ public Response listMetadataBlocks(@Context ContainerRequestContext crc,
@QueryParam("includeDatasetTypes") boolean includeDatasetTypes,
@QueryParam("datasetType") String datasetTypeIn) {
logger.info("dataset type passed in : " + datasetTypeIn);
// TODO includeDatasetTypes is an idea but is currently unused. Consider deleting.
DatasetType datasetType = datasetTypeSvc.getByName(datasetTypeIn);
try {
Dataverse dataverse = findDataverseOrDie(dvIdtf);
final List<MetadataBlock> metadataBlocks = execCommand(
new ListMetadataBlocksCommand(
createDataverseRequest(getRequestUser(crc)),
dataverse,
onlyDisplayedOnCreate
// datasetType
onlyDisplayedOnCreate,
datasetType
)
);
for (MetadataBlock metadataBlock : metadataBlocks) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,19 @@
import edu.harvard.iq.dataverse.Dataverse;
import edu.harvard.iq.dataverse.MetadataBlock;
import edu.harvard.iq.dataverse.authorization.Permission;
import edu.harvard.iq.dataverse.dataset.DatasetType;
import edu.harvard.iq.dataverse.engine.command.AbstractCommand;
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;

/**
* Lists the metadata blocks of a {@link Dataverse}.
Expand All @@ -21,26 +25,40 @@
// 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;

public ListMetadataBlocksCommand(DataverseRequest request, Dataverse dataverse, boolean onlyDisplayedOnCreate) {
public ListMetadataBlocksCommand(DataverseRequest request, Dataverse dataverse, boolean onlyDisplayedOnCreate, DatasetType datasetType) {
super(request, dataverse);
this.dataverse = dataverse;
this.onlyDisplayedOnCreate = onlyDisplayedOnCreate;
this.datasetType = datasetType;
}

@Override
public List<MetadataBlock> execute(CommandContext ctxt) throws CommandException {
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) {
if (dataverse.isMetadataBlockRoot() || dataverse.getOwner() == null) {
return ctxt.metadataBlocks().listMetadataBlocksDisplayedOnCreate(dataverse);
List<MetadataBlock> orig = ctxt.metadataBlocks().listMetadataBlocksDisplayedOnCreate(dataverse);
List<MetadataBlock> extraFromDatasetTypes = new ArrayList<>();
if (datasetType != null) {
extraFromDatasetTypes = datasetType.getMetadataBlocks();
}
return Stream.concat(orig.stream(), extraFromDatasetTypes.stream()).toList();
}
return listMetadataBlocksDisplayedOnCreate(ctxt, dataverse.getOwner());
}
Expand Down
46 changes: 23 additions & 23 deletions src/test/java/edu/harvard/iq/dataverse/api/DatasetTypesIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -440,29 +440,29 @@ public void testLinkSoftwareToCodemeta() {
UtilIT.publishDataverseViaNativeApi(dataverseAlias, apiToken).then().assertThat().statusCode(OK.getStatusCode());

Response listBlocks = null;
// System.out.println("listing root collection blocks with display on create using dataset type " + datasetType);
// listBlocks = UtilIT.listMetadataBlocks(":root", true, true, datasetType, apiToken);
// listBlocks.prettyPrint();
// listBlocks.then().assertThat()
// .statusCode(OK.getStatusCode())
// .body("data[0].name", is("citation"))
// .body("data[1].name", is("codeMeta20"))
// .body("data[2].name", nullValue())
// .body("data[0].fields.title.displayOnCreate", equalTo(true));
//// .body("data[1].fields.codeVersion.displayOnCreate", equalTo(true));
//
// System.out.println("listing root collection blocks with all fields (not display on create) using dataset type " + datasetType);
// listBlocks = UtilIT.listMetadataBlocks(":root", false, true, datasetType, apiToken);
// listBlocks.prettyPrint();
// listBlocks.then().assertThat()
// .statusCode(OK.getStatusCode())
// .body("data[0].name", is("citation"))
// .body("data[1].name", is("codeMeta20"))
// .body("data[2].name", nullValue())
// .body("data[0].fields.title.displayOnCreate", equalTo(true))
// .body("data[0].fields.subtitle.displayOnCreate", equalTo(false));
//// .body("data[1].fields.codeVersion.displayOnCreate", equalTo(true))
//// .body("data[1].fields.issueTracker.displayOnCreate", equalTo(false));
System.out.println("listing root collection blocks with display on create using dataset type " + datasetType);
listBlocks = UtilIT.listMetadataBlocks(":root", true, true, datasetType, apiToken);
listBlocks.prettyPrint();
listBlocks.then().assertThat()
.statusCode(OK.getStatusCode())
.body("data[0].name", is("citation"))
.body("data[1].name", is("codeMeta20"))
.body("data[2].name", nullValue())
.body("data[0].fields.title.displayOnCreate", equalTo(true));
// .body("data[1].fields.codeVersion.displayOnCreate", equalTo(true));

System.out.println("listing root collection blocks with all fields (not display on create) using dataset type " + datasetType);
listBlocks = UtilIT.listMetadataBlocks(":root", false, true, datasetType, apiToken);
listBlocks.prettyPrint();
listBlocks.then().assertThat()
.statusCode(OK.getStatusCode())
.body("data[0].name", is("citation"))
.body("data[1].name", is("codeMeta20"))
.body("data[2].name", nullValue())
.body("data[0].fields.title.displayOnCreate", equalTo(true))
.body("data[0].fields.subtitle.displayOnCreate", equalTo(false));
// .body("data[1].fields.codeVersion.displayOnCreate", equalTo(true))
// .body("data[1].fields.issueTracker.displayOnCreate", equalTo(false));

System.out.println("listing " + dataverseAlias + " collection blocks with display on create using dataset type " + datasetType);
listBlocks = UtilIT.listMetadataBlocks(dataverseAlias, true, true, datasetType, apiToken);
Expand Down

0 comments on commit b2605d7

Please sign in to comment.