%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page trimDirectiveWhitespaces="true" %>
<%@ page import="org.crosswire.utils.Utils" %>
<%@ page import="org.crosswire.utils.Sessions" %>
<%@ page import="org.crosswire.utils.HTTPUtils" %>
<%@ page import="com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil" %>
<%@ page import="com.liferay.portlet.messageboards.model.MBMessage" %>
<%@ page import="com.liferay.portlet.messageboards.service.MBCategoryLocalServiceUtil" %>
<%@ page import="com.liferay.portlet.messageboards.model.MBCategory" %>
<%@ page import="com.liferay.portlet.messageboards.model.MBCategoryConstants" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.Arrays" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="org.apache.log4j.Logger" %>
<%@ page import="org.crosswire.xml.XMLBlock" %>
<%@ page import="org.crosswire.webtools.annotation.*" %>
<%@ page import="org.crosswire.webtools.*" %>
<%@ page import="org.crosswire.community.projects.ntmss.data.Transcription" %>
<%@ page import="javax.validation.constraints.NotNull" %>
<%!
@Description(value = "Get information about messages.", name = "forum/message/get")
public static class MyParameters extends Parameters {
@NotNull
@Description(value = "forum group context", example = "12")
public Long groupID;
@Description(value = "category of messages to retrieve", defaultValue = "must supply one of: {categoryID} or {messagePath}", example = "120")
public Long categoryID;
@Description(value = "path describing message location on forum", example = "/Top Category/Sub Category/.../Message Subject")
public String messagePath;
@NotNull
@Description(value = "whether or not to include drafts", defaultValue = "true", example = "false")
public Boolean includeDraft = true;
@NotNull
@Description(value = "Whether or not to include deleted messages", defaultValue = "true", example = "false")
public Boolean includeDeleted = true;
@Override
protected void afterLoad() {
messagePath = Transcription.assureUnicode(messagePath);
}
@Override
protected void customValidation() {
if (categoryID == null && messagePath == null) {
addError(-5, "Must supply either {categoryID} or {messagePath}.");
return;
}
}
}
public static long getCategoryID(long groupID, long parentCategoryID, String name) {
try {
List cats = MBCategoryLocalServiceUtil.getCategories(groupID, parentCategoryID, -1, -1);
for (MBCategory c : cats) {
if (c.getName().equals(name)) {
return c.getCategoryId();
}
}
}
catch (Exception e) {}
return -1;
}
%>
<%
MyParameters params = new MyParameters().loadFromRequest(request, response, false);
if (params.getErrors().size() == 0) {
String error = null;
response.setContentType("text/xml");
List msgs = new ArrayList();
if (params.messagePath == null) {
msgs = MBMessageLocalServiceUtil.getCategoryMessages(params.groupID, params.categoryID, -1, -1, -1);
}
else {
List categoryNames = Arrays.asList(params.messagePath.split("/"));
long parentCategoryID = params.categoryID != null ? params.categoryID : 0;
for (int i = 0; (i < categoryNames.size()-1 && parentCategoryID != -1); ++i) {
String catName = categoryNames.get(i);
if (catName == null || catName.trim().length() == 0) continue;
long nextCategoryID = getCategoryID(params.groupID, parentCategoryID, catName);
parentCategoryID = nextCategoryID;
}
if (parentCategoryID != -1) {
String lastSegment = categoryNames.size() > 0 ? categoryNames.get(categoryNames.size()-1) : "";
List maybe = MBMessageLocalServiceUtil.getCategoryMessages(params.groupID, parentCategoryID, -1, -1, -1);
for (MBMessage msg : maybe) {
if (lastSegment.equals(msg.getSubject())) {
msgs.add(msg);
}
}
// finally, if we're still empty, let's see if our last segment was a category
if (msgs.isEmpty()) {
long nextCategoryID = getCategoryID(params.groupID, parentCategoryID, lastSegment);
if (nextCategoryID != -1) {
parentCategoryID = nextCategoryID;
msgs = MBMessageLocalServiceUtil.getCategoryMessages(params.groupID, parentCategoryID, -1, -1, -1);
}
}
}
params.categoryID = parentCategoryID;
}
if (!msgs.isEmpty() && (!params.includeDraft || !params.includeDeleted)) {
List maybe = msgs;
msgs = new ArrayList();
for (MBMessage m : maybe) {
if (!params.includeDraft && m.isDraft()) continue;
if (!params.includeDeleted && m.isInTrash()) continue;
msgs.add(m);
}
}
if (error != null) {
params.addError(-7, error);
}
else {
StringBuffer retVal = new StringBuffer();
retVal.append("");
for (MBMessage msg : msgs) {
retVal.append("");
}
retVal.append("");
Serializer.output(response, out, params, XMLBlock.createXMLBlock(retVal.toString()));
return;
}
}
else params.format = "html";
Serializer.reportErrors(request, response, out, params, true);
%>