%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page trimDirectiveWhitespaces="true" %>
<%@ page import="org.crosswire.data.DataObject" %>
<%@ page import="java.util.Date" %>
<%@ page import="java.util.Map" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="org.crosswire.community.projects.ntmss.data.Document" %>
<%@ page import="org.crosswire.community.projects.ntmss.data.ProjectManagement" %>
<%@ page import="org.crosswire.community.projects.ntmss.data.ProjectManagement.Project" %>
<%@ page import="org.crosswire.community.projects.ntmss.data.Transcription" %>
<%@ page import="org.crosswire.webtools.annotation.*" %>
<%@ page import="org.crosswire.webtools.*" %>
<%!
@Description(value = "Retrieve page statistics", name = "statistics/page")
public static class MyParameters extends Parameters {
protected ProjectManagement.Project project = null;
protected int minDocID = -1;
protected int maxDocID = -1;
@Description(value = "Get statistics for the primary document group for a Project ID", example = "1")
public Integer projectID;
@Description(value = "Get statistics for the primary document group for a Project Name", example = "ECM Matthew")
public String projectName;
@Description(value = "Get statistics for a Document Group", example = "1")
public Integer documentGroupID;
@Description(value = "Force recompute; don't use cache", example = "true", defaultValue = "false")
public Boolean skipCache = false;
@Description(value = "Show this usage help", example = "true", defaultValue = "false")
public Boolean help = false;
@Override
protected void customValidation() {
projectName = Transcription.assureUnicode(projectName);
if (projectID != null || projectName != null) {
project = projectID != null ? ProjectManagement.getProject(projectID) : ProjectManagement.getProject(projectName);
if (project == null) {
addError(-7, "Project not found.");
return;
}
}
if (project != null && documentGroupID == null) {
documentGroupID = project.getDocumentGroupID();
}
}
@Override
protected boolean permissionValidation(HttpServletRequest request, HttpServletResponse response) { return true; }
}
static final long CACHE_TTL = 1000L * 60L * 5L;
static private Map cachedStats = new HashMap();
static synchronized private DataObject getStats(Integer documentGroupID, boolean skipCache) {
DataObject stats = cachedStats.get(documentGroupID);
if (stats == null || skipCache || stats.getTSValue("LASTRUN").getTime() < (new Date().getTime() - CACHE_TTL)) {
stats = Document.getPageStatistics(documentGroupID);
stats.setTSValue("LASTRUN", new Date());
cachedStats.put(documentGroupID, stats);
}
return stats;
}
%>
<%
MyParameters params = new MyParameters().loadFromRequest(request, response);
if (params.getErrors().size() == 0 && !params.help) {
DataObject retVal = getStats(params.documentGroupID, params.skipCache);
if ("json".equals(params.format) || "csv".equals(params.format)) {
try {
Map result = Serializer.fromXML(retVal.toString());
Serializer.output(response, out, params, (Map)result.get("org.crosswire.data.DataObject"), null);
}
catch (Exception e) { params.getLogger().error("ERROR! " + e); e.printStackTrace(); }
return;
}
response.setContentType("text/xml");
out.print(retVal);
return;
}
params.format = "html";
if (params.getErrors().size() == 0) params.addError(0, "usage");
Serializer.reportErrors(request, response, out, params);
%>