Source code: com/clra/member/MemberSnapshot.java
1 /*
2 * Copyright (c) Carnegie Lake Rowing Association 2002. All rights reserved.
3 * Distributed under the GPL license. See doc/COPYING.
4 * $RCSfile: MemberSnapshot.java,v $
5 * $Date: 2003/02/26 03:38:45 $
6 * $Revision: 1.8 $
7 */
8
9 package com.clra.member;
10
11 import java.io.Serializable;
12 import java.util.Date;
13 import java.util.HashSet;
14 import java.util.Iterator;
15 import java.util.Map;
16 import java.util.Set;
17
18 /**
19 * Read-only information about a member.
20 *
21 * @version $Revision: 1.8 $ $Date: 2003/02/26 03:38:45 $
22 * @author <a href="mailto:rphall@pluto.njcc.com">Rick Hall</a>
23 */
24 public class MemberSnapshot implements Comparable, Serializable {
25
26 private Integer id = null;
27 private String accountName = null;
28 private String accountPassword = null;
29 private AccountType accountType = null;
30 private MemberName memberName = null;
31 private Email email = null;
32 private Map telephoneNumbers = null;
33 private Address address = null;
34 private Date accountDate = null;
35 private Date birthDate = null;
36 private int hashCode = 0;
37 private Set memberRoles = null;
38
39 /** Produces an invalid MemberSnapshot. Used only during deserialization */
40 public MemberSnapshot() {}
41
42 /** Used only when loading from the database */
43 MemberSnapshot( Integer id, String accountName, String accountPassword,
44 AccountType accountType, MemberName memberName, Email email,
45 Map telephoneNumbers, Address address, Date accountDate, Date birthDate) {
46
47 this( id, accountName, accountPassword, accountType, memberName, email,
48 telephoneNumbers, address, accountDate, birthDate, new MemberRole[0] );
49
50 }
51
52 /** Used only when loading from the database */
53 void setMemberRoles( MemberRole[] roles ) {
54 if ( roles == null ) {
55 throw new IllegalArgumentException( "null member roles" );
56 }
57 this.memberRoles = new HashSet();
58 for ( int i=0; i<roles.length; i++ ) {
59 if ( roles[i] == null ) {
60 throw new IllegalArgumentException( "null member role" );
61 }
62 this.memberRoles.add( roles[i] );
63 }
64 return;
65 } // setMemberRoles(MemberRole[])
66
67 public MemberSnapshot( Integer id, String accountName, String accountPassword,
68 AccountType accountType, MemberName memberName, Email email,
69 Map telephoneNumbers, Address address, Date accountDate, Date birthDate,
70 MemberRole[] roles ) {
71
72 // Preconditions
73 if ( accountName == null || accountName.trim().length() == 0 ) {
74 throw new IllegalArgumentException( "invalid account name" );
75 }
76 if ( accountPassword == null || accountPassword.trim().length() == 0 ) {
77 throw new IllegalArgumentException( "invalid account password" );
78 }
79 if ( accountType == null ) {
80 throw new IllegalArgumentException( "null account type" );
81 }
82 if ( telephoneNumbers == null || telephoneNumbers.isEmpty() ) {
83 throw new IllegalArgumentException( "invalid telephone numbers" );
84 }
85 else {
86 Object o = telephoneNumbers.get( Telephone.EVENING );
87 if ( !(o instanceof Telephone) ) {
88 throw new IllegalArgumentException( "no evening telephone number" );
89 }
90 }
91
92 this.id = id;
93 this.accountName = accountName.trim();
94 this.accountPassword = accountPassword.trim();
95 this.accountType = accountType;
96 this.memberName = memberName;
97 this.email = email;
98 this.telephoneNumbers = telephoneNumbers;
99 this.address = address;
100 this.accountDate = accountDate;
101 this.birthDate = birthDate;
102
103 setMemberRoles( roles );
104
105 this.hashCode = this.getId().hashCode();
106
107 } // ctor(..)
108
109 public Integer getId() {
110 return this.id;
111 }
112
113 public String getAccountName() {
114 return this.accountName;
115 }
116
117 public String getAccountPassword() {
118 return this.accountPassword;
119 }
120
121 public AccountType getAccountType() {
122 return this.accountType;
123 }
124
125 public MemberName getMemberName() {
126 return this.memberName;
127 }
128
129 public boolean hasEmail() {
130 return this.email != null;
131 }
132
133 public Email getEmail() {
134 return this.email;
135 }
136
137 public Map getTelephoneNumbers() {
138 return this.telephoneNumbers;
139 }
140
141 public Address getAddress() {
142 return this.address;
143 }
144
145 public Date getAccountDate() {
146 return this.accountDate;
147 }
148
149 public boolean hasKnownBirthDate() {
150 return this.birthDate != null;
151 }
152
153 public Date getBirthDate() {
154 return this.birthDate;
155 }
156
157 public MemberRole[] getMemberRoles() {
158 return (MemberRole[]) this.memberRoles.toArray( new MemberRole[0] );
159 }
160
161 public boolean hasRole(String role) {
162
163 boolean retVal = false;
164 if ( role != null ) {
165 Iterator iter = memberRoles.iterator();
166 while ( !retVal && iter.hasNext() ) {
167 String strRole = ((MemberRole)iter.next()).toString();
168 if (strRole.equalsIgnoreCase(role)) {
169 retVal = true;
170 }
171 } // while
172 } // if role
173
174 return retVal;
175 } // hasRole(String)
176
177 /** Two members are equal iff their id's are equal. */
178 public boolean equals( Object o ) {
179
180 boolean retVal = false;
181 if ( o instanceof MemberSnapshot ) {
182 MemberSnapshot other = (MemberSnapshot)o;
183 retVal = this.getId().equals(other.getId());
184 }
185
186 return retVal;
187 } // equals(Object)
188
189 /** Member objects are hashed by id's */
190 public int hashCode() {
191 return hashCode;
192 }
193
194 /**
195 * Defines a natural ordering for members by lastname, firstname, middlename
196 * and suffix.<p>
197 *
198 * Note: this class has a natural ordering that is inconsistent with equals.
199 * Equality is defined by member id's, not by member names.
200 *
201 * @param o A member object.
202 * @exception ClassCastException if o is not a member object.
203 */
204 public int compareTo( Object o ) throws ClassCastException {
205 // Precondition
206 if ( !(o instanceof MemberSnapshot) ) {
207 throw new ClassCastException( "not a member object" );
208 }
209
210 MemberSnapshot other = (MemberSnapshot) o;
211 int retVal;
212
213 // Compare last names (which are never null)
214 String thisName = this.getMemberName().getLastName();
215 String otherName = other.getMemberName().getLastName();
216 retVal = thisName.compareToIgnoreCase( otherName );
217
218 // Compare first names (which are never null)
219 if ( retVal == 0 ) {
220 thisName = this.getMemberName().getFirstName();
221 otherName = other.getMemberName().getFirstName();
222 retVal = thisName.compareToIgnoreCase( otherName );
223 }
224
225 // Compare middle names (which may be null)
226 thisName = this.getMemberName().getMiddleName();
227 otherName = other.getMemberName().getMiddleName();
228 if ( retVal == 0 && thisName != null && otherName != null ) {
229 retVal = thisName.compareToIgnoreCase( otherName );
230 }
231 else if ( retVal == 0 && thisName == null && otherName != null ) {
232 retVal = -1;
233 }
234 else if ( retVal == 0 && thisName != null && otherName == null ) {
235 retVal = +1;
236 }
237
238 // Compare suffices (which may be null)
239 thisName = this.getMemberName().getSuffix();
240 otherName = other.getMemberName().getSuffix();
241 if ( retVal == 0 && thisName != null && otherName != null ) {
242 retVal = thisName.compareToIgnoreCase( otherName );
243 }
244 else if ( retVal == 0 && thisName == null && otherName != null ) {
245 retVal = -1;
246 }
247 else if ( retVal == 0 && thisName != null && otherName == null ) {
248 retVal = +1;
249 }
250
251 return retVal;
252 } // compareTo(Object)
253
254 } // MemberSnapshot
255
256 /*
257 * $Log: MemberSnapshot.java,v $
258 * Revision 1.8 2003/02/26 03:38:45 rphall
259 * Added copyright and GPL license
260 *
261 * Revision 1.7 2003/02/20 04:44:37 rphall
262 * Switched from String values to Object values: Email, MemberRole, etc
263 *
264 * Revision 1.6 2003/02/19 22:08:42 rphall
265 * Removed gratuitous use of CLRA acronym
266 *
267 * Revision 1.5 2003/02/18 04:20:15 rphall
268 * Removed ValidationException
269 *
270 * Revision 1.4 2003/02/15 04:31:42 rphall
271 * Changes connected to major revision of MemberBean
272 *
273 */
274