%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page trimDirectiveWhitespaces="true" %>
<%@ page import="org.crosswire.utils.HTTPUtils" %>
<%@ 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="com.liferay.portal.service.ServiceContext" %>
<%@ page import="java.util.List" %>
<%@ page import="org.crosswire.xml.XMLBlock" %>
<%@ page import="org.crosswire.webtools.annotation.*" %>
<%@ page import="org.crosswire.webtools.*" %>
<%@ page import="javax.validation.constraints.NotNull" %>
<%@ page import="org.crosswire.community.projects.ntmss.data.Transcription" %>
<%!
@Description(value = "Assure a category exists.", name = "forum/category/assureExists")
public static class MyParameters extends Parameters {
@NotNull
@Description(value = "forum group context", example = "12")
public Integer groupID;
@NotNull
@Description(value = "parent id of categories to assure exists", example = "12")
public Integer parentCategoryID;
@NotNull
@Description(value = "name of category", example = "Category X")
public String name;
@NotNull
@Description(value = "description of category to set, if created", example = "A Very Cool Category for X")
public String description;
@Override
protected void afterLoad() {
name = Transcription.assureUnicode(name);
description = Transcription.assureUnicode(description);
}
@Override
protected void customValidation() {
if (getUser() == null) {
addError(-5, "Must be logged in.");
return;
}
}
}
%>
<%
MyParameters params = new MyParameters().loadFromRequest(request, response, false);
if (params.getErrors().size() == 0) {
Long userID = Long.parseLong(params.getUser().getInternalUserID());
params.getLogger().info("forum/category/assureExists(userID:"+userID+", groupID:"+params.groupID+", parentCategoryID:"+params.parentCategoryID+", name:"+params.name+", description:"+params.description+")");
List cats = MBCategoryLocalServiceUtil.getCategories(params.groupID, params.parentCategoryID, -1, -1);
MBCategory cat = null;
for (MBCategory c : cats) {
if (c.getName().equals(params.name)) {
cat = c;
break;
}
}
if (cat == null) {
params.getLogger().info("adding new category.");
ServiceContext sc = new ServiceContext();
sc.setScopeGroupId(params.groupID);
sc.setUserId(userID);
sc.setAddGroupPermissions(true);
sc.setAddGuestPermissions(true);
// cat = MBCategoryLocalServiceUtil.addCategory(userID, params.parentCategoryID, params.name, params.description, sc);
// backward compat
cat = MBCategoryLocalServiceUtil.addCategory(userID, params.parentCategoryID, params.name, params.description, MBCategoryConstants.DEFAULT_DISPLAY_STYLE, null, null, null, 0, false, null, null, 0, null, false, null, 0, false, null, null, false, false, sc);
}
else {
params.getLogger().info("found existing category. messageCount:"+cat.getMessageCount());
}
StringBuffer retVal = new StringBuffer();
retVal.append("");
retVal.append(HTTPUtils.canonize(cat.getDescription()));
retVal.append("");
Serializer.output(response, out, params, XMLBlock.createXMLBlock(retVal.toString()));
return;
}
else params.format = "html";
Serializer.reportErrors(request, response, out, params, true);
%>