KickJava   Java API By Example, From Geeks To Geeks.

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


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.DBException;
69 import com.jcorporate.expresso.core.servlet.CheckLogin;
70
71
72 /**
73  * This object defines the available user preference definitions.
74  * The populateDefaultValues methods of various db objects can populate
75  * this table to "register" the fact that they are providing preference
76  * values that the user can set. The actual preferences are set in UserPreference
77  * and the valid values (if any are defined) are specified in UserPreferenceVal
78  *
79  * @author Michael Nash
80  */

81 public class UserPreferenceDef
82         extends SecurityDBObject {
83
84     public static final String JavaDoc PREF_CODE = UserPreference.PREF_CODE;
85     public static final String JavaDoc CLASS_NAME = UserPreference.CLASS_NAME;
86     public static final String JavaDoc DESCRIPTION = "Descrip";
87     public static final String JavaDoc DEFAULT_VALUE = "DefaultVal";
88
89     /**
90      * constant for indicating that any class can use this preference; it is shared
91      */

92     public static final String JavaDoc ANY_CLASS = "*";
93     public static final String JavaDoc PRIMARY_GROUP_PREF = "PrimaryGroupPref";
94
95     /**
96      * @throws DBException upon creation error.
97      * @see com.jcorporate.expresso.core.dbobj.SecuredDBObject#SecuredDBObject
98      */

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

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

124     public UserPreferenceDef(ControllerRequest request)
125             throws DBException {
126         super(request);
127     }
128
129     /**
130      * @throws DBException upon initialization error
131      * @see com.jcorporate.expresso.core.dbobj.SecuredDBObject#setupFields
132      */

133     public void setupFields()
134             throws DBException {
135         setTargetTable("USERPREFDEF");
136         setDescription("DBuserPrefDef");
137         setCharset("ISO-8859-1");
138         addField(CLASS_NAME, "varchar", 128, false, "className");
139         addField(PREF_CODE, "varchar", 30, false, "prefCode");
140         addField(DESCRIPTION, "varchar", 132, true, "description");
141         addField(DEFAULT_VALUE, "varchar", 80, true, "defaultValue");
142         setStringFilter(CLASS_NAME, "stripFilter");
143         setStringFilter(PREF_CODE, "stripFilter");
144         addKey(CLASS_NAME);
145         addKey(PREF_CODE);
146         addDetail("com.jcorporate.expresso.services.dbobj.UserPreferenceVal",
147                 "ClassName|PrefCode", "ClassName|PrefCode");
148         addDetail("com.jcorporate.expresso.services.dbobj.UserPreference",
149                 "ClassName|PrefCode", "ClassName|PrefCode");
150     } /* setupFields() */
151
152
153     /**
154      * @throws DBException
155      */

156     public synchronized void populateDefaultValues()
157             throws DBException {
158
159         // add language pref
160
UserPreferenceDef oneDef = new UserPreferenceDef();
161         oneDef.setDataContext(getDataContext());
162         oneDef.setField(CLASS_NAME,
163                 com.jcorporate.expresso.core.servlet.CheckLogin.class.getName());
164         oneDef.setField(PREF_CODE, "language");
165
166         if (!oneDef.find()) {
167             oneDef.setField(DEFAULT_VALUE, "Browser");
168             oneDef.setField(DESCRIPTION, "Language");
169             oneDef.add();
170         }
171
172         // add valid values for language
173
UserPreferenceVal oneVal = new UserPreferenceVal();
174         oneVal.setDataContext(getDataContext());
175         oneVal.setField(CLASS_NAME, CheckLogin.class.getName());
176         oneVal.setField(PREF_CODE, "language");
177         oneVal.setField(UserPreference.PREF_VALUE, "Browser");
178         oneVal.setField(DESCRIPTION, "Browser Default");
179         oneVal.addIfNeeded();
180         oneVal.clear();
181         oneVal.setField(CLASS_NAME, CheckLogin.class.getName());
182         oneVal.setField(PREF_CODE, "language");
183         oneVal.setField(DESCRIPTION, "Spanish");
184         oneVal.setField(UserPreference.PREF_VALUE, "es");
185         oneVal.addIfNeeded();
186         oneVal.clear();
187         oneVal.setField(CLASS_NAME, CheckLogin.class.getName());
188         oneVal.setField(PREF_CODE, "language");
189         oneVal.setField(DESCRIPTION, "French");
190         oneVal.setField(UserPreference.PREF_VALUE, "fr");
191         oneVal.addIfNeeded();
192
193
194         // define another possible pref for country
195
oneDef.clear();
196         oneDef.setField(CLASS_NAME, CheckLogin.class.getName());
197         oneDef.setField(PREF_CODE, "country");
198         oneDef.setField(DEFAULT_VALUE, "default");
199         oneDef.setField(DESCRIPTION, "Country");
200         oneDef.addIfNeeded();
201
202
203         // add valid values for country
204
oneVal.clear();
205         oneVal.setField(CLASS_NAME, CheckLogin.class.getName());
206         oneVal.setField(PREF_CODE, "country");
207         oneVal.setField(DESCRIPTION, "Default");
208         oneVal.setField(UserPreference.PREF_VALUE, "default");
209         oneVal.addIfNeeded();
210
211
212         // add pref for primary group; any class can access
213
oneDef.clear();
214         oneDef.setField(CLASS_NAME, ANY_CLASS);
215         oneDef.setField(PREF_CODE, PRIMARY_GROUP_PREF);
216         oneDef.setField(DESCRIPTION, "Primary Group");
217         oneDef.setField(DEFAULT_VALUE, UserGroup.DEMO_GROUP);
218         oneDef.addIfNeeded();
219
220     } /* populateDefaultValues() */
221 }
Popular Tags