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

import java.util.Date;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.PosixParser;
import org.datadryad.journalstatistics.statistics.DefaultStatisticsPackage;
import org.dspace.content.DCDate;
import org.dspace.core.Context;

/**
 *
 * @author Dan Leehr <dan.leehr@nescent.org>
 */
public class RunStatistics {

    public static void main(String args[]) throws Exception {
        CommandLineParser parser = new PosixParser();
        Options options = new Options();
        options.addOption("j", true, "Journal Name (required)");
        options.addOption("f", "from", true, "Begin Date");
        options.addOption("t", "to", true, "End Date");
        options.addOption("h", "help", false, "help");
        CommandLine line = parser.parse(options, args);

        Date beginDate = null;
        Date endDate = null;
        if(line.hasOption("f")) {
            beginDate = parseDate(line.getOptionValue("f"));
        }
        if(line.hasOption("t")) {
            endDate = parseDate(line.getOptionValue("t"));
        }

        if(line.hasOption("j")) {
            String journalName = line.getOptionValue("j");
            Context c = new Context();
            c.turnOffAuthorisationSystem();
            DefaultStatisticsPackage statisticsPackage = new DefaultStatisticsPackage(c);
            if(beginDate != null) {
                statisticsPackage.setBeginDate(beginDate);
            }
            if(endDate != null) {
                statisticsPackage.setEndDate(endDate);
            }
            statisticsPackage.run(journalName);
        } else {
            printHelp(options, 0);
        }
    }

    private static Date parseDate(String dateString) {
        return new DCDate(dateString).toDate();
    }

    private static void printHelp(Options options, int exitCode) {
        HelpFormatter myhelp = new HelpFormatter();
        String footer = "\nDCDate is used for date parsing, accepts '2014', '2014-01-01', or full ISO8601 formatting";
        myhelp.printHelp("dspace journal-stats\n", null, options, footer);
        System.exit(exitCode);
    }

}

@@ -66,24 +66,65 @@
 .util.Date;%0A
+import java.util.IllegalFormatException;%0A
 import org.a
@@ -1099,24 +1099,46 @@
 ion(%22f%22)) %7B%0A
+            try %7B%0A    
             
@@ -1182,24 +1182,207 @@
 alue(%22f%22));%0A
+            %7D catch (IllegalArgumentException ex) %7B%0A                System.err.println(%22Error parsing date: %22 + ex.getMessage());%0A                printHelp(options, 1);%0A            %7D%0A
         %7D%0A  
@@ -1409,24 +1409,46 @@
 ion(%22t%22)) %7B%0A
+            try %7B%0A    
             
@@ -1490,24 +1490,207 @@
 alue(%22t%22));%0A
+            %7D catch (IllegalArgumentException ex) %7B%0A                System.err.println(%22Error parsing date: %22 + ex.getMessage());%0A                printHelp(options, 1);%0A            %7D%0A
         %7D%0A%0A 
@@ -2336,17 +2336,240 @@
 String) 
-%7B
+throws IllegalArgumentException %7B%0A        final Date returnDate = new DCDate(dateString).toDate();%0A        if(returnDate == null) %7B%0A            throw new IllegalArgumentException(%22Unparseable date: %22 + dateString);%0A        %7D
 %0A       

Check date parsing in RunStatistics CLI