<%@ page contentType="text/html;charset=GBK" %>
<%
/**
 *	$RCSfile: filters.jsp,v $
 *	$Revision: 1.1.1.1 $
 *	$Date: 2002/09/09 13:50:12 $
 */
%>

<%@ page import="java.beans.*,
                 java.util.*,
                 com.jivesoftware.util.*,
                 com.jivesoftware.forum.*,
                 com.jivesoftware.forum.database.*,
                 com.jivesoftware.forum.util.*"
    errorPage="error.jsp"
%>

<%@ include file="global.jsp" %>

<%! // Global variables/methods for this page
    private boolean isInstalledFilter(FilterManager filterManager,
            ForumMessageFilter filter)
    {
        try {
            int filterCount = filterManager.getFilterCount();
            if (filter == null) {
                return false;
            }
            if (filterCount < 1) {
                return false;
            }
            String filterClassname = filter.getClass().getName();
            for (int i=0; i<filterCount; i++) {
                ForumMessageFilter installedFilter = filterManager.getFilter(i);
                if (filterClassname.equals(installedFilter.getClass().getName())) {
                    return true;
                }
            }
        } catch (Exception e) {}
        return false;
    }

    private String getHTML(ForumMessageFilter filter, PropertyDescriptor descriptor)
    {
        // HTML of the customizer for this property
        StringBuffer html = new StringBuffer(50);
        // Get the name of the property (this becomes the name of the form element)
        String propName = descriptor.getName();
        // Get the current value of the property
        Object propValue = null;
        try {
            propValue = descriptor.getReadMethod().invoke(filter,null);
        }
        catch (Exception e) {e.printStackTrace();}

        // Get the classname of this property
        String className = descriptor.getPropertyType().getName();

        // HTML form elements for number values (rendered as small textfields)
        if ("int".equals(className)
            || "double".equals(className)
            || "long".equals(className))
        {
            html.append("<input type=\"text\" name=\"").append(propName).append("\" size=\"6\" maxlength=\"10\"");
            if (propValue != null) {
                html.append(" value=\"").append(propValue.toString()).append("\"");
            }
            html.append(">");
        }
        // HTML form elements for boolean values (rendered as Yes/No radio buttons)
        else if ("boolean".equals(className)) {
            boolean value = false;
            if ("true".equals(propValue.toString())) {
                value = true;
            }
            html.append("<input type=\"radio\" name=\"").append(propName).append("\" id=\"rb").append(propName).append("1\" ");
            html.append("value=\"true\"");
            html.append((value)?" checked":"");
            html.append("> <label for=\"rb").append(propName).append("1\">Yes</label> ");
            html.append("<input type=\"radio\" name=\"").append(propName).append("\" id=\"rb").append(propName).append("2\" ");
            html.append("value=\"false\"");
            html.append((!value)?" checked":"");
            html.append("> <label for=\"rb").append(propName).append("2\">No</label> ");
        }
        // HTML elements for a String (rendered as a wide textfield)
        else if ("java.lang.String".equals(className)) {
            if (propName.toLowerCase().equals("password")) {
                html.append("<input type=\"password\"");
            }
            else {
                html.append("<input type=\"text\"");
            }
            html.append(" name=\"").append(propName).append("\" size=\"30\" maxlength=\"150\"");
            if (propValue != null) {
                html.append(" value=\"").append(escapeHTML(propValue.toString())).append("\"");
            }
            html.append(">");
        }
        if (html.length() == 0) {
            html.append("&nbsp;");
        }
        return html.toString();
    }

    private Map getFilterPropertyValues(HttpServletRequest request,
            FilterManager filterManager, ForumMessageFilter filter)
    {
        // Map of filter property name/value pairs
        Map map = new HashMap();
        try {
            // Property descriptors
            PropertyDescriptor[] descriptors = BeanUtils.getPropertyDescriptors(filter.getClass());
            // Loop through the properties, get the value of the property as a
            // parameter from the HttpRequest object
            for (int i=0; i<descriptors.length; i++) {
                String propName = descriptors[i].getName();
                String propValue = ParamUtils.getParameter(request,propName);
                map.put(propName, propValue);
            }
        } catch (Exception e) {}
        return map;
    }

    private String escapeHTML(String html) {
        html = StringUtils.replace(html, "\"", "&quot;");
        return StringUtils.escapeHTMLTags(html);
    }
