KickJava   Java API By Example, From Geeks To Geeks.

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


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 package com.jcorporate.expresso.services.dbobj;
65
66 import com.jcorporate.expresso.core.controller.Controller;
67 import com.jcorporate.expresso.core.db.DBException;
68 import com.jcorporate.expresso.core.dbobj.RequestContext;
69 import com.jcorporate.expresso.core.dbobj.SecuredDBObject;
70 import com.jcorporate.expresso.core.dbobj.ValidValue;
71 import com.jcorporate.expresso.core.misc.ConfigManager;
72 import com.jcorporate.expresso.core.misc.StringUtil;
73 import com.jcorporate.expresso.core.security.User;
74
75 import java.util.Iterator JavaDoc;
76 import java.util.Vector JavaDoc;
77
78
79 /**
80  * A registration domain defines a registration context. A DB context can have
81  * multiple registration contexts. Each context defines a controller that
82  * takes care of registration for that context. Each context also defines
83  * whether the extended registration beyond that provided by the User object
84  * is required for the domain or not. The map of registration objects per
85  * registration domain is provided by the RegistrationObjectMap class.
86  * Creation date: (5/15/2001 12:26:11 PM)
87  *
88  * @author Shash Chatterjee
89  */

90 public class RegistrationDomain extends SecuredDBObject {
91     /**
92      * Default constructor
93      *
94      * @throws DBException The exception description.
95      */

96     public RegistrationDomain() throws DBException {
97         super();
98     }
99     /* RegistrationDomain() */
100
101     /**
102      * Use over (String) constructor. Initializes the object in the context of
103      * the user who's uid belongs to the parameter.
104      *
105      * @param uid the Uid of the user context
106      * @throws DBException if there's an initialization problem
107      */

108     public RegistrationDomain(int uid) throws DBException {
109         super(uid);
110     }
111
112     /**
113      * For using DBObjects within Controllers. Initializes based upon the
114      * current user and the requested db. [Of course this can be modified
115      * later]
116      *
117      * @param request - The controller request handed to you by the framework.
118      * @throws DBException upon initialization error
119      */

120     public RegistrationDomain(RequestContext request) throws DBException {
121         super(request);
122     }
123
124     /**
125      * Override the method getValidValues to provide specific values for our
126      * multi-valued fields
127      *
128      * @param fieldName Field name for which values are requested
129      * @return Vector The ValidValues field
130      * @throws DBException If the values cannot be retrieved
131      */

132     public Vector JavaDoc getValidValues(String JavaDoc fieldName) throws DBException {
133         if (fieldName.equals("RegRequired") || fieldName.equals("Approve") ||
134                 fieldName.equals("UserPasswd") ||
135                 fieldName.equals("EmailValidate") ||
136                 fieldName.equals("EmailAsLogin")) {
137             Vector JavaDoc myValues = new Vector JavaDoc(2);
138             myValues.addElement(new ValidValue("Y", "Yes"));
139             myValues.addElement(new ValidValue("N", "No"));
140
141             return myValues;
142         }
143
144         if (fieldName.equals("NotApprovedGrp")) {
145             Vector JavaDoc myValues = super.getValidValues(fieldName);
146             myValues.add(0, new ValidValue("", getString("notSelected")));
147             return myValues;
148         }
149
150         return super.getValidValues(fieldName);
151     }
152     /* getValidValues(String) */
153
154     /**
155      * @return Vector of ValidValue objects
156      */

157     public Vector JavaDoc getValues() throws DBException {
158         return getValuesDefault("RegDomId", "Name");
159     }
160     /* getValues() */
161
162     /**
163      * Extends the usual add method to check for duplicate entires and to
164      * provide an unique id
165      *
166      * @throws DBException If the next number could not be allocated or the add
167      * fails
168      * @see com.jcorporate.expresso.core.dbobj.SecuredDBObject#add
169      */

170     public void add() throws com.jcorporate.expresso.core.db.DBException {
171         RegistrationDomain rd = new RegistrationDomain();
172         rd.setDataContext(getDataContext());
173         rd.setField("Name", this.getField("Name"));
174
175         if (rd.find()) {
176             throw new DBException("Registration domain \"" +
177                     rd.getField("Name") + "\" already exists.");
178         }
179
180         if (getField("RegRequired").equals("")) {
181             setField("RegRequired", "Y");
182         }
183
184         if (getField("Approve").equals("")) {
185             setField("Approve", "N");
186         }
187
188         if (getField("UserPasswd").equals("")) {
189             setField("UserPasswd", "N");
190         }
191
192         if (getField("EmailValidate").equals("")) {
193             setField("EmailValidate", "Y");
194         }
195
196         if (getField("EmailAsLogin").equals("")) {
197             setField("EmailAsLogin", "Y");
198         }
199
200         if (getField("NotApprovedGrp").equals("")) {
201             setField("NotRegGrp", "NotReg");
202         }
203
204         if (getField("Approve").equals("Y")) {
205             if (getField("Approvers").equals("")) {
206                 setField("Approvers", User.ADMIN_USER);
207             }
208
209             if (getField("NotApprovedGrp").equals("")) {
210                 setField("NotApprovedGrp", "NotReg");
211             }
212         }
213
214         super.add();
215     }
216     /* add() */
217
218     /**
219      * Returns the default registration domain. Factory Method that is often
220      * called by a LoginController for self-registered users.
221      *
222      * @param dbContext the database context name
223      * @return Registration domain object
224      * @throws DBException upon construction failure
225      */

226     public static RegistrationDomain buildDefaultRegistrationDomain(String JavaDoc dbContext) throws DBException {
227         String JavaDoc regDomain = Setup.getValueRequired(dbContext, "defaultRegDomain");
228         RegistrationDomain rd = new RegistrationDomain(SecuredDBObject.SYSTEM_ACCOUNT);
229         rd.setDataContext(dbContext);
230         rd.setField("Name", regDomain);
231
232         if (rd.find()) {
233             return rd;
234         } else {
235             throw new DBException("Unable to locate default registration domain: '" + regDomain +
236                     "'");
237         }
238     }
239
240     /**
241      * Instantiates the controller based upon the current Controller Value
242      *
243      * @return Controller instance that performs registration
244      * @throws DBException upon error
245      */

246     public Controller getRegistrationObject() throws DBException {
247         StringUtil.assertNotBlank(this.getField("ControllerClass"),
248                 "Invalid Registration ControllerClassname for this object");
249
250         try {
251             return ConfigManager.getControllerFactory().getController(this.getField("ControllerClass"));
252         } catch (com.jcorporate.expresso.core.controller.ControllerException ce) {
253             throw new DBException("Error creating Registration controller", ce);
254         }
255     }
256
257     /**
258      * Extends the usual delete method to delete associated registration object
259      * entries
260      *
261      * @throws com.jcorporate.expresso.core.db.DBException
262      * If the next number
263      * could not be allocated or the add fails
264      * @see com.jcorporate.expresso.core.dbobj.SecuredDBObject#delete
265      */

266     public void delete() throws com.jcorporate.expresso.core.db.DBException {
267         RegistrationObjectMap rom = new RegistrationObjectMap();
268         rom.setDataContext(getDataContext());
269         rom.setField("RegDomId", this.getField("RegDomId"));
270
271         RegistrationObjectMap oneRom = null;
272
273         for (Iterator JavaDoc e = rom.searchAndRetrieveList().iterator(); e.hasNext();) {
274             oneRom = (RegistrationObjectMap) e.next();
275             oneRom.delete();
276         }
277
278         super.delete();
279     }
280
281     /**
282      * Extends the usual update method to check for duplicate entries
283      *
284      * @throws DBException If the next number could not be allocated or the add
285      * fails
286      */

287     public void update() throws com.jcorporate.expresso.core.db.DBException {
288         RegistrationDomain rd = new RegistrationDomain();
289         rd.setDataContext(getDataContext());
290         rd.setField("Name", this.getField("Name"));
291
292         if (rd.find() &&
293                 (!rd.getField("RegDomId").equals(this.getField("RegDomId")))) {
294             throw new DBException("Registration domain \"" +
295                     rd.getField("Name") + "\" already exists.");
296         }
297
298         if (getField("RegRequired").equals("")) {
299             setField("RegRequired", "Y");
300         }
301
302         if (getField("RegRequired").equals("")) {
303             setField("RegRequired", "Y");
304         }
305
306         if (getField("Approve").equals("")) {
307             setField("Approve", "N");
308         }
309
310         if (getField("UserPasswd").equals("")) {
311             setField("UserPasswd", "N");
312         }
313
314         if (getField("EmailValidate").equals("")) {
315             setField("EmailValidate", "Y");
316         }
317
318         if (getField("EmailAsLogin").equals("")) {
319             setField("EmailAsLogin", "Y");
320         }
321
322         if (getField("NotApprovedGrp").equals("")) {
323             setField("NotRegGrp", "NotReg");
324         }
325
326         if (getField("Approve").equals("Y")) {
327             if (getField("Approvers").equals("")) {
328                 setField("Approvers", User.ADMIN_USER);
329             }
330
331             if (getField("NotApprovedGrp").equals("")) {
332                 setField("NotApprovedGrp", "NotReg");
333             }
334         }
335
336         super.update();
337     }
338     /* update() */
339
340     /**
341      * Extends the checkAllRefs method to check for valid UserGroup or if
342      * referential integrity is violated
343      *
344      * @throws DBException If there is a problem checking the integrity
345      */

346     protected void checkAllRefs() throws DBException {
347         checkRef("GroupName", new UserGroup(SecuredDBObject.SYSTEM_ACCOUNT),
348                 "Invalid " + getString(getMetaData().getDescription("GroupName")));
349     }
350     /* checkAllRefs() */
351
352     /**
353      * Method to set up the fields for this database object. This method should
354      * be defined in the implementing object and should make calls to
355      * addField, addKey, setMultiValued, etc. as required to define the
356      * content of the DBObject
357      *
358      * @throws DBException If there is an error setting up the fields as
359      * requested. For example, if a field allowing null is requested
360      * as part of the key
361      */

362     protected void setupFields() throws DBException {
363         setTargetTable("REGDOMAIN");
364         setDescription("DBregDomain");
365         setCharset("ISO-8859-1");
366         addField("RegDomId", "auto-inc", 0, false, "regDomId");
367         addField("Name", "varchar", 30, false, "name");
368         addField("Description", "varchar", 128, false, "description");
369         addField("GroupName", "char", 10, false, "defaultGroup");
370         addField("ControllerClass", "varchar", 128, true, "regControllerClass");
371         addField("RegRequired", "char", 1, true, "regRequired");
372         addField("Approve", "char", 1, true, "approvalRequired");
373         addField("UserPasswd", "char", 1, true, "userPassword");
374         addField("EmailValidate", "char", 1, true, "emailValidate");
375         addField("EmailAsLogin", "char", 1, true, "emailAsLogin");
376         addField("Approvers", "text", 0, true, "loginNamesForApprovers");
377         addField("NotRegGrp", "char", 10, true, "groupBeforeRegistration");
378         addField("NotApprovedGrp", "char", 10, true, "groupBeforeApproval");
379         addKey("RegDomId");
380         setReadOnly("RegDomId");
381
382         //
383
//Add a database index to speed up name lookups
384
//
385
addIndex("RegDomainName", "Name", true);
386         setStringFilter("Name", "stripFilter");
387         setStringFilter("Description", "stripFilter");
388         setStringFilter("ControllerClass", "stripFilter");
389         setStringFilter("GroupName", "stripFilter");
390         setStringFilter("Approvers", "stripFilter");
391         setMultiValued("RegRequired");
392         setMultiValued("Approve");
393         setMultiValued("UserPasswd");
394         setMultiValued("EmailValidate");
395         setMultiValued("EmailAsLogin");
396         setMultiValued("GroupName");
397         setMultiValued("NotRegGrp");
398         setMultiValued("NotApprovedGrp");
399         setLookupObject("GroupName",
400                 "com.jcorporate.expresso.services.dbobj.UserGroup");
401         setLookupObject("NotRegGrp",
402                 "com.jcorporate.expresso.services.dbobj.UserGroup");
403         setLookupObject("NotApprovedGrp",
404                 "com.jcorporate.expresso.services.dbobj.UserGroup");
405         addDetail("com.jcorporate.expresso.services.dbobj.RegistrationObjectMap",
406                 "RegDomId", "RegDomId");
407     }
408     /* setupFields() */
409 }
410
Popular Tags