KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcorporate > expresso > services > controller > EditUserPreference


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.controller;
66
67 /**
68  * EditUserPreference.java
69  *
70  * Copyright 2000, 2001 Jcorporate Ltd.
71  */

72
73 import com.jcorporate.expresso.core.controller.ControllerException;
74 import com.jcorporate.expresso.core.controller.ControllerRequest;
75 import com.jcorporate.expresso.core.controller.ControllerResponse;
76 import com.jcorporate.expresso.core.controller.DBController;
77 import com.jcorporate.expresso.core.controller.Input;
78 import com.jcorporate.expresso.core.controller.Output;
79 import com.jcorporate.expresso.core.controller.ServletControllerRequest;
80 import com.jcorporate.expresso.core.controller.State;
81 import com.jcorporate.expresso.core.controller.Transition;
82 import com.jcorporate.expresso.core.db.DBException;
83 import com.jcorporate.expresso.core.i18n.Messages;
84 import com.jcorporate.expresso.core.jsdkapi.GenericSession;
85 import com.jcorporate.expresso.core.misc.StringUtil;
86 import com.jcorporate.expresso.services.dbobj.UserPreference;
87 import com.jcorporate.expresso.services.dbobj.UserPreferenceDef;
88 import com.jcorporate.expresso.services.dbobj.UserPreferenceVal;
89
90 import javax.servlet.ServletException JavaDoc;
91 import javax.servlet.http.HttpServletRequest JavaDoc;
92 import java.util.Enumeration JavaDoc;
93 import java.util.Iterator JavaDoc;
94 import java.util.StringTokenizer JavaDoc;
95
96
97 /**
98  * Allow a user to edit his/her user preferences, optionally restricting to a single
99  * set of preferences. Objects that have user preferences defined can call this
100  * controller, passing their classname, to allow editing of preferences
101  * specific to that object. Calling without specifying a classname allows
102  * editing of all of the user's own preferences.
103  *
104  * @author Michael Nash
105  */

