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

import java.util.Date;
import org.dspace.core.Context;

/**
 * Base class for extracting statistics from a database
 * @author Dan Leehr <dan.leehr@nescent.org>
 */
public abstract class DatabaseExtractor<T> implements ExtractorInterface<T> {
    static final String JOURNAL_ELEMENT = "publicationName";
    static final String JOURNAL_QUALIFIER = null;
    static final String JOURNAL_SCHEMA = "prism";
    static final Date DISTANT_PAST = new Date(Long.MIN_VALUE);
    static final Date DISTANT_FUTURE = new Date(Long.MAX_VALUE);
    
    Date beginDate = DISTANT_PAST;
    Date endDate = DISTANT_FUTURE;
    Boolean filterOnDates = Boolean.FALSE;
    
    private Context context;
    public DatabaseExtractor(Context context) {
        this.context = context;
    }

    public void setBeginDate(Date beginDate) {
        this.beginDate = beginDate;
        this.filterOnDates = Boolean.TRUE;
    }
    public Date getBeginDate() { return beginDate; }
    public void setEndDate(Date endDate) {
        this.endDate = endDate;
        this.filterOnDates = Boolean.TRUE;
    }
    public Date getEndDate() { return endDate; }

    protected DatabaseExtractor() {
    }

    final Context getContext() {
        return context;
    }

    final Boolean isDateWithinRange(final Date date) {
        if (
                (date != null) &&
                (date.compareTo(this.beginDate) >= 0) &&
                (date.compareTo(this.endDate) <= 0)) {
            return Boolean.TRUE;
        } else {
            return Boolean.FALSE;
        }
    }

    /**
     * Subclasses must override to do their work
     * @param journalName
     * @return
     */
    @Override
    public abstract T extract(String journalName);

}

@@ -1340,24 +1340,50 @@
 ate date) %7B%0A
+        checkDateOrder();%0A
         if (
@@ -1435,33 +1435,29 @@
       (date.
-compareTo
+after
 (this.beginD
@@ -1465,12 +1465,38 @@
 te) 
-%3E= 0
+%7C%7C date.equals(this.beginDate)
 ) &&
@@ -1522,17 +1522,14 @@
 ate.
-compareTo
+before
 (thi
@@ -1543,13 +1543,54 @@
 te) 
-%3C= 0)
+%7C%7C date.equals(this.endDate))%0A                
 ) %7B%0A
@@ -1686,24 +1686,248 @@
    %7D%0A    %7D%0A%0A
+    final void checkDateOrder() %7B%0A        if(filterOnDates) %7B%0A            if(beginDate.after(endDate)) %7B%0A                throw new IllegalStateException(%22beginDate must not be after endDate%22);%0A            %7D%0A        %7D%0A    %7D%0A%0A
     /**%0A    

Use Date.before and .after instead of compareTo