From 979b7433257ea23d27c47db3ffbcc8cba4b1173f Mon Sep 17 00:00:00 2001 From: James Rutherford Date: Thu, 5 Apr 2007 11:09:16 +0000 Subject: [PATCH] (Richard Jones) - SF Patch #1659837 ItemIterator now deals with item ids also git-svn-id: http://scm.dspace.org/svn/repo/trunk@1756 9c30dcfa-912a-0410-8fc2-9e0234be79fd --- dspace/CHANGES | 5 +- .../src/org/dspace/content/ItemIterator.java | 158 +++++++++++++++++- 2 files changed, 157 insertions(+), 6 deletions(-) diff --git a/dspace/CHANGES b/dspace/CHANGES index c860429afc..8624eb0a14 100644 --- a/dspace/CHANGES +++ b/dspace/CHANGES @@ -1,5 +1,8 @@ 1.4.2 beta =========== +(Richard Jones) +- SF Patch #1659837 ItemIterator now deals with item ids also + (Andrea Bollini) - SF Patch #1528142 Malformed OAI-PMH response: illegal bytes in UTF-8 for SF Bug #1490162 @@ -646,4 +649,4 @@ Bug fix for bug causing corruption of metadata if edited post-submission - Which metadata fields are indexed by lucene is now configurable. (Dave Stuve) -- Oracle compatibility added \ No newline at end of file +- Oracle compatibility added diff --git a/dspace/src/org/dspace/content/ItemIterator.java b/dspace/src/org/dspace/content/ItemIterator.java index 6567ad5108..51af44861f 100644 --- a/dspace/src/org/dspace/content/ItemIterator.java +++ b/dspace/src/org/dspace/content/ItemIterator.java @@ -41,6 +41,9 @@ package org.dspace.content; import java.sql.SQLException; +import java.util.Iterator; +import java.util.ArrayList; + import org.dspace.core.Context; import org.dspace.storage.rdbms.TableRow; import org.dspace.storage.rdbms.TableRowIterator; @@ -50,8 +53,12 @@ import org.dspace.storage.rdbms.TableRowIterator; * items into memory one by one, since in many cases, it would not make sense to * load a set of items into memory all at once. For example, loading in an * entire community or site's worth of items wouldn't make sense. + * + * Note that this class is not a real Iterator, as it does not implement + * the Iterator interface * * @author Robert Tansley + * @author Richard Jones * @version $Revision$ */ public class ItemIterator @@ -66,29 +73,55 @@ public class ItemIterator /** The table row iterator of Item rows */ private TableRowIterator itemRows; + /** a real iterator which works over the item ids when present */ + private Iterator iditr; + /** - * Construct an item iterator. This is not a public method, since this - * iterator is only created by CM API methods. + * Construct an item iterator using a set of TableRow objects from + * the item table * * @param context * our context * @param rows * the rows that correspond to the Items to be iterated over */ - ItemIterator(Context context, TableRowIterator rows) + public ItemIterator(Context context, TableRowIterator rows) { ourContext = context; itemRows = rows; } + /** + * Construct an item iterator using an array list of item ids + * + * @param context + * our context + * @param iids + * the array list to be iterated over + */ + public ItemIterator(Context context, ArrayList iids) + { + ourContext = context; + iditr = iids.iterator(); + } + /** * Find out if there are any more items to iterate over * * @return true if there are more items + * @throws SQLException */ public boolean hasNext() throws SQLException { - return itemRows.hasNext(); + if (iditr != null) + { + return iditr.hasNext(); + } + else if (itemRows != null) + { + return itemRows.hasNext(); + } + return false; } /** @@ -96,10 +129,125 @@ public class ItemIterator * are no more items. * * @return the next item, or null + * @throws SQLException */ public Item next() throws SQLException { - if (itemRows.hasNext()) + if (iditr != null) + { + return nextByID(); + } + else if (itemRows != null) + { + return nextByRow(); + } + return null; + } + + /** + * This private method knows how to get the next result out of the + * item id iterator + * + * @return the next item instantiated from the id + * @throws SQLException + */ + private Item nextByID() + throws SQLException + { + if (iditr.hasNext()) + { + // get the id + int id = ((Integer) iditr.next()).intValue(); + + // Check cache + Item fromCache = (Item) ourContext.fromCache(Item.class, id); + + if (fromCache != null) + { + return fromCache; + } + else + { + return Item.find(ourContext, id); + } + } + else + { + return null; + } + } + + /** + * return the id of the next item. + * + * @return the next id or -1 if none + */ + public int nextID() + throws SQLException + { + if (iditr != null) + { + return nextByIDID(); + } + else if (itemRows != null) + { + return nextByRowID(); + } + return -1; + } + + /** + * Sorry about the name of this one! It returns the ID of the item + * as opposed to the item itself when we are iterating over an ArrayList + * of item ids + * + * @return the item id, or -1 if none + */ + private int nextByIDID() + { + if (iditr.hasNext()) + { + // get the id + int id = ((Integer) iditr.next()).intValue(); + + return id; + } + else + { + return -1; + } + } + + /** + * Returns the ID of the item as opposed to the item itself when we are + * iterating over the TableRow array. + * + * @return the item id, or -1 if none + */ + private int nextByRowID() + throws SQLException + { + if (itemRows.hasNext()) + { + TableRow row = itemRows.next(); + return row.getIntColumn("item_id"); + } + else + { + return -1; + } + } + + /** + * Return the next item instantiated from the supplied TableRow + * + * @return the item or null if none + * @throws SQLException + */ + private Item nextByRow() + throws SQLException + { + if (itemRows.hasNext()) { // Convert the row into an Item object TableRow row = itemRows.next();