%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page trimDirectiveWhitespaces="true" %>
<%@ page import="javax.validation.constraints.NotNull" %>
<%@ 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.Map" %>
<%@ page import="java.util.Date" %>
<%@ page import="java.util.Vector" %>
<%@ page import="java.io.File" %>
<%@ page import="org.crosswire.repo.VersionedRepo" %>
<%@ page import="org.json.JSONObject" %>
<%!
@Description(value = "Retrieve project data.", name = "project/data/get")
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 for the data; multiple keys can be retrieved by separating key paths by '|'", example = "collation/regularized/Rev.1.1|collation/setvariants/Rev.1.1")
public String key;
@Description(value = "optional, a subKey to differentiate this data at the key path, if multiple values are stored for this key", example = "draft")
public String subKey = "primary";
@Description(value = "optional, scope this data for individual user", example = "jsmith")
public String userName;
@Description(value = "retrieve a previous version of this data")
public String versionHash;
@Description(value = "retrieve version history for the data instead of the actual data", defaultValue = "false", example = "true")
public Boolean history = false;
@Description(value = "used with {history}. if true, retrieve also latest versions for all users", defaultValue = "false", example = "true")
public Boolean allUsers = false;
@Description(value = "optional, use a specific session hash token", defaultValue = "the open session for the current user")
public String sessionHash;
@Override
protected void customValidation() {
// who we are
String myUserName = getUser() != null ? getUser().getUserName() : null;
// TODO: should we do this? It seems reasonable but code below seems to assume null == myself.
// Investigate and solve
//if (userName == null) userName = myUserName;
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;
}
String role="VMR Administrator";
String role2="Project Manager";
// let's check to see if we're a privileged user
boolean isAdmin = RightsAndRoles.hasRole(request, response, role);
if (!isAdmin) isAdmin = RightsAndRoles.hasRole(request, response, role2);
if (!isAdmin) isAdmin = RightsAndRoles.hasRole(request, response, "Site Administrator", project.getProjectName());
}
}
%>
<%
MyParameters params = new MyParameters().loadFromRequest(request, response, false);
if (params.getErrors().size() == 0) {
// who we want to pull transcription for
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");
Vector keys = new Vector();
for (String k : params.key.split("\\|")) {
keys.add(k);
}
StringBuffer retVal = new StringBuffer();
retVal.append("");
if (format == FORMAT_JSON) response.setContentType("application/json");
else if (format == FORMAT_TXT) response.setContentType("text/plain");
StringBuffer rawBody = new StringBuffer();
if (format == FORMAT_JSON) rawBody.append("[");
for (String key : keys) {
String path = params.project.getProjectName() + "/";
if (!key.endsWith("/")) key += "/";
path += key;
if (params.userName != null && params.userName.length() > 0)
path += "initial/" + params.userName + "/";
path += params.subKey +".txt";
if (!params.history) {
StringBuffer body = VersionedRepo.getFile(path, params.userName, params.versionHash, VersionedRepo.PATH_PROJECT);
if (format == FORMAT_JSON) {
// JSONObject o = (body!=null?new JSONObject(body):null);
//logger.info("body: " + body.toString());
//logger.info("json: " + o.toString());
if (rawBody.length() > 1) rawBody.append(",");
rawBody.append(body);
}
else rawBody.append(body);
retVal.append(""+(body!=null?HTTPUtils.canonize(body.toString()):"")+"");
}
else {
retVal.append("");
for (VersionedRepo.History h : VersionedRepo.getFileHistory(path, params.userName, VersionedRepo.PATH_PROJECT)) retVal.append(h.toFormattedXML());
if (params.allUsers) {
path = params.project.getProjectName() + "/";
if (params.userName != null) {
File potential = VersionedRepo.getFileName(path+params.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("");
}
}
retVal.append("");
if (format == FORMAT_JSON) {
if (rawBody.toString().startsWith("[<")) {
// seems we're trying to return json from XML data, let's try a conversion
try { rawBody = new StringBuffer("[" + Serializer.toJSON(Serializer.fromXML(rawBody.toString().substring(1)))); } catch (Exception e) {} // if we fail, well, we tried
}
rawBody.append("]");
}
if (format == FORMAT_JSON) {
if (!params.history) {
out.print(rawBody);
}
else {
Map result = Serializer.fromXML(retVal.toString());
Serializer.output(response, out, params, result, null);
}
}
else if (format == FORMAT_XML) {
out.print(retVal.toString());
}
else out.print(rawBody);
return;
}
params.format = "html";
if (params.getErrors().size() == 0) params.addError(0, "usage");
Serializer.reportErrors(request, response, out, params);
%>