Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/clra/web/MemberTag.java


1   /*
2    * Copyright (c) Carnegie Lake Rowing Association 2002. All rights reserved.
3    * Distributed under the GPL license. See doc/COPYING.
4    * $RCSfile: MemberTag.java,v $
5    * $Date: 2003/02/26 03:38:46 $
6    * $Revision: 1.6 $
7    */
8   
9   package com.clra.web;
10  
11  import com.clra.member.MemberDBRead;
12  import com.clra.member.MemberException;
13  import com.clra.member.MemberSnapshot;
14  import java.io.IOException;
15  import java.util.*;
16  import javax.servlet.*;
17  import javax.servlet.http.*;
18  import javax.servlet.jsp.*;
19  import javax.servlet.jsp.tagext.*;
20  
21  /**
22   * MemberTag <b>member</b>, used to get the member who is currently
23   * logged in. This class is a thin wrapper around the MemberView class.
24   * <p>
25   * MemberTag Lib Descriptor
26   * <p><pre>
27   * &lt;name&gt;memberId&lt;/name&gt;
28   * &lt;tagclass&gt;com.clra.web.MemberIdTag&lt;/tagclass&gt;
29   * &lt;bodycontent&gt;empty&lt;/bodycontent&gt;
30   * &lt;info&gt;Gets a property of an authenticated member.&lt;/info&gt;
31   * </pre>
32   * Most properties are self-explanatory. Name, Address, Telephone and
33   * ID values require special names. The birth date is returned as
34   * "mm/dd/yyyy". If no property is specified, the default is "fullName".<ul>
35   * <li> PN_ACCOUNTNAME == "accountName" == getAccountName()</li>
36   * <li> PN_STREET1 == "street1" == getAddress().getStreet1()</li>
37   * <li> PN_STREET2 == "street2" == getAddress().getStreet2()</li>
38   * <li> PN_CITY == "city" == getAddress().getCity()</li>
39   * <li> PN_STATE == "state" == getAddress().getState()</li>
40   * <li> PN_ZIP == "zip" == getAddress().getZip()</li>
41   * <li> PN_BIRTH == "birthDate" == getBirthDate() [mm/dd/yyyy]</li>
42   * <li> PN_ACCOUNTTYPESTR == "accountTypeStr" == getAccountTypeStr()</li>
43   * <li> PN_EMAIL == "email" == getEmail()</li>
44   * <li> PN_MEMBERID == "memberId" == getId()</li>
45   * <li> PN_FULLNAME == "fullName" == getMemberName().getName()</li>
46   * <li> PN_LASTNAME == "lastName" == getMemberName().getLastName()</li>
47   * <li> PN_FIRSTNAME == "firstName" == getMemberName().getFirstName()</li>
48   * <li> PN_MIDDLENAME == "middleName" == getMemberName().getMiddleName()</li>
49   * <li> PN_SUFFIX == "suffix" == getMemberName().getSuffix()</li>
50   * <li> PN_EVENINGPHONE== "eveningPhone" == [get EVENING telephone]</li>
51   * <li> PN_DAYPHONE == "dayPhone" == [get DAY telephone]</li>
52   * <li> PN_OTHERPHONE == "otherPhone" == [get OTHER telephone]</li>
53   * </ul>
54   *
55   * @version $Id: MemberTag.java,v 1.6 2003/02/26 03:38:46 rphall Exp $
56   * @author <a href="mailto:rphall@pluto.njcc.com>Rick Hall</a>
57   */
58  
59  public class MemberTag extends TagSupport {
60  
61    public final static String PN_ACCOUNTNAME = "accountName";
62    public final static String PN_STREET1 = "street1";
63    public final static String PN_STREET2 = "street2";
64    public final static String PN_CITY = "city";
65    public final static String PN_STATE = "state";
66    public final static String PN_ZIP = "zip";
67    public final static String PN_BIRTH = "birthDate";
68    public final static String PN_ACCOUNTTYPESTR = "accountTypeStr";
69    public final static String PN_EMAIL = "email";
70    public final static String PN_MEMBERID = "memberId";
71    public final static String PN_FULLNAME = "fullName";
72    public final static String PN_LASTNAME = "lastName";
73    public final static String PN_FIRSTNAME = "firstName";
74    public final static String PN_MIDDLENAME = "middleName";
75    public final static String PN_SUFFIX = "suffix";
76    public final static String PN_EVENINGPHONE= "eveningPhone";
77    public final static String PN_DAYPHONE = "dayPhone";
78    public final static String PN_OTHERPHONE = "otherPhone";
79  
80    private String _key = Configuration.KEY_MEMBER;
81    private String _property = Configuration.MEMBER_PROPERTY;
82    private MemberView _member = null;
83  
84    /**
85     * Utility that checks for the MEMBER_ID key in the session context
86     * @return member id, or null if session attribute is not set.
87     */
88    public static MemberView getMemberFromKey( HttpSession session, String key )
89      throws JspException {
90  
91      // Preconditions
92      if ( session == null ) {
93        throw new JspException( "null session" );
94      }
95      if ( key == null || key.trim().length() == 0 ) {
96        throw new JspException( "invalid key" );
97      }
98  
99      MemberView retVal = (MemberView) session.getAttribute( key );
100     return retVal;
101   } // getMemberId(HttpSession)
102 
103   /**
104    * Utility that gets the authenticated user from the request context
105    * @return user account name
106    * @exception JspException if the current user is not authenticated
107    */
108   public static MemberView getMemberFromAuthenticatedUser(
109     HttpServletRequest request) throws JspException {
110 
111     // Precondition
112     if ( request == null ) {
113       throw new JspException( "null request" );
114     }
115 
116     String user = request.getRemoteUser();
117     if ( user == null || user.trim().length() == 0 ) {
118       String msg = MemberTag.class.getName() + ": "
119         + "INVALID: HttpServletRequest.getRemoteUser() == '" + user + "'";
120       throw new JspException( msg );
121     }
122 
123     MemberView retVal = null;
124     try {
125       MemberSnapshot ms = MemberDBRead.findMemberByAccountName(user);
126       retVal = new MemberView( ms );
127     }
128     catch( MemberException x ) {
129       throw new JspException( x.toString() );
130     }
131 
132     return retVal;
133   } // getMemberFromAuthenticatedUser(HttpServletRequest)
134 
135   /**
136    * Utility that gets a MemberView for the specified memberId.
137    * @return strMemberId a String that can be parsed to a valid
138    * integer corresponding to a key in the Member table.
139    * @exception JspException if the current user is not authenticated
140    */
141   public static MemberView getMemberFromMemberId( String strMemberId )
142     throws JspException {
143 
144     MemberView retVal = null;
145     try {
146       Integer memberId = new Integer( strMemberId );
147       retVal = getMemberFromMemberId(memberId);
148     }
149     catch( NumberFormatException x ) {
150       throw new JspException( x.toString() );
151     }
152 
153     return retVal;
154   } // getMemberFromMemberId(String)
155   
156   
157   /**
158    * Utility that gets a MemberView for the specified memberId
159    * @return memberId a non-null Integer corresponding to a key in the
160    * Member table
161    * @exception JspException if the current user is not authenticated
162    */
163   public static MemberView getMemberFromMemberId( Integer memberId)
164     throws JspException {
165 
166     // Precondition
167     if ( memberId == null ) {
168       throw new JspException( "null memberId" );
169     }
170 
171     MemberView retVal = null;
172     try {
173       MemberSnapshot ms = MemberDBRead.findMemberByMemberId(memberId);
174       retVal = new MemberView( ms );
175     }
176     catch( MemberException x ) {
177       throw new JspException( x.toString() );
178     }
179 
180     return retVal;
181   } // getMemberFromMemberId(Integer)
182   
183   /**
184    * Utility that inserts member info in the session context
185    */
186   public static void setMemberInSession(HttpSession session,
187     String key, MemberView member) throws JspException {
188 
189     // Preconditions
190     if ( session == null ) {
191       throw new JspException( "null session" );
192     }
193     if ( key == null || key.trim().length() == 0 ) {
194       throw new JspException( "invalid key" );
195     }
196     if ( member == null ) {
197       throw new JspException( "null member" );
198     }
199 
200     session.setAttribute( key, member );
201     return;
202   } // setMemberInSession(HttpSession,String,MemberView)
203 
204   public String getKey() {
205     return this._key;
206   }
207 
208   public void setKey() {
209     this._key = _key;
210   }
211 
212   public String getProperty() {
213     return this._property;
214   }
215 
216   public void setProperty() {
217     this._property = _property;
218   }
219 
220   /**
221    * Method called at end of Tag to output member property
222    *
223    * @return EVAL_PAGE
224    */
225   public final int doEndTag() throws JspException {
226 
227     // Check for named member
228     String key = this.getKey();
229     HttpSession session = this.pageContext.getSession();
230     this._member = getMemberFromKey( session, key );
231 
232     // If the key is not found, create it and store it
233     if ( this._member == null ) {
234       HttpServletRequest sr = (HttpServletRequest)this.pageContext.getRequest();
235       this._member = getMemberFromAuthenticatedUser( sr );
236       session.setAttribute( key, this._member );
237       }
238 
239     // Get the requested property
240     String property = this.getProperty();
241     String value = null;
242     if ( property.equalsIgnoreCase( PN_ACCOUNTNAME ) ) {
243       value = this._member.getAccountName();
244     }
245     else if ( property.equalsIgnoreCase( PN_STREET1 ) ) {
246       value = this._member.getAddress().getStreet1();
247     }
248     else if ( property.equalsIgnoreCase( PN_STREET2 ) ) {
249       value = this._member.getAddress().getStreet2();
250     }
251     else if ( property.equalsIgnoreCase( PN_CITY ) ) {
252       value = this._member.getAddress().getCity();
253     }
254     else if ( property.equalsIgnoreCase( PN_STATE ) ) {
255       value = this._member.getAddress().getState();
256     }
257     else if ( property.equalsIgnoreCase( PN_ZIP ) ) {
258       value = this._member.getAddress().getZip();
259     }
260     else if ( property.equalsIgnoreCase( PN_BIRTH ) ) {
261       value = null; // FIXME
262     }
263     else if ( property.equalsIgnoreCase( PN_ACCOUNTTYPESTR ) ) {
264       value = this._member.getAccountTypeStr();
265     }
266     else if ( property.equalsIgnoreCase( PN_EMAIL ) ) {
267       value = this._member.getEmail();
268     }
269     else if ( property.equalsIgnoreCase( PN_MEMBERID ) ) {
270       value = "" + this._member.getId();
271     }
272     else if ( property.equalsIgnoreCase( PN_FULLNAME ) ) {
273       value = this._member.getName();
274     }
275     else if ( property.equalsIgnoreCase( PN_LASTNAME ) ) {
276       value = this._member.getMemberName().getLastName();
277     }
278     else if ( property.equalsIgnoreCase( PN_FIRSTNAME ) ) {
279       value = this._member.getMemberName().getFirstName();
280     }
281     else if ( property.equalsIgnoreCase( PN_MIDDLENAME ) ) {
282       value = this._member.getMemberName().getMiddleName();
283     }
284     else if ( property.equalsIgnoreCase( PN_SUFFIX ) ) {
285       value = this._member.getMemberName().getSuffix();
286     }
287     else if ( property.equalsIgnoreCase( PN_EVENINGPHONE) ) {
288       value = null; // FIXME
289     }
290     else if ( property.equalsIgnoreCase( PN_DAYPHONE ) ) {
291       value = null; // FIXME
292     }
293     else if ( property.equalsIgnoreCase( PN_OTHERPHONE ) ) {
294       value = null; // FIXME
295     }
296 
297     if ( value == null ) {
298       value = "";
299     }
300 
301     try {
302       this.pageContext.getOut().write(value);
303     }
304     catch( IOException x ) {
305       throw new JspException( x.toString() );
306     }
307 
308     return EVAL_PAGE;
309   } // doEndTag()
310 
311   /**
312    * Release any acquired resources.
313    */
314   public void release() {
315     super.release();
316     this._key = Configuration.KEY_MEMBER;
317     this._property = Configuration.KEY_MEMBER;
318     this._member = null;
319   }
320 
321 } // MemberIdTag
322 
323 /*
324  * $Log: MemberTag.java,v $
325  * Revision 1.6  2003/02/26 03:38:46  rphall
326  * Added copyright and GPL license
327  *
328  * Revision 1.5  2003/02/20 16:31:39  rphall
329  * Improved diagnostic message if remoteUser is null
330  *
331  * Revision 1.4  2003/02/20 04:52:36  rphall
332  * Removed gratuitous use of CLRA acronym
333  *
334  * Revision 1.3  2002/06/20 20:45:04  rphall
335  * Added JSP/DB code to look up members by id
336  */
337