%>

<%	// Get parameters
	long forumID = ParamUtils.getLongParameter(request,"forum",-1);
    String classname = ParamUtils.getParameter(request,"filters");
    boolean install = ParamUtils.getBooleanParameter(request,"install");
    boolean remove = ParamUtils.getBooleanParameter(request,"remove");
    int position = ParamUtils.getIntParameter(request,"pos",-1);
    boolean edit = ParamUtils.getBooleanParameter(request,"edit");
    boolean addFilter = ParamUtils.getBooleanParameter(request,"addFilter");
    String newClassname = ParamUtils.getParameter(request,"newClassname");
    boolean saveProperties = ParamUtils.getBooleanParameter(request,"saveProperties");
    int filterIndex = ParamUtils.getIntParameter(request,"filterIndex",-1);
    boolean changePosition = ParamUtils.getBooleanParameter(request,"changePos");
    boolean up = ParamUtils.getBooleanParameter(request,"up");
    boolean down = ParamUtils.getBooleanParameter(request,"down");

    // Indicate if we're doing global filters
    boolean isGlobal = (forumID == -1L);

    // Load the forum
    Forum forum = null;
    if (forumID > 0L) {
        forum = forumFactory.getForum(forumID);
    }

    // Get the filter manager
    FilterManager filterManager = null;
    if (forum != null) {
        filterManager = forum.getFilterManager();
    }
    else {
        filterManager = forumFactory.getFilterManager();
    }

    // Check for the case that there is a global, uncacheable filter.
    boolean globalHasUncacheableFilters = false;
    if (!isGlobal) {
        // Get the global filter manager
        FilterManager globalFilterManager = forumFactory.getFilterManager();
        globalHasUncacheableFilters = globalFilterManager.hasUncacheableFilters();
    }

    // Save filter properties
    if (saveProperties) {
        if (filterIndex >= 0) {
            // The filter we're working with
            ForumMessageFilter filter = filterManager.getFilter(filterIndex);
            // A map of name/value pairs. The names are the names of the bean
            // properties and the values come as parameters to this page
            Map properties = getFilterPropertyValues(request, filterManager, filter);
            // Set the properties
            BeanUtils.setProperties(filter, properties);
            // Save the filters
            filterManager.saveFilters();
            // Done, so redirect to this page
            response.sendRedirect("filters.jsp?forum=" + forumID);
            return;
        }
    }

    // Add a new filter to the list of installable filters
    if (addFilter) {
        try {
            if (newClassname == null) {
                throw new ClassNotFoundException("没有指定类名。");
            }
            DbFilterManager.addFilterClass(newClassname.trim());
        }
        catch (ClassNotFoundException cnfe) {
            setOneTimeMessage(session, "message",
                "\"" + newClassname + "\" 不是一个合法的类名。");
        }
        response.sendRedirect("filters.jsp?forum=" + forumID);
        return;
    }

    // Change the position of a filter
    if (changePosition) {
        if (filterIndex >= 0) {
            // Get the filter at the specified filter position
            ForumMessageFilter filter = filterManager.getFilter(filterIndex);
            // Remove it
            filterManager.removeFilter(filterIndex);
            // Re-add it based on the "direction" we're doing
            if (up) {
                filterManager.addFilter(filter, filterIndex-1);
            }
            if (down) {
                filterManager.addFilter(filter, filterIndex+1);
            }
            // done, so redirect
            response.sendRedirect("filters.jsp?forum=" + forumID);
            return;
        }
    }

    // Number of installed filters
    int filterCount = filterManager.getFilterCount();
    // All filter classes
    ForumMessageFilter[] filters = forumFactory.getAvailableFilters();

    if (install && classname != null) {
        try {
            ForumMessageFilter newFilter
                = (ForumMessageFilter)(Class.forName(classname)).newInstance();
            filterManager.addFilter(newFilter);
            String redirect = "filters.jsp";
            if (forumID > -1L) {
                redirect += "?forum=" + forumID;
            }
            response.sendRedirect(redirect);
            return;
        }
        catch (Exception e) {}
    }

    if (remove && position > -1) {
        filterManager.removeFilter(position);
        String redirect = "filters.jsp";
        if (forumID > -1L) {
            redirect += "?forum=" + forumID;
        }
        response.sendRedirect(redirect);
        return;
    }
%>

