Skip to content

Commit

Permalink
persist "software" etc in new datasetType entity IQSS#10517
Browse files Browse the repository at this point in the history
  • Loading branch information
pdurbin committed Jul 15, 2024
1 parent 830ea35 commit b3c2dce
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 12 deletions.
11 changes: 6 additions & 5 deletions src/main/java/edu/harvard/iq/dataverse/Dataset.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package edu.harvard.iq.dataverse;

import edu.harvard.iq.dataverse.dataset.DatasetThumbnail;
import edu.harvard.iq.dataverse.dataset.DatasetType;
import edu.harvard.iq.dataverse.dataset.DatasetUtil;
import edu.harvard.iq.dataverse.harvest.client.HarvestingClient;
import edu.harvard.iq.dataverse.license.License;
Expand Down Expand Up @@ -38,7 +39,6 @@
import edu.harvard.iq.dataverse.storageuse.StorageUse;
import edu.harvard.iq.dataverse.util.StringUtil;
import edu.harvard.iq.dataverse.util.SystemConfig;
import jakarta.persistence.Transient;

/**
*
Expand Down Expand Up @@ -129,8 +129,9 @@ public class Dataset extends DvObjectContainer {
*/
private boolean useGenericThumbnail;

@Transient
private String datasetType;
@OneToOne(cascade = {CascadeType.MERGE, CascadeType.PERSIST})
@JoinColumn(name = "datasettype_id", nullable = true)
private DatasetType datasetType;

@OneToOne(cascade = {CascadeType.MERGE, CascadeType.PERSIST})
@JoinColumn(name = "guestbook_id", unique = false, nullable = true, insertable = true, updatable = true)
Expand Down Expand Up @@ -741,11 +742,11 @@ public void setUseGenericThumbnail(boolean useGenericThumbnail) {
this.useGenericThumbnail = useGenericThumbnail;
}

