KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcorporate > expresso > ext > dbobj > RegisteredUser


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.ext.dbobj;
66
67 import com.jcorporate.expresso.core.db.DBConnection;
68 import com.jcorporate.expresso.core.db.DBException;
69 import com.jcorporate.expresso.core.db.exception.DBRecordNotFoundException;
70 import com.jcorporate.expresso.core.dbobj.RequestContext;
71 import com.jcorporate.expresso.core.dbobj.SecuredDBObject;
72 import com.jcorporate.expresso.core.dbobj.ValidValue;
73 import com.jcorporate.expresso.core.misc.DateTime;
74 import com.jcorporate.expresso.core.misc.EventHandler;
75 import com.jcorporate.expresso.core.misc.StringUtil;
76 import com.jcorporate.expresso.core.security.User;
77 import com.jcorporate.expresso.kernel.util.FastStringBuffer;
78
79 import java.util.Iterator JavaDoc;
80 import java.util.Vector JavaDoc;
81
82 /**
83  * Extention to the normal user object to record some additional information
84  *
85  * @author Michael Nash
86  */

87 public class RegisteredUser
88         extends com.jcorporate.expresso.services.dbobj.RegistrationDBObject {
89
90     /**
91      * Constructor
92      */

93     public RegisteredUser()
94             throws DBException {
95     } /* RegisteredUser() */
96
97     /**
98      * Constructor
99      *
100      * @param myConnection an already allocated DBConnection
101      */

102     public RegisteredUser(DBConnection myConnection)
103             throws DBException {
104         super(myConnection);
105     } /* RegisteredUser(DBConnection) */
106
107     /**
108      * Use over (String) constructor. Initializes the object in the context
109      * of the user who's uid belongs to the parameter.
110      *
111      * @param uid the Uid of the user context
112      * @throws DBException if there's an initialization problem
113      */

114     public RegisteredUser(int uid)
115             throws DBException {
116         super(uid);
117     }
118
119     /**
120      * For using DBObjects within Controllers. Initializes based upon the current
121      * user and the requested db. [Of course this can be modified later]
122      *
123      * @param request - The controller request handed to you by the framework.
124      * @throws DBException upon error
125      */

126     public RegisteredUser(RequestContext request)
127             throws DBException {
128         super(request);
129     }
130
131     /**
132      * @throws DBException
133      */

134     public void add()
135             throws DBException {
136         setField("AddedOn", DateTime.getDateTimeForDB(this.getDataContext()));
137         setField("UpdatedOn", DateTime.getDateTimeForDB(this.getDataContext()));
138         super.add();
139         sendNotice("New Registration:");
140     }
141
142     /**
143      * Sends a registration notice to the user.
144      *
145      * @param topic
146      */

147     private void sendNotice(String JavaDoc topic)
148             throws DBException {
149         FastStringBuffer theMessage = new FastStringBuffer(512);
150
151         theMessage.append(topic);
152         theMessage.append("\n\n");
153         User theUser = new User();
154         theUser.setDataContext(this.getDataContext());
155         theUser.setUid(getFieldInt("ExpUid"));
156         theUser.retrieve();
157         theMessage.append("Login Name:" + theUser.getLoginName() + "\n\n");
158
159         String JavaDoc oneField = null;
160
161         for (Iterator JavaDoc ef = this.getMetaData().getFieldListArray().iterator(); ef.hasNext();) {
162             oneField = (String JavaDoc) ef.next();
163             theMessage.append(getString(getMetaData().getDescription(oneField)) + ":" +
164                     getField(oneField) + "\n");
165         }
166
167         EventHandler.Event(getDataContext(), "REGISTER", theMessage.toString(),
168                 true);
169     } /* add() */
170
171
172     /**
173      * @param fieldName the name of the field to retrieve
174      * @return a field as a String
175      * @throws DBException upon error
176      */

177     public String JavaDoc getField(String JavaDoc fieldName)
178             throws DBException {
179         if (fieldName.equalsIgnoreCase("UserEMail")) {
180             User myUser = new User();
181             myUser.setDataContext(this.getDataContext());
182
183             if (StringUtil.notNull(getField("ExpUid")).equals("")) {
184                 return null;
185             }
186
187             myUser.setUid(getField("ExpUid"));
188
189             try {
190                 myUser.retrieve();
191
192                 return myUser.getEmail();
193             } catch (DBRecordNotFoundException de) {
194                 return "No Such User";
195             }
196         } else if (fieldName.equals("LoginName")) {
197             User u = new User();
198             u.setUid(getFieldInt("ExpUid"));
199             u.setDataContext(this.getDataContext());
200
201             if (u.find()) {
202                 return u.getLoginName();
203             }
204
205             return "No such user";
206         }
207
208         return super.getField(fieldName);
209     } /* getField(String) */
210
211
212     /**
213      * @param fieldName the name of the field to retrieve the valid values for
214      * @return java.util.Vector of ValidValue objects
215      * @throws DBException upon error
216      */

217     public Vector JavaDoc getValidValues(String JavaDoc fieldName)
218             throws DBException {
219         if (fieldName.equals("Employees")) {
220             Vector JavaDoc h = new Vector JavaDoc(5);
221             h.addElement(new ValidValue("0", "1 - 10"));
222             h.addElement(new ValidValue("A", "11 - 49"));
223             h.addElement(new ValidValue("1", "50 - 100"));
224             h.addElement(new ValidValue("2", "101-500"));
225             h.addElement(new ValidValue("3", "501-1000"));
226             h.addElement(new ValidValue("4", "1001-10,000"));
227             h.addElement(new ValidValue("5", "More than 10,000"));
228
229             return h;
230         } else if (fieldName.equals("Industry")) {
231             Vector JavaDoc h = new Vector JavaDoc(24);
232             h.addElement(new ValidValue("01", "Aerospace"));
233             h.addElement(new ValidValue("03", "Education"));
234             h.addElement(new ValidValue("04", "Engineering/Architecture"));
235             h.addElement(new ValidValue("FINANCIAL",
236                     "Financial Services/Banking"));
237             h.addElement(new ValidValue("05", "Finance/Accounting/Banking"));
238             h.addElement(new ValidValue("06", "Government"));
239             h.addElement(new ValidValue("HEALTH", "Healthcare/Medical"));
240             h.addElement(new ValidValue("MANUF", "Manufacturing"));
241             h.addElement(new ValidValue("09", "Marketing/PR"));
242             h.addElement(new ValidValue("10",
243                     "Process Industries (mine/farm/const.)"));
244             h.addElement(new ValidValue("11",
245                     "Publishing/Electronic Publishing"));
246             h.addElement(new ValidValue("12", "Research &amp; Development"));
247             h.addElement(new ValidValue("RETAIL",
248                     "Retailer/Distributor/Wholesaler"));
249             h.addElement(new ValidValue("TELECOM", "Telecommunications"));
250             h.addElement(new ValidValue("15", "Transportation"));
251             h.addElement(new ValidValue("16", "Utilities"));
252             h.addElement(new ValidValue("17", "Custom Development/Consulting"));
253             h.addElement(new ValidValue("SOFTWARE", "Software"));
254             h.addElement(new ValidValue("23", "Systems Integrator/VAR/OEM"));
255             h.addElement(new ValidValue("CONSULT", "Consulting"));
256             h.addElement(new ValidValue("OTHER", "Other (Specify in Comment)"));
257
258             return h;
259         } else if (fieldName.equals("JobTitle")) {
260             Vector JavaDoc h = new Vector JavaDoc();
261             h.addElement(new ValidValue("01", "Corporate Management"));
262             h.addElement(new ValidValue("02", "Other Management"));
263             h.addElement(new ValidValue("03", "IS/MIS/Technical Management"));
264             h.addElement(new ValidValue("04", "IS/MIS/Technical Staff"));
265             h.addElement(new ValidValue("05", "Internet/Web Management, Staff"));
266             h.addElement(new ValidValue("06", "Consultant"));
267             h.addElement(new ValidValue("07", "Educator/Trainer"));
268             h.addElement(new ValidValue("08", "Student"));
269             h.addElement(new ValidValue("09", "Developer"));
270             h.addElement(new ValidValue("10", "Independent Contractor"));
271
272             return h;
273         } else if (fieldName.equals("Country")) {
274             ISOCountryCodes countries = new ISOCountryCodes(SecuredDBObject.SYSTEM_ACCOUNT);
275             countries.setDataContext(this.getDataContext());
276             Vector JavaDoc v = countries.getValues();
277             return v;
278         }
279
280         return super.getValidValues(fieldName);
281     } /* getValidValues(String) */
282
283
284     /**
285      * @param subject subject of the email message
286      * @param message The message to send
287      * @throws DBException upon error
288      */

289     public void notify(String JavaDoc subject, String JavaDoc message)
290             throws DBException {
291         User myUser = new User();
292         myUser.setDataContext(this.getDataContext());
293         myUser.setUid(this.getField("ExpUid"));
294         if (!myUser.find()) {
295             throw new DBException("Unable to locate user: " + this.getField("ExpUid"));
296         }
297
298         myUser.notify(subject, message);
299     } /* notify(String, String) */
300
301
302     /**
303      * @throws DBException upon error
304      * @see com.jcorporate.expresso.core.dbobj.SecuredDBObject#setupFields
305      */

306     public void setupFields()
307             throws DBException {
308         setTargetTable("USERREGISTER");
309         setDescription("DBuserRegister");
310         setCharset("ISO-8859-1");
311         addField("ExpUid", "int", 0, false, "userId");
312         addVirtualField("LoginName", "char", 30, "loginName");
313         addField("FirstName", "varchar", 30, false, "firstName");
314         addField("LastName", "varchar", 80, false, "lastName");
315         addField("FullName", "varchar", 80, true, "yourName");
316         addField("CompanyName", "varchar", 30, true, "orgName");
317         addField("URL", "varchar", 255, true, "url");
318         addField("Address", "text", 0, false, "address");
319         addField("City", "varchar", 40, false, "city");
320         addField("StateProvince", "varchar", 40, false, "stateProvince");
321         addField("PostalCode", "varchar", 15, true, "postalZip");
322         addField("Country", "varchar", 40, false, "country");
323         setDefaultValue("Country", "US");
324         addField("UserComment", "text", 0, true, "userComment");
325         addField("Industry", "char", 10, false, "industry");
326         setDefaultValue("Industry", "17");
327         addField("Employees", "char", 1, false, "employees");
328         setDefaultValue("Employees", "0");
329         addField("JobTitle", "char", 2, true, "jobTitle");
330         addField("Subscribe", "char", 1, true, "subscribeToListServ");
331         setDefaultValue("Subscribe", "Y");
332         this.getMetaData().setAttribute("Subscribe", "checkbox", "Y");
333         addField("Contributing", "char", 1, true, "contributing");
334         setDefaultValue("Contributing", "N");
335         this.getMetaData().setAttribute("Contributing", "checkbox", "Y");
336         addField("Projects", "char", 1, true, "projectNotifications");
337         this.getMetaData().setAttribute("Projects", "checkbox", "Y");
338         setDefaultValue("Projects", "Y");
339         addField("IsActive", "char", 1, true, "active");
340         this.getMetaData().setAttribute("IsActive", "checkbox", "Y");
341         addField("InternalComment", "text", 0, true, "internalComment");
342         addField("AddedOn", "datetime", 0, true, "addedOn");
343         addField("UpdatedOn", "datetime", 0, true, "updatedOn");
344         addField("EmailPrivate", "char", 1, true, "keepEmailPrivate");
345         addField("Occupation", "text", 0, true, "occupation");
346         addField("Location", "text", 0, true, "location");
347         addField("Expertise", "text", 0, true, "expertise");
348         addField("Contributions", "text", 0, true, "contributions");
349         addField("OtherProjects", "text", 0, true, "otherProjects");
350         addField("Phone", "varchar", 80, true, "phone");
351         addField("Testimonial", "text", 0, true, "testimonial");
352         addField("IPAddress", "varchar", 30, true, "ipAddress");
353         setReadOnly("IPAddress");
354         setReadOnly("UpdatedOn");
355         setReadOnly("AddedOn");
356
357         //For all the other fields, the default of standard filter is fine.
358
//Saves on typing :-)
359
addVirtualField("UserEMail", "varchar", 80, "EMail Address");
360         setLookupObject("ExpUid", "com.jcorporate.expresso.core.security.User");
361         addKey("ExpUid");
362         setMultiValued("JobTitle");
363         setMultiValued("Industry");
364         setMultiValued("Employees");
365         setMultiValued("Country");
366     } /* setupFields() */
367
368
369     /**
370      * Updates the registered user
371      *
372      * @throws DBException upon error
373      */

374     public void update()
375             throws DBException {
376         setField("UpdatedOn", DateTime.getDateTimeForDB(this.getDataContext()));
377         super.update();
378         sendNotice("Updated Registration:");
379     } /* update() */
380
381
382 } /* RegisteredUser */
383
Popular Tags