Skip to content

Commit

Permalink
more cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
pdurbin committed Oct 25, 2024
1 parent aa24a7a commit ca62d99
Show file tree
Hide file tree
Showing 11 changed files with 41 additions and 476 deletions.
2 changes: 1 addition & 1 deletion linktest.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash -x
curl -s http://localhost:8080/api/datasets/datasetTypes/dataset -X PUT -d '["geospatial","astrophysics"]' | jq .
curl -s http://localhost:8080/api/datasets/datasetTypes | jq .
curl -s http://localhost:8080/api/datasets/datasetTypes/dataset | jq .
curl -s "http://localhost:8080/api/dataverses/root/metadatablocks?onlyDisplayedOnCreate=true&datasetType=dataset" | jq .
24 changes: 2 additions & 22 deletions src/main/java/edu/harvard/iq/dataverse/MetadataBlock.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package edu.harvard.iq.dataverse;

import edu.harvard.iq.dataverse.dataset.DatasetType;
import edu.harvard.iq.dataverse.util.BundleUtil;
import edu.harvard.iq.dataverse.util.SystemConfig;
import edu.harvard.iq.dataverse.util.json.JsonLDNamespace;
Expand All @@ -17,15 +16,13 @@
import jakarta.persistence.Id;
import jakarta.persistence.Index;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToMany;
import jakarta.persistence.NamedQueries;
import jakarta.persistence.NamedQuery;
import jakarta.persistence.OneToMany;
import jakarta.persistence.OneToOne;
import jakarta.persistence.OrderBy;
import jakarta.persistence.Table;
import jakarta.persistence.Transient;
import java.util.ArrayList;

/**
*
Expand All @@ -35,8 +32,7 @@
, @Index(columnList="owner_id")})
@NamedQueries({
@NamedQuery( name="MetadataBlock.listAll", query = "SELECT mdb FROM MetadataBlock mdb"),
@NamedQuery( name="MetadataBlock.findByName", query = "SELECT mdb FROM MetadataBlock mdb WHERE mdb.name=:name"),
@NamedQuery( name="MetadataBlock.findByDatasetType", query = "SELECT mdb FROM MetadataBlock mdb WHERE mdb.name=:name")
@NamedQuery( name="MetadataBlock.findByName", query = "SELECT mdb FROM MetadataBlock mdb WHERE mdb.name=:name")
})
@Entity
public class MetadataBlock implements Serializable, Comparable {
Expand All @@ -54,14 +50,7 @@ public class MetadataBlock implements Serializable, Comparable {

@Column( name = "namespaceuri", columnDefinition = "TEXT")
private String namespaceUri;

/**
* The dataset types this metadata block is associated with.
*/
// @ManyToMany(mappedBy = "metadataBlocks", cascade = {CascadeType.MERGE})
// @ManyToMany(cascade = {CascadeType.MERGE})
// private List<DatasetType> datasetTypes = new ArrayList<>();


public Long getId() {
return id;
}
Expand All @@ -76,15 +65,6 @@ public void setName(String name) {
this.name = name;
}

// public List<DatasetType> getDatasetTypes() {
// return datasetTypes;
// }
//
// public void setDatasetTypes(List<DatasetType> datasetTypes) {
// // should this be ArrayList? this.metadataBlocks = new ArrayList<>(metadataBlocks);
// this.datasetTypes = datasetTypes;
// }

