mirror of
https://github.com/DSpace/DSpace.git
synced 2025-10-18 15:33:09 +00:00
Add REST support for limit / offset to page through comm.collections and collection.items, and item list
This commit is contained in:
@@ -63,7 +63,7 @@ public class CollectionsResource {
|
|||||||
@GET
|
@GET
|
||||||
@Path("/")
|
@Path("/")
|
||||||
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
|
@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 {
|
try {
|
||||||
if(context == null || !context.isValid() ) {
|
if(context == null || !context.isValid() ) {
|
||||||
context = new org.dspace.core.Context();
|
context = new org.dspace.core.Context();
|
||||||
@@ -71,11 +71,19 @@ public class CollectionsResource {
|
|||||||
context.getDBConnection().setAutoCommit(true);
|
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>();
|
ArrayList<org.dspace.rest.common.Collection> collectionArrayList = new ArrayList<org.dspace.rest.common.Collection>();
|
||||||
for(org.dspace.content.Collection collection : collections) {
|
for(org.dspace.content.Collection collection : collections) {
|
||||||
if(AuthorizeManager.authorizeActionBoolean(context, collection, org.dspace.core.Constants.READ)) {
|
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);
|
collectionArrayList.add(restCollection);
|
||||||
} // Not showing restricted-access collections
|
} // Not showing restricted-access collections
|
||||||
}
|
}
|
||||||
@@ -91,7 +99,7 @@ public class CollectionsResource {
|
|||||||
@GET
|
@GET
|
||||||
@Path("/{collection_id}")
|
@Path("/{collection_id}")
|
||||||
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
|
@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 {
|
try {
|
||||||
if(context == null || !context.isValid() ) {
|
if(context == null || !context.isValid() ) {
|
||||||
context = new Context();
|
context = new Context();
|
||||||
@@ -101,7 +109,7 @@ public class CollectionsResource {
|
|||||||
|
|
||||||
org.dspace.content.Collection collection = org.dspace.content.Collection.find(context, collection_id);
|
org.dspace.content.Collection collection = org.dspace.content.Collection.find(context, collection_id);
|
||||||
if(AuthorizeManager.authorizeActionBoolean(context, collection, org.dspace.core.Constants.READ)) {
|
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 {
|
} else {
|
||||||
throw new WebApplicationException(Response.Status.UNAUTHORIZED);
|
throw new WebApplicationException(Response.Status.UNAUTHORIZED);
|
||||||
}
|
}
|
||||||
|
@@ -54,7 +54,7 @@ public class HandleResource {
|
|||||||
case Constants.COMMUNITY:
|
case Constants.COMMUNITY:
|
||||||
return new Community((org.dspace.content.Community) dso, expand, context);
|
return new Community((org.dspace.content.Community) dso, expand, context);
|
||||||
case Constants.COLLECTION:
|
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:
|
case Constants.ITEM:
|
||||||
return new Item((org.dspace.content.Item) dso, expand, context);
|
return new Item((org.dspace.content.Item) dso, expand, context);
|
||||||
default:
|
default:
|
||||||
|
@@ -64,12 +64,12 @@ public class Collection extends DSpaceObject {
|
|||||||
|
|
||||||
public Collection(){}
|
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);
|
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>();
|
List<String> expandFields = new ArrayList<String>();
|
||||||
if(expand != null) {
|
if(expand != null) {
|
||||||
expandFields = Arrays.asList(expand.split(","));
|
expandFields = Arrays.asList(expand.split(","));
|
||||||
@@ -93,7 +93,13 @@ public class Collection extends DSpaceObject {
|
|||||||
|
|
||||||
//TODO: Item paging. limit, offset/page
|
//TODO: Item paging. limit, offset/page
|
||||||
if(expandFields.contains("items") || expandFields.contains("all")) {
|
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>();
|
items = new ArrayList<DSpaceObject>();
|
||||||
while(childItems.hasNext()) {
|
while(childItems.hasNext()) {
|
||||||
org.dspace.content.Item item = childItems.next();
|
org.dspace.content.Item item = childItems.next();
|
||||||
|
Reference in New Issue
Block a user