Skip to content

Commit

Permalink
DD-387 Add license entity and api (IQSS#57)
Browse files Browse the repository at this point in the history
* First db table and api.

* Final changes for prototype.

* Add integration tests.

* Fix indentation.

* Add prototype of newest changes.

* Add URI and URL objects, and new endpoints.

* Add Apache icons.

* Change tokens to licenses.

* Change URIException to IllegalStateException.
  • Loading branch information
JingMa87 authored Mar 31, 2021
1 parent 99a92cb commit 4b6c367
Show file tree
Hide file tree
Showing 12 changed files with 597 additions and 5 deletions.
7 changes: 7 additions & 0 deletions scripts/api/data/license.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "Apache License 1.0",
"shortDescription": "This is the original Apache License which applies only to very old versions of Apache packages (such as version 1.2 of the Web server).",
"uri": "https://www.apache.org/licenses/LICENSE-1.0",
"iconUrl": "https://itgala.xyz/wp-content/uploads/2017/10/Apache-HTTP-Server.png",
"active": false
}
8 changes: 8 additions & 0 deletions scripts/api/data/licenseError.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"id": 6,
"name": "Apache License 1.0",
"shortDescription": "This is the original Apache License which applies only to very old versions of Apache packages (such as version 1.2 of the Web server).",
"uri": "https://www.apache.org/licenses/LICENSE-1.0",
"iconUrl": "https://itgala.xyz/wp-content/uploads/2017/10/Apache-HTTP-Server.png",
"active": false
}
7 changes: 7 additions & 0 deletions scripts/api/data/licenseUpdate.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "Apache License 2.0",
"shortDescription": "The 2.0 version of the Apache License, approved by the ASF in 2004.",
"uri": "https://www.apache.org/licenses/LICENSE-2.0",
"iconUrl": "https://itgala.xyz/wp-content/uploads/2017/10/Apache-HTTP-Server.png",
"active": true
}
154 changes: 154 additions & 0 deletions src/main/java/edu/harvard/iq/dataverse/License.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
package edu.harvard.iq.dataverse;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.Objects;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;

/**
* @author Jing Ma
*/
@NamedQueries({
@NamedQuery( name="License.findAll",
query="SELECT l FROM License l"),
@NamedQuery( name="License.findById",
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.deleteById",
query="DELETE FROM License l WHERE l.id=:id"),
@NamedQuery( name="License.deleteByName",
query="DELETE FROM License l WHERE l.name=:name")
})
@Entity
@Table(uniqueConstraints = {
@UniqueConstraint(columnNames = "name"),
@UniqueConstraint(columnNames = "uri")}
)
public class License {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Column(columnDefinition="TEXT", nullable = false)
private String name;

@Column(columnDefinition="TEXT")
private String shortDescription;

@Column(columnDefinition="TEXT", nullable = false)
private String uri;

@Column(columnDefinition="TEXT")
private String iconUrl;

@Column(nullable = false)
private boolean active;

public License() {
}

public License(String name, String shortDescription, URI uri, URI iconUrl, boolean active) {
this.name = name;
this.shortDescription = shortDescription;
this.uri = uri.toASCIIString();
this.iconUrl = iconUrl.toASCIIString();
this.active = active;
}

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getShortDescription() {
return shortDescription;
}

public void setShortDescription(String shortDescription) {
this.shortDescription = shortDescription;
}

public URI getUri() {
try {
return new URI(uri);
} catch (URISyntaxException e) {
throw new IllegalStateException("Incorrect URI in JSON");
}
}

public void setUri(URI uri) {
this.uri = uri.toASCIIString();
}

public URI getIconUrl() {
try {
return new URI(iconUrl);
} catch (URISyntaxException e) {
throw new IllegalStateException("Incorrect URI in JSON");
}
}

public void setIconUrl(URI iconUrl) {
this.iconUrl = iconUrl.toASCIIString();
}

public boolean isActive() {
return active;
}

public void setActive(boolean active) {
this.active = active;
}

@Override
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 &&
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
public int hashCode() {
return Objects.hash(id, name, shortDescription, uri, iconUrl, active);
}

@Override
public String toString() {
return "License{" +
"id=" + id +
", name='" + name + '\'' +
", shortDescription='" + shortDescription + '\'' +
", uri=" + uri +
", iconUrl=" + iconUrl +
", active=" + active +
'}';
}

}
119 changes: 119 additions & 0 deletions src/main/java/edu/harvard/iq/dataverse/LicenseServiceBean.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package edu.harvard.iq.dataverse;

