All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.dspace.app.statistics.HTMLReport Maven / Gradle / Ivy

/**
 * The contents of this file are subject to the license and copyright
 * detailed in the LICENSE and NOTICE files at the root of the source
 * tree and available online at
 *
 * http://www.dspace.org/license/
 */
package org.dspace.app.statistics;

import org.dspace.core.ConfigurationManager;

import java.text.DateFormat;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.io.*;

/**
 * This class provides HTML reports for the ReportGenerator class
 *
 * @author  Richard Jones
 */
public class HTMLReport implements Report
{
    // FIXME: all of these methods should do some content escaping before
    // outputting anything
    
    /** a list of the statistic blocks being managed by this class */
    private List blocks = new ArrayList();
    
    /** the title for the page */
    private String pageTitle = null;
    
    /** the main title for the page */
    private String mainTitle = null;
    
    /** start date for report */
    private Date start = null;
    
    /** end date for report */
    private Date end = null;

    /** the output file to which to write aggregation data */
   private String output = ConfigurationManager.getProperty("dspace.dir") +
                            File.separator + "log" + File.separator + "report";
    
    /**
     * constructor for HTML reporting
     */
    public HTMLReport()
    {
        // empty constructor
    }

    public void setOutput(String newOutput)
    {
        if (newOutput != null)
        {
            output = newOutput;
        }
    }
    
