<%
/**
 *	$RCSfile: editCache.jsp,v $
 *	$Revision: 1.1.1.1 $
 *	$Date: 2002/09/09 13:50:25 $
 */
%>

<%@ page import="java.util.*,
				 java.text.*,
				 com.jivesoftware.util.*,
                 com.jivesoftware.forum.*,
				 com.jivesoftware.forum.database.*,
				 com.jivesoftware.forum.util.*"
%>

<%!	// global variables
	
	// decimal formatter for cache values
	static final DecimalFormat kFormat = new DecimalFormat("#");
	static final DecimalFormat mbFormat = new DecimalFormat("#0.00");
	static final DecimalFormat percentFormat = new DecimalFormat("#0.0");
    
	// cache identifiers, names
	static final int[] CACHE_TYPES = {
		DatabaseCacheManager.FORUM_CACHE,
		DatabaseCacheManager.THREAD_CACHE,
		DatabaseCacheManager.MESSAGE_CACHE,
		DatabaseCacheManager.USER_CACHE,
		DatabaseCacheManager.USER_PERMS_CACHE,
		DatabaseCacheManager.GROUP_CACHE
	};
	static final String[] CACHE_NAMES = {
		"Forum",
		"Thread",
		"Message",
		"User",
		"User Permissions",
		"Group"
	};
    
    static final int CACHE_PRESET_SMALL = 0;
    static final int CACHE_PRESET_MEDIUM = 1;
    static final int CACHE_PRESET_LARGE = 2;
    static final int CACHE_PRESET_CUSTOM = 3;
    
	static final String[] CACHE_PRESET_NAMES = {
		"Small",
		"Medium",
		"Large",
		"Custom"
	};
    
    // Preset mem sizes (in bytes)
    static final int[][] CACHE_PRESET_SIZES = {
        {  512*1024, 1024*1024,  5120*1024 },
        {  512*1024, 3072*1024, 10240*1024 },
        { 1152*1024, 8192*1024, 32768*1024 },
        {  512*1024, 1024*1024,  6144*1024 },
        {  256*1024,  768*1024,  4608*1024 },
        {  128*1024,  256*1024,   512*1024 }
    };
    
	static final String[] CACHE_PRESET_DESCRIPTIONS = {
		"Suitable for workgroups or small to medium communities. (3 MB total cache)",
		"Suitable for medium to large communities. (14 MB total cache)",
		"Suitable for very large communities on servers with a lot of memory. (60 MB total cache)",
		"Use this option to be able to edit each of the cache sizes below."
	};
    
    private int getCurrentPreset(DatabaseCacheManager dbCacheManager) {
        // Custom by default (we loop through the 3 presets, if none match,
        // the current set of presets must be custom).
        int currentPreset = CACHE_PRESET_CUSTOM;
        // The array of presets to check
        int[] presetChecks = {
            CACHE_PRESET_SMALL, CACHE_PRESET_MEDIUM, CACHE_PRESET_LARGE
        };
        int preset;
        for (preset=0; preset<presetChecks.length; preset++) {
            boolean matches = true;
            for (int i=0; i<CACHE_PRESET_SIZES.length; i++) {
                if (dbCacheManager.getMaxSize(CACHE_TYPES[i]) != CACHE_PRESET_SIZES[i][preset]) {
                    matches = false;
                }
            }
            if (matches) {
                currentPreset = preset;
                break;
            }
        }
        return currentPreset;
    }
%>
 
<%@ include file="include/global.jsp" %>
 
<%	// Load up the DbCacheManager first. Starty by getting the underlying
    // DbForumFactory
	DbForumFactory dbForumFactory = null;
	try {
		dbForumFactory = (DbForumFactory)(((DbForumFactoryProxy)forumFactory).getProxiedForumFactory());
	} catch (Exception e) { }
	
	// Get the cache manager
	DatabaseCacheManager cacheManager = dbForumFactory.getCacheManager();
    
    // get parameters
	boolean setCacheSize = ParamUtils.getBooleanParameter(request,"setCacheSize");
    boolean custom = ParamUtils.getBooleanParameter(request,"custom");
    int cachePreset = ParamUtils.getIntParameter(request,"cachePreset",getCurrentPreset(cacheManager));
    // set the cache size if requested
    if (setCacheSize) {
        if (cachePreset == CACHE_PRESET_CUSTOM) {
            custom = true;
            session.setAttribute("cache.customMode","true");
        }
        else {
            session.removeAttribute("cache.customMode");
            for (int i=0; i<CACHE_TYPES.length; i++) {
                cacheManager.setMaxSize(CACHE_TYPES[i], CACHE_PRESET_SIZES[i][cachePreset]);
            }
            response.sendRedirect("editCache.jsp?cachePreset=" + cachePreset);
            return;
        }
    }
    if ("true".equals((String)session.getAttribute("cache.customMode"))) {
        cachePreset = CACHE_PRESET_CUSTOM;
    }
    if (cachePreset == CACHE_PRESET_CUSTOM) {
        custom = true;
    }
    boolean setCustom = ParamUtils.getBooleanParameter(request,"setCustom");
    // custom cache values (in K)
    int customForumSize = ParamUtils.getIntParameter(request,"custom"+DatabaseCacheManager.FORUM_CACHE+"K",-1);
    int customThreadSize = ParamUtils.getIntParameter(request,"custom"+DatabaseCacheManager.THREAD_CACHE+"K",-1);
    int customMessageSize = ParamUtils.getIntParameter(request,"custom"+DatabaseCacheManager.MESSAGE_CACHE+"K",-1);
    int customUserSize = ParamUtils.getIntParameter(request,"custom"+DatabaseCacheManager.USER_CACHE+"K",-1);
    int customUserPermSize = ParamUtils.getIntParameter(request,"custom"+DatabaseCacheManager.USER_PERMS_CACHE+"K",-1);
    int customGroupSize = ParamUtils.getIntParameter(request,"custom"+DatabaseCacheManager.GROUP_CACHE+"K",-1);
    
    // Check for the "cutom" value in the session
    
    // set the custom sizes
    if (custom && setCustom) {
        if (customForumSize > -1) {
            cacheManager.setMaxSize(DatabaseCacheManager.FORUM_CACHE, customForumSize*1024);
        }
        if (customThreadSize > -1) {
            cacheManager.setMaxSize(DatabaseCacheManager.THREAD_CACHE, customThreadSize*1024);
        }
        if (customMessageSize > -1) {
            cacheManager.setMaxSize(DatabaseCacheManager.MESSAGE_CACHE, customMessageSize*1024);
        }
        if (customUserSize > -1) {
            cacheManager.setMaxSize(DatabaseCacheManager.USER_CACHE, customUserSize*1024);
        }
        if (customUserPermSize > -1) {
            cacheManager.setMaxSize(DatabaseCacheManager.USER_PERMS_CACHE, customUserPermSize*1024);
        }
        if (customGroupSize > -1) {
            cacheManager.setMaxSize(DatabaseCacheManager.GROUP_CACHE, customGroupSize*1024);
        }
        response.sendRedirect("editCache.jsp");
        return;
    }
