Source code: com/clra/web/ApplicationSet.java
1 /*
2 * Copyright (c) Carnegie Lake Rowing Association 2002. All rights reserved.
3 * Distributed under the GPL license. See doc/COPYING.
4 * $RCSfile: ApplicationSet.java,v $
5 * $Date: 2003/02/26 03:38:46 $
6 * $Revision: 1.2 $
7 */
8
9 package com.clra.web;
10
11 import com.clra.util.ISerializableComparator;
12 import com.clra.visitor.ApplicantDBRead;
13 import com.clra.visitor.ApplicantSnapshot;
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 * @version $Revision: 1.2 $ $Date: 2003/02/26 03:38:46 $
24 * @author <a href="mailto:donaldzhu@sympatico.ca">Angela Yue</a>
25 */
26 public class ApplicationSet implements IEventList {
27
28 private final static String base = ApplicationSet.class.getName();
29 private final static Category theLog = Category.getInstance( base );
30
31 /** Holds the value of the month and year for restricted iterations */
32 private final Calendar calendar;
33 private Boolean restricted = new Boolean(true);
34
35 /**
36 * Returns a flag that indicates whether an iterator will be restricted
37 * to events that are scheduled to start within a particular month
38 * and year.
39 */
40 public Boolean getRestricted() {
41 return this.restricted;
42 }
43
44 /**
45 * Sets whether an iterator will be restricted to events that start
46 * within a particular month and year. A null values does not change
47 * the current restriction.
48 */
49 public void setRestricted( Boolean restricted ) {
50 if ( restricted != null ) {
51 this.restricted = restricted;
52 }
53 }
54
55 /** Returns the month used by restricted iterations */
56 public Integer getMonth() {
57 return new Integer( this.calendar.get( Calendar.MONTH ) );
58 }
59
60 /** Sets the month used by restricted iterations */
61 public void setMonth( Integer month ) {
62 if ( month.intValue() < 0 || month.intValue() > 11 ) {
63 throw new IllegalArgumentException( "bad month == " + month );
64 }
65 this.calendar.set( Calendar.MONTH, month.intValue() );
66 }
67
68 /** Returns the year used by restricted iterations */
69 public Integer getYear() {
70 return new Integer( this.calendar.get( Calendar.YEAR ) );
71 }
72
73 /** Sets the year used by restricted iterations */
74 public void setYear( Integer year ) {
75 if ( year.intValue() < 1000 || year.intValue() > 9999 ) {
76 String msg = "bad year == " + year + "; years must be four digits";
77 throw new IllegalArgumentException( "bad year == " + year );
78 }
79 this.calendar.set( Calendar.YEAR, year.intValue() );
80 }
81
82 /** Stubbed method that returns null */
83 public ISerializableComparator getComparator() { return null; }
84
85 /** Stubbed method that does nothing */
86 public void setComparator( ISerializableComparator comparator ) { }
87
88 /** Creates an invalid ApplicationSet, but required by Bean contract */
89 public ApplicationSet() throws WebException {
90 this.calendar = new GregorianCalendar();
91 this.calendar.set( Calendar.DATE, 1 );
92 }
93
94 /**
95 * Prepares a restricted iterator for the specified month of the
96 * current year.
97 * @param memberId the member to whom this enrollment applies
98 */
99 public ApplicationSet( Integer month )
100 throws WebException {
101 this();
102 setMonth( month );
103 }
104
105 /** Prepares a restricted iterator for the specified month and year */
106 public ApplicationSet( Integer month, Integer year )
107 throws WebException {
108 this();
109 setMonth( month );
110 setYear( year );
111 }
112
113 /** Returns an iterator constructed to the current setting */
114 public Iterator getIterator() throws WebException {
115
116 Iterator retVal = null;
117 try {
118 Collection c = null;
119 if ( restricted.booleanValue() ) {
120
121 final int m = getMonth().intValue();
122 final int y = getYear().intValue();
123 final Date start = WebUtils.minimumDateInMonth( m, y );
124 final Date finish = WebUtils.maximumDateInMonth( m, y );
125
126 if ( theLog.isDebugEnabled() ) {
127 try {
128 theLog.debug( "restricted iteration" );
129 FormattedDate fd =
130 new FormattedDate("EEEE', 'MM/dd/yy', 'h:mm:ss.SSS a");
131 fd.setDate( start );
132 theLog.debug( "SessionSet.start == " + fd.getValue() );
133 fd.setDate( finish );
134 theLog.debug( "SessionSet.finish== " + fd.getValue() );
135 } catch( JspException x ) {}
136 } // if isDebugEnabled
137
138 c = ApplicantDBRead.findAllApplicantsInRange(start,finish);
139 }
140 else {
141 if ( theLog.isDebugEnabled() ) {
142 theLog.debug( "unrestricted iteration" );
143 }
144 c = ApplicantDBRead.findAllApplicants( );
145 }
146
147 final Iterator cIter = c.iterator();
148
149 retVal = new Iterator() {
150 public boolean hasNext() {
151 return cIter.hasNext();
152 }
153 public Object next() {
154 ApplicantSnapshot es = (ApplicantSnapshot) cIter.next();
155 return es;
156 }
157 public void remove() {
158 throw new UnsupportedOperationException( "remove not supported" );
159 }
160 }; // new Iterator()
161
162 }
163 catch( Exception x ) {
164 String msg = "SessionSet.getIterator: " + x.getClass().getName()
165 + ": " + x.getMessage();
166 theLog.error( msg, x );
167 throw new WebException( msg );
168 }
169
170 return retVal;
171 } // getIterator()
172
173 } // ApplicationSet
174