Source code: com/clra/web/EnrollmentSet.java
1 /*
2 * Copyright (c) Carnegie Lake Rowing Association 2002. All rights reserved.
3 * Distributed under the GPL license. See doc/COPYING.
4 * $RCSfile: EnrollmentSet.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.EnrollmentSnapshot;
12 import com.clra.rowing.RowingDBRead;
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 EnrollmentViews. This class
24 * is a thin wrapper around calls to RowingUtils. 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 * @version $Revision: 1.3 $ $Date: 2003/02/26 03:38:46 $
38 * @author <a href="mailto:rphall@pluto.njcc.com">Rick Hall</a>
39 */
40 public class EnrollmentSet implements IEventList {
41
42 private final static String base = EnrollmentSet.class.getName();
43 private final static Category theLog = Category.getInstance( base );
44
45 /** The primary key of the member to whom this enrollment applies */
46 private Integer memberId = null;
47
48 /** Returns the primary key of the member to whom this enrollment applies */
49 public Integer getMemberId() {
50 if ( this.memberId == null ) throw new IllegalStateException();
51 return this.memberId;
52 }
53
54 /** Sets the member to whom this enrollment applies */
55 public void setMemberId( Integer memberId ) {
56 if ( memberId == null ) {
57 throw new IllegalStateException( "null member id" );
58 }
59 this.memberId = memberId;
60 }
61
62 /** Holds the restriction flag */
63 private Boolean restricted = new Boolean(true);
64
65 /** Holds the value of the month and year for restricted iterations */
66 private final Calendar calendar;
67
68 /**
69 * Returns a flag that indicates whether an iterator will be restricted
70 * to events that are scheduled to start within a particular month
71 * and year.
72 */
73 public Boolean getRestricted() {
74 return this.restricted;
75 }
76
77 /**
78 * Sets whether an iterator will be restricted to events that start
79 * within a particular month and year. A null values does not change
80 * the current restriction.
81 */
82 public void setRestricted( Boolean restricted ) {
83 if ( restricted != null ) {
84 this.restricted = restricted;
85 }
86 }
87
88 /** Returns the month used by restricted iterations */
89 public Integer getMonth() {
90 return new Integer( this.calendar.get( Calendar.MONTH ) );
91 }
92
93 /** Sets the month used by restricted iterations */
94 public void setMonth( Integer month ) {
95 if ( month.intValue() < 0 || month.intValue() > 11 ) {
96 throw new IllegalArgumentException( "bad month == " + month );
97 }
98 this.calendar.set( Calendar.MONTH, month.intValue() );
99 }
100
101 /** Returns the year used by restricted iterations */
102 public Integer getYear() {
103 return new Integer( this.calendar.get( Calendar.YEAR ) );
104 }
105
106 /** Sets the year used by restricted iterations */
107 public void setYear( Integer year ) {
108 if ( year.intValue() < 1000 || year.intValue() > 9999 ) {
109 String msg = "bad year == " + year + "; years must be four digits";
110 throw new IllegalArgumentException( "bad year == " + year );
111 }
112 this.calendar.set( Calendar.YEAR, year.intValue() );
113 }
114
115 /** Stubbed method that returns null */
116 public ISerializableComparator getComparator() { return null; }
117
118 /** Stubbed method that does nothing */
119 public void setComparator( ISerializableComparator comparator ) { }
120
121 /** Creates an invalid EnrollmentSet, but required by Bean contract */
122 public EnrollmentSet() throws WebException {
123 this.calendar = new GregorianCalendar();
124 this.calendar.set( Calendar.DATE, 1 );
125 }
126
127 /**
128 * Prepares a restricted iterator for the specified month of the
129 * current year.
130 * @param memberId the member to whom this enrollment applies
131 */
132 public EnrollmentSet( Integer memberId, Integer month )
133 throws WebException {
134 this();
135 setMemberId( memberId );
136 setMonth( month );
137 }
138
139 /** Prepares a restricted iterator for the specified month and year */
140 public EnrollmentSet( Integer memberId, Integer month, Integer year )
141 throws WebException {
142 this();
143 setMemberId( memberId );
144 setMonth( month );
145 setYear( year );
146 }
147
148 /** Returns an iterator constructed to the current setting */
149 public Iterator getIterator() throws WebException {
150
151 // Preconditions
152 if ( this.memberId == null ) {
153 theLog.error( "null memberId" );
154 throw new IllegalStateException( "null memberId" );
155 }
156 final int id = this.getMemberId().intValue();
157
158 Iterator retVal = null;
159 try {
160 Collection c = null;
161 if ( restricted.booleanValue() ) {
162
163 final int m = getMonth().intValue();
164 final int y = getYear().intValue();
165 final Date start = WebUtils.minimumDateInMonth( m, y );
166 final Date finish = WebUtils.maximumDateInMonth( m, y );
167
168 if ( theLog.isDebugEnabled() ) {
169 try {
170 theLog.debug( "restricted iteration" );
171 FormattedDate fd =
172 new FormattedDate("EEEE', 'MM/dd/yy', 'h:mm:ss.SSS a");
173 fd.setDate( start );
174 theLog.debug( "SessionSet.start == " + fd.getValue() );
175 fd.setDate( finish );
176 theLog.debug( "SessionSet.finish== " + fd.getValue() );
177 } catch( JspException x ) {}
178 } // if isDebugEnabled
179
180 c = RowingDBRead.findAllEnrollmentSnapshotsInRange(id,start,finish);
181 }
182 else {
183 if ( theLog.isDebugEnabled() ) {
184 theLog.debug( "unrestricted iteration" );
185 }
186 c = RowingDBRead.findAllEnrollmentSnapshots( id );
187 }
188
189 final Iterator cIter = c.iterator();
190
191 retVal = new Iterator() {
192 public boolean hasNext() {
193 return cIter.hasNext();
194 }
195 public Object next() {
196 EnrollmentSnapshot es = (EnrollmentSnapshot) cIter.next();
197 return new EnrollmentView( es );
198 }
199 public void remove() {
200 throw new UnsupportedOperationException( "remove not supported" );
201 }
202 }; // new Iterator()
203
204 }
205 catch( Exception x ) {
206 String msg = "SessionSet.getIterator: " + x.getClass().getName()
207 + ": " + x.getMessage();
208 theLog.error( msg, x );
209 throw new WebException( msg );
210 }
211
212 return retVal;
213 } // getIterator()
214
215 } // EnrollmentSet
216
217 /*
218 * $Log: EnrollmentSet.java,v $
219 * Revision 1.3 2003/02/26 03:38:46 rphall
220 * Added copyright and GPL license
221 *
222 * Revision 1.2 2002/01/30 15:17:30 rphall
223 * Fixed a bug for day in month > 28
224 *
225 * Revision 1.4 2002/01/30 01:30:23 rphall
226 * Fixed bug for day in month > 28
227 *
228 * Revision 1.3 2002/01/01 03:40:33 rphall
229 * Moved getName() from MemberName to MemberView
230 *
231 * Revision 1.2 2001/12/13 21:19:40 rphall
232 * More logging
233 *
234 * Revision 1.1 2001/12/13 01:30:21 rphall
235 * Enrollment business and web objects
236 *
237 */
238