diff --git a/src/main/java/edu/harvard/iq/dataverse/License.java b/src/main/java/edu/harvard/iq/dataverse/License.java index 2b78ab46278..60f2805e072 100644 --- a/src/main/java/edu/harvard/iq/dataverse/License.java +++ b/src/main/java/edu/harvard/iq/dataverse/License.java @@ -23,6 +23,8 @@ query = "SELECT l FROM License l WHERE l.id=:id"), @NamedQuery( name="License.findByName", query = "SELECT l FROM License l WHERE l.name=:name"), + @NamedQuery( name="License.findByNameOrUri", + query = "SELECT l FROM License l WHERE l.name=:name OR l.uri=:uri"), @NamedQuery( name="License.deleteById", query="DELETE FROM License l WHERE l.id=:id"), @NamedQuery( name="License.deleteByName", @@ -126,7 +128,12 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; License license = (License) o; - return active == license.active && id.equals(license.id) && name.equals(license.name) && shortDescription.equals(license.shortDescription) && uri.equals(license.uri) && iconUrl.equals(license.iconUrl); + return active == license.active && + Objects.equals(id, license.id) && + Objects.equals(name, license.name) && + Objects.equals(shortDescription, license.shortDescription) && + Objects.equals(uri, license.uri) && + Objects.equals(iconUrl, license.iconUrl); } @Override diff --git a/src/main/java/edu/harvard/iq/dataverse/LicenseServiceBean.java b/src/main/java/edu/harvard/iq/dataverse/LicenseServiceBean.java index 4508db83111..7e760bb9bf7 100644 --- a/src/main/java/edu/harvard/iq/dataverse/LicenseServiceBean.java +++ b/src/main/java/edu/harvard/iq/dataverse/LicenseServiceBean.java @@ -2,11 +2,11 @@ import edu.harvard.iq.dataverse.actionlogging.ActionLogRecord; import edu.harvard.iq.dataverse.actionlogging.ActionLogServiceBean; +import edu.harvard.iq.dataverse.api.ConflictException; import edu.harvard.iq.dataverse.api.FetchException; import edu.harvard.iq.dataverse.api.RequestBodyException; import edu.harvard.iq.dataverse.api.UpdateException; import java.net.URI; -import java.net.URL; import java.util.List; import javax.ejb.EJB; import javax.ejb.Stateless; @@ -52,14 +52,19 @@ public License getByName(String name) throws FetchException { return licenses.get(0); } - public void save(License license) throws PersistenceException, RequestBodyException { - if (license.getId() == null) { - em.persist(license); - em.flush(); - return license; - } else { + public License save(License license) throws RequestBodyException, ConflictException { + if (license.getId() != null) { throw new RequestBodyException("There shouldn't be an ID in the request body"); } + List licenses = em.createNamedQuery("License.findByNameOrUri", License.class) + .setParameter("name", license.getName() ) + .setParameter("uri", license.getUri().toASCIIString() ) + .getResultList(); + if (!licenses.isEmpty()) { + throw new ConflictException("A license with the same URI or name is already present."); + } + em.persist(license); + return license; } public void setById(long id, String name, String shortDescription, URI uri, URI iconUrl, boolean active) throws UpdateException { @@ -82,13 +87,14 @@ public void setById(long id, String name, String shortDescription, URI uri, URI } } - public void setByName(String name, String shortDescription, URI uri, URI iconUrl, boolean active) throws UpdateException { + public void setByName(String nameArg, String name, String shortDescription, URI uri, URI iconUrl, boolean active) throws UpdateException { List licenses = em.createNamedQuery("License.findByName", License.class) - .setParameter("name", name ) + .setParameter("name", nameArg ) .getResultList(); if(licenses.size() > 0) { License license = licenses.get(0); + license.setName(name); license.setShortDescription(shortDescription); license.setUri(uri); license.setIconUrl(iconUrl); diff --git a/src/main/java/edu/harvard/iq/dataverse/api/Admin.java b/src/main/java/edu/harvard/iq/dataverse/api/Admin.java index 2ecf9210acb..140d764d196 100644 --- a/src/main/java/edu/harvard/iq/dataverse/api/Admin.java +++ b/src/main/java/edu/harvard/iq/dataverse/api/Admin.java @@ -46,7 +46,6 @@ import edu.harvard.iq.dataverse.engine.command.impl.PublishDataverseCommand; import edu.harvard.iq.dataverse.settings.Setting; import edu.harvard.iq.dataverse.util.json.JsonPrinter; -import java.net.URI; import javax.json.Json; import javax.json.JsonArrayBuilder; import javax.json.JsonObjectBuilder; @@ -76,7 +75,6 @@ import javax.ws.rs.core.Response.Status; import org.apache.commons.io.IOUtils; -import org.apache.commons.lang3.StringUtils; import java.util.List; import edu.harvard.iq.dataverse.authorization.AuthTestDataServiceBean; @@ -109,7 +107,6 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Date; -import java.util.function.Consumer; import javax.inject.Inject; import javax.json.JsonArray; import javax.persistence.Query; @@ -161,8 +158,7 @@ public class Admin extends AbstractApiBean { @EJB BannerMessageServiceBean bannerMessageService; @EJB - LicenseServiceBean licenseService; - + LicenseServiceBean licenseService; // Make the session available @Inject @@ -2013,21 +2009,14 @@ public Response getLicenseByName(@PathParam("name") String name) { @POST @Path("/licenses") - public Response addLicense(JsonObject jsonObject) { + public Response addLicense(License license) { try { - License license = new License(); - license.setName(jsonObject.getString("name")); - license.setShortDescription(jsonObject.getString("shortDescription")); - license.setUri(new URI(jsonObject.getString("uri"))); - license.setIconUrl(new URI(jsonObject.getString("iconUrl"))); - license.setActive(jsonObject.getBoolean("active")); licenseService.save(license); - String location = "/api/admin/licenses/name/" + UrlEscapers.urlFragmentEscaper().escape(license.getName()); - return created(location, Json.createObjectBuilder().add("message", "License created")); + return created("/api/admin/licenses", Json.createObjectBuilder().add("message", "License created")); } catch (RequestBodyException e) { - return error(Response.Status.BAD_REQUEST, e.getMessage()); - } catch (Exception e) { - return error(Response.Status.BAD_REQUEST, "Something went wrong."); + return error(Response.Status.BAD_REQUEST, e.getMessage()); + } catch(ConflictException e) { + return error(Response.Status.CONFLICT, e.getMessage()); } } @@ -2044,13 +2033,13 @@ public Response putLicenseById(@PathParam("id") long id, License license) { @PUT @Path("/licenses/name/{name}") - public Response putLicenseByName(@PathParam("name") String name, License license) { + public Response putLicenseByName(@PathParam("name") String nameArg, License license) { try { - licenseService.setByName(license.getName(), license.getShortDescription(), license.getUri(), license.getIconUrl(), license.isActive()); + licenseService.setByName(nameArg, license.getName(), license.getShortDescription(), license.getUri(), license.getIconUrl(), license.isActive()); } catch (UpdateException e) { return error(Response.Status.BAD_REQUEST, e.getMessage()); } - return ok("License with name " + name + " was replaced."); + return ok("License with name " + nameArg + " was replaced."); } @DELETE @@ -2072,5 +2061,5 @@ public Response deleteLicenseByName(@PathParam("name") String name) { } return error(Response.Status.NOT_FOUND, "A license with name " + name + " doesn't exist."); } - + } diff --git a/src/main/java/edu/harvard/iq/dataverse/api/ConflictException.java b/src/main/java/edu/harvard/iq/dataverse/api/ConflictException.java new file mode 100644 index 00000000000..60e60ed41a4 --- /dev/null +++ b/src/main/java/edu/harvard/iq/dataverse/api/ConflictException.java @@ -0,0 +1,17 @@ +package edu.harvard.iq.dataverse.api; + +/** + * + * @author Jing Ma + */ +public class ConflictException extends Exception { + + public ConflictException(String message) { + super(message); + } + + public ConflictException(String message, Throwable cause) { + super(message, cause); + } + +}