001: /* ====================================================================
002: * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
003: *
004: * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * 2. Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in
015: * the documentation and/or other materials provided with the
016: * distribution.
017: *
018: * 3. The end-user documentation included with the redistribution,
019: * if any, must include the following acknowledgment:
020: * "This product includes software developed by Jcorporate Ltd.
021: * (http://www.jcorporate.com/)."
022: * Alternately, this acknowledgment may appear in the software itself,
023: * if and wherever such third-party acknowledgments normally appear.
024: *
025: * 4. "Jcorporate" and product names such as "Expresso" must
026: * not be used to endorse or promote products derived from this
027: * software without prior written permission. For written permission,
028: * please contact info@jcorporate.com.
029: *
030: * 5. Products derived from this software may not be called "Expresso",
031: * or other Jcorporate product names; nor may "Expresso" or other
032: * Jcorporate product names appear in their name, without prior
033: * written permission of Jcorporate Ltd.
034: *
035: * 6. No product derived from this software may compete in the same
036: * market space, i.e. framework, without prior written permission
037: * of Jcorporate Ltd. For written permission, please contact
038: * partners@jcorporate.com.
039: *
040: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
041: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
042: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
043: * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
044: * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
045: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
046: * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
047: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
048: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
049: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
050: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
051: * SUCH DAMAGE.
052: * ====================================================================
053: *
054: * This software consists of voluntary contributions made by many
055: * individuals on behalf of the Jcorporate Ltd. Contributions back
056: * to the project(s) are encouraged when you make modifications.
057: * Please send them to support@jcorporate.com. For more information
058: * on Jcorporate Ltd. and its products, please see
059: * <http://www.jcorporate.com/>.
060: *
061: * Portions of this software are based upon other open source
062: * products and are subject to their respective licenses.
063: */
064:
065: package com.jcorporate.expresso.services.controller;
066:
067: /**
068: * EditUserPreference.java
069: *
070: * Copyright 2000, 2001 Jcorporate Ltd.
071: */
072:
073: import com.jcorporate.expresso.core.controller.ControllerException;
074: import com.jcorporate.expresso.core.controller.ControllerRequest;
075: import com.jcorporate.expresso.core.controller.ControllerResponse;
076: import com.jcorporate.expresso.core.controller.DBController;
077: import com.jcorporate.expresso.core.controller.Input;
078: import com.jcorporate.expresso.core.controller.Output;
079: import com.jcorporate.expresso.core.controller.ServletControllerRequest;
080: import com.jcorporate.expresso.core.controller.State;
081: import com.jcorporate.expresso.core.controller.Transition;
082: import com.jcorporate.expresso.core.db.DBException;
083: import com.jcorporate.expresso.core.i18n.Messages;
084: import com.jcorporate.expresso.core.jsdkapi.GenericSession;
085: import com.jcorporate.expresso.core.misc.StringUtil;
086: import com.jcorporate.expresso.services.dbobj.UserPreference;
087: import com.jcorporate.expresso.services.dbobj.UserPreferenceDef;
088: import com.jcorporate.expresso.services.dbobj.UserPreferenceVal;
089:
090: import javax.servlet.ServletException;
091: import javax.servlet.http.HttpServletRequest;
092: import java.util.Enumeration;
093: import java.util.Iterator;
094: import java.util.StringTokenizer;
095:
096: /**
097: * Allow a user to edit his/her user preferences, optionally restricting to a single
098: * set of preferences. Objects that have user preferences defined can call this
099: * controller, passing their classname, to allow editing of preferences
100: * specific to that object. Calling without specifying a classname allows
101: * editing of all of the user's own preferences.
102: *
103: * @author Michael Nash
104: */
105: public class EditUserPreference extends DBController {
106:
107: /**
108: * Our constructor "declares" what states we handle
109: */
110: public EditUserPreference() {
111: super ();
112:
113: State edit = new State("edit", "Edit Preferences");
114: edit.addOptionalParameter("class");
115: addState(edit);
116: setInitialState("edit");
117:
118: State update = new State("update", "Update Preferences");
119: addState(update);
120: this
121: .setSchema(com.jcorporate.expresso.core.ExpressoSchema.class);
122: } /* EditUserPreference() */
123:
124: /**
125: * Return the title of this Controller
126: *
127: * @return java.lang.String The Title of the controller
128: */
129: public String getTitle() {
130: return ("Edit User Preferences");
131: } /* getTitle() */
132:
133: /**
134: * Transition to a new state
135: *
136: * @param newState The new state to transition into
137: * @throws ControllerException
138: */
139: // public ControllerResponse newState(String newState, ControllerRequest params)
140: // throws ControllerException, NonHandleableException {
141: //
142: // ControllerResponse myResponse = super.newState(newState, params);
143: //
144: // try {
145: // if (newState.equals("edit")) {
146: // editState(myResponse, params);
147: //
148: // } else if (newState.equals("update")) {
149: // updateState(myResponse, params);
150: // }
151: // } catch (DBException de) {
152: // throw new ControllerException(de);
153: // }
154: //
155: // return myResponse;
156: //
157: // } /* newState(String) */
158: /**
159: * Prompt for values for all available preferences, or for only the specified class
160: * if a class is given
161: *
162: * @param params the ControllerRequest object
163: * @param myResponse the ControllerResponse object
164: * @throws DBException upon database access error
165: */
166: protected void runEditState(ControllerRequest params,
167: ControllerResponse myResponse) throws ControllerException,
168: DBException {
169: UserPreferenceDef defList = new UserPreferenceDef();
170: defList.setDataContext(params.getDataContext());
171:
172: if (!StringUtil.notNull(params.getParameter("class"))
173: .equals("")) {
174: defList.setField("ClassName", params.getParameter("class"));
175: }
176:
177: UserPreferenceDef oneDef = null;
178:
179: for (Iterator ee = defList.searchAndRetrieveList("ClassName")
180: .iterator(); ee.hasNext();) {
181: oneDef = (UserPreferenceDef) ee.next();
182:
183: String 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
192: .getField("ClassName"));
193: validVals.setField("PrefCode", oneDef.getField("PrefCode"));
194:
195: UserPreferenceVal oneVal = null;
196:
197: for (Iterator ev = validVals.searchAndRetrieveList()
198: .iterator(); ev.hasNext();) {
199: oneVal = (UserPreferenceVal) ev.next();
200: oneInput.addValidValue(oneVal.getField("PrefValue"),
201: oneVal.getField("Descrip"));
202: } /* for each valid value for this preference code */
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
209: .getField("ClassName"));
210: currentVal
211: .setField("PrefCode", oneDef.getField("PrefCode"));
212:
213: if (currentVal.find()) {
214: oneInput.setDefaultValue(currentVal
215: .getField("PrefValue"));
216: } else {
217: oneInput.setDefaultValue(oneDef.getField("DefaultVal"));
218: }
219: } /* for each defined preference */
220:
221: Transition update = new Transition("Update", getClass()
222: .getName());
223: update.addParam(STATE_PARAM_KEY, "update");
224: myResponse.addTransition(update);
225: } /* editState() */
226:
227: /**
228: * Update the values in UserPreference for the specified user
229: *
230: * @param params the ControllerRequest object
231: * @param myResponse the ControllerResponse object
232: * @throws DBException upon database access error
233: */
234: protected void runUpdateState(ControllerRequest params,
235: ControllerResponse myResponse) throws ControllerException,
236: DBException {
237: String oneParamName = null;
238: String oneClassName = null;
239: String onePrefCode = null;
240: int updatedCount = 0;
241: UserPreferenceDef oneDef = new UserPreferenceDef();
242: oneDef.setDataContext(params.getDataContext());
243:
244: UserPreference onePreference = new UserPreference();
245: onePreference.setDataContext(params.getDataContext());
246:
247: boolean reestablishLocale = false;
248: for (Enumeration p = params.getParameters().keys(); p
249: .hasMoreElements();) {
250: oneParamName = (String) p.nextElement();
251:
252: StringTokenizer stk = new StringTokenizer(oneParamName, "_");
253:
254: if (!stk.hasMoreTokens()) {
255: continue;
256: }
257:
258: oneClassName = stk.nextToken();
259:
260: if (!stk.hasMoreTokens()) {
261: continue;
262: }
263:
264: onePrefCode = stk.nextToken();
265: oneDef.clear();
266: oneDef.setField("ClassName", oneClassName);
267: oneDef.setField("PrefCode", onePrefCode);
268:
269: /* If it's a valid definition, either update or add the preference */
270: if (oneDef.find()) {
271: onePreference.clear();
272: onePreference.setField("ClassName", oneClassName);
273: onePreference.setField("PrefCode", onePrefCode);
274: onePreference.setField("ExpUid", params.getUid());
275:
276: if (onePreference.find()) {
277: if (!onePreference.getField("PrefValue").equals(
278: params.getParameter(oneParamName))) {
279: onePreference.setField("PrefValue", params
280: .getParameter(oneParamName));
281: onePreference.update();
282: updatedCount++;
283: }
284: } else {
285: onePreference.clear();
286: onePreference.setField("ClassName", oneClassName);
287: onePreference.setField("PrefCode", onePrefCode);
288: onePreference.setField("ExpUid", params.getUid());
289: onePreference.setField("PrefValue", params
290: .getParameter(oneParamName));
291: onePreference.add();
292: updatedCount++;
293: } /* else add new value */
294:
295: if ("com.jcorporate.expresso.core.servlet.CheckLogin"
296: .equals(oneClassName)) {
297: reestablishLocale = true;
298: }
299:
300: } /* if this was a valid definition */
301:
302: } /* updateState() */
303:
304: // need to reset the user's locale if they changed their language or country
305: if (reestablishLocale) {
306: ServletControllerRequest sr = (ServletControllerRequest) params;
307: HttpServletRequest hreq = (HttpServletRequest) sr
308: .getServletRequest();
309: try {
310: GenericSession.removeAttribute(hreq,
311: Messages.LOCALE_KEY);
312: Messages.establishLocale(hreq);
313: } catch (ServletException se) {
314: throw new ControllerException(se);
315: }
316: }
317:
318: Output count = new Output("" + updatedCount);
319: count.setLabel("Number of Settings Updated");
320: myResponse.addOutput(count);
321: } /* editState() */
322:
323: } /* UserPreference */
|