%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page trimDirectiveWhitespaces="true" %>
<%@ page import="javax.validation.constraints.NotNull" %>
<%@ page import="java.util.Map" %>
<%@ page import="org.crosswire.utils.Sessions" %>
<%@ page import="org.crosswire.webtools.annotation.*" %>
<%@ page import="org.crosswire.webtools.*" %>
<%@ page import="org.crosswire.community.projects.ntmss.data.ProjectManagement" %>
<%@ page import="org.crosswire.community.projects.ntmss.data.Transcription" %>
<%@ page import="org.crosswire.utils.HTTPUtils" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="java.util.HashSet" %>
<%@ page import="java.util.StringTokenizer" %>
<%@ page import="java.util.Date" %>
<%@ page import="java.util.Vector" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.io.File" %>
<%@ page import="java.util.PriorityQueue" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="org.crosswire.repo.VersionedRepo" %>
<%@ page import="org.json.JSONObject" %>
<%@ page import="org.json.JSONArray" %>
<%@ page import="org.apache.log4j.Logger" %>
<%!
static {
Parameters.defaultFormat = "xml";
}
@Description(value = "Discover project data children keys for a given key", name = "project/data/listchildren")
public static class MyParameters extends Parameters {
protected ProjectManagement.Project project = null;
@Description(value = "Project ID from which data should be retrieved", example = "1", defaultValue = "either projectID or projectName is required")
public Integer projectID;
@Description(value = "Project Name from which data should be retrieved", example = "ECM Matthew")
public String projectName;
@NotNull
@Description(value = "key path to inspect", example = "collation/regularized/Rev.1.1")
public String key;
@Description(value = "optional, scope this data for individual user", example = "jsmith")
public String userName;
@Description(value = "optional, use a specific session hash token", defaultValue = "the open session for the current user")
public String sessionHash;
@Override
protected void customValidation() {
String myUserName = Sessions.getSessionStringValue(request, "userName");
if (projectID == null && projectName == null) {
addError(-4, "Must supply either {projectID} or {projectName}.");
return;
}
projectName = Transcription.assureUnicode(projectName);
project = projectID != null ? ProjectManagement.getProject(projectID) : ProjectManagement.getProject(projectName);
if (project == null) {
addError(-7, "Project not found.");
return;
}
}
}
%>
<%
MyParameters params = new MyParameters().loadFromRequest(request, response, false);
if (params.getErrors().size() == 0) {
// future?
boolean allUsers = false;
boolean history = "true".equals(request.getParameter("history"));
// who we are
String userName = Sessions.getSessionStringValue(request, "userName");
final int FORMAT_XML = 0;
final int FORMAT_JSON = 1;
final int FORMAT_TXT = 2;
int format = FORMAT_XML;
if ("json".equals(params.format)) format = FORMAT_JSON;
else if ("txt".equals(params.format)) format = FORMAT_TXT;
if ("PUBLISHED".equals(params.userName)) params.userName = null;
response.setContentType("text/xml");
StringBuffer retVal = new StringBuffer();
if (format == FORMAT_JSON) response.setContentType("application/json");
else if (format == FORMAT_TXT) response.setContentType("text/plain");
// JSONArray jsonResults = (format == FORMAT_JSON ? new JSONArray() : null);
StringBuffer jsonResults = (format == FORMAT_JSON ? new StringBuffer("[") : null);
String path = params.project.getProjectName() + "/";
if (!params.key.endsWith("/")) params.key += "/";
if (params.key.startsWith("/")) params.key = params.key.substring(1);
path += params.key;
File[] keys = VersionedRepo.getFolders(path, VersionedRepo.PATH_PROJECT); // getFolders actually simply returns children
retVal.append("");
for (File kf : keys) {
String key = kf.getName();
path = params.project.getProjectName() + "/" + params.key;
if (kf.isDirectory() && !key.endsWith("/")) key += "/";
path += key;
if (params.userName != null && params.userName.length() > 0)
path += "initial/" + params.userName + "/";
// path += subKey +".txt";
String keyPath = path.substring(path.indexOf("/"));
if (!history) {
StringBuffer body = VersionedRepo.getFile(path, params.userName, null, VersionedRepo.PATH_PROJECT);
retVal.append("");
}
else {
retVal.append("");
for (VersionedRepo.History h : VersionedRepo.getFileHistory(path, params.userName, VersionedRepo.PATH_PROJECT)) retVal.append(h.toFormattedXML());
if (allUsers) {
path = params.project.getProjectName() + "/";
if (params.userName != null) {
File potential = VersionedRepo.getFileName(path+/*subKey+*/".txt", VersionedRepo.PATH_PROJECT);
if (potential != null) {
VersionedRepo.History n = new VersionedRepo.History(potential.getName(), "");
n.author = "PUBLISHED";
n.date = new Date(potential.lastModified());
n.committer = "";
n.comment = "";
n.versionHash = "HEAD";
retVal.append(n.toFormattedXML());
}
}
path += "initial/";
File[] users = VersionedRepo.getFolders(path, VersionedRepo.PATH_PROJECT); // getFolders actually simply returns children
for (File f : users) {
// skip if we're ourselve-- we already added us, above
if (params.userName != null && (params.userName+".txt").equals(f.getName()))
continue;
String str = f.getName();
if (str.endsWith(".txt")) {
str = str.substring(0, str.lastIndexOf('.'));
VersionedRepo.History n = new VersionedRepo.History(str, "");
n.author = f.getName();
n.date = new Date(f.lastModified());
n.committer = "";
n.comment = "";
n.versionHash = "HEAD";
retVal.append(n.toFormattedXML());
}
}
}
%>
<%
}
}
retVal.append("");
if (!"xml".equals(params.format)) {
Map result = Serializer.fromXML(retVal.toString());
Serializer.output(response, out, params, result, null);
return;
}
out.print(retVal);
return;
}
params.format = "html";
if (params.getErrors().size() == 0) params.addError(0, "usage");
Serializer.reportErrors(request, response, out, params);
%>