diff --git a/doc/release-notes/11918-template-apis.md b/doc/release-notes/11918-template-apis.md new file mode 100644 index 00000000000..b970fd02c70 --- /dev/null +++ b/doc/release-notes/11918-template-apis.md @@ -0,0 +1,17 @@ +## New Endpoint: GET `/dataverses/{id}/template` + +A new endpoint has been implemented to manage templates belonging to a given dataverse collection. + +### Functionality +- Returns the template of the given {id} in json format. +- You must have add dataset permission in the collection in order to use this endpoint. + +## New Endpoint: DELETE `/dataverses/{id}/template` + +A new endpoint has been implemented to manage templates belonging to a given dataverse collection. + +### Functionality +- Deletes the template of the given {id}. +- You must have Edit Dataverse permission in order to use this endpoint. + + diff --git a/doc/sphinx-guides/source/api/native-api.rst b/doc/sphinx-guides/source/api/native-api.rst index 6c1720e2b5b..8e5b5ac4e35 100644 --- a/doc/sphinx-guides/source/api/native-api.rst +++ b/doc/sphinx-guides/source/api/native-api.rst @@ -1576,6 +1576,48 @@ The fully expanded example above (without environment variables) looks like this curl -H "X-Dataverse-key:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" -X GET "https://demo.dataverse.org/api/dataverses/1/templates" +List Single Template by its Identifier +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Gets the json representation of a template by its ``id``: + +.. code-block:: bash + + export API_TOKEN=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx + export SERVER_URL=https://demo.dataverse.org + export ID=1 + + curl -H "X-Dataverse-key:$API_TOKEN" -X GET "$SERVER_URL/api/dataverses/{ID}/template" + +The fully expanded example above (without environment variables) looks like this: + +.. code-block:: bash + + curl -H "X-Dataverse-key:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" -X GET "https://demo.dataverse.org/api/dataverses/1/template" + +You must have Create Dataset permission within the given dataverse collection to invoke this api. + +Delete a Template by its Identifier +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Deletes a template by its ``id``: + +.. code-block:: bash + + export API_TOKEN=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx + export SERVER_URL=https://demo.dataverse.org + export ID=1 + + curl -H "X-Dataverse-key:$API_TOKEN" -X DELETE "$SERVER_URL/api/dataverses/{ID}/template" + +The fully expanded example above (without environment variables) looks like this: + +.. code-block:: bash + + curl -H "X-Dataverse-key:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" -X DELETE "https://demo.dataverse.org/api/dataverses/1/template" + +You must have Edit Dataverse permission within the given dataverse collection to invoke this api. + Create a Template for a Collection ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/src/main/java/edu/harvard/iq/dataverse/api/Dataverses.java b/src/main/java/edu/harvard/iq/dataverse/api/Dataverses.java index ed078d85518..1356cf55422 100644 --- a/src/main/java/edu/harvard/iq/dataverse/api/Dataverses.java +++ b/src/main/java/edu/harvard/iq/dataverse/api/Dataverses.java @@ -3,6 +3,7 @@ import com.google.common.collect.Lists; import com.google.api.client.util.ArrayMap; import edu.harvard.iq.dataverse.*; +import static edu.harvard.iq.dataverse.api.AbstractApiBean.error; import edu.harvard.iq.dataverse.api.auth.AuthRequired; import edu.harvard.iq.dataverse.api.datadeposit.SwordServiceBean; import edu.harvard.iq.dataverse.api.dto.*; @@ -23,6 +24,7 @@ import edu.harvard.iq.dataverse.dataverse.featured.DataverseFeaturedItem; import edu.harvard.iq.dataverse.dataverse.featured.DataverseFeaturedItemServiceBean; import edu.harvard.iq.dataverse.engine.command.DataverseRequest; +import edu.harvard.iq.dataverse.engine.command.exception.CommandException; import edu.harvard.iq.dataverse.engine.command.impl.*; import edu.harvard.iq.dataverse.pidproviders.PidProvider; import edu.harvard.iq.dataverse.pidproviders.PidUtil; @@ -118,6 +120,9 @@ public class Dataverses extends AbstractApiBean { @EJB PermissionServiceBean permissionService; + + @EJB + TemplateServiceBean templateService; @EJB DataverseFeaturedItemServiceBean dataverseFeaturedItemServiceBean; @@ -1989,6 +1994,21 @@ public Response getTemplates(@Context ContainerRequestContext crc, @PathParam("i return e.getResponse(); } } + + @GET + @AuthRequired + @Path("/template/{id}") + public Response getTemplate(@Context ContainerRequestContext crc, @PathParam("id") Long templateId) { + try { + Template template = templateService.find(templateId); + if (template == null){ + return error(Response.Status.NOT_FOUND, "Template with id " + templateId + " - not found."); + } + return ok(jsonTemplate(execCommand(new GetTemplateCommand(createDataverseRequest(getRequestUser(crc)), template)))); + } catch (WrappedResponse e) { + return e.getResponse(); + } + } @POST @AuthRequired @@ -2002,7 +2022,10 @@ public Response createTemplate(@Context ContainerRequestContext crc, String body } catch (JsonParseException ex) { return error(Status.BAD_REQUEST, MessageFormat.format(BundleUtil.getStringFromBundle("dataverse.createTemplate.error.jsonParseMetadataFields"), ex.getMessage())); } - return ok(jsonTemplate(execCommand(new CreateTemplateCommand(newTemplateDTO.toTemplate(), createDataverseRequest(getRequestUser(crc)), dataverse, true)))); + Template created = execCommand(new CreateTemplateCommand(newTemplateDTO.toTemplate(), createDataverseRequest(getRequestUser(crc)), dataverse, true)); + + return created("/dataverses/template/" + created.getId(), jsonTemplate(created)); + } catch (WrappedResponse e) { return e.getResponse(); } @@ -2036,6 +2059,27 @@ public Response setMetadataLanguage(@Context ContainerRequestContext crc, @PathP }, getRequestUser(crc)); } + @Path("{id}/template") + @AuthRequired + @DELETE + public Response deleteTemplate(@Context ContainerRequestContext crc, @PathParam("id") long id) { + + Template doomed = templateService.find(id); + if (doomed == null) { + return error(Response.Status.NOT_FOUND, "Template with id " + id + " - not found."); + } + + Dataverse dv = doomed.getDataverse(); + List dataverseWDefaultTemplate = templateService.findDataversesByDefaultTemplateId(doomed.getId()); + try { + execCommand(new DeleteTemplateCommand(createDataverseRequest(getRequestUser(crc)), dv, doomed, dataverseWDefaultTemplate)); + } catch (WrappedResponse wr) { + return handleWrappedResponse(wr); + } + + return ok("Template " + doomed.getName() + " deleted."); + } + @GET @AuthRequired @Path("{identifier}/assignments/history") diff --git a/src/main/java/edu/harvard/iq/dataverse/engine/command/impl/CreateTemplateCommand.java b/src/main/java/edu/harvard/iq/dataverse/engine/command/impl/CreateTemplateCommand.java index 69e19378b1a..96773f24ca9 100644 --- a/src/main/java/edu/harvard/iq/dataverse/engine/command/impl/CreateTemplateCommand.java +++ b/src/main/java/edu/harvard/iq/dataverse/engine/command/impl/CreateTemplateCommand.java @@ -49,16 +49,15 @@ public Template execute(CommandContext ctxt) throws CommandException { } Template createdTemplate = ctxt.templates().save(template); - createdTemplate.setIsDefaultForDataverse(template.isIsDefaultForDataverse()); if (initialize && createdTemplate.isIsDefaultForDataverse()) { dataverse.setDefaultTemplate(createdTemplate); ctxt.em().merge(dataverse); - } + } + //Flush so that api response can include the id ctxt.em().flush(); return createdTemplate; - } private static void updateTermsOfUseAndAccess(CommandContext ctxt, Template template) { diff --git a/src/main/java/edu/harvard/iq/dataverse/engine/command/impl/GetTemplateCommand.java b/src/main/java/edu/harvard/iq/dataverse/engine/command/impl/GetTemplateCommand.java new file mode 100644 index 00000000000..9ac4128a151 --- /dev/null +++ b/src/main/java/edu/harvard/iq/dataverse/engine/command/impl/GetTemplateCommand.java @@ -0,0 +1,50 @@ + +package edu.harvard.iq.dataverse.engine.command.impl; + +import edu.harvard.iq.dataverse.Template; +import edu.harvard.iq.dataverse.authorization.Permission; +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.Collections; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +/** + * + * @author stephenkraffmiller + */ +public class GetTemplateCommand extends AbstractCommand