Skip to content

Commit

Permalink
Merge pull request #61 from JingMa87/DD-422-posting-same-license-error
Browse files Browse the repository at this point in the history
DD-422 posting same license error
  • Loading branch information
PaulBoon authored Apr 28, 2021
2 parents 5cee794 + 49f5d0a commit f670d73
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 31 deletions.
9 changes: 8 additions & 1 deletion src/main/java/edu/harvard/iq/dataverse/License.java
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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
Expand Down
24 changes: 15 additions & 9 deletions src/main/java/edu/harvard/iq/dataverse/LicenseServiceBean.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<License> 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 {
Expand All @@ -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<License> 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);
Expand Down
31 changes: 10 additions & 21 deletions src/main/java/edu/harvard/iq/dataverse/api/Admin.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -161,8 +158,7 @@ public class Admin extends AbstractApiBean {
@EJB
BannerMessageServiceBean bannerMessageService;
@EJB
LicenseServiceBean licenseService;

LicenseServiceBean licenseService;

// Make the session available
@Inject
Expand Down Expand Up @@ -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());
}
}

Expand All @@ -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
Expand All @@ -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.");
}

}
17 changes: 17 additions & 0 deletions src/main/java/edu/harvard/iq/dataverse/api/ConflictException.java
Original file line number Diff line number Diff line change
@@ -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);
}

}

0 comments on commit f670d73

Please sign in to comment.