%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page trimDirectiveWhitespaces="true" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="org.crosswire.community.projects.ntmss.data.Document" %>
<%@ page import="org.crosswire.community.projects.ntmss.data.Page" %>
<%@ page import="org.crosswire.xml.XMLBlock" %>
<%@ page import="org.crosswire.webtools.annotation.*" %>
<%@ page import="org.crosswire.webtools.*" %>
<%@ page import="javax.validation.constraints.NotNull" %>
<%!
@Description(value = "Delete page (along with all surrogates for page) from a manuscript.", name = "manuscript/deletepage")
public static class MyParameters extends Parameters {
boolean all = false;
List pageIDs = new ArrayList();
@NotNull
@Description(value = "manuscript id", example = "10046")
public Integer docID;
@NotNull
@Description(value = "page id (can repeat) or 'all'", example = "10")
public String pageID;
@Override
protected void customValidation() {
if (getUser() == null) {
addError(-5, "Must be logged in.");
return;
}
if ("all".equals(pageID)) all = true;
else {
String pageIDStrings[] = request.getParameterValues("pageID");
if (pageIDStrings != null) {
for (String p : pageIDStrings) {
try { pageIDs.add(Integer.parseInt(p)); } catch (Exception e) {}
}
}
}
}
}
%>
<%
MyParameters params = new MyParameters().loadFromRequest(request, response, false);
if (params.getErrors().size() == 0) {
Document doc = Document.getDocument(params.docID);
if (doc == null) {
params.addError(-6, "Document docID: " + params.docID + " not found.");
}
else {
String role = "VMR Administrator";
boolean permission = params.getUser().hasRole(role) || params.getUser().getUserName().equals(doc.getUserID());
if (!permission) {
params.getUser().includeUserRoles();
XMLBlock ur = params.getUser().getBlock("userRoles");
if (ur != null) {
XMLBlock rs[] = ur.getBlocks("role");
for (XMLBlock r : rs) {
String roleName = r.getAttribute("roleName");
if (roleName.startsWith("Catalog Admin:")) {
try {
int min = Integer.parseInt(roleName.split(":")[1].split("-")[0].trim());
int max = Integer.parseInt(roleName.split("-")[1].trim());
if (params.docID >= min && params.docID <= max) permission = true;
}
catch(Exception e) {}
}
}
}
}
if (!permission) {
params.addError(-7, "User is not the owner of this document nor has the role: " + role + ".");
}
else {
StringBuffer retVal = new StringBuffer();
if (params.all) {
doc.removeAllPagesAndSurrogates();
retVal.append("");
}
else {
int success = 0;
for (Integer id : params.pageIDs) {
Page p = Page.getPage(params.docID, id);
if (p != null) {
Page.deletePage(p.getDocumentID(), p.getPageID());
++success;
}
}
retVal.append("");
}
Serializer.output(response, out, params, XMLBlock.createXMLBlock(retVal.toString()));
return;
}
}
}
else params.format = "html";
Serializer.reportErrors(request, response, out, params, true);
%>