package org.apache.jetspeed.modules.actions.portlets;

import org.apache.jetspeed.portal.portlets.VelocityPortlet;

// Turbine stuff
import org.apache.turbine.util.Log;
import org.apache.turbine.util.RunData;

// Velocity Stuff
import org.apache.velocity.context.Context;


/**
 * 
 * 
 * <p>Don't call it from the URL, the Portlet and the Action are automatically
 * associated through the registry PortletName
 * 
 * @author <a href="mailto:joshhone@yahoo.com">Josh Hone</a>
 */
public class PermTestAction extends VelocityPortletAction
{

    /** 
     * Default behavior is to do the same as normal context.
     */
    protected void buildMaximizedContext( VelocityPortlet portlet, 
                                          Context context,
                                          RunData rundata )
    {
        buildNormalContext( portlet, context, rundata);
        
        String test = (String) rundata.getUser().getPerm("test");

        if (test == null)
        {
            rundata.getUser().setPerm("test",test);
            test = "This test object was set in buildMaximizedContent since the object returned with getPerm() is null";
        }

        context.put("test", test);
    }

    /** 
     * Subclasses should override this method if they wish to
     * provide their own customization behavior.
     * Default is to use Portal base customizer action
     */
    protected void buildConfigureContext( VelocityPortlet portlet, 
                                          Context context,
                                          RunData rundata )
    {

        buildNormalContext( portlet, context, rundata);
        
        setTemplate(rundata, "permtest-customize");
        
    }

    /** 
     * Subclasses must override this method to provide default behavior 
     * for the portlet action
     */
    protected void buildNormalContext( VelocityPortlet portlet, 
                                       Context context,
                                       RunData rundata )
    {
        context.put("test",(String) rundata.getUser().getPerm("test"));
    }

    public void doUpdate(RunData data, Context context)
    {
        String test = data.getParameters().getString("test");
        
        if (test!=null)
        {
            data.getUser().setPerm("test", test);
            context.put("test",test);
            context.put("message", "Test successfully updated");
        }
        else
        {
            context.put("message", "You must specify a new test object");
        }
    }
}
