/*  Copyright (c) 2000, 2002 by Oracle Corporation. All Rights Reserved. */

package oracle.portal.provider.v1.http; 
import java.io.IOException; 
import java.io.PrintWriter; 
import java.util.Date;
import oracle.portal.provider.v1.PortletRenderRequest; 
import oracle.portal.provider.v1.PortletRendererUtil; 
import oracle.portal.provider.v1.PortletException; 
import oracle.portal.provider.v1.PortletParameter; 
import oracle.portal.provider.v1.URLPortletParameter; 
import oracle.portal.provider.v1.AccessControlException; 
import oracle.portal.provider.v1.ProviderSession;
import oracle.portal.provider.v1.ProviderUser;
import oracle.portal.provider.v1.PortletReference;
import oracle.portal.provider.v1.Portlet;

/**
 * Class for rendering edit screen of URLPortlet.
 * Can customize portlet title and portlet parameters if any.
 *
 */
public class URLEditRenderer extends BaseManagedRenderer 
{ 
    String mQualifyParams = UrlServicesUtils.FALSE; 
    public static final String PORTLET_TITLE = "portlet_title"; 
    public static final String LAST_EDITED_TIME = "lasteditedtime";
    
    /**
     * Overrides base class's renderBody method. Displays customization screen
     * for customizing portlet title as well as portlet parameters, if any.
     * @param PortletRenderRequest pr
     *
     */
    public void renderBody(PortletRenderRequest pr) 
    throws PortletException, AccessControlException 
    { 
        try  
        { 
            PortletReference pref = pr.getPortletReference();
            Portlet portlet = pref.getPortlet();
            
            
            // Retrieve the name of the parameter used to represent the user
            // action taken on the edit form. This name was assigned by
            // RenderManager but can be overridden by the portlet developer.
            String actionParam = PortletRendererUtil.getEditFormParameter(pr); 
            
            // Now retrieve the value of that action parameter.
            String action = pr.getParameter(actionParam); 
            
            // Retrieve the data object for later use.
            URLPersonalizationObject data = URLRenderer.getCustomizationData(pr);
            // If the action was non null, then we have to save our newly
            // customized data. Otherwise, we have to render the edit form.
            if (action != null) 
            { 
                // Write the personalized values into the data object.
                //Portlet Title
                data.setPortletTitle(pr.getParameter(PORTLET_TITLE)); 
                String useQualifiedParams = 
                  pr.getParameter(URLPersonalizationObject.QUALIFY_PARAMETERS); 
                if (useQualifiedParams != null) 
                useQualifiedParams = UrlServicesUtils.TRUE; 
                else  
                useQualifiedParams = UrlServicesUtils.FALSE; 
                data.putString(URLPersonalizationObject.QUALIFY_PARAMETERS, 
                useQualifiedParams); 
                PortletParameter params[] = pr.getPortlet().getInputParameters(); 
                for (int i = 0; params != null && i < params.length; i++) 
                { 
                    String paramValue = pr.getParameter(params[i].getName()); 
                    if (paramValue == null) 
                    {
                        paramValue = ""; 
                    }
                    data.putString( 
                    URLPersonalizationObject.PORTLET_PARAMETER_VALUE + (i + 1), 
                    paramValue); 
                } 
                
                Date lastEdited = getLastEditTime(data);
                if(lastEdited != null)
                {
                    lastEdited.setTime(System.currentTimeMillis());
                    data.putString(LAST_EDITED_TIME, ""+lastEdited.getTime());
                }
                // Submit the data object back to the PersonalizationManager
                // for storing.
                PortletRendererUtil.submitEditData(pr, data); 
            } 
            else  
            { 
                // Get a writer for the edit form.
                PrintWriter out = pr.getWriter(); 
                
                // Render the form.
                out.println("<TABLE BORDER=\"0\" width = \"90%\"" + 
                " cellspacing=\"0\" cellpadding = \"5\" > "); 
                out.println("<TR><TD NOWRAP>Title</TD>"); 
                out.print("<TD><INPUT TYPE=\"text\" NAME = \""); 
                out.print(PORTLET_TITLE); 
                out.print("\" SIZE = \"30\" VALUE = \"" + data.getPortletTitle()); 
                out.println("\" </ TD ></ TR > "); 
                out.println("<TR><TD COLSPAN=\"2\" > "); 
                out.println("<FONT class=\"OraHeaderSubSub\" > "); 
                out.println("Customize Portlet Parameters :</FONT></TD></TR>"); 
                PortletParameter params[] = pr.getPortlet().getInputParameters(); 
                if (params != null) 
                { 
                    for (int i = 0; i < params.length; i++) 
                    { 
                        URLPortletParameter parameter = (URLPortletParameter)params[i]; 
                        out.print("<TR><TD>" + parameter.getDisplayName()); 
                        if (parameter.isMandatory()) 
                        {
                            out.println(" * "); 
                        }
                        out.println("</TD>"); 
                        out.println(); 
                        out.print("<TD><INPUT TYPE=\"text\" "); 
                        out.print("NAME=\""); 
                        out.print(parameter.getName()); 
                        out.print("\" SIZE = \"20\" VALUE = \""); 
                        String paramValue= 
                        data.getString( 
                        URLPersonalizationObject.PORTLET_PARAMETER_VALUE+ 
                        (i + 1)); 
                        if (paramValue == null) 
                        paramValue = ""; 
                        out.print(paramValue + "\" ></ TD ></ TR > "); 
                        out.println(); 
                    } 
                } 
                out.print("<TR><TD COLSPAN=\"2\" > "); 
                out.print("<INPUT TYPE=\"checkbox\" name = \"qualify_params\""); 
                String useQualifiedParams= 
                data.getString(URLPersonalizationObject.QUALIFY_PARAMETERS); 
                if ( (useQualifiedParams != null) &&
                  useQualifiedParams.equalsIgnoreCase(UrlServicesUtils.TRUE)) 
                {
                    out.print(" checked"); 
                }
                out.println("> Use Qualified Parameters</TD></TR>"); 
                out.println("</TABLE>"); 
            } 
        } 
        catch (IOException ioe) 
        { 
            throw new PortletException(ioe); 
        } 
    } 
    
    /**
     * Determines whether to use qualified parameters.
     * Qualifying a portlet parameter will be useful in avoiding name clash
     * between paramter names of different portlet present in the same page.
     * Parameter names will be hashed using a special function and it will
     * ensure that HTTP request header carries different parameter names/values
     * even if by mistake the user has used same paramter names.
     * @param flag true/false.
     */
    public void setQualifyParameters(String flag) 
    { 
        mQualifyParams = flag; 
    } 
    /**
     * Method for determining last time this portlet was edited. Fetches this
     * information from user's personalization data.
     * @param data Personlization Object
     */
    public static Date getLastEditTime(URLPersonalizationObject data)
    {
        if(data == null)
        {
            return null;
        }
        String sDate = data.getString(LAST_EDITED_TIME);
        // Make sure no one else tries to create the same object at the same
        // time
        Date lastEdited = null;
        if(sDate == null)
        {
            lastEdited = new Date();
        }
        else
        {
            lastEdited = new Date(Long.parseLong(sDate));
        }
        return lastEdited;
    }
    
} 