Add REST support for limit / offset to page through comm.collections and collection.items, and item list

This commit is contained in:
Peter Dietz
2013-10-18 13:04:51 -04:00
parent 5eee997e7a
commit 98ee886687
3 changed files with 24 additions and 10 deletions

View File

@@ -63,7 +63,7 @@ public class CollectionsResource {
@GET
@Path("/")
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public org.dspace.rest.common.Collection[] list(@QueryParam("expand") String expand) {
public org.dspace.rest.common.Collection[] list(@QueryParam("expand") String expand, @QueryParam("limit") @DefaultValue("100") Integer limit, @QueryParam("offset") @DefaultValue("0") Integer offset) {
try {
if(context == null || !context.isValid() ) {
context = new org.dspace.core.Context();
@@ -71,11 +71,19 @@ public class CollectionsResource {
context.getDBConnection().setAutoCommit(true);
}
org.dspace.content.Collection[] collections = org.dspace.content.Collection.findAll(context);
org.dspace.content.Collection[] collections;
//Only support paging if limit/offset are 0 or positive values.
if(limit != null && limit >= 0 && offset != null && offset != 0) {
collections = org.dspace.content.Collection.findAll(context, limit, offset);
} else {
collections = org.dspace.content.Collection.findAll(context);
}
ArrayList<org.dspace.rest.common.Collection> collectionArrayList = new ArrayList<org.dspace.rest.common.Collection>();
for(org.dspace.content.Collection collection : collections) {
if(AuthorizeManager.authorizeActionBoolean(context, collection, org.dspace.core.Constants.READ)) {
org.dspace.rest.common.Collection restCollection = new org.dspace.rest.common.Collection(collection, expand, context);
org.dspace.rest.common.Collection restCollection = new org.dspace.rest.common.Collection(collection, expand, context, limit, offset);
collectionArrayList.add(restCollection);
} // Not showing restricted-access collections
}
@@ -91,7 +99,7 @@ public class CollectionsResource {
@GET
@Path("/{collection_id}")
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public org.dspace.rest.common.Collection getCollection(@PathParam("collection_id") Integer collection_id, @QueryParam("expand") String expand) {
public org.dspace.rest.common.Collection getCollection(@PathParam("collection_id") Integer collection_id, @QueryParam("expand") String expand, @QueryParam("limit") @DefaultValue("100") Integer limit, @QueryParam("offset") @DefaultValue("0") Integer offset) {
try {
if(context == null || !context.isValid() ) {
context = new Context();
@@ -101,7 +109,7 @@ public class CollectionsResource {
org.dspace.content.Collection collection = org.dspace.content.Collection.find(context, collection_id);
if(AuthorizeManager.authorizeActionBoolean(context, collection, org.dspace.core.Constants.READ)) {
return new org.dspace.rest.common.Collection(collection, expand, context);
return new org.dspace.rest.common.Collection(collection, expand, context, limit, offset);
} else {
throw new WebApplicationException(Response.Status.UNAUTHORIZED);
}

View File

@@ -54,7 +54,7 @@ public class HandleResource {
case Constants.COMMUNITY:
return new Community((org.dspace.content.Community) dso, expand, context);
case Constants.COLLECTION:
return new Collection((org.dspace.content.Collection) dso, expand, context);
return new Collection((org.dspace.content.Collection) dso, expand, context, null, null);
case Constants.ITEM:
return new Item((org.dspace.content.Item) dso, expand, context);
default:

View File

@@ -64,12 +64,12 @@ public class Collection extends DSpaceObject {
public Collection(){}
public Collection(org.dspace.content.Collection collection, String expand, Context context) throws SQLException, WebApplicationException{
public Collection(org.dspace.content.Collection collection, String expand, Context context, Integer limit, Integer offset) throws SQLException, WebApplicationException{
super(collection);
setup(collection, expand, context);
setup(collection, expand, context, limit, offset);
}
private void setup(org.dspace.content.Collection collection, String expand, Context context) throws SQLException{
private void setup(org.dspace.content.Collection collection, String expand, Context context, Integer limit, Integer offset) throws SQLException{
List<String> expandFields = new ArrayList<String>();
if(expand != null) {
expandFields = Arrays.asList(expand.split(","));
@@ -93,7 +93,13 @@ public class Collection extends DSpaceObject {
//TODO: Item paging. limit, offset/page
if(expandFields.contains("items") || expandFields.contains("all")) {
ItemIterator childItems = collection.getItems();
ItemIterator childItems;
if(limit != null && limit >= 0 && offset != null && offset >= 0) {
childItems = collection.getItems(limit, offset);
} else {
childItems = collection.getItems();
}
items = new ArrayList<DSpaceObject>();
while(childItems.hasNext()) {
org.dspace.content.Item item = childItems.next();