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

Quick Search    Search Deep

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


1   /*
2    * Copyright (c) Carnegie Lake Rowing Association 2002. All rights reserved.
3    * Distributed under the GPL license. See doc/COPYING.
4    * $RCSfile: SessionSet.java,v $
5    * $Date: 2003/02/26 03:38:46 $
6    * $Revision: 1.3 $
7    */
8   
9   package com.clra.web;
10  
11  import com.clra.rowing.RowingDBRead;
12  import com.clra.rowing.RowingSessionSnapshot;
13  import com.clra.util.ISerializableComparator;
14  import java.util.Calendar;
15  import java.util.Collection;
16  import java.util.Date;
17  import java.util.GregorianCalendar;
18  import java.util.Iterator;
19  import javax.servlet.jsp.JspException;
20  import org.apache.log4j.Category;
21  
22  /**
23   * A JSP bean that provides ordered lists of SessionViews. This class
24   * is a thin wrapper around calls to RowingDBRead. In the future, it might
25   * add further value by caching and sorting views on the client side.<p>
26   *
27   * FIXME: this class is basically a list, not a set. A set hides duplicates,
28   * whereas a list does not. Rename this class.<p>
29   *
30   * In the current implementation, a list is created on the fly whenever
31   * a JSP is displayed, and the list is tossed after the page is written.
32   * For lists of 60 or so items, a page will update in 2 - 3 seconds when
33   * the app and web servers are lightly loaded. This is adequate performance,
34   * since most lists will be restricted by month, and therefore will contain
35   * roughly 30 items.<p>
36   *
37   * See revision 1.10 and 1.11 for a flawed implementation of
38   * caching and sorting. See ../bugs/SEVERITY3_slow_sessionlist_2001.12.05a.txt
39   * for an analysis of the resulting bugs.<p>
40   *
41   * @version $Revision: 1.3 $ $Date: 2003/02/26 03:38:46 $
42   * @author <a href="mailto:rphall@pluto.njcc.com">Rick Hall</a>
43   */
44  public class SessionSet implements IEventList {
45  
46    private final static String base = SessionSet.class.getName();
47    private final static Category theLog = Category.getInstance( base );
48  
49    /** Holds the restriction flag */
50    private Boolean restricted = new Boolean(true);
51  
52    /** Holds the value of the month and year for restricted iterations */
53    private final Calendar calendar;
54  
55    /**
56     * Returns a flag that indicates whether an iterator will be restricted
57     * to events that are scheduled to start within a particular month
58     * and year. 
59     */
60    public Boolean getRestricted() {
61      return this.restricted;
62    }
63  
64    /**
65     * Sets whether an iterator will be restricted to events that start
66     * within a particular month and year. A null values does not change
67     * the current restriction.
68     */
69    public void setRestricted( Boolean restricted ) {
70      if ( restricted != null ) {
71        this.restricted = restricted;
72      }
73    }
74  
75    /** Returns the month used by restricted iterations */
76    public Integer getMonth() {
77      return new Integer( this.calendar.get( Calendar.MONTH ) );
78    }
79  
80    /** Sets the month used by restricted iterations */
81    public void setMonth( Integer month ) {
82      if ( month.intValue() < 0 || month.intValue() > 11 ) {
83        throw new IllegalArgumentException( "bad month == " + month );
84      }
85      this.calendar.set( Calendar.MONTH, month.intValue() );
86    }
87  
88    /** Returns the year used by restricted iterations */
89    public Integer getYear() {
90      return new Integer( this.calendar.get( Calendar.YEAR ) );
91    }
92  
93    /** Sets the year used by restricted iterations */
94    public void setYear( Integer year ) {
95      if ( year.intValue() < 1000 || year.intValue() > 9999 ) {
96        String msg = "bad year == " + year + "; years must be four digits";
97        throw new IllegalArgumentException( "bad year == " + year );
98      }
99      this.calendar.set( Calendar.YEAR, year.intValue() );
100   }
101 
102   /** Stubbed method that returns null */
103   public ISerializableComparator getComparator() { return null; }
104 
105   /** Stubbed method that does nothing */
106   public void setComparator( ISerializableComparator comparator ) { }
107 
108   /** Prepares a restricted iterator for the current month and year */
109   public SessionSet() throws WebException {
110     this.calendar = new GregorianCalendar();
111     this.calendar.set( Calendar.DATE, 1 );
112   }
113 
114   /**
115    * Prepares a restricted iterator for the specified month of the
116    * current year
117    */
118   public SessionSet( Integer month ) throws WebException {
119     this();
120     setMonth( month );
121   }
122 
123   /** Prepares a restricted iterator for the specified month and year */
124   public SessionSet( Integer month, Integer year ) throws WebException {
125     this();
126     setMonth( month );
127     setYear( year );
128   }
129 
130   /** Returns an iterator constructed to the current setting */
131   public Iterator getIterator() throws WebException {
132 
133     Iterator retVal = null;
134     try {
135       Collection c = null;
136       if ( restricted.booleanValue() ) {
137 
138         final int m = getMonth().intValue();
139         final int y = getYear().intValue();
140         final Date start  = WebUtils.minimumDateInMonth( m, y ); 
141         final Date finish = WebUtils.maximumDateInMonth( m, y );
142 
143         if ( theLog.isDebugEnabled() ) {
144           try {
145             FormattedDate fd =
146               new FormattedDate("EEEE', 'MM/dd/yy', 'h:mm:ss.SSS a");
147             fd.setDate( start );
148             theLog.debug( "SessionSet.start == " + fd.getValue() );
149             fd.setDate( finish );
150             theLog.debug( "SessionSet.finish== " + fd.getValue() );
151           } catch( JspException x ) {}
152         } // if isDebugEnabled
153 
154         c = RowingDBRead.findAllRowingSessionSnapshotsInRange(start,finish);
155       }
156       else {
157         c = RowingDBRead.findAllRowingSessionSnapshots();
158       }
159 
160       final Iterator cIter = c.iterator();
161 
162       retVal = new Iterator() {
163         public boolean hasNext() {
164           return cIter.hasNext();
165         }
166         public Object next() {
167           RowingSessionSnapshot rss = (RowingSessionSnapshot) cIter.next();
168           return new SessionView( rss );
169         }
170         public void remove() {
171           throw new UnsupportedOperationException( "remove not supported" );
172         }
173       }; // new Iterator()
174 
175     }
176     catch( Exception x ) {
177       String msg = "SessionSet.getIterator: " + x.getClass().getName()
178         + ": " + x.getMessage();
179       theLog.error( msg, x );
180       throw new WebException( msg );
181     }
182 
183     return retVal;
184   } // getIterator()
185 
186 } // SessionSet
187 
188 /*
189  * $Log: SessionSet.java,v $
190  * Revision 1.3  2003/02/26 03:38:46  rphall
191  * Added copyright and GPL license
192  *
193  * Revision 1.2  2002/01/30 15:17:30  rphall
194  * Fixed a bug for day in month > 28
195  *
196  * Revision 1.19  2002/01/30 01:30:23  rphall
197  * Fixed bug for day in month > 28
198  *
199  * Revision 1.18  2002/01/01 18:48:07  rphall
200  * Removed unnecessary import statements
201  *
202  * Revision 1.17  2002/01/01 03:40:33  rphall
203  * Moved getName() from MemberName to MemberView
204  *
205  * Revision 1.16  2001/12/13 01:30:21  rphall
206  * Enrollment business and web objects
207  *
208  * Revision 1.15  2001/12/11 23:47:32  rphall
209  * Documentation
210  *
211  * Revision 1.14  2001/12/09 20:29:19  rphall
212  * Fixed date range bug
213  *
214  * Revision 1.13  2001/12/09 05:58:32  rphall
215  * Implements IEventViewList
216  *
217  * Revision 1.12  2001/12/07 21:25:16  rphall
218  * Removed (now unused) client-side caching
219  *
220  * Revision 1.11  2001/12/07 14:44:46  rphall
221  * Hacked removal of caching
222  *
223  * Revision 1.10  2001/12/07 01:07:24  rphall
224  * Checkpt: before debugging slow perf
225  *
226  * Revision 1.9  2001/12/06 21:26:48  rphall
227  * Checkpt
228  *
229  * Revision 1.8  2001/12/06 04:54:35  rphall
230  * Deprecated (until client-side caching is fixed)
231  *
232  * Revision 1.7  2001/12/05 19:53:01  rphall
233  * New implementation using EJB's -- but not final
234  *
235  * Revision 1.6  2001/12/05 03:54:01  rphall
236  * New implementation based on RowingSessionList
237  *
238  * Revision 1.5  2001/11/30 11:38:00  rphall
239  * First working version, RowingSession entity bean
240  *
241  * Revision 1.4  2001/11/27 06:01:47  rphall
242  * Trim strings returned from DB
243  *
244  * Revision 1.2  2001/11/24 22:52:29  rphall
245  * Moved to a bean-friendly design. Exposed ctor, new caching scheme.
246  *
247  * Revision 1.1  2001/11/23 19:40:02  rphall
248  * Major revision
249  *
250  * Revision 1.2  2001/11/18 17:07:08  rphall
251  * Checkpt before major revision of rowing package
252  *
253  */
254