%@ 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.webtools.RightsAndRoles.User" %>
<%@ page import="org.crosswire.community.projects.ntmss.data.ProjectManagement" %>
<%@ page import="org.crosswire.community.projects.ntmss.data.Transcription" %>
<%@ page import="org.crosswire.community.projects.ntmss.data.Document" %>
<%@ page import="org.crosswire.repo.VersionedRepo" %>
<%@ page import="org.crosswire.utils.Utils" %>
<%@ page import="org.apache.log4j.Logger" %>
<%!
static {
Parameters.defaultFormat = "xml";
}
@Description(value = "Save project data", name = "project/data/put")
public static class MyParameters extends Parameters {
protected ProjectManagement.Project project = null;
@Description(value = "Project ID for which data should be saved", example = "1", defaultValue = "either projectID or projectName is required")
public Integer projectID;
@Description(value = "Project Name for which data should be saved", example = "ECM Matthew")
public String projectName;
@NotNull
@Description(value = "key path for the data", example = "collation/regularized/Rev.1.1")
public String key;
@Description(value = "optional, a subKey to differentiate this data at the key path, if multiple values will be 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 = "body of data to set", defaultValue = "must supply either {data} or {dataConvert}")
public String data;
@Description(value = "save as above, but converts from iso8859-1 datastream")
public String dataConvert;
@Description(value = "Should the repository be pushed immediately", defaultValue = "false", example = "true")
public Boolean push = false;
@Description(value = "optional, use a specific session hash token", defaultValue = "the open session for the current user")
public String sessionHash;
private boolean isAdmin = false;
@Override
protected void afterLoad() {
if (dataConvert != null) {
data = Transcription.assureUnicode(dataConvert);
}
projectName = Transcription.assureUnicode(projectName);
project = projectID != null ? ProjectManagement.getProject(projectID) : ProjectManagement.getProject(projectName);
User user = getUser();
if (user != null) {
isAdmin = user.hasRole("VMR Administrator")
|| user.hasRole("Project Manager")
|| (project != null && (
user.hasRole("Site Administrator", project.getProjectName())
|| user.hasRole("Project Editor", project.getProjectName())
|| user.hasRole("Project Managing Editor", project.getProjectName())
)
);
}
/*
if (key == null) {
key = request.getParameter("key");
getLogger().info("key is null, trying manual load... key: " + key + "; key.length: " + (request.getParameterValues("key") != null ? request.getParameterValues("key").length : 0));
}
*/
}
@Override
protected void customValidation() {
if (getUser() == null) {
addError(-5, "Permission denied.");
return;
}
if (data == null) {
addError(-4, "{data} may not be null");
return;
}
if (projectID == null && projectName == null) {
addError(-4, "Must supply either {projectID} or {projectName}.");
return;
}
if (project == null) {
addError(-7, "Project not found.");
return;
}
}
}
%>
<%
MyParameters params = new MyParameters().loadFromRequest(request, response, false);
if (params.getErrors().size() == 0) {
do {
if (!params.isAdmin && (params.userName == null || params.userName.length() < 1)) {
params.addError(-5, "Must be a VMR Administrator or Project Manager to publish global project data.");
break;
}
if (!params.isAdmin && !params.userName.equals(params.getUser().getUserName())) {
params.addError(-5, "Must be a VMR Administrator or Project Manager to edit another user's project data.");
break;
}
response.setContentType("text/xml");
String path = params.project.getProjectName() + "/";
if (!params.key.endsWith("/")) params.key += "/";
path += params.key;
if (params.userName != null && params.userName.length() > 0)
path += "initial/" + params.userName + "/";
path += params.subKey + ".txt";
VersionedRepo.putFile(path, params.getUser().getUserName(), new StringBuffer(params.data), VersionedRepo.PATH_PROJECT);
if (params.push) {
VersionedRepo.push();
}
String keyPath = path.substring(path.indexOf("/"));
String retVal = "";
if (!"xml".equals(params.format)) {
Map result = Serializer.fromXML(retVal.toString());
Serializer.output(response, out, params, result, null);
return;
}
out.print(retVal);
return;
} while (false);
} else params.format = "html";
// if (params.getErrors().size() == 0) params.addError(0, "usage");
Serializer.reportErrors(request, response, out, params);
%>