/*
 *
 * Portlet which displays the member id box for the CSR to lookup information.
 */

package prototype;

import java.io.IOException;

import org.apache.jetspeed.portlet.*;
import org.apache.jetspeed.portlet.event.*;

/**
 * @author jjf3692
 *
 */
public class MemberIDPortlet extends PortletAdapter implements ActionListener {

	public static final String ACTION     = "prototype.MemberIDAction";
	public static final String ACTION_URI = "prototype.MemberIDActionURI";
	public static final String TEXT       = "prototype.MemberIDText";	// Parameter name for member id string
	public static final String SUBMIT     = "prototype.MemberIDSubmit";// Parameter name for form submit	
	public static final String SESSION_BEAN     = "prototype.MemberSessionBean";    // Bean name for the portlet session
	
	/**
	 * Shows the portlet information in the web page.
	 * 
	 * @see org.apache.jetspeed.portlet.PortletAdapter#doView(PortletRequest, PortletResponse)
	 */
	public void doView(PortletRequest request, PortletResponse response) throws PortletException, IOException {
		//Initialize the action URI so that messages can be passed
		PortletURI messageActionURI = response.createURI();
		messageActionURI.addAction(ACTION);
		request.setAttribute(ACTION_URI, messageActionURI.toString());
		
		// Invoke the JSP to render        		
		getPortletConfig().getContext().include("/prototype/jsp/MemberIDView.jsp", request, response);
		
	}

	/**
	 * Called when an action occurs on the portlet webpage (ie. form is submitted).
	 * 
	 * @see org.apache.jetspeed.portlet.event.ActionListener#actionPerformed(ActionEvent)
	 */
	public void actionPerformed(ActionEvent event) throws PortletException {
		try{
			MemberSessionBean sessionBean = getSessionBean(event.getRequest());
			//Set the member id information for the member
			sessionBean.setMemberID((String)event.getRequest().getParameter(TEXT));
		}
		catch(Exception e){
			
		}
		if (ACTION.equals(event.getActionString())){
			//Since there's only one other portlet on the page, we can get by with just a broadcast of the message
			getPortletConfig().getContext().send(null, new DefaultPortletMessage((String)event.getRequest().getParameter(TEXT)));
		}
	
			
			
	}
	
	/**
	 * Get SessionBean.
	 * 
	 * @param request PortletRequest 	current HTTP request object
	 * @return MemberSessionBean 		The stored information about the member.
	 */
	private MemberSessionBean getSessionBean(PortletRequest request) {
		User user = request.getUser();
		MemberSessionBean sessionBean = null;
		//If the user object does exist (the user is logged in)
		if( user != null ) {
			try{
				//Get the stored member information
				sessionBean = (MemberSessionBean)user.getAttribute(SessionKeys.MEMBER);
			}
			catch( Exception e ){
				//Debug for the exceptions
			}
		}
		//If the user does not exist (not yet logged in)
		if( sessionBean == null ) {
			//Create a new bean and store it in the user object
			sessionBean = new MemberSessionBean();
			user.setAttribute(SESSION_BEAN,sessionBean);
		}
		return sessionBean;
	}
}

/*
 *	$Log: MemberIDPortlet.java,v $
 *	Revision 1.13  2004/05/11 21:35:52  jjf3692
 *	Commented files for Excellus delivery.
 *	
 *	Revision 1.12  2004/04/11 18:27:19  jjf3692
 *	Commented system.out statements to clean up output.
 *	
 *	Revision 1.11  2004/04/10 19:12:48  cxh2620
 *	finding null session bug
 *	
 *	Revision 1.10  2004/04/08 22:48:55  jjf3692
 *	Updated Claim Portlets.
 *	Cleaned up debugging output.
 *	Current errors are with session passing in the Claims portlets.
 *	
 *	Revision 1.9  2004/04/03 17:57:22  jjf3692
 *	Changed jsp page name to correct name.
 *	
 *	Revision 1.8  2004/04/02 18:43:07  cxh2620
 *	*** empty log message ***
 *	
 *	Revision 1.7  2004/03/28 20:44:13  jjf3692
 *	*** empty log message ***
 *	
 *	Revision 1.6  2004/03/28 20:04:03  jjf3692
 *	Added system.out
 *	
 *	Revision 1.5  2004/03/28 20:00:52  cxh2620
 *	user session not portlet session
 *	
 *	Revision 1.4  2004/03/27 20:31:11  jjf3692
 *	*** empty log message ***
 *	
 *
 */