%>

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

<p>

<%  // Title of this page and breadcrumbs
    String title = "Edit Cache Settings";
    String[][] breadcrumbs = {
        {"Main", "main.jsp"},
        {"Cache Settings", "cache.jsp"},
        {"Edit Cache Settings", "editCache.jsp"}
        
    };
%>
<%@ include file="include/title.jsp" %>

<font size="-1">
Customize your cache settings using the forms below.
You should never set your cache sizes to total more than half of your
total JVM memory. By default, most JVMs are configured to use 64 MB of memory,
although the default may vary or have been changed on your system.
<p>
Setting the caches too large will not result in increased performance. Instead,
tune your cache larger only as long as it results in greater effectivness over 
time (as measured on the <a href="cache.jsp">main cache</a> page).
</font>

<p>

<font size="-1"><b>Cache Presets</b></font>
<form action="editCache.jsp">
<input type="hidden" name="setCacheSize" value="true">
<ul>
<%  for (int i=0; i<CACHE_PRESET_NAMES.length; i++) {
        String checked = "";
        if (cachePreset == i) {
            checked = " checked";
        }
%>
<input type="radio" name="cachePreset" value="<%= i %>" id="rb0<%= i %>"<%= checked %>>
<font size="-1"><label for="rb0<%= i %>"><b><%= CACHE_PRESET_NAMES[i] %></b> -- <%= CACHE_PRESET_DESCRIPTIONS[i] %></label></font>
<p>
<%  } %>
<input type="submit" value="Save Settings">
</ul>
</form>

<p>

<font size="-1"><b>Cache Sizes</b></font>
<form action="editCache.jsp" name="cacheForm">
<input type="hidden" name="custom" value="<%= custom %>">
<input type="hidden" name="setCustom" value="true">
<ul>
<table bgcolor="<%= tableBorderColor %>" cellpadding="0" cellspacing="0" border="0" width="300">
<tr><td>
<table bgcolor="<%= tableBorderColor %>" cellpadding="3" cellspacing="1" border="0" width="100%">
<tr bgcolor="#eeeeee">
    <td align="center" width="50%"><font face="verdana" size="-2"><b>CACHE NAME</b></font></td>
    <td align="center" width="50%" colspan="2"><font face="verdana" size="-2"><b>SIZE</b></font></td>
</tr>
<%  for (int i=0; i<CACHE_TYPES.length; i++) {
        int type = CACHE_TYPES[i];
        double sizeMB = (double)cacheManager.getMaxSize(type)/(1024*1024);
        double sizeK = (double)cacheManager.getMaxSize(type)/(1024);
%>
<tr bgcolor="#ffffff">
    <td width="50%">
    <font size="-1"><b><%= CACHE_NAMES[i] %></b></font>
    </td>
    <td width="25%">
    <%  if (custom) { %>
    <input type="text" name="custom<%= CACHE_TYPES[i] %>K" size="6" value="<%= kFormat.format(sizeK) %>" maxlength="6"><font size="-1">K</font>
    <%  } else { %>
    <font size="-1"><%= kFormat.format(sizeK) %> K</font>
    <%  } %>
    </td>
    <td width="25%">
    <font size="-1">(<%= mbFormat.format(sizeMB) %> MB)</font>
    </td>
</tr>
<%  } %>
</table>
</td></tr>
</table>
<%  if (custom) { %>
    <p>
    <input type="submit" value="Save Custom Settings">
<%  } %>
<p>
<font size="-1"><i>Note: 1 MB = 1024 K</i></font>
</ul>
</form>

<p>

<center>
<form action="cache.jsp">
<input type="submit" value="Return to Cache Page">
</form>
</center>

<p>

<%@ include file="include/footer.jsp" %>