<%@ include file="header.jsp" %>

<%  // Title of this page and breadcrumbs
    String title = "";
    if (isGlobal) {
        title = "全局消息过虑";
    } else {
        title = "消息过虑";
    }

    String[][] breadcrumbs = null;
    if (isGlobal) {
        breadcrumbs = new String[][] {
            {"主页面", "main.jsp"},
            {title, "filters.jsp"}
        };
    }
    else {
        breadcrumbs = new String[][] {
            {"主页面", "main.jsp"},
            {"论坛", "forums.jsp"},
            {"编辑论坛", "editForum.jsp?forum="+forumID},
            {title, "filters.jsp?forum="+forumID}
        };
    }
%>
<%@ include file="title.jsp" %>

<span>
<font size="-1">
<%  if (isGlobal) { %>
    过滤器动态的重新格式化消息内容。使用下面的表单安装和自定义过滤器。
<%  } else { %>
    过滤器动态的重新格式化消息内容。使用下面的表单安装和自定义论坛的过滤器。请注意全局消息过滤器总在论坛消息过滤器前应用。
<%  } %>
</font>
</span>

<p>

<script language="JavaScript" type="text/javascript">
var filterInfo = new Array(
<%	for (int i=0; i<filters.length; i++) {
        BeanDescriptor descriptor = (Introspector.getBeanInfo(filters[i].getClass())).getBeanDescriptor();
%>
    new Array(
    "<%= descriptor.getBeanClass().getName() %>",
    "<%= descriptor.getValue("version") %>",
    "<%= descriptor.getValue("author") %>",
    "<%= descriptor.getShortDescription() %>",
    "<%= filters[i].isCacheable() %>"
    )
<%      if ((filters.length-i) > 1) { %>
		,
<%	    }
    }
%>
);
function properties(theForm) {
    var className = theForm.filters.options[theForm.filters.selectedIndex].value;
    var selected = 0;
    for (selected=0; selected<filterInfo.length; selected++) {
        if (filterInfo[selected][0] == className) {
            var version = filterInfo[selected][1];
            var author = filterInfo[selected][2];
            var description = filterInfo[selected][3];
            var cacheable = filterInfo[selected][4];
            theForm.version.value = ((version=="null")?"":version);
            theForm.author.value = ((author=="null")?"":author);
            theForm.description.value = ((description=="null")?"":description);
            if (cacheable == "true") {
                theForm.cacheable.value = "Yes";
            } else {
                theForm.cacheable.value = "No";
            }
            break;
        }
    }
}
</script>

<%  // Print out a message if one exists
    String oneTimeMessage = getOneTimeMessage(session, "message");
    if (oneTimeMessage != null) {
%>
    <font size="-1" color="#ff0000">
    <i><%= oneTimeMessage %></i>
    </font>
    <p>
<%  }
%>

