Create a base DSO class that others extend, to DRY up some code...

This commit is contained in:
Peter Dietz
2013-10-07 15:36:57 -04:00
parent 0822c85863
commit cb4268bb45
11 changed files with 148 additions and 407 deletions

View File

@@ -61,6 +61,11 @@
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>org.atteo</groupId>
<artifactId>evo-inflector</artifactId>
<version>1.0.1</version>
</dependency>
</dependencies>
<build>
<plugins>

View File

@@ -1,6 +1,7 @@
package org.dspace.rest;
import org.apache.log4j.Logger;
import org.dspace.authorize.AuthorizeManager;
import org.dspace.core.Context;
@@ -20,6 +21,8 @@ http://localhost:8080/<webapp>/collections
*/
@Path("/collections")
public class CollectionsResource {
private static Logger log = Logger.getLogger(CollectionsResource.class);
@javax.ws.rs.core.Context ServletContext servletContext;
private static org.dspace.core.Context context;
@@ -43,6 +46,7 @@ public class CollectionsResource {
}
} catch (SQLException e) {
log.error(e.getMessage());
return "ERROR: " + e.getMessage();
}
@@ -70,6 +74,7 @@ public class CollectionsResource {
return collectionArrayList.toArray(new org.dspace.rest.common.Collection[0]);
} catch (SQLException e) {
e.getMessage();
throw new WebApplicationException(Response.Status.INTERNAL_SERVER_ERROR);
}
}
@@ -90,6 +95,7 @@ public class CollectionsResource {
throw new WebApplicationException(Response.Status.UNAUTHORIZED);
}
} catch (SQLException e) {
log.error(e.getMessage());
throw new WebApplicationException(Response.Status.INTERNAL_SERVER_ERROR);
}
}

View File

