<%@ page contentType="text/html;charset=UTF-8" %>
<%@page import="java.io.*, java.util.*" %>
<%@page import="oracle.portal.provider.v2.*" %>
<%@page import="oracle.portal.provider.v2.render.*" %>
<%@page import=
          "oracle.portal.provider.v2.render.http.HttpPortletRendererUtil" %>
<%@page import="oracle.portal.provider.v2.url.UrlUtils" %>
<%@page import="oracle.portal.provider.v2.http.HttpCommonConstants" %>

<%!
  private static final String PROV_KEY_TO_CUST_INFO_FILE_NAME = 
                                  "providerKeyToSubscriberInfoMap.ser";
  private static Hashtable provKeyToSubscriberInfoMap = null;
  private static String completeMapFilePath = null;
  private static File mapFile = null;
%>

<%
  PortletRenderRequest portletRequest = (PortletRenderRequest)
        request.getAttribute(HttpCommonConstants.PORTLET_RENDER_REQUEST);
  ProviderInstance provider = portletRequest.getProviderInstance();
  if (provider != null && mapFile== null)
  {
    completeMapFilePath = provider.getProviderContext().getRepositoryPath();

    if (completeMapFilePath.charAt(completeMapFilePath.length() - 1) 
                                                  == File.separatorChar)
    {
      completeMapFilePath = completeMapFilePath 
                                + PROV_KEY_TO_CUST_INFO_FILE_NAME;
    }
    else
    {
      completeMapFilePath = completeMapFilePath 
                               + File.separatorChar 
                               + PROV_KEY_TO_CUST_INFO_FILE_NAME;
    }
    mapFile = new File(completeMapFilePath);
  }
%>

<%
    // The form submit URL refers to the current Portal page.  All portlets
    // on this page share this URL.  This means that the per portlet parameters
    // are in the same request.  Portlets must ensure that its paramerters don't
    // collide either with other portlets or other instances of itself.  This
    // is generally accomplished by using "fully-qualified" parameter names. A
    // fully-qualified parameter name prepends the (unique) portlet reference to
    // the parameter.  The JPDK provides a utility to accomplish this.

    String personNameText  = "personName";
    String companyNameText = "companyName";

    String personName 
               = HttpPortletRendererUtil
                     .portletParameter(request, personNameText);
    String companyName  
              = HttpPortletRendererUtil
                     .portletParameter(request, companyNameText);
    String provKeyFormSubmit 
              = HttpPortletRendererUtil
                     .portletParameter(request, "provKeyFormSubmit");
%>

<%!
  private static void loadProviderKey2SubscriberInfoMap(File mapFile)
  {
    FileInputStream fis = null;
    ObjectInputStream ois = null;

    try
    {
      fis = new FileInputStream(mapFile);
      ois = new ObjectInputStream(fis);
      provKeyToSubscriberInfoMap = (Hashtable) ois.readObject();
      fis.close();
      ois.close();
    }
    catch (Exception e)
    {
      // Handle the exeception
      // Unable to Load map.
      // Create the memory map.
      createMemoryMap();
    }
    finally
    {
      try
      {
        if (fis != null) fis.close();
        if (ois != null) ois.close();
      }
      catch(IOException e)
      {
        // Handle the exception
      }
    }
  }

  private static void storeProviderKey2SubscriberInfoMap(File mapFile)
  {
    FileOutputStream fos = null;
    ObjectOutputStream oos = null;

    try
    {
      fos = new FileOutputStream(mapFile);
      oos = new ObjectOutputStream(fos);
      oos.writeObject(provKeyToSubscriberInfoMap);
      oos.flush();
      fos.close();
      oos.close();
    }
    catch (Exception e)
    {
      // Handle the exception
      // Unable to Store map.
    }
    finally
    {
      try
      {
        if (fos != null) fos.close();
        if (oos != null) oos.close();
      }
      catch(IOException e)
      {
        // Handle the exceptions.
      }
    }
  }

  private static void createMemoryMap()
  {
    provKeyToSubscriberInfoMap = new Hashtable();
  }

  private String generateProviderKey(String persName, String compName)
  {
    String firstPartOfName = (persName.indexOf(" ") != -1) 
                                 ? persName.substring(0, persName.indexOf(" "))
                                 : persName ;
    String firstPartOfCompName = (compName.indexOf(" ") != -1) 
                                 ? compName.substring(0, compName.indexOf(" ")) 
                                 : compName ;
    return firstPartOfName 
              + firstPartOfCompName 
              + Math.round(Math.random() * 1000000);
  }
%>

<!-- Output the HTML content -->

<center>
<table WIDTH="70%" >
<tr><td><b>This portlet generates a Provider Key(Subscriber ID) 
given a name and the company name.
</b></td></tr>
</table>
</center>
<center>
<form name="<%= UrlUtils.htmlFormName(portletRequest,null) %>" 
  method="POST" 
  action="<%= UrlUtils.htmlFormActionLink(
                               portletRequest,UrlUtils.PAGE_LINK) %>">
<%= UrlUtils.htmlFormHiddenFields(portletRequest,UrlUtils.PAGE_LINK) %>
</center>
<center>
<table>
<tr>
<td><b>Person Name :</b></td>
<td><input type="text" size="40" name="<%= personName %>" value=""></td>
</tr>
<tr>
<td><b>Company Name :&nbsp;</b></td>
<td><input type="text" size="40" name="<%= companyName %>" value=""></td>
</tr>
</table></center>
<center>
<p><input TYPE=submit name="<%= provKeyFormSubmit %>" Value="Submit"></form></center>
<br>
<%
PortletRenderRequest prr = 
    (PortletRenderRequest) 
        request.getAttribute(HttpCommonConstants.PORTLET_RENDER_REQUEST);

String personNameStr = prr.getQualifiedParameter(personNameText);
String companyNameStr = prr.getQualifiedParameter(companyNameText);
if ((personNameStr == null)&& (companyNameStr == null)) {
%>
   <b>Enter Your Name and your company name and 
Hit Submit to get your Provider Key.</b>
<%
} else {
%>
<center>
<%
if (provKeyToSubscriberInfoMap == null)
{
  loadProviderKey2SubscriberInfoMap(mapFile);
}
//String providerKey = personNameStr + companyNameStr;
String  providerKey = generateProviderKey(personNameStr, companyNameStr);
provKeyToSubscriberInfoMap.put(providerKey, 
                               new String[] {personNameStr,companyNameStr });
storeProviderKey2SubscriberInfoMap(mapFile);
%>
<b> Your Provider key is <h2> <%= providerKey %></h2> <b>
</center>
<%
}
%>


