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

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

The newest version!
/**
 * 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 java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.dspace.services.ConfigurationService;
import org.dspace.services.factory.DSpaceServicesFactory;

/**
 * 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 final 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 LocalDate start = null;

    /**
     * end date for report
     */
    private LocalDate end = null;

    /**
     * the output file to which to write aggregation data
     */
    private String output;

    /**
     * Output file path is set to {@code ${dspace.dir}/log/report}.
     */
    public HTMLReport() {
        ConfigurationService configurationService
                = DSpaceServicesFactory.getInstance().getConfigurationService();
        output = configurationService.getProperty("dspace.dir")
                + File.separator + "log" + File.separator + "report";
    }

    /**
     * Set a non-default output file path.
     *
     * @param newOutput new path to the report.
     */
    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
     */
    @Override
    public String render() {
        StringBuilder frag = new StringBuilder();

        // 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() {
        StringBuilder frag = new StringBuilder();

        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 */ @Override public void addBlock(Statistics stat) { blocks.add(stat); } /** * set the starting date for the report * * @param start the start date for the report */ @Override public void setStartDate(LocalDate start) { this.start = start; } /** * set the end date for the report * * @param end the end date for the report */ @Override public void setEndDate(LocalDate end) { this.end = end; } /** * 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 */ @Override public String dateRange() { StringBuilder frag = new StringBuilder(); 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 */ @Override 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 */ @Override public void setMainTitle(String name, String serverName) { mainTitle = "Statistics for " + name + " on " + serverName; if (pageTitle == null) { pageTitle = mainTitle; } } /** * output any top headers that this page needs * * @return a string containing the header for the report */ @Override 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 */ @Override 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 StringBuilder frag = new StringBuilder(); 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 */ @Override 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 * should be "name of report element" to "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 */ @Override public String statBlock(Statistics content) { StringBuilder frag = new StringBuilder(); 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").append(content.getStatName()).append("\n"); } else { frag.append("\t\t\t \n"); } frag.append("\t\t\n"); if (content.getResultName() != null) { frag.append("\t\t\t").append(content.getResultName()).append("\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 */ @Override public String floorInfo(int floor) { if (floor > 0) { StringBuilder frag = new StringBuilder(); frag.append("
"); frag.append("(more than ").append(ReportTools.numberFormat(floor)).append(" 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 explanation HTML formatted */ @Override public String blockExplanation(String explanation) { if (explanation != null) { StringBuilder frag = new StringBuilder(); 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 */ @Override public String footer() { return ""; } /** * Clean Strings 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