106 public class EditUserPreference
107         extends DBController {
108
109     /**
110      * Our constructor "declares" what states we handle
111      */

112     public EditUserPreference() {
113         super();
114
115         State edit = new State("edit", "Edit Preferences");
116         edit.addOptionalParameter("class");
117         addState(edit);
118         setInitialState("edit");
119
120         State update = new State("update", "Update Preferences");
121         addState(update);
122         this.setSchema(com.jcorporate.expresso.core.ExpressoSchema.class);
123     } /* EditUserPreference() */
124
125     /**
126      * Return the title of this Controller
127      *
128      * @return java.lang.String The Title of the controller
129      */

130     public String JavaDoc getTitle() {
131         return ("Edit User Preferences");
132     } /* getTitle() */
133
134     /**
135      * Transition to a new state
136      *
137      * @param newState The new state to transition into
138      * @throws ControllerException
139      */

140     // public ControllerResponse newState(String newState, ControllerRequest params)
141
// throws ControllerException, NonHandleableException {
142
//
143
// ControllerResponse myResponse = super.newState(newState, params);
144
//
145
// try {
146
// if (newState.equals("edit")) {
147
// editState(myResponse, params);
148
//
149
// } else if (newState.equals("update")) {
150
// updateState(myResponse, params);
151
// }
152
// } catch (DBException de) {
153
// throw new ControllerException(de);
154
// }
155
//
156
// return myResponse;
157
//
158
// } /* newState(String) */
159
/**
160      * Prompt for values for all available preferences, or for only the specified class
161      * if a class is given
162      *
163      * @param params the ControllerRequest object
164      * @param myResponse the ControllerResponse object
165      * @throws DBException upon database access error
166      */

167     protected void runEditState(ControllerRequest params,
168                                 ControllerResponse myResponse)
169             throws ControllerException, DBException {
170         UserPreferenceDef defList = new UserPreferenceDef();
171         defList.setDataContext(params.getDataContext());
172
173         if (!StringUtil.notNull(params.getParameter("class")).equals("")) {
174             defList.setField("ClassName", params.getParameter("class"));
175         }
176
177         UserPreferenceDef oneDef = null;
178
179         for (Iterator JavaDoc ee = defList.searchAndRetrieveList("ClassName").iterator();
180              ee.hasNext();) {
181             oneDef = (UserPreferenceDef) ee.next();
182
183             String JavaDoc oneInputName = oneDef.getField("ClassName") + "_" +
184                     oneDef.getField("PrefCode");
185             Input oneInput = new Input(oneInputName);
186             oneInput.setLabel(oneDef.getField("Descrip"));
187             myResponse.addInput(oneInput);
188
189             UserPreferenceVal validVals = new UserPreferenceVal();
190             validVals.setDataContext(params.getDataContext());
191             validVals.setField("ClassName", oneDef.getField("ClassName"));
192             validVals.setField("PrefCode", oneDef.getField("PrefCode"));
193
194             UserPreferenceVal oneVal = null;
195
196             for (Iterator JavaDoc ev = validVals.searchAndRetrieveList().iterator();
197                  ev.hasNext();) {
198                 oneVal = (UserPreferenceVal) ev.next();
199                 oneInput.addValidValue(oneVal.getField("PrefValue"),
200                         oneVal.getField("Descrip"));
201             } /* for each valid value for this preference code */
202
203
204             /* See if there is a current value */
205             UserPreference currentVal = new UserPreference();
206             currentVal.setDataContext(params.getDataContext());
207             currentVal.setField("ExpUid", params.getUid());
208             currentVal.setField("ClassName", oneDef.getField("ClassName"));
209             currentVal.setField("PrefCode", oneDef.getField("PrefCode"));
210
211             if (currentVal.find()) {
212                 oneInput.setDefaultValue(currentVal.getField("PrefValue"));
213             } else {
214                 oneInput.setDefaultValue(oneDef.getField("DefaultVal"));
215             }
216         } /* for each defined preference */
217
218
219         Transition update = new Transition("Update", getClass().getName());
220         update.addParam(STATE_PARAM_KEY, "update");
221         myResponse.addTransition(update);
222     } /* editState() */
223
224
225     /**
226      * Update the values in UserPreference for the specified user
227      *
228      * @param params the ControllerRequest object
229      * @param myResponse the ControllerResponse object
230      * @throws DBException upon database access error
231      */

232     protected void runUpdateState(ControllerRequest params,
233                                   ControllerResponse myResponse)
234             throws ControllerException, DBException {
235         String JavaDoc oneParamName = null;
236         String JavaDoc oneClassName = null;
237         String JavaDoc onePrefCode = null;
238         int updatedCount = 0;
239         UserPreferenceDef oneDef = new UserPreferenceDef();
240         oneDef.setDataContext(params.getDataContext());
241
242         UserPreference onePreference = new UserPreference();
243         onePreference.setDataContext(params.getDataContext());
244
245         boolean reestablishLocale = false;
246         for (Enumeration JavaDoc p = params.getParameters().keys();
247              p.hasMoreElements();) {
248             oneParamName = (String JavaDoc) p.nextElement();
249
250             StringTokenizer JavaDoc stk = new StringTokenizer JavaDoc(oneParamName, "_");
251
252             if (!stk.hasMoreTokens()) {
253                 continue;
254             }
255
256             oneClassName = stk.nextToken();
257
258             if (!stk.hasMoreTokens()) {
259                 continue;
260             }
261
262             onePrefCode = stk.nextToken();
263             oneDef.clear();
264             oneDef.setField("ClassName", oneClassName);
265             oneDef.setField("PrefCode", onePrefCode);
266
267             /* If it's a valid definition, either update or add the preference */
268             if (oneDef.find()) {
269                 onePreference.clear();
270                 onePreference.setField("ClassName", oneClassName);
271                 onePreference.setField("PrefCode", onePrefCode);
272                 onePreference.setField("ExpUid", params.getUid());
273
274                 if (onePreference.find()) {
275                     if (!onePreference.getField("PrefValue").equals(params.getParameter(oneParamName))) {
276                         onePreference.setField("PrefValue",
277                                 params.getParameter(oneParamName));
278                         onePreference.update();
279                         updatedCount++;
280                     }
281                 } else {
282                     onePreference.clear();
283                     onePreference.setField("ClassName", oneClassName);
284                     onePreference.setField("PrefCode", onePrefCode);
285                     onePreference.setField("ExpUid", params.getUid());
286                     onePreference.setField("PrefValue",
287                             params.getParameter(oneParamName));
288                     onePreference.add();
289                     updatedCount++;
290                 } /* else add new value */
291
292                 if ("com.jcorporate.expresso.core.servlet.CheckLogin".equals(oneClassName)) {
293                     reestablishLocale = true;
294                 }
295
296             } /* if this was a valid definition */
297
298         } /* updateState() */
299
300         // need to reset the user's locale if they changed their language or country
301
if (reestablishLocale) {
302             ServletControllerRequest sr = (ServletControllerRequest) params;
303             HttpServletRequest JavaDoc hreq = (HttpServletRequest JavaDoc) sr.getServletRequest();
304             try {
305                 GenericSession.removeAttribute(hreq, Messages.LOCALE_KEY);
306                 Messages.establishLocale(hreq);
307             } catch (ServletException JavaDoc se) {
308                 throw new ControllerException(se);
309             }
310         }
311
312         Output count = new Output("" + updatedCount);
313         count.setLabel("Number of Settings Updated");
314         myResponse.addOutput(count);
315     } /* editState() */
316
317
318 } /* UserPreference */
319
Popular Tags