import edu.harvard.iq.dataverse.actionlogging.ActionLogRecord;
import edu.harvard.iq.dataverse.actionlogging.ActionLogServiceBean;
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;
import javax.inject.Named;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceException;

/**
* @author Jing Ma
*/
@Stateless
@Named
public class LicenseServiceBean {

@PersistenceContext
EntityManager em;

@EJB
ActionLogServiceBean actionLogSvc;

public List<License> listAll() {
return em.createNamedQuery("License.findAll", License.class).getResultList();
}

public License getById(long id) throws FetchException {
List<License> licenses = em.createNamedQuery("License.findById", License.class)
.setParameter("id", id )
.getResultList();
if (licenses.isEmpty()) {
throw new FetchException("License with that ID doesn't exist.");
}
return licenses.get(0);
}

public License getByName(String name) throws FetchException {
List<License> licenses = em.createNamedQuery("License.findByName", License.class)
.setParameter("name", name )
.getResultList();
if (licenses.isEmpty()) {
throw new FetchException("License with that name doesn't exist.");
}
return licenses.get(0);
}

public License save(License license) throws PersistenceException, RequestBodyException {
if (license.getId() == null) {
em.persist(license);
return license;
} else {
throw new RequestBodyException("There shouldn't be an ID in the request body");
}
}

public void setById(long id, String name, String shortDescription, URI uri, URI iconUrl, boolean active) throws UpdateException {
List<License> licenses = em.createNamedQuery("License.findById", License.class)
.setParameter("id", id )
.getResultList();

if(licenses.size() > 0) {
License license = licenses.get(0);
license.setName(name);
license.setShortDescription(shortDescription);
license.setUri(uri);
license.setIconUrl(iconUrl);
license.setActive(active);
em.merge(license);
actionLogSvc.log( new ActionLogRecord(ActionLogRecord.ActionType.Admin, "set")
.setInfo(name + ": " + shortDescription + ": " + uri + ": " + iconUrl + ": " + active));
} else {
throw new UpdateException("There is no existing License with that ID. To add a license use POST.");
}
}

public void setByName(String name, String shortDescription, URI uri, URI iconUrl, boolean active) throws UpdateException {
List<License> licenses = em.createNamedQuery("License.findByName", License.class)
.setParameter("name", name )
.getResultList();

if(licenses.size() > 0) {
License license = licenses.get(0);
license.setShortDescription(shortDescription);
license.setUri(uri);
license.setIconUrl(iconUrl);
license.setActive(active);
em.merge(license);
actionLogSvc.log( new ActionLogRecord(ActionLogRecord.ActionType.Admin, "set")
.setInfo(name + ": " + shortDescription + ": " + uri + ": " + iconUrl + ": " + active));
} else {
throw new UpdateException("There is no existing License with that name. To add a license use POST.");
}
}

public int deleteById(long id) throws PersistenceException {
actionLogSvc.log( new ActionLogRecord(ActionLogRecord.ActionType.Admin, "delete")
.setInfo(Long.toString(id)));
return em.createNamedQuery("License.deleteById")
.setParameter("id", id)
.executeUpdate();
}

public int deleteByName(String name) throws PersistenceException {
actionLogSvc.log( new ActionLogRecord(ActionLogRecord.ActionType.Admin, "delete")
.setInfo(name));
return em.createNamedQuery("License.deleteByName")
.setParameter("name", name)
.executeUpdate();
}

}
Loading

0 comments on commit 4b6c367

Please sign in to comment.