@@ -1,5 +1,6 @@
package org.dspace.rest;
import org.apache.log4j.Logger;
import org.dspace.authorize.AuthorizeManager;
import org.dspace.core.Context;
@@ -18,6 +19,8 @@ http://localhost:8080/<webapp>/communities
*/
@Path("/communities")
public class CommunitiesResource {
private static Logger log = Logger.getLogger(CommunitiesResource.class);
private static Context context;
/*
@@ -38,6 +41,7 @@ public class CommunitiesResource {
}
} catch (SQLException e) {
log.error(e.getMessage());
return "ERROR: " + e.getMessage();
}
@@ -68,6 +72,7 @@ public class CommunitiesResource {
return communityArrayList.toArray(new org.dspace.rest.common.Community[0]);
} catch (SQLException e) {
log.error(e.getMessage());
throw new WebApplicationException(Response.Status.INTERNAL_SERVER_ERROR);
}
}
@@ -88,6 +93,7 @@ public class CommunitiesResource {
throw new WebApplicationException(Response.Status.UNAUTHORIZED);
}
} catch (SQLException e) {
log.error(e.getMessage());
throw new WebApplicationException(Response.Status.INTERNAL_SERVER_ERROR);
}
}

View File

@@ -0,0 +1,11 @@
package org.dspace.rest;
/**
* Created with IntelliJ IDEA.
* User: peterdietz
* Date: 10/7/13
* Time: 1:54 PM
* To change this template use File | Settings | File Templates.
*/
public class HandleResource {
}

View File

@@ -21,29 +21,20 @@ import java.util.List;
* To change this template use File | Settings | File Templates.
*/
@XmlRootElement(name = "collection")
public class Collection {
public class Collection extends DSpaceObject {
Logger log = Logger.getLogger(Collection.class);
//Internal value
private Integer collectionID;
//Relationships to other objects
private Integer logoID;
@XmlElement(name = "type", required = true)
final String type = "collection";
//Exandable relationships
private Integer parentCommunityID;
private List<Integer> parentCommunityIDList = new ArrayList<Integer>();
private List<Integer> itemIDList = new ArrayList<Integer>();
@XmlElement(name = "items")
private List<LiteItem> items = new ArrayList<LiteItem>();
private List<DSpaceObject> items = new ArrayList<DSpaceObject>();
//Internal metadata
private String name;
private String handle;
private String license;
//unused-metadata
@@ -53,25 +44,6 @@ public class Collection {
//String copyright_text;
//String side_bar_text;
@XmlElement(name = "link", required = true)
private String link;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getHandle() {
return handle;
}
public void setHandle(String handle) {
this.handle = handle;
}
public String getLicense() {
return license;
}
@@ -80,16 +52,13 @@ public class Collection {
this.license = license;
}
private List<String> expand = new ArrayList<String>();
//Calculated
private Integer numberItems;
public Collection(){}
public Collection(org.dspace.content.Collection collection, String expand, Context context) throws SQLException, WebApplicationException{
super(collection);
setup(collection, expand, context);
}
@@ -99,10 +68,6 @@ public class Collection {
expandFields = Arrays.asList(expand.split(","));
}
setCollectionID(collection.getID());
setName(collection.getName());
setHandle(collection.getHandle());
if(expandFields.contains("parentCommunityIDList") || expandFields.contains("all")) {
org.dspace.content.Community[] parentCommunities = collection.getCommunities();
for(org.dspace.content.Community parentCommunity : parentCommunities) {
@@ -122,11 +87,11 @@ public class Collection {
//TODO: Item paging. limit, offset/page
if(expandFields.contains("items") || expandFields.contains("all")) {
ItemIterator childItems = collection.getItems();
items = new ArrayList<LiteItem>();
items = new ArrayList<DSpaceObject>();
while(childItems.hasNext()) {
org.dspace.content.Item item = childItems.next();
if(AuthorizeManager.authorizeActionBoolean(context, item, org.dspace.core.Constants.READ)) {
items.add(new LiteItem(item));
items.add(new DSpaceObject(item));
}
}
} else {
@@ -148,15 +113,6 @@ public class Collection {
}
this.setNumberItems(collection.countItems());
//collection.getMetadata()
}
public Integer getCollectionID() {
return collectionID;
}
public void setCollectionID(Integer id) {
this.collectionID = id;
}
public Integer getLogoID() {
@@ -195,7 +151,6 @@ public class Collection {
this.parentCommunityIDList.add(communityParentID);
}
public List<Integer> getItemIDList() {
return itemIDList;
}
@@ -207,22 +162,4 @@ public class Collection {
public void addItemIDToList(Integer itemID) {
this.itemIDList.add(itemID);
}
public List<String> getExpand() {
return expand;
}
public void setExpand(List<String> expand) {
this.expand = expand;
}
public void addExpand(String expandableAttribute) {
this.expand.add(expandableAttribute);
}
public String getLink() {
return link;
}
}

View File

@@ -20,41 +20,26 @@ import java.util.List;
* To change this template use File | Settings | File Templates.
*/
@XmlRootElement(name = "community")
public class Community {
public class Community extends DSpaceObject{
private static Logger log = Logger.getLogger(Community.class);
//Internal value
private Integer communityID;
@XmlElement(name = "type", required = true)
final String type = "community";
//Exandable relationships
@XmlElement(name = "parentCommunity")
private LiteCommunity parentCommunity;
private List<String> expand = new ArrayList<String>();
//Metadata
private String name;
private String handle;
private DSpaceObject parentCommunity;
private String copyrightText, introductoryText, shortDescription, sidebarText;
private Integer countItems;
@XmlElement(name = "link", required = true)
private String link;
@XmlElement(name = "subcommunities", required = true)
private List<LiteCommunity> subCommunities = new ArrayList<LiteCommunity>();
private List<DSpaceObject> subCommunities = new ArrayList<DSpaceObject>();
@XmlElement(name = "collections")
private List<LiteCollection> collections = new ArrayList<LiteCollection>();
private List<DSpaceObject> collections = new ArrayList<DSpaceObject>();
public Community(){}
public Community(org.dspace.content.Community community, String expand, Context context) throws SQLException, WebApplicationException{
super(community);
setup(community, expand, context);
}
@@ -64,20 +49,15 @@ public class Community {
expandFields = Arrays.asList(expand.split(","));
}
this.setCommunityID(community.getID());
this.setName(community.getName());
this.setHandle(community.getHandle());
this.setCopyrightText(community.getMetadata(org.dspace.content.Community.COPYRIGHT_TEXT));
this.setIntroductoryText(community.getMetadata(org.dspace.content.Community.INTRODUCTORY_TEXT));
this.setSidebarText(community.getMetadata(org.dspace.content.Community.SIDEBAR_TEXT));
this.setCountItems(community.countItems());
this.link = "/communities/" + this.communityID;
if(expandFields.contains("parentCommunityID") || expandFields.contains("all")) {
org.dspace.content.Community parentCommunity = community.getParentCommunity();
if(parentCommunity != null) {
setParentCommunity(new LiteCommunity(parentCommunity));
setParentCommunity(new DSpaceObject(parentCommunity));
}
} else {
this.addExpand("parentCommunityID");
@@ -85,10 +65,10 @@ public class Community {
if(expandFields.contains("subCollections") || expandFields.contains("all")) {
org.dspace.content.Collection[] collectionArray = community.getCollections();
collections = new ArrayList<LiteCollection>();
collections = new ArrayList<DSpaceObject>();
for(org.dspace.content.Collection collection : collectionArray) {
if(AuthorizeManager.authorizeActionBoolean(context, collection, org.dspace.core.Constants.READ)) {
collections.add(new LiteCollection(collection));
collections.add(new DSpaceObject(collection));
} else {
log.info("Omitted restricted collection: " + collection.getID() + " _ " + collection.getName());
}
@@ -99,10 +79,10 @@ public class Community {
if(expandFields.contains("subCommunities") || expandFields.contains("all")) {
org.dspace.content.Community[] communityArray = community.getSubcommunities();
subCommunities = new ArrayList<LiteCommunity>();
subCommunities = new ArrayList<DSpaceObject>();
for(org.dspace.content.Community subCommunity : communityArray) {
if(AuthorizeManager.authorizeActionBoolean(context, subCommunity, org.dspace.core.Constants.READ)) {
subCommunities.add(new LiteCommunity(subCommunity));
subCommunities.add(new DSpaceObject(subCommunity));
} else {
log.info("Omitted restricted subCommunity: " + subCommunity.getID() + " _ " + subCommunity.getName());
}
@@ -116,47 +96,11 @@ public class Community {
}
}
public List<String> getExpand() {
return expand;
}
public void setExpand(List<String> expand) {
this.expand = expand;
}
public void addExpand(String expandableAttribute) {
this.expand.add(expandableAttribute);
}
public Integer getCommunityID() {
return communityID;
}
public void setCommunityID(Integer communityID) {
this.communityID = communityID;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getHandle() {
return handle;
}
public void setHandle(String handle) {
this.handle = handle;
}
public List<LiteCollection> getCollections() {
public List<DSpaceObject> getCollections() {
return collections;
}
public void setCollections(List<LiteCollection> collections) {
public void setCollections(List<DSpaceObject> collections) {
this.collections = collections;
}
@@ -200,19 +144,11 @@ public class Community {
this.copyrightText = copyrightText;
}
public String getType() {
return type;
}
public LiteCommunity getParentCommunity() {
public DSpaceObject getParentCommunity() {
return parentCommunity;
}
public void setParentCommunity(LiteCommunity parentCommunity) {
public void setParentCommunity(DSpaceObject parentCommunity) {
this.parentCommunity = parentCommunity;
}
public String getLink() {
return link;
}
}

View File

@@ -0,0 +1,98 @@
package org.dspace.rest.common;
import org.atteo.evo.inflector.English;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import java.util.ArrayList;
import java.util.List;
/**
* Created with IntelliJ IDEA.
* User: peterdietz
* Date: 10/7/13
* Time: 12:11 PM
* To change this template use File | Settings | File Templates.
*/
@XmlRootElement(name = "DSpaceObject")
public class DSpaceObject {
@XmlElement(name = "ID", required = true)
public Integer ID;
@XmlElement(name = "name", required = true)
public String name;
@XmlElement(name = "handle")
public String handle;
@XmlElement(name = "type", required = true)
public String type;
@XmlElement(name = "link", required = true)
public String link;
@XmlElement(name = "expand")
public List<String> expand = new ArrayList<String>();
public DSpaceObject() {
}
public DSpaceObject(org.dspace.content.DSpaceObject dso) {
setID(dso.getID());
setName(dso.getName());
setHandle(dso.getHandle());
setType(dso.getTypeText().toLowerCase());
}
public Integer getID() {
return ID;
}
public void setID(Integer ID) {
this.ID = ID;
}
public String getName(){
return this.name;
}
public void setName(String name) {
this.name = name;
}
public String getHandle() {
return handle;
}
public void setHandle(String handle) {
this.handle = handle;
}
public String getLink() {
//TODO, I'm not 100% sure this pluralizer will work...
//How to get actual contextPath of /rest/
return "/rest/" + English.plural(getType()) + "/" + getID();
}
public String getType() {
return this.type;
}
public void setType(String type) {
this.type = type;
}
public List<String> getExpand() {
return expand;
}
public void setExpand(List<String> expand) {
this.expand = expand;
}
public void addExpand(String expandableAttribute) {
this.expand.add(expandableAttribute);
}
}

View File

@@ -23,18 +23,11 @@ import java.util.List;
* To change this template use File | Settings | File Templates.
*/
@XmlRootElement(name = "item")
public class Item {
public class Item extends DSpaceObject {
Logger log = Logger.getLogger(Item.class);
Integer itemID;
String handle;
String name;
String isArchived;
String isWithdrawn;
String lastModified;
@@ -50,14 +43,12 @@ public class Item {
List<Collection> parentCollections;
@XmlElement(name = "link", required = true)
private String link;
//Bitstreams
public Item(){}
public Item(org.dspace.content.Item item, String expand, Context context) throws SQLException, WebApplicationException{
super(item);
setup(item, expand, context);
}
@@ -67,8 +58,6 @@ public class Item {
expandFields = Arrays.asList(expand.split(","));
}
this.setItemID(item.getID());
//Add Item metadata, omit restricted metadata fields (i.e. provenance).
metadata = new Metadata();
@@ -79,14 +68,11 @@ public class Item {
}
}
this.setHandle(item.getHandle());
this.setName(item.getName());
this.setArchived(Boolean.toString(item.isArchived()));
this.setWithdrawn(Boolean.toString(item.isWithdrawn()));
this.setLastModified(item.getLastModified().toString());
//TODO make optional, and set to object
this.setOwningCollectionID(item.getOwningCollection().getID());
this.setOwningCollectionName(item.getOwningCollection().getName());
@@ -105,31 +91,6 @@ public class Item {
}
}
public Integer getItemID() {
return itemID;
}
public void setItemID(Integer itemID) {
this.itemID = itemID;
}
public String getHandle() {
return handle;
}
public void setHandle(String handle) {
this.handle = handle;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getArchived() {
return isArchived;
}
@@ -169,9 +130,4 @@ public class Item {
public void setOwningCollectionName(String owningCollectionName) {
this.owningCollectionName = owningCollectionName;
}
public String getLink() {
return link;
}
}

View File

@@ -1,70 +0,0 @@
package org.dspace.rest.common;
import javax.xml.bind.annotation.XmlElement;
/**
* Created with IntelliJ IDEA.
* User: peterdietz
* Date: 9/29/13
* Time: 11:28 AM
* To change this template use File | Settings | File Templates.
*/
public class LiteCollection {
//Internal value
private Integer collectionID;
@XmlElement(name = "type", required = true)
final String type = "collection";
@XmlElement(name = "link", required = true)
private String link;
//Internal metadata
private String name;
private String handle;
public LiteCollection() {
}
public LiteCollection(org.dspace.content.Collection collection){
this.collectionID = collection.getID();
this.name = collection.getName();
this.handle = collection.getHandle();
link = "/collections/" + this.collectionID;
}
public Integer getCollectionID() {
return collectionID;
}
public void setCollectionID(Integer collectionID) {
this.collectionID = collectionID;
}
public String getType() {
return type;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getHandle() {
return handle;
}
public void setHandle(String handle) {
this.handle = handle;
}
public String getLink() {
return link;
}
}

View File

@@ -1,74 +0,0 @@
package org.dspace.rest.common;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
/**
* Created with IntelliJ IDEA.
* User: peterdietz
* Date: 9/29/13
* Time: 11:27 AM
* To change this template use File | Settings | File Templates.
*/
@XmlRootElement(name = "community")
public class LiteCommunity {
@XmlElement(name = "type", required = true)
final String type = "community";
@XmlElement(name = "communityID", required = true)
Integer communityID;
@XmlElement(name = "handle", required = true)
String handle;
@XmlElement(name = "name", required = true)
String name;
@XmlElement(name = "link", required = true)
private String link;
public LiteCommunity() {
}
public LiteCommunity(org.dspace.content.Community community) {
this.communityID = community.getID();
this.handle = community.getHandle();
this.name = community.getName();
this.link = "/communities/" + this.communityID;
}
String getType() {
return type;
}
String getHandle() {
return handle;
}
void setHandle(String handle) {
this.handle = handle;
}
Integer getCommunityID() {
return communityID;
}
void setCommunityID(Integer communityID) {
this.communityID = communityID;
}
String getName() {
return name;
}
void setName(String name) {
this.name = name;
}
public String getLink() {
return link;
}
}

View File

@@ -1,70 +0,0 @@
package org.dspace.rest.common;
import javax.xml.bind.annotation.XmlElement;
/**
* Created with IntelliJ IDEA.
* User: peterdietz
* Date: 9/29/13
* Time: 11:28 AM
* To change this template use File | Settings | File Templates.
*/
public class LiteItem {
//Internal value
private Integer itemID;
@XmlElement(name = "type", required = true)
final String type = "item";
@XmlElement(name = "link", required = true)
private String link;
//Internal metadata
private String name;
private String handle;
public LiteItem() {
}
public LiteItem(org.dspace.content.Item item) {
this.itemID = item.getID();
this.name = item.getName();
this.handle = item.getHandle();
link = "/items/" + this.itemID;
}
public Integer getItemID() {
return itemID;
}
public void setItemID(Integer itemID) {
this.itemID = itemID;
}
public String getType() {
return type;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getHandle() {
return handle;
}
public void setHandle(String handle) {
this.handle = handle;
}
public String getLink() {
return link;
}
}