public String getNamespaceUri() {
return namespaceUri;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package edu.harvard.iq.dataverse;

import edu.harvard.iq.dataverse.dataset.DatasetType;
import jakarta.ejb.Stateless;
import jakarta.inject.Named;
import jakarta.persistence.EntityManager;
Expand Down Expand Up @@ -36,14 +35,6 @@ public List<MetadataBlock> listMetadataBlocks(boolean onlyDisplayedOnCreate) {
return em.createNamedQuery("MetadataBlock.listAll", MetadataBlock.class).getResultList();
}

public List<MetadataBlock> listMetadataBlocksAssociatedWithDatasetTypes() {
return em.createNamedQuery("MetadataBlock.listAll", MetadataBlock.class).getResultList();
}

public List<MetadataBlock> listMetadataBlocksAssociatedWithDatasetType(DatasetType datasetType) {
return em.createNamedQuery("MetadataBlock.listAll", MetadataBlock.class).getResultList();
}

public MetadataBlock findById(Long id) {
return em.find(MetadataBlock.class, id);
}
Expand All @@ -59,7 +50,6 @@ public MetadataBlock findByName(String name) {
}

public List<MetadataBlock> listMetadataBlocksDisplayedOnCreate(Dataverse ownerDataverse) {
// TODO return based on dataset type
CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
CriteriaQuery<MetadataBlock> criteriaQuery = criteriaBuilder.createQuery(MetadataBlock.class);
Root<MetadataBlock> metadataBlockRoot = criteriaQuery.from(MetadataBlock.class);
Expand Down
19 changes: 5 additions & 14 deletions src/main/java/edu/harvard/iq/dataverse/api/Datasets.java
Original file line number Diff line number Diff line change
Expand Up @@ -5108,21 +5108,11 @@ public Response resetPidGenerator(@Context ContainerRequestContext crc, @PathPar
@GET
@Path("datasetTypes")
public Response getDatasetTypes() {
System.out.println("got here");
JsonArrayBuilder jab = Json.createArrayBuilder();
List<DatasetType> datasetTypes = datasetTypeSvc.listAll();
for (DatasetType datasetType : datasetTypes) {
JsonObjectBuilder job = Json.createObjectBuilder();
job.add("id", datasetType.getId());
job.add("name", datasetType.getName());
JsonArrayBuilder linkedMetadataBlocks = Json.createArrayBuilder();
for (MetadataBlock metadataBlock : datasetType.getMetadataBlocks()) {
linkedMetadataBlocks.add(metadataBlock.getName());
}
job.add("linkedMetadataBlocks", linkedMetadataBlocks);
jab.add(job);
}
return ok(jab.build());
for (DatasetType datasetType : datasetTypeSvc.listAll()) {
jab.add(datasetType.toJson());
}
return ok(jab);
}

@GET
Expand Down Expand Up @@ -5237,6 +5227,7 @@ public Response deleteDatasetType(@Context ContainerRequestContext crc, @PathPar
}
}

// TODO: PUT is better than POST, right?
@AuthRequired
@PUT
@Path("datasetTypes/{idOrName}")
Expand Down
69 changes: 0 additions & 69 deletions src/main/java/edu/harvard/iq/dataverse/api/MetadataBlocks.java
Original file line number Diff line number Diff line change
@@ -1,35 +1,12 @@
package edu.harvard.iq.dataverse.api;

import edu.harvard.iq.dataverse.DatasetFieldType;
import edu.harvard.iq.dataverse.MetadataBlock;
import static edu.harvard.iq.dataverse.api.AbstractApiBean.error;
import edu.harvard.iq.dataverse.api.auth.AuthRequired;
import edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser;
import edu.harvard.iq.dataverse.dataset.DatasetType;
import edu.harvard.iq.dataverse.engine.command.DataverseRequest;
import edu.harvard.iq.dataverse.engine.command.impl.UpdateDataverseMetadataBlocksCommand;
import edu.harvard.iq.dataverse.engine.command.impl.UpdateMetadataBlockDatasetTypeAssociations;
import jakarta.ws.rs.*;
import jakarta.ws.rs.core.Response;

import java.util.List;

import static edu.harvard.iq.dataverse.util.json.JsonPrinter.json;
import edu.harvard.iq.dataverse.util.json.JsonUtil;
import jakarta.json.Json;
import jakarta.json.JsonArray;
import jakarta.json.JsonArrayBuilder;
import jakarta.json.JsonNumber;
import jakarta.json.JsonObject;
import jakarta.json.JsonString;
import jakarta.json.JsonValue;
import jakarta.ws.rs.container.ContainerRequestContext;
import jakarta.ws.rs.core.Context;
import jakarta.ws.rs.core.MediaType;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
* Api bean for managing metadata blocks.
Expand All @@ -53,50 +30,4 @@ public Response getMetadataBlock(@PathParam("identifier") String idtf) {
MetadataBlock b = findMetadataBlock(idtf);
return (b != null) ? ok(json(b)) : notFound("Can't find metadata block '" + idtf + "'");
}

@AuthRequired
@PUT
@Path("{identifier}/datasetTypes")
public Response updateAssociationsWithDatasetTypes(@Context ContainerRequestContext crc, @PathParam("identifier") String idtf, String jsonBody) {
System.out.println("jsonbody: " + jsonBody);
// TODO move this to command?
MetadataBlock metadataBlock = findMetadataBlock(idtf);
if (metadataBlock == null) {
return notFound("Can't find metadata block '" + idtf + "'");
}
// List<DatasetType> datasetTypesExisting = metadataBlock.getDatasetTypes();
JsonArrayBuilder datasetTypesBefore = Json.createArrayBuilder();
// for (DatasetType datasetType : datasetTypesExisting) {
// datasetTypesBefore.add(datasetType.getName());
// }
List<DatasetType> datasetTypesToSave = new ArrayList<>();
if (jsonBody != null && !jsonBody.isEmpty()) {
JsonArray json = JsonUtil.getJsonArray(jsonBody);
for (JsonString jsonValue : json.getValuesAs(JsonString.class)) {
String typeName = jsonValue.getString();
System.out.println("typename: " + typeName);
DatasetType datasetType = datasetTypeSvc.getByName(typeName);
datasetTypesToSave.add(datasetType);
}
}
try {
MetadataBlock saved = execCommand(new UpdateMetadataBlockDatasetTypeAssociations(createDataverseRequest(getRequestUser(crc)), metadataBlock, datasetTypesToSave));
// Move this to command
// List<DatasetType> savedMdb = saved.getDatasetTypes();
JsonArrayBuilder datasetTypesAfter = Json.createArrayBuilder();
// for (DatasetType savedDatasetType : savedMdb) {
// System.out.println("found one: " + savedDatasetType.getName());
// datasetTypesAfter.add(savedDatasetType.getName());
// }
return ok(Json.createObjectBuilder()
.add("associatedDatasetTypes", Json.createObjectBuilder()
.add("before", datasetTypesBefore)
.add("after", datasetTypesAfter))
);

} catch (WrappedResponse ex) {
return ex.getResponse();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import edu.harvard.iq.dataverse.MetadataBlock;
import jakarta.json.Json;
import jakarta.json.JsonArrayBuilder;
import jakarta.json.JsonObjectBuilder;
import jakarta.persistence.CascadeType;
import jakarta.persistence.Column;
Expand Down Expand Up @@ -81,9 +82,14 @@ public void setMetadataBlocks(List<MetadataBlock> metadataBlocks) {
}

public JsonObjectBuilder toJson() {
JsonArrayBuilder linkedMetadataBlocks = Json.createArrayBuilder();
for (MetadataBlock metadataBlock : this.getMetadataBlocks()) {
linkedMetadataBlocks.add(metadataBlock.getName());
}
return Json.createObjectBuilder()
.add("id", getId())
.add("name", getName());
.add("name", getName())
.add("linkedMetadataBlocks", linkedMetadataBlocks);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// inspired by UpdateDataverseMetadataBlocksCommand
@RequiredPermissions({})
public class UpdateDatasetTypeLinksWithMetadataBlocks extends AbstractVoidCommand {
// rename to "to"? UpdateDatasetTypeLinksToMetadataBlocks

final DatasetType datasetType;
List<MetadataBlock> metadataBlocks;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -657,12 +657,6 @@ public static JsonObjectBuilder json(MetadataBlock metadataBlock, boolean printO
}

jsonObjectBuilder.add("fields", fieldsBuilder);
JsonArrayBuilder jab = Json.createArrayBuilder();
// for (DatasetType datasetType : metadataBlock.getDatasetTypes()) {
// System.out.println("in jsonPrinter, for block " + metadataBlock.getName() + " adding " + datasetType.getName());
// jab.add(datasetType.getName());
// }
jsonObjectBuilder.add("associatedDatasetTypes", jab);
return jsonObjectBuilder;
}

Expand Down
Loading

0 comments on commit ca62d99

Please sign in to comment.