KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcorporate > expresso > services > dbobj > UserGroup


1 /* ====================================================================
2  * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
3  *
4  * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution,
19  * if any, must include the following acknowledgment:
20  * "This product includes software developed by Jcorporate Ltd.
21  * (http://www.jcorporate.com/)."
22  * Alternately, this acknowledgment may appear in the software itself,
23  * if and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. "Jcorporate" and product names such as "Expresso" must
26  * not be used to endorse or promote products derived from this
27  * software without prior written permission. For written permission,
28  * please contact info@jcorporate.com.
29  *
30  * 5. Products derived from this software may not be called "Expresso",
31  * or other Jcorporate product names; nor may "Expresso" or other
32  * Jcorporate product names appear in their name, without prior
33  * written permission of Jcorporate Ltd.
34  *
35  * 6. No product derived from this software may compete in the same
36  * market space, i.e. framework, without prior written permission
37  * of Jcorporate Ltd. For written permission, please contact
38  * partners@jcorporate.com.
39  *
40  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
41  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
42  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
43  * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
44  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
45  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
46  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
47  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
48  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
49  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
50  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  * ====================================================================
53  *
54  * This software consists of voluntary contributions made by many
55  * individuals on behalf of the Jcorporate Ltd. Contributions back
56  * to the project(s) are encouraged when you make modifications.
57  * Please send them to support@jcorporate.com. For more information
58  * on Jcorporate Ltd. and its products, please see
59  * <http://www.jcorporate.com/>.
60  *
61  * Portions of this software are based upon other open source
62  * products and are subject to their respective licenses.
63  */

64
65 package com.jcorporate.expresso.services.dbobj;
66
67 import com.jcorporate.expresso.core.controller.ControllerRequest;
68 import com.jcorporate.expresso.core.db.DBConnection;
69 import com.jcorporate.expresso.core.db.DBException;
70 import com.jcorporate.expresso.core.dbobj.SecuredDBObject;
71 import com.jcorporate.expresso.core.i18n.Messages;
72
73 import java.util.Iterator JavaDoc;
74 import java.util.List JavaDoc;
75 import java.util.Vector JavaDoc;
76
77
78 /**
79  * <p>UserGroup is a grouping of a number of users for security purposes.
80  * UserGroups are equivalent to 'roles' in other terminology.</p>
81  *
82  * @author Michael Nash
83  * @since Expresso 1.0
84  */

