Skip to content

Instantly share code, notes, and snippets.

@raprasad
Created January 9, 2017 16:24
Show Gist options
  • Select an option

  • Save raprasad/c962ce00217a169238f5461138326135 to your computer and use it in GitHub Desktop.

Select an option

Save raprasad/c962ce00217a169238f5461138326135 to your computer and use it in GitHub Desktop.
Endpoint to update a single file's metadata (non-working start point)
@PUT
@Path("{id}/metadata")
public Response updateFileMetadata(
@PathParam("id") Long fileToUpdateId,
String jsonData
){
// -------------------------------------
// (1) Get the user from the API key
// -------------------------------------
User authUser;
try {
authUser = this.findUserOrDie();
} catch (AbstractApiBean.WrappedResponse ex) {
return error(Response.Status.FORBIDDEN,
ResourceBundle.getBundle("Bundle").getString("file.addreplace.error.auth")
);
}
// -------------------------------------
// (2) Check/Parse the JSON
// -------------------------------------
if (jsonData == null){
logger.log(Level.SEVERE, "jsonData is null");
return error( Response.Status.BAD_REQUEST, "No JSON data");
}
JsonObject jsonObj = new Gson().fromJson(jsonData, JsonObject.class);
// (2d) Load up optional params via JSON
// - Will skip extra attributes which includes fileToReplaceId and forceReplace
//---------------------------------------
OptionalFileParams optionalFileParams = null;
try {
optionalFileParams = new OptionalFileParams(jsonData);
} catch (DataFileTagException ex) {
return error( Response.Status.BAD_REQUEST, ex.getMessage());
}
DataverseRequest dvRequest = createDataverseRequest(authUser);
UpdateFileMetadataHelper updateFileMetadataHelper = new UpdateFileMetadataHelper(dvRequest,
this.datasetService,
this.fileService,
this.permissionSvc,
this.commandEngine,
this.systemConfig);
updateFileMetadataHelper.updateFileMetadata(fileToUpdateId, optionalFileParams);
if (updateFileMetadataHelper.hasError()){
msg("yes, has error");
return error(updateFileMetadataHelper.getHttpErrorCode(), updateFileMetadataHelper.getErrorMessagesAsString("\n"));
}else{
msg("no error");
String successMsg = ResourceBundle.getBundle("Bundle").getString("file.addreplace.success.replace");
msgt("as String: " + updateFileMetadataHelper.getSuccessResult());
return ok(successMsg,
updateFileMetadataHelper.getSuccessResultAsJsonObjectBuilder());
}
} // end: updateFileMetadata
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment