Skip to content

Commit

Permalink
hacking with g portas
Browse files Browse the repository at this point in the history
  • Loading branch information
pdurbin committed Oct 30, 2024
1 parent 9fa10c7 commit 30ab3e9
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 67 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package edu.harvard.iq.dataverse;

import edu.harvard.iq.dataverse.dataset.DatasetType;
import edu.harvard.iq.dataverse.dataset.DatasetTypeServiceBean;
import java.io.IOException;
import java.io.StringReader;
import java.net.URI;
Expand Down Expand Up @@ -52,6 +54,7 @@
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;
import edu.harvard.iq.dataverse.settings.SettingsServiceBean;
import java.util.stream.Stream;

/**
*
Expand All @@ -69,6 +72,9 @@ public class DatasetFieldServiceBean implements java.io.Serializable {
@EJB
SettingsServiceBean settingsService;

@EJB
DatasetTypeServiceBean datasetTypeService;

private static final String NAME_QUERY = "SELECT dsfType from DatasetFieldType dsfType where dsfType.name= :fieldName";

/*
Expand Down Expand Up @@ -864,7 +870,8 @@ public String getFieldLanguage(String languages, String localeCode) {
return null;
}

public List<DatasetFieldType> findAllDisplayedOnCreateInMetadataBlock(MetadataBlock metadataBlock) {
// public List<DatasetFieldType> findAllDisplayedOnCreateInMetadataBlock(MetadataBlock metadataBlock) {
public List<DatasetFieldType> findAllDisplayedOnCreateInMetadataBlock(MetadataBlock metadataBlock, String datasetTypeIn) {
CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
CriteriaQuery<DatasetFieldType> criteriaQuery = criteriaBuilder.createQuery(DatasetFieldType.class);

Expand Down Expand Up @@ -955,7 +962,32 @@ public List<DatasetFieldType> findAllInMetadataBlockAndDataverse(MetadataBlock m

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

return em.createQuery(criteriaQuery).getResultList();
if (true) {
System.out.println("returning early if true!!");
return em.createQuery(criteriaQuery).getResultList();
}

System.out.println("got here 2 - non-root: " + 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());
}

List<DatasetFieldType> extraFromDatasetTypes = new ArrayList<>();
DatasetType datasetType = datasetTypeService.getByName("software");
System.out.println("found software, adding");
for (MetadataBlock mdb : datasetType.getMetadataBlocks()) {
System.out.println("software, found mdb: " + mdb.getName());
if (mdb.equals(metadataBlock)) {
for (DatasetFieldType datasetFieldType : metadataBlock.getDatasetFieldTypes()) {
extraFromDatasetTypes.add(datasetFieldType);
}
}
}
System.out.println("done adding software fields");

return Stream.concat(orig.stream(), extraFromDatasetTypes.stream()).toList();
}

private Predicate buildRequiredInDataversePredicate(CriteriaBuilder criteriaBuilder, Root<DatasetFieldType> datasetFieldTypeRoot) {
Expand Down
12 changes: 9 additions & 3 deletions src/main/java/edu/harvard/iq/dataverse/api/Dataverses.java
Original file line number Diff line number Diff line change
Expand Up @@ -756,13 +756,16 @@ public Response deleteDataverseLinkingDataverse(@Context ContainerRequestContext
}, getRequestUser(crc));
}


// everything is persisted
@GET
@AuthRequired
@Path("{identifier}/metadatablocks")
public Response listMetadataBlocks(@Context ContainerRequestContext crc,
@PathParam("identifier") String dvIdtf,
@QueryParam("onlyDisplayedOnCreate") boolean onlyDisplayedOnCreate,
@QueryParam("returnDatasetFieldTypes") boolean returnDatasetFieldTypes,
@QueryParam("includeDatasetTypes") boolean includeDatasetTypes,
@QueryParam("datasetType") String datasetTypeIn) {
logger.info("dataset type passed in : " + datasetTypeIn);
DatasetType datasetType = datasetTypeSvc.getByName(datasetTypeIn);
Expand All @@ -772,11 +775,14 @@ public Response listMetadataBlocks(@Context ContainerRequestContext crc,
new ListMetadataBlocksCommand(
createDataverseRequest(getRequestUser(crc)),
dataverse,
onlyDisplayedOnCreate,
datasetType
onlyDisplayedOnCreate
// datasetType
)
);
return ok(json(metadataBlocks, returnDatasetFieldTypes, onlyDisplayedOnCreate, dataverse));
for (MetadataBlock metadataBlock : metadataBlocks) {
logger.info("found metadata block: " + metadataBlock.getName());
}
return ok(json(metadataBlocks, returnDatasetFieldTypes, onlyDisplayedOnCreate, dataverse, datasetType.getName()));
} catch (WrappedResponse we) {
return we.getResponse();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,15 @@
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 @@ -25,40 +21,26 @@
// 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, DatasetType datasetType) {
public ListMetadataBlocksCommand(DataverseRequest request, Dataverse dataverse, boolean onlyDisplayedOnCreate) {
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);
}
List<MetadataBlock> orig = dataverse.getMetadataBlocks();
List<MetadataBlock> extraFromDatasetTypes = new ArrayList<>();
if (datasetType != null) {
extraFromDatasetTypes = datasetType.getMetadataBlocks();
}
return Stream.concat(orig.stream(), extraFromDatasetTypes.stream()).toList();
return dataverse.getMetadataBlocks();
}

private List<MetadataBlock> listMetadataBlocksDisplayedOnCreate(CommandContext ctxt, Dataverse dataverse) {
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(orig.stream(), extraFromDatasetTypes.stream()).toList();
return ctxt.metadataBlocks().listMetadataBlocksDisplayedOnCreate(dataverse);
}
return listMetadataBlocksDisplayedOnCreate(ctxt, dataverse.getOwner());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public JsonObjectBuilder json( DatasetVersion dsv ) {
}

public JsonObjectBuilder json( MetadataBlock blk ) {
System.out.println("calling brief");
return ( blk==null )
? null
: jsonObjectBuilder().add("id", blk.getId())
Expand Down
25 changes: 19 additions & 6 deletions src/main/java/edu/harvard/iq/dataverse/util/json/JsonPrinter.java
Original file line number Diff line number Diff line change
Expand Up @@ -596,13 +596,17 @@ public static JsonObjectBuilder json(MetadataBlock block, List<DatasetField> fie
}

public static JsonArrayBuilder json(List<MetadataBlock> metadataBlocks, boolean returnDatasetFieldTypes, boolean printOnlyDisplayedOnCreateDatasetFieldTypes) {
return json(metadataBlocks, returnDatasetFieldTypes, printOnlyDisplayedOnCreateDatasetFieldTypes, null);
// return json(metadataBlocks, returnDatasetFieldTypes, printOnlyDisplayedOnCreateDatasetFieldTypes, null);
return json(metadataBlocks, returnDatasetFieldTypes, printOnlyDisplayedOnCreateDatasetFieldTypes, null, null);
}

public static JsonArrayBuilder json(List<MetadataBlock> metadataBlocks, boolean returnDatasetFieldTypes, boolean printOnlyDisplayedOnCreateDatasetFieldTypes, Dataverse ownerDataverse) {
// public static JsonArrayBuilder json(List<MetadataBlock> metadataBlocks, boolean returnDatasetFieldTypes, boolean printOnlyDisplayedOnCreateDatasetFieldTypes, Dataverse ownerDataverse) {
public static JsonArrayBuilder json(List<MetadataBlock> metadataBlocks, boolean returnDatasetFieldTypes, boolean printOnlyDisplayedOnCreateDatasetFieldTypes, Dataverse ownerDataverse, String datasetTypeIn) {
logger.info("returnDatasetFieldTypes: " + returnDatasetFieldTypes + " dataset type: " +datasetTypeIn);
JsonArrayBuilder arrayBuilder = Json.createArrayBuilder();
for (MetadataBlock metadataBlock : metadataBlocks) {
arrayBuilder.add(returnDatasetFieldTypes ? json(metadataBlock, printOnlyDisplayedOnCreateDatasetFieldTypes, ownerDataverse) : brief.json(metadataBlock));
// arrayBuilder.add(returnDatasetFieldTypes ? json(metadataBlock, printOnlyDisplayedOnCreateDatasetFieldTypes, ownerDataverse) : brief.json(metadataBlock));
arrayBuilder.add(returnDatasetFieldTypes ? json(metadataBlock, printOnlyDisplayedOnCreateDatasetFieldTypes, ownerDataverse, datasetTypeIn) : brief.json(metadataBlock));
}
return arrayBuilder;
}
Expand Down Expand Up @@ -630,10 +634,14 @@ public static JsonObject json(DatasetField dfv) {
}

public static JsonObjectBuilder json(MetadataBlock metadataBlock) {
return json(metadataBlock, false, null);
// return json(metadataBlock, false, null);
return json(metadataBlock, false, null, null);
}

public static JsonObjectBuilder json(MetadataBlock metadataBlock, boolean printOnlyDisplayedOnCreateDatasetFieldTypes, Dataverse ownerDataverse) {
// public static JsonObjectBuilder json(MetadataBlock metadataBlock, boolean printOnlyDisplayedOnCreateDatasetFieldTypes, Dataverse ownerDataverse) {
// rename ownerDataverse
public static JsonObjectBuilder json(MetadataBlock metadataBlock, boolean printOnlyDisplayedOnCreateDatasetFieldTypes, Dataverse ownerDataverse, String datasetTypeIn) {
logger.info("calling full, printOnlyDisplayedOnCreateDatasetFieldTypes=" +printOnlyDisplayedOnCreateDatasetFieldTypes + " datasettype : " + datasetTypeIn + " block=" +metadataBlock.getName());
JsonObjectBuilder jsonObjectBuilder = jsonObjectBuilder()
.add("id", metadataBlock.getId())
.add("name", metadataBlock.getName())
Expand All @@ -643,16 +651,21 @@ public static JsonObjectBuilder json(MetadataBlock metadataBlock, boolean printO
Set<DatasetFieldType> datasetFieldTypes;

if (ownerDataverse != null) {
// use case: creating a dataset
datasetFieldTypes = new TreeSet<>(datasetFieldService.findAllInMetadataBlockAndDataverse(
metadataBlock, ownerDataverse, printOnlyDisplayedOnCreateDatasetFieldTypes));
} else {
// use case: retrieving info about metadata blocks
datasetFieldTypes = printOnlyDisplayedOnCreateDatasetFieldTypes
? new TreeSet<>(datasetFieldService.findAllDisplayedOnCreateInMetadataBlock(metadataBlock))
// ? new TreeSet<>(datasetFieldService.findAllDisplayedOnCreateInMetadataBlock(metadataBlock))
? new TreeSet<>(datasetFieldService.findAllDisplayedOnCreateInMetadataBlock(metadataBlock, datasetTypeIn))
: new TreeSet<>(metadataBlock.getDatasetFieldTypes());
}

JsonObjectBuilder fieldsBuilder = Json.createObjectBuilder();
logger.info(metadataBlock.getName() + " num datasetFieldTypes: " + datasetFieldTypes.size());
for (DatasetFieldType datasetFieldType : datasetFieldTypes) {
// logger.info("datasetFieldType.getName(): " + datasetFieldType.getName());
fieldsBuilder.add(datasetFieldType.getName(), json(datasetFieldType, ownerDataverse));
}

Expand Down
77 changes: 43 additions & 34 deletions src/test/java/edu/harvard/iq/dataverse/api/DatasetTypesIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -389,9 +389,10 @@ public void testUpdateDatasetTypeLinksWithMetadataBlocks() {
* This test is disabled because it relies on the experimental "codeMeta20"
* metadata block to be loaded.
*/
@Disabled
// @Disabled
@Test
public void testLinkSoftwareToCodemeta() {
// public void testLinkSoftwareToCompWork() {
Response listBlocksAvailable = UtilIT.listMetadataBlocks(false, true);
// if (true) {return;}

Expand All @@ -403,14 +404,18 @@ public void testLinkSoftwareToCodemeta() {

String metadataBlockToAssociateDatasetTypeWith = """
["codeMeta20"]
""";

//""";
// String metadataBlockToAssociateDatasetTypeWith = """
// ["computationalworkflow"]
//""";
String datasetType = "software";
// String datasetType = "geospatial";
Response associateGeospatialWithDatasetType1 = UtilIT.updateDatasetTypeLinksWithMetadataBlocks(datasetType, metadataBlockToAssociateDatasetTypeWith, apiToken);
associateGeospatialWithDatasetType1.prettyPrint();
associateGeospatialWithDatasetType1.then().assertThat().
statusCode(OK.getStatusCode())
.body("data.linkedMetadataBlocks.after[0]", CoreMatchers.is("codeMeta20"));
// .body("data.linkedMetadataBlocks.after[0]", CoreMatchers.is("computationalworkflow"));

Response createDataverse = UtilIT.createRandomDataverse(apiToken);
createDataverse.then().assertThat().statusCode(CREATED.getStatusCode());
Expand All @@ -421,44 +426,46 @@ 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));
// TODO: why not shown?
// .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())
// TODO: why is "fields" empty?
.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));
// why not shown?
// .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);
listBlocks.prettyPrint();
listBlocks.then().assertThat()
.statusCode(OK.getStatusCode())
.body("data[0].name", is("citation"))
.body("data[1].name", is("codeMeta20"))
// .body("data[1].name", is("codeMeta20"))
.body("data[2].name", nullValue())
// why not shown?
.body("data[0].fields.title.displayOnCreate", equalTo(true))
.body("data[1].fields.codeVersion.displayOnCreate", equalTo(true));
.body("data[0].fields.title.displayOnCreate", equalTo(true)); //failing, why? not present
// .body("data[1].fields.codeVersion.displayOnCreate", equalTo(true));

System.out.println("ending early");
if (true) {
return;
}

System.out.println("listing " + dataverseAlias + " collection blocks with all fields (not display on create) using dataset type " + datasetType);
listBlocks = UtilIT.listMetadataBlocks(dataverseAlias, false, true, datasetType, apiToken);
Expand All @@ -469,11 +476,13 @@ public void testLinkSoftwareToCodemeta() {
.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))
// why not shown?
// .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("last line");
}

}
Loading

0 comments on commit 30ab3e9

Please sign in to comment.