85 public class UserGroup
86         extends SecurityDBObject {
87
88     public static final String JavaDoc GROUP_NAME_FIELD = "GroupName";
89     public static final String JavaDoc GROUP_DESCRIPTION = "Descrip";
90     public static final int GROUP_NAME_MAX_LEN = 10;
91     public static final int GROUP_DESCRIP_MAX_LEN = 80;
92     public static final String JavaDoc TABLE_NAME = "USERROLES";
93
94     /**
95      * used as default group for all
96      * users who register and their reg domain has no other group set
97      *
98      * @see com.jcorporate.expresso.services.controller.SimpleRegistration
99      */

100     public static final String JavaDoc ALL_USERS_GROUP = "Everybody";
101     public static final String JavaDoc DEMO_GROUP = "Demo";
102
103     /**
104      * groups created as part of DBTool.setupSecurity bootstrap
105      */

106     public static final String JavaDoc UNKNOWN_USERS_GROUP = "Nobody";
107     public static final String JavaDoc NOT_REG_USERS_GROUP = "NotReg";
108     public static final String JavaDoc ADMIN_GROUP = "Admin";
109
110
111     /**
112      * construct object with superuser privileges
113      *
114      * @see com.jcorporate.expresso.core.dbobj.SecuredDBObject
115      */

116     public UserGroup() throws DBException {
117     } /* UserGroup() */
118
119     /**
120      * Initializes the usergroup with the permissions of the given user.
121      *
122      * @param uid the User's uid
123      * @throws DBException upon instantion error
124      */

125     public UserGroup(int uid) throws DBException {
126         super(uid);
127     }
128
129     /**
130      * For using DBObjects within Controllers. Initializes based upon the current
131      * user and the requested db. [Of course this can be modified later]
132      *
133      * @param request - The controller request handed to you by the framework.
134      */

135     public UserGroup(ControllerRequest request) throws DBException {
136         super(request);
137     }
138
139     /**
140      * constructor for db transactions; object will have superuser privileges unless you separately call setRequestingUid()
141      *
142      * @param localConnection the connection which should be used, typically because of an ongoing transaction
143      */

144     public UserGroup(DBConnection localConnection) throws DBException {
145         if (localConnection != null) {
146             setConnection(localConnection);
147         }
148     }
149
150     /**
151      * Check referential integrity of objects referring to this object
152      *
153      * @throws DBException If the integrity cannot be verified
154      */

155     protected void checkAllReferredToBy()
156             throws DBException {
157         referredToBy(new DBObjSecurity(SecuredDBObject.SYSTEM_ACCOUNT),
158                 GROUP_NAME_FIELD,
159                 "This Group (" + getField(GROUP_NAME_FIELD) +
160                 ") is in use by a Database Object security entry");
161         referredToBy(new ControllerSecurity(SecuredDBObject.SYSTEM_ACCOUNT),
162                 GROUP_NAME_FIELD,
163                 "This Group (" + getField(GROUP_NAME_FIELD) +
164                 ") is in use by a Controller security entry");
165         referredToBy(new JobSecurity(SecuredDBObject.SYSTEM_ACCOUNT),
166                 GROUP_NAME_FIELD,
167                 "This Group (" + getField(GROUP_NAME_FIELD) +
168                 ") is in use by a Job security entry");
169         referredToBy(new GroupMembers(SecuredDBObject.SYSTEM_ACCOUNT),
170                 GROUP_NAME_FIELD,
171                 "This Group (" + getField(GROUP_NAME_FIELD) +
172                 ") still has members ");
173         referredToBy(new GroupNest(SecuredDBObject.SYSTEM_ACCOUNT),
174                 GroupNest.FLD_GROUPNAME,
175                 "This Group (" + getField(GROUP_NAME_FIELD) +
176                 ") is in use by a Group Member Nesting entry");
177         referredToBy(new GroupNest(SecuredDBObject.SYSTEM_ACCOUNT),
178                 GroupNest.FLD_MEMBEROF,
179                 "This Group (" + getField(GROUP_NAME_FIELD) +
180                 ") is in use by a Group Member Nesting entry");
181     } /* checkAllReferredToBy() */
182
183
184     /**
185      * Extend the super.delete() method to first delete the GroupMembers
186      * elements that refer to the group being deleted
187      */

188     public void delete()
189             throws DBException {
190         GroupMembers groupMList = new GroupMembers(SecuredDBObject.SYSTEM_ACCOUNT);
191         groupMList.setDataContext(getDataContext());
192         groupMList.setField(GROUP_NAME_FIELD, getField(GROUP_NAME_FIELD));
193
194         GroupMembers groupM = null;
195
196         for (Iterator JavaDoc e = groupMList.searchAndRetrieveList().iterator();
197              e.hasNext();) {
198             groupM = (GroupMembers) e.next();
199             groupM.delete();
200         }
201
202         super.delete();
203     } /* delete() */
204
205
206     /**
207      * @see com.jcorporate.expresso.core.dbobj.SecuredDBObject
208      */

209     protected synchronized void setupFields()
210             throws DBException {
211         setTargetTable(TABLE_NAME);
212         setDescription("DBuserGroup");
213         setCharset("ISO-8859-1");
214         addField(GROUP_NAME_FIELD, "char", GROUP_NAME_MAX_LEN, false, "groupName");
215         addField(GROUP_DESCRIPTION, "varchar", GROUP_DESCRIP_MAX_LEN, true, "groupDescrip");
216         addField("LoginEvent", "char", 30, true, "loginEvent");
217         setStringFilter(GROUP_NAME_FIELD, "stripFilter");
218         setStringFilter("LoginEvent", "stripFilter");
219         setStringFilter(GROUP_DESCRIPTION, "standardFilter");
220         addKey(GROUP_NAME_FIELD);
221         setLookupObject("LoginEvent",
222                 "com.jcorporate.expresso.services.dbobj.Event");
223         addDetail("com.jcorporate.expresso.services.dbobj.GroupMembers",
224                 GROUP_NAME_FIELD, GROUP_NAME_FIELD);
225     } /* setupFields() */
226
227
228     /**
229      * Gets the valid values, specifically it returns a map of GroupNames
230      * to GroupDescriptions
231      *
232      * @return a vector of valid values.
233      */

234     public Vector JavaDoc getValues()
235             throws DBException {
236         return getValuesDefault(GROUP_NAME_FIELD, GROUP_DESCRIPTION);
237     } /* getValues() */
238
239
240     /**
241      * Populates the default user groups.
242      */

243     public synchronized void populateDefaultValues()
244             throws DBException {
245         UserGroup oneGroup = new UserGroup();
246         oneGroup.setDataContext(getDataContext());
247         oneGroup.setField(GROUP_NAME_FIELD, DEMO_GROUP);
248
249         if (!oneGroup.find()) {
250             oneGroup.setField(GROUP_NAME_FIELD, DEMO_GROUP);
251             oneGroup.setField(GROUP_DESCRIPTION, "Demonstration Group");
252             oneGroup.add();
253         }
254
255         oneGroup.clear();
256         oneGroup.setField(GROUP_NAME_FIELD, ALL_USERS_GROUP);
257
258         if (!oneGroup.find()) {
259             oneGroup.setField(GROUP_NAME_FIELD, ALL_USERS_GROUP);
260             oneGroup.setField(GROUP_DESCRIPTION, Messages.getString("All_Users"));
261             oneGroup.add();
262         }
263     } /* populateDefaultValues() */
264
265     /**
266      * convenience method
267      *
268      * @return name of group
269      * @throws DBException upon error
270      */

271     public String JavaDoc getGroupName() throws DBException {
272         return getField(GROUP_NAME_FIELD);
273     }
274
275     /**
276      * convenience method
277      *
278      * @param groupName the new gropu name
279      * @throws DBException upon error
280      */

281     public void setGroupName(String JavaDoc groupName) throws DBException {
282         setField(GROUP_NAME_FIELD, groupName);
283     }
284
285     /**
286      * @param groupname the new group name
287      * @return group for this name, or null if not found; uses "default" dbcontext
288      * @throws DBException upon error
289      */

290     public static UserGroup getGroup(String JavaDoc groupname) throws DBException {
291         UserGroup result = null;
292         if (groupname != null && groupname.length() > 0) {
293             UserGroup oneGroup = new UserGroup();
294             oneGroup.setDBName(DBConnection.DEFAULT_DB_CONTEXT_NAME);
295             oneGroup.setGroupName(groupname);
296             if (oneGroup.find()) {
297                 result = oneGroup;
298             }
299         }
300
301         return result;
302     }
303
304     /**
305      * convenience method
306      *
307      * @return java.lang.String the group description
308      * @throws DBException upon error
309      */

310     public String JavaDoc getGroupDescription() throws DBException {
311         return getField(GROUP_DESCRIPTION);
312     }
313
314     /**
315      * convenience method
316      * @return list of all groups
317      * @throws DBException upon error
318      */

319     public static List JavaDoc getAllGroups() throws DBException {
320         UserGroup search = new UserGroup();
321         return search.searchAndRetrieveList();
322     }
323 } /* UserGroup */
Popular Tags