001/*
002 * #%L
003 * Netarchivesuite - harvester
004 * %%
005 * Copyright (C) 2005 - 2014 The Royal Danish Library, the Danish State and University Library,
006 *             the National Library of France and the Austrian National Library.
007 * %%
008 * This program is free software: you can redistribute it and/or modify
009 * it under the terms of the GNU Lesser General Public License as
010 * published by the Free Software Foundation, either version 2.1 of the
011 * License, or (at your option) any later version.
012 * 
013 * This program is distributed in the hope that it will be useful,
014 * but WITHOUT ANY WARRANTY; without even the implied warranty of
015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
016 * GNU General Lesser Public License for more details.
017 * 
018 * You should have received a copy of the GNU General Lesser Public
019 * License along with this program.  If not, see
020 * <http://www.gnu.org/licenses/lgpl-2.1.html>.
021 * #L%
022 */
023package dk.netarkivet.harvester.harvesting.frontier;
024
025import java.io.PrintWriter;
026import java.lang.reflect.Method;
027
028import org.slf4j.Logger;
029import org.slf4j.LoggerFactory;
030
031/**
032 * Utility class implementing the export of a frontier report object to a CSV file.
033 */
034public class FrontierReportCsvExport {
035
036    /** The logger for this class. */
037         private static final Logger LOG = LoggerFactory.getLogger(FrontierReportCsvExport.class);
038
039    private static enum FIELD {
040        domainName, currentSize, totalEnqueues, sessionBalance, lastCost, averageCost, 
041        lastDequeueTime, wakeTime, 
042        totalSpend, totalBudget, errorCount, lastPeekUri, lastQueuedUri;
043
044        private String getterName() {
045            return "get" + String.valueOf(name().charAt(0)).toUpperCase() + name().substring(1);
046        }
047
048        static String genLine(FrontierReportLine l, String separator) {
049            String line = "";
050            FIELD[] allFields = values();
051            for (int i = 0; i < allFields.length; i++) {
052                FIELD field = allFields[i];
053                try {
054                    String getterName = field.getterName();
055                    Method getter = l.getClass().getMethod(getterName);
056                    Object value = getter.invoke(l);
057
058                    String valueStr = "?";
059                    if (value instanceof String) {
060                        valueStr = (String) value;
061                    } else if (value instanceof Double) {
062                        valueStr = getDisplayValue(((Double) value).doubleValue());
063                    } else if (value instanceof Long) {
064                        valueStr = getDisplayValue(((Long) value).longValue());
065                    }
066
067                    line += valueStr + (i < allFields.length - 1 ? separator : "");
068                } catch (Exception e) {
069                    LOG.error("Failed to invoke getter FrontierReportLine#" + field.getterName(), e);
070                }
071            }
072            return line;
073        }
074
075        static String genHeaderLine(String separator) {
076            String header = "";
077            FIELD[] allFields = values();
078            for (int i = 0; i < allFields.length; i++) {
079                header += allFields[i].name() + (i < allFields.length - 1 ? separator : "");
080            }
081            return header;
082        }
083    }
084
085    
086
087    /**
088     * Outputs the report as CSV, using the given writer and the given field separator. Note that writer is not closed
089     * by this method.
090     * @param report A given InMemoryFrontierReport 
091     * @param pw the writer to output to
092     * @param separator the field separator.
093     */
094    public static void outputAsCsv(InMemoryFrontierReport report, PrintWriter pw, String separator) {
095
096        pw.println(FIELD.genHeaderLine(separator));
097        for (FrontierReportLine l : report.getLines()) {
098            pw.println(FIELD.genLine(l, separator));
099        }
100
101    }
102
103    private static String getDisplayValue(long val) {
104        return Long.MIN_VALUE == val ? FrontierReportLine.EMPTY_VALUE_TOKEN : "" + val;
105    }
106
107    private static String getDisplayValue(double val) {
108        if (Double.MIN_VALUE == val) {
109            return FrontierReportLine.EMPTY_VALUE_TOKEN;
110        }
111        return (Math.rint(val) == val ? Integer.toString((int) val) : "" + Double.toString(val));
112    }
113
114}