Source code: com/clra/web/MemberSet.java
1 /*
2 * Copyright (c) Carnegie Lake Rowing Association 2002. All rights reserved.
3 * Distributed under the GPL license. See doc/COPYING.
4 * $RCSfile: MemberSet.java,v $
5 * $Date: 2003/02/26 03:38:46 $
6 * $Revision: 1.4 $
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 com.clra.util.ErrorUtils;
15 import com.clra.util.ISerializableComparator;
16 import java.util.Collection;
17 import java.util.Iterator;
18 import javax.servlet.jsp.JspException;
19 import org.apache.log4j.Category;
20
21 /**
22 * A JSP bean that provides ordered lists of MemberViews. This class
23 * is a thin wrapper around calls to MemberDBRead. In the future, it might
24 * add further value by caching and sorting views on the client side.<p>
25 *
26 * FIXME: this class is basically a list, not a set. A set hides duplicates,
27 * whereas a list does not. Rename this class.<p>
28 *
29 * FIXME: this class could be less tightly coupled to the GUI, by using
30 * String properties lowerBound and upperBound. If both lowerBound and
31 * upperBound are non-null, then select for lowerBound <= names < upperBound.
32 * If lowerBound is null, then select for names < upperBound. If upperBound
33 * is null, then select for lowerBound <= names. If both are null, throw an
34 * IllegalStateException.<p>
35 *
36 * In the current implementation, a list is created on the fly whenever
37 * a JSP is displayed, and the list is tossed after the page is written.
38 * For lists of 60 or so items, a page will update in 2 - 3 seconds when
39 * the app and web servers are lightly loaded. This is adequate performance,
40 * since most lists will be restricted by name, and therefore will contain
41 * roughly 40 (==300/8) items.<p>
42 *
43 * @version $Revision: 1.4 $ $Date: 2003/02/26 03:38:46 $
44 * @author <a href="mailto:rphall@pluto.njcc.com">Rick Hall</a>
45 */
46 public class MemberSet implements INameList {
47
48 private final static String base = MemberSet.class.getName();
49 private final static Category theLog = Category.getInstance( base );
50
51 private final static Integer DEFAULT_GROUP_INDEX = new Integer(2);
52
53 /** Holds the restriction flag */
54 private Boolean restricted = new Boolean(true);
55
56 /**
57 * Holds the group index that specifies which names should be displayed.
58 * By default, this group is <tt>ABC</tt> (index 2).
59 */
60 private Integer group = DEFAULT_GROUP_INDEX;
61
62 /**
63 * Returns a flag that indicates whether an iterator will be restricted
64 * to a particular group of names.
65 */
66 public Boolean getRestricted() {
67 return this.restricted;
68 }
69
70 /**
71 * Sets whether an iterator will be restricted to names within a
72 * within a group. A null values does not change the current restriction.
73 */
74 public void setRestricted( Boolean restricted ) {
75 if ( restricted != null ) {
76 this.restricted = restricted;
77 }
78 }
79
80 /** Returns the group used by restricted iterations */
81 public Integer getGroup() {
82 return group;
83 }
84
85 /** Sets the month used by restricted iterations */
86 public void setGroup( Integer group ) {
87 if ( group.intValue() < 2 || group.intValue() > 9 ) {
88 throw new IllegalArgumentException( "bad group == " + group );
89 }
90 this.group = group;
91 }
92
93 /** Stubbed method that returns null */
94 public ISerializableComparator getComparator() { return null; }
95
96 /** Stubbed method that does nothing */
97 public void setComparator( ISerializableComparator comparator ) { }
98
99 /** Prepares a restricted iterator for the current group and year */
100 public MemberSet() throws WebException {
101 }
102
103 /**
104 * Prepares a restricted iterator for the specified group of the
105 * current year
106 */
107 public MemberSet( Integer group ) throws WebException {
108 setGroup( group );
109 }
110
111 /** Returns an iterator constructed to the current setting */
112 public Iterator getIterator() throws WebException {
113
114 Iterator retVal = null;
115 try {
116
117 Collection c = null;
118 if ( restricted.booleanValue() ) {
119 c = findMemberSnapshots( this.getGroup() );
120 }
121 else {
122 c = MemberDBRead.findAllMembersByLastName();
123 }
124
125 final Iterator cIter = c.iterator();
126
127 retVal = new Iterator() {
128 public boolean hasNext() {
129 return cIter.hasNext();
130 }
131 public Object next() {
132 MemberSnapshot ms = (MemberSnapshot) cIter.next();
133 return new MemberView( ms );
134 }
135 public void remove() {
136 throw new UnsupportedOperationException( "remove not supported" );
137 }
138 }; // new Iterator()
139
140 }
141 catch( Exception x ) {
142 String msg = ErrorUtils.createDbgMsg( "MemberSet.getIterator", x );
143 theLog.error( msg, x );
144 throw new WebException( msg );
145 }
146
147 return retVal;
148 } // getIterator()
149
150 /**
151 * Returns a collection of member snapshots based on the current group.
152 */
153 Collection findMemberSnapshots( Integer group ) throws MemberException {
154
155 // Preconditions
156 if ( group == null || group.intValue() < 2 || group.intValue() > 9 ) {
157 throw new IllegalArgumentException( "invalid group == " + group );
158 }
159
160 Collection retVal = null;
161
162 switch( group.intValue() ) {
163 case 2:
164 retVal = MemberDBRead.findMembersWithLastNamesLT( "D" );
165 break;
166 case 9:
167 retVal = MemberDBRead.findMembersWithLastNamesGTE( "W" );
168 break;
169 default:
170 String lower = getLowerBound( group );
171 String upper = getUpperBound( group );
172 retVal = MemberDBRead.findMembersWithLastNamesInRange( lower, upper );
173 break;
174 } // switch group
175
176 return retVal;
177 } // findMemberSnapshots(Integer)
178
179 /** Calculates the lower bound of names in a group */
180 String getLowerBound( Integer group ) {
181
182 String retVal = null;
183 switch( group.intValue() ) {
184 case 3: retVal = "D"; break;
185 case 4: retVal = "G"; break;
186 case 5: retVal = "J"; break;
187 case 6: retVal = "M"; break;
188 case 7: retVal = "P"; break;
189 case 8: retVal = "T"; break;
190 default:
191 throw new IllegalArgumentException( "invalid group == " + group );
192 } // switch
193
194 return retVal;
195 } // getLowerBound(Integer)
196
197 /** Calculates the uppder bound of names in a group */
198 String getUpperBound( Integer group ) {
199
200 String retVal = null;
201 switch( group.intValue() ) {
202 case 3: retVal = "G"; break;
203 case 4: retVal = "J"; break;
204 case 5: retVal = "M"; break;
205 case 6: retVal = "P"; break;
206 case 7: retVal = "T"; break;
207 case 8: retVal = "W"; break;
208 default:
209 throw new IllegalArgumentException( "invalid group == " + group );
210 } // switch
211
212 return retVal;
213 } // getUpperBound(Integer)
214
215 /**
216 * Finds all active members of the CLRA. Members are sorted by
217 * lastname-firstname (the natural comparator for members).<p>
218 * @deprecated
219 */
220 public Iterator getAllActiveMembers()
221 throws MemberException {
222
223 final java.util.TreeSet data = new java.util.TreeSet();
224
225 try {
226 Iterator snapshots = MemberDBRead.findAllMembersByLastName().iterator();
227 while ( snapshots.hasNext() ) {
228 MemberView mv = new MemberView( (MemberSnapshot) snapshots.next() );
229 data.add( mv );
230 }
231 }
232 catch(Exception x) {
233 String msg = x.getClass().getName() + ": " + x.getMessage();
234 theLog.fatal( msg, x );
235 throw new MemberException( msg );
236 }
237
238 return data.iterator();
239 } // findAllActiveMembers()
240
241 } // MemberSet
242
243 /*
244 * $Log: MemberSet.java,v $
245 * Revision 1.4 2003/02/26 03:38:46 rphall
246 * Added copyright and GPL license
247 *
248 * Revision 1.3 2002/02/24 21:15:10 rphall
249 * Fixed bug #501041
250 *
251 * Revision 1.2 2002/02/18 18:06:26 rphall
252 * Ran dos2unix to remove ^M (carriage return) from end of lines
253 *
254 * Revision 1.1.1.1 2002/01/03 21:57:28 rphall
255 * Initial load, 5th try, Jan-03-2002 4:57 PM
256 *
257 * Revision 1.5 2002/01/01 18:46:49 rphall
258 * Working: name selector
259 *
260 */
261