/**********************************************************************************
 * $URL: https://source.sakaiproject.org/svn/memory/tags/sakai_2-3-1/memory-tool/tool/src/java/org/sakaiproject/memory/tool/MemoryAction.java $
 * $Id: MemoryAction.java 9917 2006-05-25 03:07:52Z ggolden@umich.edu $
 ***********************************************************************************
 *
 * Copyright (c) 2003, 2004, 2005, 2006 The Sakai Foundation.
 * 
 * Licensed under the Educational Community License, Version 1.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at
 * 
 *      http://www.opensource.org/licenses/ecl1.php
 * 
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License.
 *
 **********************************************************************************/

package org.sakaiproject.memory.tool;

import org.sakaiproject.authz.cover.SecurityService;
import org.sakaiproject.cheftool.Context;
import org.sakaiproject.cheftool.JetspeedRunData;
import org.sakaiproject.cheftool.RunData;
import org.sakaiproject.cheftool.VelocityPortlet;
import org.sakaiproject.cheftool.VelocityPortletPaneledAction;
import org.sakaiproject.cheftool.api.Menu;
import org.sakaiproject.event.api.SessionState;
import org.sakaiproject.memory.api.MemoryPermissionException;
import org.sakaiproject.memory.cover.MemoryService;
import org.sakaiproject.util.ResourceLoader;

/**
 * <p>
 * MemoryAction is the Sakai memory tool.
 * </p>
 */
public class MemoryAction extends VelocityPortletPaneledAction
{

	/** Resource bundle using current language locale */
	private static ResourceLoader rb = new ResourceLoader("admin");

	/**
	 * build the context
	 */
	public String buildMainPanelContext(VelocityPortlet portlet, Context context, RunData rundata, SessionState state)
	{
		context.put("tlang", rb);
		
		// if not logged in as the super user, we won't do anything
		if (!SecurityService.isSuperUser())
		{
			return (String) getContext(rundata).get("template") + "_noaccess";
		}

		// put $action into context for menus, forms and links
		context.put(Menu.CONTEXT_ACTION, state.getAttribute(STATE_ACTION));

		// put the current available memory into the context
		context.put("memory", Long.toString(MemoryService.getAvailableMemory()));

		// status, if there
		if (state.getAttribute("status") != null)
		{
			context.put("status", state.getAttribute("status"));
			state.removeAttribute("status");
		}

		return (String) getContext(rundata).get("template");

	} // buildMainPanelContext

	/**
	 * doNew called when "eventSubmit_doReset" is in the request parameters to reset memory useage (caches)
	 */
	public void doReset(RunData data, Context context)
	{
		SessionState state = ((JetspeedRunData) data).getPortletSessionState(((JetspeedRunData) data).getJs_peid());

		try
		{
			MemoryService.resetCachers();
		}
		catch (MemoryPermissionException e)
		{
			state.setAttribute("message", rb.getString("memory.notpermis"));
		}

	} // doReset

	/**
	 * doNew called when "eventSubmit_doStatus" is in the request parameters to reset memory useage (caches)
	 */
	public void doStatus(RunData data, Context context)
	{
		SessionState state = ((JetspeedRunData) data).getPortletSessionState(((JetspeedRunData) data).getJs_peid());

		state.setAttribute("status", MemoryService.getStatus());

	} // doReset

} // MemoryAction

