Skip to content

Commit f670d73

Browse files
authored
Merge pull request #61 from JingMa87/DD-422-posting-same-license-error
DD-422 posting same license error
2 parents 5cee794 + 49f5d0a commit f670d73

4 files changed

Lines changed: 50 additions & 31 deletions

File tree

src/main/java/edu/harvard/iq/dataverse/License.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
query = "SELECT l FROM License l WHERE l.id=:id"),
2424
@NamedQuery( name="License.findByName",
2525
query = "SELECT l FROM License l WHERE l.name=:name"),
26+
@NamedQuery( name="License.findByNameOrUri",
27+
query = "SELECT l FROM License l WHERE l.name=:name OR l.uri=:uri"),
2628
@NamedQuery( name="License.deleteById",
2729
query="DELETE FROM License l WHERE l.id=:id"),
2830
@NamedQuery( name="License.deleteByName",
@@ -126,7 +128,12 @@ public boolean equals(Object o) {
126128
if (this == o) return true;
127129
if (o == null || getClass() != o.getClass()) return false;
128130
License license = (License) o;
129-
return active == license.active && id.equals(license.id) && name.equals(license.name) && shortDescription.equals(license.shortDescription) && uri.equals(license.uri) && iconUrl.equals(license.iconUrl);
131+
return active == license.active &&
132+
Objects.equals(id, license.id) &&
133+
Objects.equals(name, license.name) &&
134+
Objects.equals(shortDescription, license.shortDescription) &&
135+
Objects.equals(uri, license.uri) &&
136+
Objects.equals(iconUrl, license.iconUrl);
130137
}
131138

132139
@Override

src/main/java/edu/harvard/iq/dataverse/LicenseServiceBean.java

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
import edu.harvard.iq.dataverse.actionlogging.ActionLogRecord;
44
import edu.harvard.iq.dataverse.actionlogging.ActionLogServiceBean;
5+
import edu.harvard.iq.dataverse.api.ConflictException;
56
import edu.harvard.iq.dataverse.api.FetchException;
67
import edu.harvard.iq.dataverse.api.RequestBodyException;
78
import edu.harvard.iq.dataverse.api.UpdateException;
89
import java.net.URI;
9-
import java.net.URL;
1010
import java.util.List;
1111
import javax.ejb.EJB;
1212
import javax.ejb.Stateless;
@@ -52,14 +52,19 @@ public License getByName(String name) throws FetchException {
5252
return licenses.get(0);
5353
}
5454

55-
public void save(License license) throws PersistenceException, RequestBodyException {
56-
if (license.getId() == null) {
57-
em.persist(license);
58-
em.flush();
59-
return license;
60-
} else {
55+
public License save(License license) throws RequestBodyException, ConflictException {
56+
if (license.getId() != null) {
6157
throw new RequestBodyException("There shouldn't be an ID in the request body");
6258
}
59+
List<License> licenses = em.createNamedQuery("License.findByNameOrUri", License.class)
60+
.setParameter("name", license.getName() )
61+
.setParameter("uri", license.getUri().toASCIIString() )
62+
.getResultList();
63+
if (!licenses.isEmpty()) {
64+
throw new ConflictException("A license with the same URI or name is already present.");
65+
}
66+
em.persist(license);
67+
return license;
6368
}
6469

6570
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
8287
}
8388
}
8489

85-
public void setByName(String name, String shortDescription, URI uri, URI iconUrl, boolean active) throws UpdateException {
90+
public void setByName(String nameArg, String name, String shortDescription, URI uri, URI iconUrl, boolean active) throws UpdateException {
8691
List<License> licenses = em.createNamedQuery("License.findByName", License.class)
87-
.setParameter("name", name )
92+
.setParameter("name", nameArg )
8893
.getResultList();
8994

9095
if(licenses.size() > 0) {
9196
License license = licenses.get(0);
97+
license.setName(name);
9298
license.setShortDescription(shortDescription);
9399
license.setUri(uri);
94100
license.setIconUrl(iconUrl);

src/main/java/edu/harvard/iq/dataverse/api/Admin.java

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
import edu.harvard.iq.dataverse.engine.command.impl.PublishDataverseCommand;
4747
import edu.harvard.iq.dataverse.settings.Setting;
4848
import edu.harvard.iq.dataverse.util.json.JsonPrinter;
49-
import java.net.URI;
5049
import javax.json.Json;
5150
import javax.json.JsonArrayBuilder;
5251
import javax.json.JsonObjectBuilder;
@@ -76,7 +75,6 @@
7675
import javax.ws.rs.core.Response.Status;
7776

7877
import org.apache.commons.io.IOUtils;
79-
import org.apache.commons.lang3.StringUtils;
8078

8179
import java.util.List;
8280
import edu.harvard.iq.dataverse.authorization.AuthTestDataServiceBean;
@@ -109,7 +107,6 @@
109107
import java.util.ArrayList;
110108
import java.util.Arrays;
111109
import java.util.Date;
112-
import java.util.function.Consumer;
113110
import javax.inject.Inject;
114111
import javax.json.JsonArray;
115112
import javax.persistence.Query;
@@ -161,8 +158,7 @@ public class Admin extends AbstractApiBean {
161158
@EJB
162159
BannerMessageServiceBean bannerMessageService;
163160
@EJB
164-
LicenseServiceBean licenseService;
165-
161+
LicenseServiceBean licenseService;
166162

167163
// Make the session available
168164
@Inject
@@ -2013,21 +2009,14 @@ public Response getLicenseByName(@PathParam("name") String name) {
20132009

20142010
@POST
20152011
@Path("/licenses")
2016-
public Response addLicense(JsonObject jsonObject) {
2012+
public Response addLicense(License license) {
20172013
try {
2018-
License license = new License();
2019-
license.setName(jsonObject.getString("name"));
2020-
license.setShortDescription(jsonObject.getString("shortDescription"));
2021-
license.setUri(new URI(jsonObject.getString("uri")));
2022-
license.setIconUrl(new URI(jsonObject.getString("iconUrl")));
2023-
license.setActive(jsonObject.getBoolean("active"));
20242014
licenseService.save(license);
2025-
String location = "/api/admin/licenses/name/" + UrlEscapers.urlFragmentEscaper().escape(license.getName());
2026-
return created(location, Json.createObjectBuilder().add("message", "License created"));
2015+
return created("/api/admin/licenses", Json.createObjectBuilder().add("message", "License created"));
20272016
} catch (RequestBodyException e) {
2028-
return error(Response.Status.BAD_REQUEST, e.getMessage());
2029-
} catch (Exception e) {
2030-
return error(Response.Status.BAD_REQUEST, "Something went wrong.");
2017+
return error(Response.Status.BAD_REQUEST, e.getMessage());
2018+
} catch(ConflictException e) {
2019+
return error(Response.Status.CONFLICT, e.getMessage());
20312020
}
20322021
}
20332022

@@ -2044,13 +2033,13 @@ public Response putLicenseById(@PathParam("id") long id, License license) {
20442033

20452034
@PUT
20462035
@Path("/licenses/name/{name}")
2047-
public Response putLicenseByName(@PathParam("name") String name, License license) {
2036+
public Response putLicenseByName(@PathParam("name") String nameArg, License license) {
20482037
try {
2049-
licenseService.setByName(license.getName(), license.getShortDescription(), license.getUri(), license.getIconUrl(), license.isActive());
2038+
licenseService.setByName(nameArg, license.getName(), license.getShortDescription(), license.getUri(), license.getIconUrl(), license.isActive());
20502039
} catch (UpdateException e) {
20512040
return error(Response.Status.BAD_REQUEST, e.getMessage());
20522041
}
2053-
return ok("License with name " + name + " was replaced.");
2042+
return ok("License with name " + nameArg + " was replaced.");
20542043
}
20552044

20562045
@DELETE
@@ -2072,5 +2061,5 @@ public Response deleteLicenseByName(@PathParam("name") String name) {
20722061
}
20732062
return error(Response.Status.NOT_FOUND, "A license with name " + name + " doesn't exist.");
20742063
}
2075-
2064+
20762065
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package edu.harvard.iq.dataverse.api;
2+
3+
/**
4+
*
5+
* @author Jing Ma
6+
*/
7+
public class ConflictException extends Exception {
8+
9+
public ConflictException(String message) {
10+
super(message);
11+
}
12+
13+
public ConflictException(String message, Throwable cause) {
14+
super(message, cause);
15+
}
16+
17+
}

0 commit comments

Comments
 (0)