001 package biz.wedoit4u;
002
003 import java.util.Collection;
004 import java.util.Date;
005 import java.util.Iterator;
006
007 import javax.ejb.FinderException;
008 import javax.naming.NamingException;
009 import javax.servlet.http.HttpServletRequest;
010 import javax.servlet.http.HttpSession;
011
012 import biz.wedoit4u.databeans.*;
013
014 import biz.wedoit4u.ejb.session.SessionBeanProvider;
015 import biz.wedoit4u.ejb.session.DateFormatter;
016
017 /**
018 * A class that is used to store sessional information for a
019 * partner CMA user. This class also contains utility methods that are
020 * used to abstract business logic away from the presentation layer.
021 *
022 * <p>Copyright 2004, Rakesh Vidyadharan and wedoit4u.biz</p>
023 *
024 * @author Rakesh Vidyadharan 22<sup><small>nd</small></sup> February 2004
025 *
026 * @version $Id: PartnerSession.java,v 1.2 2004/05/26 11:42:30 rakesh Exp $
027 */
028 public final class PartnerSession extends CMASession
029 {
030 /**
031 * A <code>flag</code> used to indicate that a login session is
032 * valid.
033 */
034 private boolean loggedOn = false;
035
036 /**
037 * The <code>partners.partner_id</code> column value. This is used
038 * to store and provide access to the <code>partnerId</code> for the
039 * currently logged in user.
040 */
041 private int partnerId;
042
043 /**
044 * The web session for the user using the CMA.
045 */
046 private HttpSession httpSession = null;
047
048 /**
049 * The default constructor. Does nothing special.
050 */
051 public PartnerSession() {}
052
053 /**
054 * Log the partner in to the CMA.
055 *
056 * @param request - The request from the client that
057 * contains the partner username and password information from
058 * the form POST.
059 * @param session - The {@link #httpSession} instance to set.
060 * @throws CMAException - If system errors are encountered while
061 * attempting to log the user on, or if the username/password
062 * combination are invalid.
063 */
064 public void login( HttpServletRequest request, HttpSession session )
065 throws CMAException
066 {
067 PartnerBean pb = PartnerBean.getInstance( request.getParameter( "username" ), request.getParameter( "password" ) );
068 this.partnerId = pb.getPartnerId();
069 setHttpSession( session );
070 loggedOn = true;
071 }
072
073 /**
074 * Log the partner out of the CMA.
075 */
076 public void logout()
077 {
078 loggedOn = false;
079 if ( httpSession != null ) httpSession.invalidate();
080 }
081
082 /**
083 * Return an <code>Array</code> of java bean instances of
084 * type {@link biz.wedoit4u.databeans.PartnerStyleBean} that are
085 * associated with the specified <code>partner_id</code>.
086 *
087 * @see biz.wedoit4u.databeans.PartnerStyleBean#findByPartner( int )
088 * @return PartnerStyleBean[] - The array of java bean instances.
089 * @throws CMAException - If Naming or Finder exceptions are
090 * encountered while fetching the collection of java beans.
091 */
092 public PartnerStyleBean[] getPartnerStyleList()
093 throws CMAException
094 {
095 Collection collection = PartnerStyleBean.findByPartner( partnerId );
096 PartnerStyleBean[] styleClassArray =
097 new PartnerStyleBean[ collection.size() ];
098
099 Iterator iterator = collection.iterator();
100 for ( int i = 0; iterator.hasNext(); ++i )
101 {
102 styleClassArray[i] = (PartnerStyleBean) iterator.next();
103 }
104
105 return styleClassArray;
106 }
107
108 /**
109 * Create/edit a <code>customer_types</code> record. If the specified
110 * <code>customer_type_id</code> is <code>0</code> then a new record
111 * is created. The the specified <code>customer_type_id</code>
112 * exists in the database, then the associated record is updated,
113 * otherwise, a new record is created with the specified value.
114 *
115 * @param request - The request from the client that
116 * contains the form elements with all the value for the database
117 * fields.
118 * @return CustomerTypeBean - The appropriate instance of the java
119 * bean that represents a record in the table.
120 * @throws CMAException - If database errors are encountered while
121 * creating/editing the database record.
122 */
123 public CustomerTypeBean editCustomerType( HttpServletRequest request )
124 throws CMAException
125 {
126 // Fetch all the form parameters
127 int customerTypeId = Integer.parseInt( request.getParameter( "customerTypeId" ) );
128 String type = request.getParameter( "type" );
129 String description = request.getParameter( "description" );
130 if ( description.equals( "" ) ) description = null;
131
132 return super.editCustomerType( customerTypeId, type, description, partnerId );
133 }
134
135 /**
136 * Returns {@link #loggedOn}.
137 *
138 * @return boolean - The value/reference of/to loggedOn.
139 */
140 public final boolean getLoggedOn()
141 {
142 return loggedOn;
143 }
144
145 /**
146 * Returns {@link #partnerId}.
147 *
148 * @return int - The value/reference of/to partnerId.
149 */
150 public final int getPartnerId()
151 {
152 return partnerId;
153 }
154
155 /**
156 * Returns {@link #httpSession}.
157 *
158 * @return HttpSession - The value/reference of/to httpSession.
159 */
160 public final HttpSession getHttpSession()
161 {
162 return httpSession;
163 }
164
165 /**
166 * Set {@link #httpSession}.
167 *
168 * @param httpSession - The value to set.
169 */
170 public final void setHttpSession( HttpSession httpSession )
171 {
172 this.httpSession = httpSession;
173 }
174 }