<%  // Colors
    String red = "#ffeeee";
    String yellow = "#ffffee";
    if (filterCount > 0) { %>
<font size="-1"><b>当前过滤器</b></font>
<ul>
	<table bgcolor="<%= tblBorderColor %>" cellpadding="0" cellspacing="0" border="0">
    <tr><td>
    <table cellpadding="4" cellspacing="1" border="0" width="100%">
	<tr bgcolor="#eeeeee">
	<td align="center"><font size="-2" face="verdana"><b>顺序</b></font></td>
	<td align="center"><font size="-2" face="verdana"><b>名称</b></font></td>
	<td align="center"><font size="-2" face="verdana"><b>描述</b></font></td>
    <%  if (filterCount > 1) { %>
	<td align="center"><font size="-2" face="verdana"><b>移动</b></font></td>
    <%  } %>
	<td align="center"><font size="-2" face="verdana"><b>编辑</b></font></td>
	<td align="center"><font size="-2" face="verdana"><b>删除</b></font></td>
    </tr>
<%  // Indicates if the previous filter was cacheable
    boolean wasCacheable = false;
    boolean cacheWarning = false;
    // Loop through all filters
    for (int i=0; i<filterCount; i++) {
        ForumMessageFilter filter = filterManager.getFilter(i);
        // check for the case that a non-cached filter is first
        if (i==0) {
            if (!filter.isCacheable()) {
                wasCacheable = true;
            }
        }

        // Descriptor for this filter
        BeanDescriptor descriptor = (Introspector.getBeanInfo(filter.getClass())).getBeanDescriptor();
        // Properties for this filter
        PropertyDescriptor[] descriptors = BeanUtils.getPropertyDescriptors(filter.getClass());
        // Version of this filter
        String version = (String)descriptor.getValue("version");
        // Description of this filter
        String description = StringUtils.escapeHTMLTags(descriptor.getShortDescription());
        // is this filter cacheable?
        boolean cacheable = filter.isCacheable();
        // The color of this row
        String bgcolor = "#ffffff"; // white by default
        if (wasCacheable && !cacheable) {
            bgcolor = red;
            cacheWarning = true;
        }
        wasCacheable = cacheable;
        if (wasCacheable && cacheWarning) {
            bgcolor = yellow;
        }
        // check for the case that there is a global, uncacheable filter:
        if (!isGlobal && globalHasUncacheableFilters) {
            cacheWarning = true;
            bgcolor = yellow;
        }
%>
    <tr bgcolor="#ffffff">
        <td><font size="-1"><%= (i+1) %></font></td>
        <td bgcolor="<%= bgcolor %>" nowrap><font size="-1"><%= descriptor.getDisplayName() %></font></td>
        <td><font size="-1"><%= (description!=null)?description:"&nbsp;" %></font></td>
        <%  if (filterCount > 1) { %>
        <td>
            <%  if ((i+1)<filterCount) { %>
                <a href="filters.jsp?forum=<%= forumID %>&changePos=true&down=true&filterIndex=<%= i %>"
                ><img src="images/arrow_down.gif" width="13" height="9" alt="将此过虑器下移" border="0"></a>
            <%  } else { %>
                <img src="images/blank.gif" width="13" height="9" border="0">
            <%  } %>

            <%  if (i != 0) { %>
                <a href="filters.jsp?forum=<%= forumID %>&changePos=true&up=true&filterIndex=<%= i %>"
                ><img src="images/arrow_up.gif" width="13" height="9" alt="将此过滤器上移" border="0"></a>
            <%  } else { %>
                <img src="images/blank.gif" width="13" height="9" border="0">
            <%  } %>
        </td>
        <%  } %>
        <td align="center">
        <%  if (descriptors.length > 0) { %>
            <a href="filters.jsp?edit=true&forum=<%= forumID %>&pos=<%= i %>"
            ><img src="images/button_edit.gif" width="17" height="17" alt="编辑过滤器属性" border="0"
            ></a>
        <%  } else { %>
            &nbsp;
        <%  } %>
        </td>
        <td align="center">
            <a href="filters.jsp?remove=true&forum=<%= forumID %>&pos=<%= i %>"
            ><img src="images/button_delete.gif" width="17" height="17" alt="删除此过滤器" border="0"
            ></a>
        </td>
    </tr>
<%      if (position == i && edit) { %>
    <form action="filters.jsp">
    <input type="hidden" name="forum" value="<%= forumID %>">
    <input type="hidden" name="saveProperties" value="true">
    <input type="hidden" name="filterIndex" value="<%= i %>">
    <tr bgcolor="#ffffff">
        <td>&nbsp;</td>
        <td colspan="<%= (filterCount > 1)?"5":"4" %>">
            <table cellpadding="2" cellspacing="0" border="0" width="100%">
<%          int color = 0;
            for (int j=0; j<descriptors.length; j++) {
    %>
            <tr bgcolor=<%= (color++%2==0)?"#f4f5f7":"#ffffff" %>>
                <td width="60%">
                <font size="-1"><%= descriptors[j].getDisplayName() %></font>
                <br>
                <font size="-2"><%= descriptors[j].getShortDescription() %></font>
                </td>
                <td width="20%">&nbsp;</td>
                <td width="20%" nowrap>
                    <font size="-1">
                    <%= getHTML(filter, descriptors[j]) %>
                    </font>
                </td>
            </tr>
<%          } %>
            <tr>
                <td colspan="4" align="right">
                    <font size="-1">
                    <input type="submit" value="Save Properties">
                    </font>
                </td>
            </tr>
            </table>
        </td>
    </tr>
    </form>
<%      } %>
<%  }
%>
    </table>
    </td></tr>
    </table>
    <br>

<%      if (cacheWarning) { %>
    <table cellpadding="2" cellspacing="2" border="0">
    <tr>
    	<td>
            <table bgcolor="<%= tblBorderColor %>" cellpadding="0" cellspacing="0" border="0" width="1%">
            <tr><td>
            <table bgcolor="<%= tblBorderColor %>" cellpadding="3" cellspacing="1" border="0" width="100%">
            <tr bgcolor="<%= red %>">
                <td><img src="images/blank.gif" width="10" height="10" border="0"></td>
            </tr>
            </table>
            </td></tr>
            </table>
        </td>
    	<td>
            <font size="-2">不可缓存的过滤器。</font>
        </td>
    	<td>&nbsp;</td>
    	<td>
            <table bgcolor="<%= tblBorderColor %>" cellpadding="0" cellspacing="0" border="0" width="1%">
            <tr><td>
            <table bgcolor="<%= tblBorderColor %>" cellpadding="3" cellspacing="1" border="0" width="100%">
            <tr bgcolor="<%= yellow %>">
                <td><img src="images/blank.gif" width="10" height="10" border="0"></td>
            </tr>
            </table>
            </td></tr>
            </table>
        </td>
    	<td>
            <font size="-2">防止被缓存的过滤器。</font>
        </td>
    </tr>
    </table>
<%      }
    }
%>
</ul>
</form>

<p>

<form action="filters.jsp">
<input type="hidden" name="forum" value="<%= forumID %>">
<input type="hidden" name="install" value="true">
<span>
<font size="-1"><b>安装过滤器</b></font>
</span>
<ul>
	<table bgcolor="<%= tblBorderColor %>" cellpadding="0" cellspacing="0" border="0" width="1%">
    <tr><td>
	<table cellpadding="4" cellspacing="1" border="0" width="100%">
	<tr bgcolor="#eeeeee">
	<td align="center"><font size="-2" face="verdana"><b>可用过滤器</b></font></td>
    </tr>
	<tr bgcolor="#ffffff">
	<td>
        <table cellpadding="1" cellspacing="0" border="0">
        <tr>
            <td width="48%" valign="top">
            <font size="-1">
            <select size="8" name="filters" onchange="properties(this.form);"">
<%  for (int i=0; i<filters.length; i++) {
        if (!isInstalledFilter(filterManager, filters[i])) {
            BeanDescriptor descriptor = (Introspector.getBeanInfo(filters[i].getClass())).getBeanDescriptor();
%>
            <option value="<%= descriptor.getBeanClass().getName() %>"><%= descriptor.getDisplayName() %>
<%      }
    }
%>
            </select>
            </font>
            </td>
            <td width="2%"><img src="images/blank.gif" width="5" height="1" border="0"></td>
            <td width="48%" valign="top">
                <table cellpadding="2" cellspacing="0" border="0" width="100%">
                <tr>
                    <td><font size="-2">版本</font></td>
                    <td><input type="text" size="20" name="version" style="width:100%"></td>
                </tr>
                <tr>
                    <td><font size="-2">作者</font></td>
                    <td><input type="text" size="20" name="author" style="width:100%"></td>
                </tr>
                <tr>
                    <td valign="top"><font size="-2">缓存性</font></td>
                    <td><input type="text" size="3" name="cacheable"></td>
                </tr>
                <tr>
                    <td valign="top"><font size="-2">描述</font></td>
                    <td><textarea name="description" cols="20" rows="5" wrap="virtual"></textarea></td>
                </tr>
                </table>
            </td>
        </tr>
        <tr>
            <td colspan="3" align="center">
            <font size="-1">
            <input type="submit" value="安装...">
            </font>
            </td>
        </tr>
        </table>
    </td>
    </tr>
    </table>
    </td></tr>
    </table>
</ul>
</form>

<p>

<form action="filters.jsp">
<input type="hidden" name="addFilter" value="true">
<input type="hidden" name="forum" value="<%= forumID %>">
<span>
<font size="-1"><b>添加过虑器</b></font>
</span>
<ul>
    <table cellpadding="2" cellspacing="0" border="0">
    <tr>
    	<td><font size="-1">类名:</font></td>
    	<td><input type="text" name="newClassname" value="" size="30" maxlength="100"></td>
    	<td><font size="-1"><input type="submit" value="添加过虑器"></font></td>
    </tr>
    </table>
</ul>
</form>

<p>

<%--
<font size="-1"><b>Remove Filter</b></font>
<ul>

</ul>
</form>
--%>


</body>
</html>

