Skip to content

Commit

Permalink
Prov: API messaging moved to bundle #4343
Browse files Browse the repository at this point in the history
  • Loading branch information
matthew-a-dunlap committed May 3, 2018
1 parent 9882820 commit 5b0f2d6
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 19 deletions.
16 changes: 15 additions & 1 deletion src/main/java/Bundle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -1773,4 +1773,18 @@ file.provAlert.unpublished.json=Your Provenance File changes will be saved to th
file.provAlert.freeform=Your Provenance Description changes will be saved to this version of the Dataset once you click on the Save Changes button.
file.provAlert.filePage.published.json=Your Provenance File changes have been saved to the Dataset.
file.provAlert.filePage.unpublished.json=Your Provenance File changes have been saved to this version of the Dataset.
file.provAlert.filePage.freeform=Your Provenance Description changes have been saved to this version of the Dataset.
file.provAlert.filePage.freeform=Your Provenance Description changes have been saved to this version of the Dataset.

api.prov.provJsonSaved=PROV-JSON provenance data saved for Data File:
api.prov.provJsonDeleted=PROV-JSON deleted for the selected Data File.

api.prov.error.provDisabled=This functionality has been administratively disabled.
api.prov.error.badDataFileId=Invalid DataFile ID.
api.prov.error.jsonUpdateNotAllowed=PROV-JSON cannot be updated for a published file that already has PROV-JSON.
api.prov.error.entityMismatch=Entity name provided does not match any entities parsed from the uploaded PROV-JSON.
api.prov.error.jsonDeleteNotAllowed=PROV-JSON cannot be deleted for a published file.
api.prov.error.jsonNoContent=No provenance json available for this file.
api.prov.error.freeformInvalidJson=A valid JSON object could not be found.
api.prov.error.freeformMissingJsonKey=The JSON object you send must have a key called 'text'.
api.prov.error.freeformNoText=No provenance free form text available for this file.
api.prov.error.noDataFileFound=Could not find a file based on ID.
37 changes: 19 additions & 18 deletions src/main/java/edu/harvard/iq/dataverse/api/Prov.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import edu.harvard.iq.dataverse.engine.command.impl.PersistProvFreeFormCommand;
import edu.harvard.iq.dataverse.engine.command.impl.PersistProvJsonCommand;
import edu.harvard.iq.dataverse.engine.command.impl.UpdateDatasetCommand;
import edu.harvard.iq.dataverse.util.BundleUtil;
import java.io.StringReader;
import java.util.HashMap;
import java.util.logging.Logger;
Expand Down Expand Up @@ -44,15 +45,15 @@ public class Prov extends AbstractApiBean {
//MAD: SHOULD NOT WORK ON PUBLISHED
public Response addProvJson(String body, @PathParam("id") String idSupplied, @QueryParam("entityName") String entityName) {
if(!systemConfig.isProvCollectionEnabled()) {
return error(FORBIDDEN, "This functionality has been administratively disabled.");
return error(FORBIDDEN, BundleUtil.getStringFromBundle("api.prov.error.provDisabled"));
}
try {
DataFile dataFile = findDataFileOrDie(idSupplied);
if(null == dataFile.getFileMetadata()) { // can happen when a datafile is not fully initialized, though unlikely in our current implementation
return error(BAD_REQUEST, "Invalid DataFile Id, file not fully initialized");
return error(BAD_REQUEST, BundleUtil.getStringFromBundle("api.prov.error.badDataFileId"));
}
if(dataFile.isReleased() && dataFile.getProvEntityName() != null){
return error(FORBIDDEN, "Provenance JSON cannot be updated for a published file that already has Provenance JSON.");
return error(FORBIDDEN, BundleUtil.getStringFromBundle("api.prov.error.jsonUpdateNotAllowed"));
}

/*Add when we actually integrate provCpl*/
Expand All @@ -62,12 +63,12 @@ public Response addProvJson(String body, @PathParam("id") String idSupplied, @Qu
HashMap<String,ProvEntityFileData> provJsonParsedEntities = provUtil.startRecurseNames(body);
if(!provJsonParsedEntities.containsKey(entityName)) {
//TODO: We should maybe go a step further and provide a way through the api to see the parsed entity names.
return error(BAD_REQUEST, "Entity name provided does not match any entities parsed from the uploaded prov json");
return error(BAD_REQUEST, BundleUtil.getStringFromBundle("api.prov.error.entityMismatch"));
}

execCommand(new PersistProvJsonCommand(createDataverseRequest(findUserOrDie()), dataFile , body, entityName, true));
JsonObjectBuilder jsonResponse = Json.createObjectBuilder();
jsonResponse.add("message", "PROV-JSON provenance data saved for Data File: " + dataFile.getDisplayName());
jsonResponse.add("message", BundleUtil.getStringFromBundle("api.prov.provJsonSaved") + " " + dataFile.getDisplayName());
return ok(jsonResponse);
} catch (WrappedResponse ex) {
return ex.getResponse();
Expand All @@ -79,15 +80,15 @@ public Response addProvJson(String body, @PathParam("id") String idSupplied, @Qu
//MAD: SHOULD NOT WORK ON PUBLISHED
public Response deleteProvJson(String body, @PathParam("id") String idSupplied) {
if(!systemConfig.isProvCollectionEnabled()) {
return error(FORBIDDEN, "This functionality has been administratively disabled.");
return error(FORBIDDEN, BundleUtil.getStringFromBundle("api.prov.error.provDisabled"));
}
try {
DataFile dataFile = findDataFileOrDie(idSupplied);
if(dataFile.isReleased()){
return error(FORBIDDEN, "Provenance JSON cannot be deleted for a published file.");
return error(FORBIDDEN, BundleUtil.getStringFromBundle("api.prov.error.jsonDeleteNotAllowed"));
}
execCommand(new DeleteProvJsonCommand(createDataverseRequest(findUserOrDie()), dataFile, true));
return ok("Provenance URL deleted");
return ok(BundleUtil.getStringFromBundle("api.prov.provJsonDeleted"));
} catch (WrappedResponse ex) {
return ex.getResponse();
}
Expand All @@ -99,27 +100,27 @@ public Response deleteProvJson(String body, @PathParam("id") String idSupplied)
@Consumes("application/json")
public Response addProvFreeForm(String body, @PathParam("id") String idSupplied) {
if(!systemConfig.isProvCollectionEnabled()) {
return error(FORBIDDEN, "This functionality has been administratively disabled.");
return error(FORBIDDEN, BundleUtil.getStringFromBundle("api.prov.error.provDisabled"));
}
StringReader rdr = new StringReader(body);
JsonObject jsonObj = null;

try {
jsonObj = Json.createReader(rdr).readObject();
} catch (JsonException ex) {
return error(BAD_REQUEST, "A valid JSON object could not be found.");
return error(BAD_REQUEST, BundleUtil.getStringFromBundle("api.prov.error.freeformInvalidJson"));
}
String provFreeForm;
try {
provFreeForm = jsonObj.getString("text");
} catch (NullPointerException ex) {
return error(BAD_REQUEST, "The JSON object you send must have a key called 'text'.");
return error(BAD_REQUEST, BundleUtil.getStringFromBundle("api.prov.error.freeformMissingJsonKey"));
}
try {
DataverseRequest dr= createDataverseRequest(findUserOrDie());
DataFile dataFile = findDataFileOrDie(idSupplied);
if (dataFile == null) {
return error(BAD_REQUEST, "Could not find datafile with id " + idSupplied);
return error(BAD_REQUEST, BundleUtil.getStringFromBundle("api.prov.error.badDataFileId"));
}
execCommand(new PersistProvFreeFormCommand(dr, dataFile, provFreeForm));
execCommand(new UpdateDatasetCommand(dataFile.getOwner(), dr));
Expand All @@ -137,12 +138,12 @@ public Response addProvFreeForm(String body, @PathParam("id") String idSupplied)
@Path("{id}/prov-freeform")
public Response getProvFreeForm(String body, @PathParam("id") String idSupplied) {
if(!systemConfig.isProvCollectionEnabled()) {
return error(FORBIDDEN, "This functionality has been administratively disabled.");
return error(FORBIDDEN, BundleUtil.getStringFromBundle("api.prov.error.provDisabled"));
}
try {
String freeFormText = execCommand(new GetProvFreeFormCommand(createDataverseRequest(findUserOrDie()), findDataFileOrDie(idSupplied)));
if(null == freeFormText) {
return error(BAD_REQUEST, "No provenance free form text available for this file.");
return error(BAD_REQUEST, BundleUtil.getStringFromBundle("api.prov.error.freeformNoText"));
}
JsonObjectBuilder jsonResponse = Json.createObjectBuilder();
jsonResponse.add("text", freeFormText);
Expand All @@ -156,12 +157,12 @@ public Response getProvFreeForm(String body, @PathParam("id") String idSupplied)
@Path("{id}/prov-json")
public Response getProvJson(String body, @PathParam("id") String idSupplied) {
if(!systemConfig.isProvCollectionEnabled()) {
return error(FORBIDDEN, "This functionality has been administratively disabled.");
return error(FORBIDDEN, BundleUtil.getStringFromBundle("api.prov.error.provDisabled"));
}
try {
JsonObject jsonText = execCommand(new GetProvJsonCommand(createDataverseRequest(findUserOrDie()), findDataFileOrDie(idSupplied)));
if(null == jsonText) {
return error(BAD_REQUEST, "No provenance json available for this file.");
return error(BAD_REQUEST, BundleUtil.getStringFromBundle("api.prov.error.jsonNoContent"));
}
JsonObjectBuilder jsonResponse = Json.createObjectBuilder();
jsonResponse.add("json", jsonText.toString());
Expand All @@ -179,11 +180,11 @@ private DataFile findDataFileOrDie(String idSupplied) throws WrappedResponse {
try {
idSuppliedAsLong = new Long(idSupplied);
} catch (NumberFormatException ex) {
throw new WrappedResponse(badRequest("Could not find a number based on " + idSupplied));
throw new WrappedResponse(badRequest(BundleUtil.getStringFromBundle("api.prov.error.badDataFileId")));
}
DataFile dataFile = fileSvc.find(idSuppliedAsLong);
if (dataFile == null) {
throw new WrappedResponse(badRequest("Could not find a file based on id " + idSupplied));
throw new WrappedResponse(badRequest(BundleUtil.getStringFromBundle("api.prov.error.noDataFileFound")));
}
return dataFile;
}
Expand Down

0 comments on commit 5b0f2d6

Please sign in to comment.