Source code: com/RuntimeCollective/webapps/form/SearchUserForm.java
1 /* $Header: /home/CVS/rjp/src/com/RuntimeCollective/webapps/form/SearchUserForm.java,v 1.18 2003/09/30 15:13:14 joe Exp $
2 * $Revision: 1.18 $
3 * $Date: 2003/09/30 15:13:14 $
4 *
5 * ====================================================================
6 *
7 * Josephine : http://www.runtime-collective.com/josephine/index.html
8 *
9 * Copyright (C) 2003 Runtime Collective
10 *
11 * This product includes software developed by the
12 * Apache Software Foundation (http://www.apache.org/).
13 *
14 * This library is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU Lesser General Public
16 * License as published by the Free Software Foundation; either
17 * version 2.1 of the License, or (at your option) any later version.
18 *
19 * This library is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * Lesser General Public License for more details.
23 *
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 *
28 */
29
30 package com.RuntimeCollective.webapps.form;
31
32 import com.RuntimeCollective.webapps.RuntimeParameters;
33 import com.RuntimeCollective.webapps.SearchResults;
34 import com.RuntimeCollective.webapps.bean.SimpleUser;
35 import com.RuntimeCollective.webapps.bean.TrackedUser;
36 import com.RuntimeCollective.webapps.bean.User;
37 import com.RuntimeCollective.webapps.bean.SearchResult;
38 import com.RuntimeCollective.webapps.bean.EntityBean;
39
40 import javax.servlet.http.HttpServletRequest;
41 import org.apache.struts.action.ActionError;
42 import org.apache.struts.action.ActionErrors;
43 import org.apache.struts.action.ActionForm;
44 import org.apache.struts.action.ActionMapping;
45
46 import java.util.ArrayList;
47 import java.util.List;
48
49 /**
50 * A form for searching for users that match a certain set of criteria.
51 * This bean includes both the criteria for searching, and the results.
52 * The results can be populated using <code>User.searchUser</code>.
53 *
54 * @version $Id: SearchUserForm.java,v 1.18 2003/09/30 15:13:14 joe Exp $, 13/8/2001
55 */
56 public class SearchUserForm extends ActionForm {
57
58 // == Properties ==========================================
59
60 /** Surname of user. */
61 private String surname = "";
62 /** Set surname of user. */
63 public void setLastName(String surname) { this.surname = surname; }
64 /** Get surname of user. */
65 public String getLastName() { return this.surname; }
66
67
68 /** First name of user. */
69 private String first = "";
70 /** Set first name of user. */
71 public void setFirstName(String first) { this.first = first; }
72 /** Get first name of user. */
73 public String getFirstName() { return this.first; }
74
75
76 /** Part of the Email of user -- ie user will match if their email address contains this string. */
77 private String email = "";
78 /** Set email part of user. */
79 public void setEmail(String email) { this.email = email; }
80 /** Get email part of user. */
81 public String getEmail() { return this.email; }
82
83 /** Nationality code of user. */
84 private String nationality = "";
85 /** Set nationality code of user. */
86 public void setNationality(String nationality) { this.nationality = nationality; }
87 /** Get nationality code of user. */
88 public String getNationality() { return this.nationality; }
89
90 /** Part postcode of user, ie user will match if their postcode starts with this string. */
91 private String postcode = "";
92 /** Set postcode of user. */
93 public void setPostcode(String postcode) { this.postcode = postcode; }
94 /** Get postcode of user. */
95 public String getPostcode() { return this.postcode; }
96
97 /** City of user. */
98 private String city = "";
99 /** Set city of user. */
100 public void setCity(String city) { this.city = city; }
101 /** Get city of user. */
102 public String getCity() { return this.city; }
103
104 /** Country code of residence of user. */
105 private String country = "";
106 /** Set country code of user. */
107 public void setCountry(String country) { this.country = country; }
108 /** Get country code of user. */
109 public String getCountry() { return this.country; }
110
111 /** Role of user. */
112 private int role = EntityBean.NULL_ID;
113 /** Set role. */
114 public void setRole(int role) { this.role = role; }
115 /** Get role. */
116 public int getRole() { return this.role; }
117
118 /** The id of a group the user belongs to. */
119 private int groupId = EntityBean.NULL_ID;
120 /** Set groupId. */
121 public void setGroupId(int groupId) { this.groupId = groupId; }
122 /** Get groupId. */
123 public int getGroupId() { return this.groupId; }
124
125 /** Results of the search. */
126 private SearchResults results=null;
127 /** Set results.*/
128 public void setResults(SearchResults results) { this.results = results; }
129 /** Get results. */
130 public SearchResults getResults() { return this.results; }
131
132 /** AcceptsEmptyForm: if true we let pass empty forms (thus returning all users). */
133 private boolean acceptsEmptyForm = false;
134 /** Set acceptsEmptyForm.*/
135 public void setAcceptsEmptyForm(boolean acceptsEmptyForm) { this.acceptsEmptyForm = acceptsEmptyForm; }
136 /** Get acceptsEmptyForm. */
137 public boolean getAcceptsEmptyForm() { return this.acceptsEmptyForm; }
138
139 /** StartsOnly: if true we look for like 'xxx%' rather than '%xxx%'. */
140 private boolean startsOnly = false;
141 /** Set startsOnly.*/
142 public void setStartsOnly(boolean startsOnly) { this.startsOnly = startsOnly; }
143 /** Get startsOnly. */
144 public boolean getStartsOnly() { return this.startsOnly; }
145
146
147 /** Reset all properties to their default values.
148 * NB this ONLY clears the form properties, not the results.
149 * @param mapping The mapping used to select this instance
150 * @param request The servlet request we are processing
151 */
152 public void reset(ActionMapping mapping, HttpServletRequest request) {
153 this.role = EntityBean.NULL_ID;
154 this.surname="";
155 this.first="";
156 this.email="";
157 this.nationality="";
158 this.postcode="";
159 this.city="";
160 this.country="";
161 this.groupId = EntityBean.NULL_ID;
162 this.acceptsEmptyForm = false;
163 this.startsOnly = false;
164
165 setResults(null);
166 }
167
168 /** Validate search criteria.
169 * Returns error with code=error.userSearch.null if no criteria entered. */
170 public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
171 ActionErrors errors = new ActionErrors();
172 if (!acceptsEmptyForm) {
173 if ((role == EntityBean.NULL_ID) &&
174 (isEmpty(surname)) &&
175 (isEmpty(first)) &&
176 (isEmpty(email)) &&
177 (isEmpty(postcode)) &&
178 (isEmpty(city)) &&
179 (groupId == EntityBean.NULL_ID))
180 errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.userSearch.null"));
181 }
182
183 return errors;
184 }
185
186
187 /** Get a String description of the class of users returned, eg
188 * "users whose name start with XXX and ...".
189 * FIXME: need to implement role, nationality, group.
190 */
191 public String getDescription() {
192
193 List criteria = new ArrayList(10);
194
195 String verb;
196 if (getStartsOnly())
197 verb = "starts with";
198 else
199 verb = "contains";
200
201 if (!isEmpty(first))
202 criteria.add("whose first name "+verb+" '"+first+"'");
203 if (!isEmpty(surname))
204 criteria.add("whose last name "+verb+" '"+surname+"'");
205 if (!isEmpty(email))
206 criteria.add("whose email "+verb+" '"+email+"'");
207 if (!isEmpty(postcode))
208 criteria.add("whose postcode "+verb+" '"+postcode+"'");
209 if (!isEmpty(city))
210 criteria.add("whose city "+verb+" '"+city+"'");
211 if (!isEmpty(country))
212 criteria.add("whose country "+verb+" '"+country+"'");
213
214 if (criteria.isEmpty()) {
215 return "All users";
216
217 } else {
218 StringBuffer result = (new StringBuffer()).append("Users ");
219 for (int i=0; i<criteria.size()-1; i++)
220 result.append(criteria.get(i)).append(", ");
221 result.append(" and ").append(criteria.get(criteria.size()-1));
222 return result.toString();
223 }
224 }
225
226 protected static boolean isEmpty(String string) {
227 return ((string == null) || (string.equals("")));
228 }
229
230 public void excludeArchivedTrackedUsers() {
231 User aUser;
232 for (int i=0; i<results.size(); i++) {
233 aUser = (User) RuntimeParameters.getStore().get(User.class.getName(), results.getResult(i).getId());
234 if ((aUser instanceof TrackedUser) && (((TrackedUser) aUser).isArchived())) {
235 results.remove(i);
236 i--;
237 }
238 }
239 }
240 }
241
242
243
244
245
246
247
248
249
250
251
252