Source code: com/clra/web/DataUtils.java
1 /*
2 * Copyright (c) Carnegie Lake Rowing Association 2002. All rights reserved.
3 * Distributed under the GPL license. See doc/COPYING.
4 * $RCSfile: DataUtils.java,v $
5 * $Date: 2003/03/02 15:30:56 $
6 * $Revision: 1.7 $
7 */
8
9 package com.clra.web;
10
11 import com.clra.util.ValidationException;
12 import java.io.Serializable;
13 import java.text.SimpleDateFormat;
14 import java.util.ArrayList;
15 import java.util.Calendar;
16 import java.util.GregorianCalendar;
17 import java.util.Iterator;
18
19 /**
20 * Collections of data constants. These constants could be stored
21 * in the application database, but currently they are maintained
22 * separately.
23 *
24 * @version $Revision: 1.7 $ $Date: 2003/03/02 15:30:56 $
25 * @author <a href="mailto:rphall@pluto.njcc.com">Rick Hall</a>
26 */
27 public class DataUtils implements Serializable {
28
29 private DataUtils() {}
30
31 private static int FIRST_YEAR = 1985;
32
33 private static String[][] stateLabelValues = {
34 { Text.getMessage( "data.states.AL" ), "AL" },
35 { Text.getMessage( "data.states.AK" ), "AK" },
36 { Text.getMessage( "data.states.AZ" ), "AZ" },
37 { Text.getMessage( "data.states.AR" ), "AR" },
38 { Text.getMessage( "data.states.CA" ), "CA" },
39 { Text.getMessage( "data.states.CO" ), "CO" },
40 { Text.getMessage( "data.states.CT" ), "CT" },
41 { Text.getMessage( "data.states.DE" ), "DE" },
42 { Text.getMessage( "data.states.DC" ), "DC" },
43 { Text.getMessage( "data.states.FL" ), "FL" },
44 { Text.getMessage( "data.states.GA" ), "GA" },
45 { Text.getMessage( "data.states.HI" ), "HI" },
46 { Text.getMessage( "data.states.ID" ), "ID" },
47 { Text.getMessage( "data.states.IL" ), "IL" },
48 { Text.getMessage( "data.states.IN" ), "IN" },
49 { Text.getMessage( "data.states.IA" ), "IA" },
50 { Text.getMessage( "data.states.KS" ), "KS" },
51 { Text.getMessage( "data.states.KY" ), "KY" },
52 { Text.getMessage( "data.states.LA" ), "LA" },
53 { Text.getMessage( "data.states.ME" ), "ME" },
54 { Text.getMessage( "data.states.MD" ), "MD" },
55 { Text.getMessage( "data.states.MA" ), "MA" },
56 { Text.getMessage( "data.states.MI" ), "MI" },
57 { Text.getMessage( "data.states.MN" ), "MN" },
58 { Text.getMessage( "data.states.MS" ), "MS" },
59 { Text.getMessage( "data.states.MO" ), "MO" },
60 { Text.getMessage( "data.states.MT" ), "MT" },
61 { Text.getMessage( "data.states.NE" ), "NE" },
62 { Text.getMessage( "data.states.NV" ), "NV" },
63 { Text.getMessage( "data.states.NH" ), "NH" },
64 { Text.getMessage( "data.states.NJ" ), "NJ" },
65 { Text.getMessage( "data.states.NM" ), "NM" },
66 { Text.getMessage( "data.states.NY" ), "NY" },
67 { Text.getMessage( "data.states.NC" ), "NC" },
68 { Text.getMessage( "data.states.ND" ), "ND" },
69 { Text.getMessage( "data.states.OH" ), "OH" },
70 { Text.getMessage( "data.states.OK" ), "OK" },
71 { Text.getMessage( "data.states.OR" ), "OR" },
72 { Text.getMessage( "data.states.PA" ), "PA" },
73 { Text.getMessage( "data.states.RI" ), "RI" },
74 { Text.getMessage( "data.states.SC" ), "SC" },
75 { Text.getMessage( "data.states.SD" ), "SD" },
76 { Text.getMessage( "data.states.TN" ), "TN" },
77 { Text.getMessage( "data.states.TX" ), "TX" },
78 { Text.getMessage( "data.states.UT" ), "UT" },
79 { Text.getMessage( "data.states.VT" ), "VT" },
80 { Text.getMessage( "data.states.VA" ), "VA" },
81 { Text.getMessage( "data.states.WA" ), "WA" },
82 { Text.getMessage( "data.states.WV" ), "WV" },
83 { Text.getMessage( "data.states.WI" ), "WI" },
84 { Text.getMessage( "data.states.WY" ), "WY" }
85 };
86
87 private static String[][] accountTypeLabelValues = {
88 { Text.getMessage( "data.accountType.FULL" ), "FULL" },
89 { Text.getMessage( "data.accountType.NOVICE" ), "NOVICE" },
90 { Text.getMessage( "data.accountType.LTR" ), "LTR" },
91 { Text.getMessage( "data.accountType.ALUMNI" ), "ALUMNI" },
92 { Text.getMessage( "data.accountType.LTR-ALUM" ), "LTR-ALUM" },
93 { Text.getMessage( "data.accountType.CONTRACT" ), "CONTRACT" },
94 { Text.getMessage( "data.accountType.DUPLICAT" ), "DUPLICAT" }
95 };
96
97 private static String[][] roleLabelValues = {
98 { Text.getMessage( "data.role.MEMBER" ), "MEMBER" },
99 { Text.getMessage( "data.role.MEMBERMGR" ), "MEMBERMGR" },
100 { Text.getMessage( "data.role.COACH" ), "COACH" },
101 { Text.getMessage( "data.role.CAPTAIN" ), "CAPTAIN" },
102 { Text.getMessage( "data.role.SESSIONMGR" ), "SESSIONMGR" },
103 { Text.getMessage( "data.role.TREASURER" ), "TREASURER" },
104 { Text.getMessage( "data.role.INACTIVE" ), "INACTIVE" }
105 };
106
107 public static ArrayList accountYearList() {
108 final int thisYear = new GregorianCalendar().get(Calendar.YEAR);
109 ArrayList retVal = new ArrayList();
110 for ( int i = FIRST_YEAR; i <= thisYear; i++ ) {
111 String label = "" + i;
112 LabelValueBean lvb = new LabelValueBean( label, label );
113 retVal.add( lvb );
114 }
115 return retVal;
116 }
117
118 public static ArrayList stateList() {
119 ArrayList retVal = new ArrayList();
120 for ( int i=0; i<stateLabelValues.length; i++ ) {
121 LabelValueBean lvb =
122 new LabelValueBean( stateLabelValues[i][0], stateLabelValues[i][1] );
123 retVal.add( lvb );
124 }
125 return retVal;
126 }
127
128 public static ArrayList accountTypeList() {
129 ArrayList retVal = new ArrayList();
130 for ( int i=0; i<accountTypeLabelValues.length; i++ ) {
131 LabelValueBean lvb = new LabelValueBean(
132 accountTypeLabelValues[i][0], accountTypeLabelValues[i][1] );
133 retVal.add( lvb );
134 }
135 return retVal;
136 }
137
138 public static ArrayList roleList() {
139 ArrayList retVal = new ArrayList();
140 for ( int i=0; i<roleLabelValues.length; i++ ) {
141 LabelValueBean lvb =
142 new LabelValueBean( roleLabelValues[i][0], roleLabelValues[i][1] );
143 retVal.add( lvb );
144 }
145 return retVal;
146 }
147
148 public static class AccountYears implements Iterator {
149 private Iterator iterator = accountYearList().iterator();
150 public boolean hasNext() { return iterator.hasNext(); }
151 public Object next() { return iterator.next(); }
152 public void remove() { throw new UnsupportedOperationException(); }
153 }
154
155 public static class States implements Iterator {
156 private Iterator iterator = stateList().iterator();
157 public boolean hasNext() { return iterator.hasNext(); }
158 public Object next() { return iterator.next(); }
159 public void remove() { throw new UnsupportedOperationException(); }
160 }
161
162 public static class AccountTypes implements Iterator {
163 private Iterator iterator = accountTypeList().iterator();
164 public boolean hasNext() { return iterator.hasNext(); }
165 public Object next() { return iterator.next(); }
166 public void remove() { throw new UnsupportedOperationException(); }
167 }
168
169 public static class Roles implements Iterator {
170 private Iterator iterator = roleList().iterator();
171 public boolean hasNext() { return iterator.hasNext(); }
172 public Object next() { return iterator.next(); }
173 public void remove() { throw new UnsupportedOperationException(); }
174 }
175
176 public static void requiredStringValue( String name, String value )
177 throws ValidationException {
178
179 // Precondition: non-null, non-blank name for string
180 if ( name == null || name.trim().length() == 0 ) {
181 throw new Error( "null or blank string name" );
182 }
183
184 // Test the string value
185 if ( value == null || value.trim().length() == 0 ) {
186 String msg = "null or blank " + name;
187 throw new ValidationException( msg );
188 }
189
190 return;
191 } // requiredString(String,String)
192
193 public static String validateString( String str, String[] allowed )
194 throws ValidationException {
195
196 if ( str == null ) {
197 throw new IllegalArgumentException( "null string" );
198 }
199 str = str.trim();
200
201 String retVal = null;
202 for ( int i=0; i<allowed.length; i++ ) {
203 if ( allowed[i].equalsIgnoreCase(str) ) {
204 retVal = allowed[i];
205 break;
206 }
207 }
208
209 if ( retVal == null ) {
210 throw new ValidationException( "invalid value == '" +str+ "'" );
211 }
212
213 return retVal;
214 } // validateString(String,String[])
215
216 } // DataUtils
217
218 /*
219 * $Log: DataUtils.java,v $
220 * Revision 1.7 2003/03/02 15:30:56 rphall
221 * Moved hard-coded text from DataUtils to clra.properties
222 *
223 * Revision 1.6 2003/02/26 03:38:46 rphall
224 * Added copyright and GPL license
225 *
226 * Revision 1.5 2003/02/21 04:59:54 rphall
227 * Shortened DB value for 'DUPLICATE'
228 *
229 * Revision 1.4 2003/02/20 04:47:01 rphall
230 * Removed gratuitous use of CLRA acronym
231 *
232 * Revision 1.3 2003/02/19 03:19:13 rphall
233 * Added 'contractor' notation to Coach role
234 *
235 * Revision 1.2 2003/02/18 04:26:30 rphall
236 * Added general validation methods
237 *
238 * Revision 1.1 2003/02/10 05:35:31 rphall
239 * Data constants
240 *
241 */
242