Contab sample - Generate HTML report

This examples generates an HTML report from the OpenKM activity log:

package com.openkm.plugin.cron;

import com.openkm.db.service.LegacySrv;
import com.openkm.util.MailUtils;
import net.xeoh.plugins.base.annotations.PluginImplementation;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.File;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

@PluginImplementation
public class SampleHtmlReport extends BaseCronPlugin implements CronAdapter {
	private static final Logger log = LoggerFactory.getLogger(SampleHtmlReport.class);

	@Autowired
	private LegacySrv legacySrv;

	@Override
	public String getName() {
		return "Sample HTML report";
	}

	@Override
	public String getCronExpression() {
		return "0 0 0 * * *";
	}

	@Override
	public void execute() {
		try {
			Calendar to = Calendar.getInstance();
			Calendar from = (Calendar) to.clone();
			from.add(Calendar.DAY_OF_YEAR, -7);
			DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
			String query = String.format("select ACT_DATE, ACT_USER, ACT_ACTION, ACT_ITEM, ACT_PATH from OKM_ACTIVITY "
					+ "where ACT_DATE between '%s' and '%s'", df.format(from.getTime()), df.format(to.getTime()));

			StringBuilder result = new StringBuilder();
			result.append("<h1>Sample report</h1>");
			result.append("</br></br>");
			result.append("<table boder=\"0\" cellpadding=\"2\" cellspacing=\"0\" width=\"100%\">");
			result.append("<tr>");
			result.append("<th bgcolor=\"silver\"><b>Date</b></td>");
			result.append("<th bgcolor=\"silver\"><b>User</b></td>");
			result.append("<th bgcolor=\"silver\"><b>Action</b></td>");
			result.append("<th bgcolor=\"silver\"><b>Item</b></td>");
			result.append("<th bgcolor=\"silver\"><b>Path</b></td>");
			result.append("</tr>");

			for (List<String> row : legacySrv.executeSQL(query)) {
				result.append("<tr>");
				result.append("<td>").append(row.get(0)).append("</td>");
				result.append("<td>").append(row.get(1)).append("</td>");
				result.append("<td>").append(row.get(2)).append("</td>");
				result.append("<td>").append(row.get(3)).append("</td>");
				result.append("<td>").append(row.get(4)).append("</td>");
				result.append("</tr>");
			}

			result.append("</table>");

			DateFormat dfHour = new SimpleDateFormat("yyyy-MM-dd_HHmmss");
			File output = new File("report-" + dfHour.format(to.getTime()) + ".html");
			FileUtils.writeStringToFile(output, result.toString(), StandardCharsets.UTF_8);
			log.info("Report written to {}", output.getAbsolutePath());
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		}
	}
} 

Register a new plugin

  • To install new plugin create a jar file and copy into your $TOMCAT/plugins folder. More information at Creating a JAR file with Eclipse.
  • Go to Administration > Utilities > Plugins and click at top right the Reload plugins button.
  • In plugins table will be shown the new plugin. 

 

Table of contents [ Hide Show ]