public String getDatasetType() {
public DatasetType getDatasetType() {
return datasetType;
}

public void setDatasetType(String datasetType) {
public void setDatasetType(DatasetType datasetType) {
this.datasetType = datasetType;
}

Expand Down
79 changes: 79 additions & 0 deletions src/main/java/edu/harvard/iq/dataverse/dataset/DatasetType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package edu.harvard.iq.dataverse.dataset;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import java.io.Serializable;
import java.util.Arrays;

@Entity
public class DatasetType implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Column(nullable = false)
@Enumerated(EnumType.STRING)
private Type baseType;

/**
* This default constructor is only here to prevent this error at
* deployment:
*
* Exception Description: The instance creation method
* [...DatasetType.<Default Constructor>], with no parameters, does not
* exist, or is not accessible
*
* Don't use it.
*/
@Deprecated
public DatasetType() {
}

public DatasetType(Type baseType) {
this.baseType = baseType;
}

public enum Type {

DATASET("dataset"),
SOFTWARE("software"),
WORKFLOW("workflow");

private final String text;

private Type(final String text) {
this.text = text;
}

public static Type fromString(String text) {
if (text != null) {
for (Type type : Type.values()) {
if (text.equals(type.text)) {
return type;
}
}
}
throw new IllegalArgumentException("Type must be one of these values: " + Arrays.asList(Type.values()) + ".");
}

@Override
public String toString() {
return text;
}
}

public Type getBaseType() {
return baseType;
}

public void setBaseType(Type baseType) {
this.baseType = baseType;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,12 @@ public static void checkMetadataLangauge(Dataset ds, Dataverse owner, Map<String

public static void checkDatasetType(Dataset ds, boolean enabled) {
logger.info("called checkDatasetType...");
String datasetType = ds.getDatasetType();
String datasetType = ds.getDatasetType().getBaseType().name();
logger.info("datasetType: " + datasetType);
if (datasetType != null) {
if (!enabled) {
throw new BadRequestException("The dataset types feature is not enabled but a type was sent: " + datasetType);
}
// TODO: check for valid types.
logger.info("The dataset type sent was: " + datasetType);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import edu.harvard.iq.dataverse.authorization.groups.impl.ipaddress.ip.IpAddress;
import edu.harvard.iq.dataverse.authorization.groups.impl.ipaddress.ip.IpAddressRange;
import edu.harvard.iq.dataverse.authorization.groups.impl.maildomain.MailDomainGroup;
import edu.harvard.iq.dataverse.dataset.DatasetType;
import edu.harvard.iq.dataverse.datasetutility.OptionalFileParams;
import edu.harvard.iq.dataverse.harvest.client.HarvestingClient;
import edu.harvard.iq.dataverse.license.License;
Expand Down Expand Up @@ -331,7 +332,7 @@ public Dataset parseDataset(JsonObject obj) throws JsonParseException {
String datasetType = obj.getString("datasetType",null);
logger.info("datasetType: " + datasetType);
if (datasetType != null) {
dataset.setDatasetType(datasetType);
dataset.setDatasetType(new DatasetType(DatasetType.Type.fromString(datasetType)));
}

DatasetVersion dsv = new DatasetVersion();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import edu.harvard.iq.dataverse.authorization.users.User;
import edu.harvard.iq.dataverse.branding.BrandingUtil;
import edu.harvard.iq.dataverse.dataaccess.DataAccess;
import edu.harvard.iq.dataverse.dataset.DatasetType;
import edu.harvard.iq.dataverse.dataset.DatasetUtil;
import edu.harvard.iq.dataverse.datavariable.CategoryMetadata;
import edu.harvard.iq.dataverse.datavariable.DataVariable;
Expand Down Expand Up @@ -406,6 +407,10 @@ public static JsonObjectBuilder json(Dataset ds, Boolean returnOwners) {
if (returnOwners){
bld.add("isPartOf", getOwnersFromDvObject(ds));
}
DatasetType datasetType = ds.getDatasetType();
if (datasetType != null) {
bld.add("datasetType", datasetType.getBaseType().toString());
}
return bld;
}

Expand Down
13 changes: 9 additions & 4 deletions src/test/java/edu/harvard/iq/dataverse/api/DatasetTypesIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import io.restassured.response.Response;
import static jakarta.ws.rs.core.Response.Status.CREATED;
import static jakarta.ws.rs.core.Response.Status.OK;
import org.junit.jupiter.api.Assertions;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
Expand All @@ -28,20 +30,23 @@ public void testCreateSoftwareDatasetNative() {
String dataverseAlias = UtilIT.getAliasFromResponse(createDataverse);
Integer dataverseId = UtilIT.getDataverseIdFromResponse(createDataverse);

// String datasetJsonPath = "doc/sphinx-guides/source/_static/api/dataset-create-software.json";
String jsonIn = UtilIT.getDatasetJson("doc/sphinx-guides/source/_static/api/dataset-create-software.json");
// System.out.println("native: " + datasetJsonPath);

Response createSoftware = UtilIT.createDataset(dataverseAlias, jsonIn, apiToken);
createSoftware.prettyPrint();
createSoftware.then().assertThat()
.statusCode(CREATED.getStatusCode());

//TODO: try sending "junk" instead of "software".

//TODO: try sending "junk" instead of "software".
Integer datasetId = UtilIT.getDatasetIdFromResponse(createSoftware);
String datasetPid = JsonPath.from(createSoftware.getBody().asString()).getString("data.persistentId");

Response getDatasetJson = UtilIT.nativeGet(datasetId, apiToken);
getDatasetJson.prettyPrint();
getDatasetJson.then().assertThat().statusCode(OK.getStatusCode());
String dataseType = JsonPath.from(getDatasetJson.getBody().asString()).getString("data.datasetType");
System.out.println("datasetType: " + dataseType);
assertEquals("software", dataseType);
}

@Disabled
Expand Down

0 comments on commit b3c2dce

Please sign in to comment.