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

package oracle.portal.provider.v1.http;
import oracle.portal.provider.v1.PortletRendererUtil;
import oracle.portal.provider.v1.PortletRenderRequest;
import oracle.portal.provider.v1.PortletException;
import oracle.portal.provider.v1.ExternalPrincipal;
import oracle.portal.provider.v1.AccessControlException;
import oracle.portal.provider.v1.ProviderSession;
import oracle.portal.utils.v1.MultiByteEncoder;
import java.util.Enumeration;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * BasicURLRenderer does the following things :
 * <ul><li> Get the external application from provider.</li>
 * <li> Construct a query string based on additional fields defined in external
 * application</li>
 * <li> Append the query string to login URL.</li>
 * <li> Set this new URL as an attribute in PortletRenderer request's object
 * store.</li>
 * <li> Override getInlinePageUrl of super class to return the pageUrl set
 * in above step. </li>
 * </ul>
 */
public class BasicURLRenderer extends URLRenderer
{
    /**
     * Over-ridden method returns the attribute value of pageUrl, set through 
     * pr.setAttribute method.
     */
    protected String getPageUrl(PortletRenderRequest pr, int debugLevel)
        throws PortletException
    {
        //Check if we are getting any request for inline rendering.
        String inlinePageUrl = 
            pr.getQualifiedParameter(UrlServicesUtils.PAGE_URL);
              
        if(inlinePageUrl != null)
        {
            //Delegate rendering to super class as we don't have anything to 
            //do over here.
            return inlinePageUrl;
        }
      
        BasicURLProvider provider = ((BasicURLProvider)pr.getProvider());
        UrlServicesUtils.logMessage(provider, 
            "BasicURLRenderer : Entering renderBody ...", debugLevel);
        ProviderSession pSession = pr.getUser().getSession();
        ExternalPrincipal externalApp = null;
        if(pSession != null)
        {
            externalApp = (ExternalPrincipal) 
                pSession.getAttribute(UrlServicesUtils.EXTERNAL_APP);
        }

        if(externalApp == null)
        {
            String msg = 
                "Provider not associated with any external application." +
                "Please see provider log for more details.";
            try
            {
                PrintWriter out = pr.getWriter();
                out.println("<FONT color=\"#FF0000\" face=\"arial,helvetica\">");
                out.println("<CENTER><B>Error : </B></FONT>");
                out.println("<FONT face=\"arial,helvetica\">" + msg + "</CENTER>");
            }
            catch(IOException ie)
            {
                UrlServicesUtils.logMessage(provider, 
                    "IOException in BasicURLRenderer.", debugLevel);
            }
            return null;
        }
        
        String user = externalApp.getName();
        String password = externalApp.getPassword();
        
        StringBuffer queryString = new StringBuffer();
        Enumeration fieldNames = externalApp.getFields(); 
        if (fieldNames != null) 
        { 
            //Get the optional field names and their corresponding values.
            //Check if they exist.
            String fieldName = null; 
            String fieldValue = null; 
            boolean firstTime = true;
            while (fieldNames.hasMoreElements()) 
            { 
                fieldName = (String)fieldNames.nextElement(); 
                UrlServicesUtils.logMessage(provider,
                    "BasicURLRenderer : field name --- : " + fieldName,
                    debugLevel); 
                fieldValue = externalApp.getField(fieldName); 
                UrlServicesUtils.logMessage(provider,
                    "BasicURLRenderer : field value --- : " + fieldValue,
                    debugLevel); 
                if(!firstTime)
                {
                  queryString.append("&"); 
                }
                else
                {
                  firstTime = false;
                }
                queryString.append(
                    makeNVPair(fieldName, 
                               fieldValue, 
                               pr.getURLCharSet()));
            } 
        }
        String tPageURL = 
          PortletRendererUtil.parameterizeLink(getPageUrl(), queryString.toString());
        return tPageURL;
    }
}