    /**
     * return a string containing the report as generated by this class
     *
     * @return      the HTML report
     */
    public String render()
    {
        StringBuffer frag = new StringBuffer();
        
        // get the page headings
        frag.append(header(pageTitle));
        frag.append(mainTitle());
        frag.append(dateRange());
        
        // output the report blocks
        // FIXME: perhaps the order of report blocks should be configurable
        Iterator statSets = blocks.iterator();
        while (statSets.hasNext())
        {
            frag.append(navigation());
            
            Statistics stats = statSets.next();
            frag.append(sectionHeader(stats.getSectionHeader()));
            frag.append(topLink());
            frag.append(blockExplanation(stats.getExplanation()));
            frag.append(floorInfo(stats.getFloor()));
            frag.append(statBlock(stats));
        }
        
        // output the footer and return
        frag.append(footer());

        // NB: HTMLReport now takes responsibility to write the output file,
        // so that the Report/ReportGenerator can have more general usage
        // finally write the string into the output file
        try
        {
        	FileOutputStream fos = new FileOutputStream(output);
            OutputStreamWriter osr = new OutputStreamWriter(fos, "UTF-8");
            PrintWriter out = new PrintWriter(osr);
            out.write(frag.toString());
            out.close();
        }
        catch (IOException e)
        {
            System.out.println("Unable to write to output file " + output);
            System.exit(0);
        }
        
        return frag.toString();
    }
    
    
    /**
     * provide a link back to the top of the page
     *
     * @return      a string containing the link text HTML formatted
     */
    public String topLink()
    {
        return "";
    }
    
    
    /**
     * build the internal navigation for the report
     *
     * @return      an HTML string providing internal page navigation
     */
    public String navigation()
    {
        StringBuffer frag = new StringBuffer();
        
        frag.append("
"); frag.append("General Overview"); frag.append(" | "); frag.append("Archive Information"); frag.append(" | "); frag.append("Items Viewed"); frag.append(" | "); frag.append("All Actions Performed"); frag.append(" | "); frag.append("User Logins"); frag.append(" | "); frag.append("Words Searched"); frag.append(" | "); frag.append("Averaging Information"); frag.append(" | "); frag.append("Log Level Information"); frag.append(" | "); frag.append("Processing Information"); frag.append("
"); return frag.toString(); } /** * add a statistics block to the report to the class register * * @param stat the statistics object to be added to the report */ public void addBlock(Statistics stat) { blocks.add(stat); return; } /** * set the starting date for the report * * @param start the start date for the report */ public void setStartDate(Date start) { this.start = (start == null ? null : new Date(start.getTime())); } /** * set the end date for the report * * @param end the end date for the report */ public void setEndDate(Date end) { this.end = (end == null ? null : new Date(end.getTime())); } /** * output the date range in the relevant format. This requires that the * date ranges have been set using setStartDate() and setEndDate() * * @return a string containing date range information */ public String dateRange() { StringBuffer frag = new StringBuffer(); DateFormat df = DateFormat.getDateInstance(); frag.append("
"); if (start != null) { frag.append(df.format(start)); } else { frag.append("from start of records "); } frag.append(" to "); if (end != null) { frag.append(df.format(end)); } else { frag.append(" end of records"); } frag.append("
\n\n"); return frag.toString(); } /** * output the title in the relevant format. This requires that the title * has been set with setMainTitle() * * @return a string containing the title of the report */ public String mainTitle() { return "\n\n"; } /** * set the main title for the report * * @param name the name of the service * @param serverName the name of the server */ public void setMainTitle(String name, String serverName) { mainTitle = "Statistics for " + name + " on " + serverName; if (pageTitle == null) { pageTitle = mainTitle; } return; } /** * output any top headers that this page needs * * @return a string containing the header for the report */ public String header() { return header(""); } /** * output any top headers that this page needs, and include a title * argument (Title support currently not implemented) * * @param title the title of the item being headered */ public String header(String title) { // FIXME: this need to be figured out to integrate nicely into the // whole JSTL thing, but for the moment it's just going to deliver // some styles StringBuffer frag = new StringBuffer(); frag.append("\n"); return frag.toString(); } /** * output the section header in HTML format * * @param title the title of the section * * @return a string containing the section title HTML formatted */ public String sectionHeader(String title) { // prepare the title to be an style link // FIXME: this should be made more generic and used in a number of locations String aName = title.toLowerCase(); Pattern space = Pattern.compile(" "); Matcher matchSpace = space.matcher(aName); aName = matchSpace.replaceAll("_"); return "\n\n"; } /** * output the report block based on the passed mapping, where the mapping * sould be "name of report element" => "value", where both sides of the * mapping should be Strings. This class also assumes that the reference * is a linkable URL to the resource * * @param content the statistic object array to be displayed * * @return a string containing the statistics block HTML formatted */ public String statBlock(Statistics content) { StringBuffer frag = new StringBuffer(); Stat[] stats = content.getStats(); // start the table frag.append("\n"); // prepare the table headers if (content.getStatName() != null || content.getResultName() != null) { frag.append("\t\n"); frag.append("\t\t\n"); frag.append("\t\t\n"); frag.append("\t\n"); } // output the statistics in the table for (int i = 0; i < stats.length; i++) { String style = null; if ((i & 1) == 1) { style = "reportOddRow"; } else { style = "reportEvenRow"; } frag.append("\t\n\t\t\n\t\t\n\t\n"); } frag.append("
\n"); if (content.getStatName() != null) { frag.append("\t\t\t" + content.getStatName() + "\n"); } else { frag.append("\t\t\t \n"); } frag.append("\t\t\n"); if (content.getResultName() != null) { frag.append("\t\t\t" + content.getResultName() + "\n"); } else { frag.append("\t\t\t \n"); } frag.append("\t\t
\n"); frag.append("\t\t\t"); if (stats[i].getReference() != null) { frag.append(""); } frag.append(this.clean(stats[i].getKey())); if (stats[i].getReference() != null) { frag.append(""); } frag.append("\n"); frag.append("\t\t\n"); frag.append("\t\t\t").append(ReportTools.numberFormat(stats[i].getValue())); if (stats[i].getUnits() != null) { frag.append(" ").append(stats[i].getUnits()); } frag.append("\n"); frag.append("\t\t
\n"); return frag.toString(); } /** * output the floor information in HTML format * * @param floor the floor number for the section being displayed * * @return a string containing floor information HTML formatted */ public String floorInfo(int floor) { if (floor > 0) { StringBuffer frag = new StringBuffer(); frag.append("
"); frag.append("(more than " + ReportTools.numberFormat(floor) + " times)"); frag.append("
\n"); return frag.toString(); } else { return ""; } } /** * output the explanation of the report block in HTML format * * @param explanation some text explaining the coming report block * * @return a string containing an explanaton HTML formatted */ public String blockExplanation(String explanation) { if (explanation != null) { StringBuffer frag = new StringBuffer(); frag.append("
"); frag.append(explanation); frag.append("
\n\n"); return frag.toString(); } else { return ""; } } /** * output the final footers for this file * * @return a string containing the report footer */ public String footer() { return ""; } /** * Clean Stirngs for display in HTML * * @param s The String to clean * @return The cleaned String */ private String clean(String s) { // Clean up the statistics keys s = s.replace("<", "<"); s = s.replaceAll(">", ">"); return s; } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy