/*
 */
package org.datadryad.journalstatistics.extractor;

import java.io.IOException;
import java.sql.SQLException;
import org.apache.log4j.Logger;
import org.dspace.authorize.AuthorizeException;
import org.dspace.content.Collection;
import org.dspace.content.Item;
import org.dspace.content.ItemIterator;
import org.dspace.core.Context;

/**
 * Counts items in a DSpace Collection, such as Dryad Data Packages or files
 * @author Dan Leehr <dan.leehr@nescent.org>
 */
public abstract class DataItemCount extends DatabaseExtractor<Integer> {

    private static Logger log = Logger.getLogger(DataItemCount.class);

    public DataItemCount(Context context) {
        super(context);
    }

    protected abstract Collection getCollection() throws SQLException;

    @Override
    public Integer extract(final String journalName) {
        Context context = this.getContext();
        Collection collection;
        Integer count = 0;
        try {
            /*
             * Initial implementation is to fetch items by metadata field ID
             * then filter on collection. This may be too slow since it will
             * result in a lot of queries and could be done with a single join
             *
             * Note: findByMetadataField only returns items in archive
             */
            ItemIterator itemsByJournal = Item.findByMetadataField(this.getContext(), JOURNAL_SCHEMA, JOURNAL_ELEMENT, JOURNAL_QUALIFIER, journalName);
            collection = getCollection();
            while (itemsByJournal.hasNext()) {
                Item item = itemsByJournal.next();
                if (item.isOwningCollection(collection)) {
                    count++;
                }
            }
        } catch (AuthorizeException ex) {
            log.error("AuthorizeException getting item count per journal", ex);
        } catch (IOException ex) {
            log.error("IOException getting item count per journal", ex);
        } catch (SQLException ex) {
            log.error("SQLException getting item count per journal", ex);
        }
        return count;
    }

}

@@ -102,32 +102,55 @@
 l.SQLException;%0A
+import java.util.Date;%0A
 import org.apach
@@ -165,16 +165,54 @@
 Logger;%0A
+import org.datadryad.api.DryadObject;%0A
 import o
@@ -815,16 +815,79 @@
 ception;
+%0A    protected abstract DryadObject makeDryadObject(Item item);
 %0A%0A    @O
@@ -1791,16 +1791,643 @@
     
-count++;
+/*%0A                     * Since comparing dates requires reading metadata from DB,%0A                     * only check if filterOnDates is true%0A                     */%0A                    if(this.filterOnDates) %7B%0A                        // get the date, compare to ranges%0A                        DryadObject dryadObject = makeDryadObject(item);%0A                        Date dateAccessioned = dryadObject.getDateAccessioned();%0A                        if(isDateWithinRange(dateAccessioned)) %7B%0A                            count++;%0A                        %7D%0A                    %7D else %7B%0A                        count++;%0A                    %7D
 %0A   

Implement date range checking on DataItemCount