KickJava   Java API By Example, From Geeks To Geeks.

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


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.DBField;
71 import com.jcorporate.expresso.core.dbobj.SecuredDBObject;
72 import com.jcorporate.expresso.core.misc.StringUtil;
73 import com.jcorporate.expresso.core.security.User;
74 import com.jcorporate.expresso.core.security.UserListener;
75 import org.apache.log4j.Logger;
76
77 import java.util.Date JavaDoc;
78 import java.util.Iterator JavaDoc;
79
80
81 /**
82  * Group Member definitions table. IMPORTANT: Use getGroups() from the User object
83  * to determine which groups a certain user belongs to, as the implementation may
84  * or may not use this table - LDAP may be used instead, for example, or an external
85  * application
86  *
87  * @author Michael Nash
88  * @since Expresso 1.0
89  */

90 public class GroupMembers
91         extends SecurityDBObject
92         implements UserListener {
93
94     /* How many levels of nesting do we support? */
95     private static int maxDepth = 20;
96     private static Logger log = Logger.getLogger(GroupMembers.class);
97
98     public static final String JavaDoc GROUP_NAME = "GroupName";
99     public static final String JavaDoc EXPIRES = "Expires";
100     public static final String JavaDoc EXPUID = "ExpUid";
101     public static final String JavaDoc TABLE = "GROUPMEMBERS";
102
103
104     /* Add some user listeners, so that whenever a User object is added/modded/deleted some action can
105     * be taken by these classes. These classes all implement the UserListener interface.
106     */

107
108     static {
109         try {
110             User.addListener(new GroupMembers());
111         } catch (Exception JavaDoc e) {
112             e.printStackTrace();
113             System.err.println(e.getMessage());
114         }
115     }
116
117 //
118
// user.addListener((UserListener) new com.jcorporate.expresso.services.dbobj.GroupMembers());
119
// user.addListener((UserListener)new com.jcorporate.expresso.services.dbobj.UserPreference());
120

121     /**
122      * @see com.jcorporate.expresso.core.dbobj.SecuredDBObject
123      */

124     public GroupMembers()
125             throws DBException {
126     } /* GroupMembers() */
127
128
129     /**
130      * Initializes object with permissions given by user id.
131      *
132      * @param uid The Owner's uid
133      */

134     public GroupMembers(int uid)
135             throws DBException {
136         super(uid);
137     }
138
139     /**
140      * For using DBObjects within Controllers. Initializes based upon the current
141      * user and the requested db. [Of course this can be modified later]
142      *
143      * @param request - The controller request handed to you by the framework.
144      */

145     public GroupMembers(ControllerRequest request)
146             throws DBException {
147         super(request);
148     }
149
150     /**
151      * constructor for db transactions
152      *
153      * @param localConnection the DB connection which should be used, typically because of an ongoing transaction
154      */

155     public GroupMembers(DBConnection localConnection) throws DBException {
156         if (localConnection != null) {
157             setConnection(localConnection);
158         }
159     }
160
161     /**
162      * Extend add to handle nested groups
163      */

164     public void add()
165             throws DBException {
166         add(0);
167     } /* add() */
168
169
170     /**
171      * Extend add to handle nested groups
172      *
173      * @param depth The security depth to add nesting
174      * @throws DBException if there's an error nesting group members
175      */

176     private void add(int depth)
177             throws DBException {
178         if (depth > maxDepth) {
179             log.warn("Tried to add security group nesting more than " +
180                     maxDepth + " levels. Security was only added to " +
181                     maxDepth + " levels.");
182         }
183
184         GroupNest gnl = new GroupNest(SecuredDBObject.SYSTEM_ACCOUNT);
185         gnl.setDataContext(getDataContext());
186
187         GroupNest oneNest = null;
188         gnl.setField("GroupName", getField(GROUP_NAME));
189
190         for (Iterator JavaDoc enl = gnl.searchAndRetrieveList().iterator();
191              enl.hasNext();) {
192             oneNest = (GroupNest) enl.next();
193
194             GroupMembers newMember = new GroupMembers(SecuredDBObject.SYSTEM_ACCOUNT);
195             newMember.setDataContext(getDataContext());
196
197             /* check to see if the user is a member of the nested group. */
198
199             /* If not, add them */
200             newMember.clear();
201             newMember.setField(EXPUID, getField(EXPUID));
202             newMember.setField(GROUP_NAME, oneNest.getField("MemberOf"));
203
204             if (!newMember.find()) {
205                 newMember.add(depth + 1);
206             }
207         } /* for each nesting entry for this group */
208
209
210         super.add();
211     } /* add(int) */
212
213
214     public void addedUser(User user)
215             throws DBException {
216     }
217
218     /**
219      * Extends the checkAllRefs method to check for valid UserGroup
220      *
221      * @throws DBException If a referential integrity violation is found
222      */

223     protected void checkAllRefs()
224             throws DBException {
225         checkRef(GROUP_NAME, new UserGroup(),
226                 "Invalid " + getString(getMetaData().getDescription(GROUP_NAME)));
227     } /* checkAllRefs() */
228
229
230     /**
231      * Called when a User is deleted from the system.
232      *
233      * @param user The user that is deleted
234      * @throws DBException upon data access error
235      */

236     public void deletedUser(User user)
237             throws DBException {
238         GroupMembers gml = new GroupMembers();
239         gml.setDataContext(user.getDataContext());
240         gml.setField(EXPUID, user.getUid());
241
242         GroupMembers oneMember = null;
243
244         for (Iterator JavaDoc e = gml.searchAndRetrieveList().iterator(); e.hasNext();) {
245             oneMember = (GroupMembers) e.next();
246
247             /* We check this in case it's been removed by the time
248
249             /* we get to it by group nesting */

250             if (oneMember.find()) {
251                 oneMember.delete();
252             }
253         } /* for each group member listing */
254
255     }
256
257     public void loggedOffUser(User user)
258             throws DBException {
259     }
260
261     public void loggedOnUser(User user)
262             throws DBException {
263     }
264
265     public void modifiedUser(User user)
266             throws DBException {
267     }
268
269     /**
270      * Checks to see if the current date is past the expiration date.
271      *
272      * @return true if this group membership has expired
273      */

274     public boolean hasExpired() {
275         String JavaDoc expireDateString;
276         Date JavaDoc expireDate;
277
278         try {
279             expireDateString = StringUtil.notNull(this.getField(EXPIRES));
280
281             if (expireDateString.equals("")) {
282                 return false;
283             }
284
285             expireDate = this.getFieldDate(EXPIRES);
286         } catch (DBException e) {
287             log.error("Groupnest: Error getting Expiration Field");
288
289             return false;
290         }
291
292         Date JavaDoc now = new Date JavaDoc();
293
294         if (now.after(expireDate)) {
295             return true;
296         } else {
297             return false;
298         }
299     }
300
301     /**
302      * Set up the fields/tables for this object
303      */

304     protected synchronized void setupFields()
305             throws DBException {
306         setTargetTable(TABLE);
307         setName(TABLE);
308         setDescription("DBgroupMembers");
309         setCharset("ISO-8859-1");
310         addField(GROUP_NAME, DBField.CHAR_TYPE, 10, false, "groupName");
311         addField(EXPIRES, "date", 0, true, "dateExpires");
312         addField(EXPUID, DBField.INTEGER_TYPE, 0, false, "userId");
313         addVirtualField("LoginName", DBField.CHAR_TYPE, 30, "loginName");
314         addKey(EXPUID);
315         addKey(GROUP_NAME);
316         setStringFilter(GROUP_NAME, "stripFilter");
317         setMultiValued(GROUP_NAME);
318         setLookupObject(GROUP_NAME, UserGroup.class.getName());
319         setLookupObject(EXPUID, User.class.getName());
320
321         addIndex("groupname_idx", GROUP_NAME, false);
322     } /* setupFields() */
323
324
325     /**
326      * Extend the normal getField method
327      *
328      * @param fieldName the name of the field to retrieve
329      * @return the value of the field
330      */

331     public String JavaDoc getField(String JavaDoc fieldName)
332             throws DBException {
333         if (fieldName.equals("LoginName")) {
334             User u = new User();
335             String JavaDoc uidStr = getField(EXPUID);
336             if ((uidStr == null) || (uidStr.equals(""))) {
337                 u.setUid(0);
338             } else {
339                 u.setUid(getFieldInt(EXPUID));
340             }
341             u.setDataContext(this.getDataContext());
342
343             if (u.find()) {
344                 return u.getLoginName();
345             }
346
347             return "No such user";
348         }
349
350         return super.getField(fieldName);
351     } /* getField(String) */
352
353
354     /**
355      * set group name; convenience method
356      * @param grpname name to set
357      */

358     public void setGroupName(String JavaDoc grpname) throws DBException {
359         setField(GROUP_NAME, grpname);
360     }
361
362     /**
363      * set uid; convenience method
364      * @param uid uid to set
365      */

366     public void setUID(int uid) throws DBException {
367         setField(EXPUID, uid);
368     }
369 } /* GroupMembers */
Popular Tags