0001: /*
0002: * Copyright (c) 2001 - 2005 ivata limited.
0003: * All rights reserved.
0004: * -----------------------------------------------------------------------------
0005: * ivata groupware may be redistributed under the GNU General Public
0006: * License as published by the Free Software Foundation;
0007: * version 2 of the License.
0008: *
0009: * These programs are free software; you can redistribute them and/or
0010: * modify them under the terms of the GNU General Public License
0011: * as published by the Free Software Foundation; version 2 of the License.
0012: *
0013: * These programs are distributed in the hope that they will be useful,
0014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
0015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
0016: *
0017: * See the GNU General Public License in the file LICENSE.txt for more
0018: * details.
0019: *
0020: * If you would like a copy of the GNU General Public License write to
0021: *
0022: * Free Software Foundation, Inc.
0023: * 59 Temple Place - Suite 330
0024: * Boston, MA 02111-1307, USA.
0025: *
0026: *
0027: * To arrange commercial support and licensing, contact ivata at
0028: * http://www.ivata.com/contact.jsp
0029: * -----------------------------------------------------------------------------
0030: * $Log: SettingForm.java,v $
0031: * Revision 1.4 2005/10/11 18:57:17 colinmacleod
0032: * Fixed some checkstyle and javadoc issues.
0033: *
0034: * Revision 1.3 2005/10/03 10:21:15 colinmacleod
0035: * Fixed some style and javadoc issues.
0036: *
0037: * Revision 1.2 2005/10/02 14:08:59 colinmacleod
0038: * Added/improved log4j logging.
0039: *
0040: * Revision 1.1 2005/09/29 13:06:05 colinmacleod
0041: * First version of setting subproject.
0042: * Existing classes restructured, new setting user interface created.
0043: * Flexible XML UI configuration makes it easy to reuse the same web pages in
0044: * other projects.
0045: * Web files work as stand-alone webapp for testing.
0046: *
0047: * Revision 1.15 2003/03/05 13:29:11 peter
0048: * fixed the validation for boolean
0049: *
0050: * Revision 1.14 2003/03/05 11:02:23 peter
0051: * added handling for setting checkboxes - boolean settings
0052: *
0053: * Revision 1.13 2003/03/05 07:36:44 peter
0054: * fixed fieldNames in client-side validation
0055: *
0056: * Revision 1.12 2003/02/28 09:38:28 jano
0057: * RuntimeException(e) -> IntrnetRuntimeException
0058: *
0059: * Revision 1.11 2003/02/24 18:56:15 colin
0060: * added to admin
0061: *
0062: * Revision 1.10 2003/02/20 20:47:34 colin
0063: * improved validation by adding ValidationField and ValidationException
0064: *
0065: * Revision 1.9 2003/02/19 18:20:46 peter
0066: * fixed validation and input format doubling
0067: *
0068: * Revision 1.8 2003/02/18 17:04:42 peter
0069: * fixed checkBox resetting in reset()
0070: *
0071: * Revision 1.7 2003/02/14 17:02:02 peter
0072: * dateTime settings added
0073: *
0074: * Revision 1.6 2003/02/12 14:01:42 peter
0075: * reset for settingOverride checkbox Map fixed
0076: *
0077: * Revision 1.3 2003/02/05 18:15:07 peter
0078: * works on validation, and changed to Map backed form
0079: */
0080: package com.ivata.groupware.admin.setting.struts;
0081:
0082: import org.apache.log4j.Logger;
0083:
0084: import java.util.Arrays;
0085: import java.util.HashMap;
0086: import java.util.Iterator;
0087: import java.util.List;
0088: import java.util.Locale;
0089: import java.util.Map;
0090: import java.util.ResourceBundle;
0091: import java.util.Set;
0092: import java.util.Vector;
0093:
0094: import javax.servlet.http.HttpServletRequest;
0095: import javax.servlet.http.HttpSession;
0096:
0097: import org.apache.struts.Globals;
0098: import org.apache.struts.action.ActionMapping;
0099:
0100: import com.ivata.groupware.admin.security.server.SecuritySession;
0101: import com.ivata.groupware.admin.setting.SettingConstants;
0102: import com.ivata.groupware.admin.setting.Settings;
0103: import com.ivata.mask.util.StringHandling;
0104: import com.ivata.mask.util.SystemException;
0105: import com.ivata.mask.validation.ValidationError;
0106: import com.ivata.mask.validation.ValidationErrors;
0107: import com.ivata.mask.web.struts.DialogForm;
0108:
0109: /**
0110: * <p>
0111: * This form is processed in the action to change user or system-wide settings.
0112: * </p>
0113: *
0114: * @since ivata groupware 1.0 (2003-01-29)
0115: * @author Peter Illes
0116: * @author Colin MacLeod
0117: * <a href='mailto:colin.macleod@ivata.com'>colin.macleod@ivata.com</a>
0118: * @version $Revision: 1.4 $
0119: */
0120: public class SettingForm extends DialogForm {
0121: /**
0122: * Logger for this class.
0123: */
0124: private static final Logger logger = Logger
0125: .getLogger(SettingForm.class);
0126:
0127: /**
0128: * Serialization version (for <code>Serializable</code> interface).
0129: */
0130: private static final long serialVersionUID = 1L;
0131:
0132: /**
0133: * <copyDoc>Refer to {@link #isAdministrator}.</copyDoc>
0134: */
0135: private boolean administrator = false;
0136: /**
0137: * Contains extra info about the field, as a map of {@link FieldInfo}
0138: * instances, keyed by field name. If the map returns empty, a standard
0139: * text field is shown.
0140: */
0141: private Map fieldInfo = new HashMap();
0142: /**
0143: * <p>
0144: * Stores the area where the settings change takes place: "user"
0145: * or "system".
0146: * </p>
0147: */
0148: private String settingArea = "user";
0149: /**
0150: * <p>this map holds the override information for for all settings
0151: * stored in <code>userSettings</code> and <code>systemSettings</code>
0152: * as
0153: * boolean, it's only used when a user with rights to change system
0154: * settings is the current user. An element with the key setting name
0155: * has value <code>true</code> if users can override it....</p>
0156: */
0157: private Map settingOverride = new HashMap();
0158: /**
0159: * <copyDoc>Refer to {@link SettingForm#SettingForm}.</copyDoc>
0160: */
0161: private Settings settings;
0162: /**
0163: * <copyDoc>Refer to {@link #getSettingTabActiveTab()}.</copyDoc>
0164: */
0165: private Integer settingTabActiveTab = null;
0166: /**
0167: * <copyDoc>Refer to {@link #getSettingTabActiveTabKey()}.</copyDoc>
0168: */
0169: private String settingTabActiveTabKey;
0170: /**
0171: * <p>Stores the names of all settings (system and user) for individual tabs
0172: * as <code>List</code>s of <code>String</code>s.</p>
0173: */
0174: private List settingTabs = new Vector();
0175: /**
0176: * <p>Holds the types for all settings stored in
0177: * <code>userSettings</code> and <code>systemSettings</code>.</p>
0178: */
0179: private Map settingTypes = new HashMap();
0180: /**
0181: * <p>Holds the values of the system (as opposed to user) settings.</p>
0182: */
0183: private final Map systemSettings = new HashMap();
0184: /**
0185: * <p>Stores the names of system settings for individual tabs as
0186: * <code>List</code>s of <code>String</code>s.</p>
0187: */
0188: private List systemSettingTabs = new Vector();
0189: /**
0190: * Message resource strings for the tab labels.
0191: */
0192: private List tabKeys = new Vector();
0193: /**
0194: * <p>Holds the values of the user (as opposed to system) settings.</p>
0195: */
0196: private Map userSettings = new HashMap();
0197: /**
0198: * <p>stores the names of user settings for individual tabs as
0199: * <code>List</code>s of <code>String</code>s.</p>
0200: */
0201: private List userSettingTabs = new Vector();
0202:
0203: /**
0204: * Constructor. Normally called by <code>PicoContainer</code>.
0205: *
0206: * @param settingsParam Used for settings validation.
0207: */
0208: public SettingForm(final Settings settingsParam) {
0209: this .settings = settingsParam;
0210: }
0211:
0212: /**
0213: * <p>
0214: * Defines whether or not the current user can change system settings, or
0215: * only her own.
0216: * </p>
0217: *
0218: * @return <code>true</code> if the current user can change system-wide
0219: * settings.
0220: */
0221: public boolean isAdministrator() {
0222: if (logger.isDebugEnabled()) {
0223: logger.debug("isAdministrator() - start");
0224: }
0225:
0226: if (logger.isDebugEnabled()) {
0227: logger.debug("isAdministrator() - end - return value = "
0228: + administrator);
0229: }
0230: return administrator;
0231: }
0232:
0233: /**
0234: * Contains extra info about the field, as a map of {@link FieldInfo}
0235: * instances, keyed by field name. If the map returns empty, a standard
0236: * text field is shown.
0237: * @param settingName name of the settig for which to return the extra
0238: * info.
0239: * @return Returns the fieldInfo.
0240: */
0241: public FieldInfo getFieldInfo(final String settingName) {
0242: if (logger.isDebugEnabled()) {
0243: logger.debug("getFieldInfo(String settingName = "
0244: + settingName + ") - start");
0245: }
0246:
0247: FieldInfo returnFieldInfo = (FieldInfo) fieldInfo
0248: .get(settingName);
0249: if (logger.isDebugEnabled()) {
0250: logger.debug("getFieldInfo(String) - end - return value = "
0251: + returnFieldInfo);
0252: }
0253: return returnFieldInfo;
0254: }
0255:
0256: /**
0257: * <p>
0258: * Returns the user setting if <code>settingArea</code> is currently
0259: * "<code>user</code>", otherwise the system setting value is
0260: * returned.
0261: * </p>
0262: * <p>
0263: * The modifier (setter) for this property does nothing. The settings
0264: * should be set via hidden fields and JavaScript.
0265: * </p>
0266: * @param key the name of the setting
0267: * @return empty string
0268: */
0269: public String getSetting(final String key) {
0270: if (logger.isDebugEnabled()) {
0271: logger
0272: .debug("getSetting(String key = " + key
0273: + ") - start");
0274: }
0275:
0276: if ("user".equals(settingArea)) {
0277: assert (userSettings.get(key) != null);
0278: String returnString = userSettings.get(key).toString();
0279: if (logger.isDebugEnabled()) {
0280: logger
0281: .debug("getSetting(String) - end - return value = "
0282: + returnString);
0283: }
0284: return returnString;
0285: }
0286: assert (systemSettings.get(key) != null);
0287: String returnString = systemSettings.get(key).toString();
0288: if (logger.isDebugEnabled()) {
0289: logger.debug("getSetting(String) - end - return value = "
0290: + returnString);
0291: }
0292: return returnString;
0293: }
0294:
0295: /**
0296: * <p>Gets the area where the settings change takes place:
0297: * either "user" or "system".</p>
0298: *
0299: * @return "system" if system-wide settings are being changed,
0300: * otherwise "user" means the current user's personal settings.
0301: */
0302: public String getSettingArea() {
0303: if (logger.isDebugEnabled()) {
0304: logger.debug("getSettingArea() - start");
0305: }
0306:
0307: if (logger.isDebugEnabled()) {
0308: logger.debug("getSettingArea() - end - return value = "
0309: + settingArea);
0310: }
0311: return settingArea;
0312: }
0313:
0314: /**
0315: * <p>returns the names of all settings from the setting form as a
0316: * <code>Set</code>. The set may vary because of user rights and the
0317: * fact that some settings can be disabled (locked).</p>
0318: * @return names of settings
0319: */
0320: public Set getSettingName() {
0321: if (logger.isDebugEnabled()) {
0322: logger.debug("getSettingName() - start");
0323: }
0324:
0325: Set returnSet = settingTypes.keySet();
0326: if (logger.isDebugEnabled()) {
0327: logger.debug("getSettingName() - end - return value = "
0328: + returnSet);
0329: }
0330: return returnSet;
0331: }
0332:
0333: /**
0334: * <p>Get the override status for a setting, only used when the
0335: * current user has rights to change system settings.</p>
0336: *
0337: * @param key The name of the setting.
0338: * @return <code>true</code> when this setting can be overridden by
0339: * users, <code>false</code> otherwise.
0340: */
0341: public boolean getSettingOverride(final String key) {
0342: if (logger.isDebugEnabled()) {
0343: logger.debug("getSettingOverride(String key = " + key
0344: + ") - start");
0345: }
0346:
0347: boolean returnboolean = ((Boolean) settingOverride.get(key))
0348: .booleanValue();
0349: if (logger.isDebugEnabled()) {
0350: logger
0351: .debug("getSettingOverride(String) - end - return value = "
0352: + returnboolean);
0353: }
0354: return returnboolean;
0355: }
0356:
0357: /**
0358: * <p>
0359: * Get the number of the active tab field.
0360: * </p>
0361: *
0362: * @return the id of the active tab as <code>Integer</code>
0363: */
0364: public Integer getSettingTabActiveTab() {
0365: if (logger.isDebugEnabled()) {
0366: logger.debug("getSettingTabActiveTab() - start");
0367: }
0368:
0369: if (logger.isDebugEnabled()) {
0370: logger
0371: .debug("getSettingTabActiveTab() - end - return value = "
0372: + settingTabActiveTab);
0373: }
0374: return settingTabActiveTab;
0375: }
0376:
0377: /**
0378: * <p>
0379: * Get the currently selected message resource string.
0380: * </p>
0381: * <p>
0382: * This is more reliable than the tab index, as some tabs may not be shown
0383: * (depending on the system we're implementing, or the user rights).
0384: * </p>
0385: *
0386: * @return Returns the message resource string for the active tab.
0387: */
0388: public String getSettingTabActiveTabKey() {
0389: if (logger.isDebugEnabled()) {
0390: logger.debug("getSettingTabActiveTabKey() - start");
0391: }
0392:
0393: if (logger.isDebugEnabled()) {
0394: logger
0395: .debug("getSettingTabActiveTabKey() - end - return value = "
0396: + settingTabActiveTabKey);
0397: }
0398: return settingTabActiveTabKey;
0399: }
0400:
0401: /**
0402: * <p>Stores the names of all settings (system and user) for individual tabs
0403: * as <code>List</code>s of <code>String</code>s.</p>
0404: * @return Returns the settingTabs.
0405: */
0406: public List getSettingTabs() {
0407: if (logger.isDebugEnabled()) {
0408: logger.debug("getSettingTabs() - start");
0409: }
0410:
0411: if (logger.isDebugEnabled()) {
0412: logger.debug("getSettingTabs() - end - return value = "
0413: + settingTabs);
0414: }
0415: return settingTabs;
0416: }
0417:
0418: /**
0419: * <p>Get the names of system-wide settings to be shown on the current tab.
0420: * The setting names are returned as comma-separated string with quotes
0421: * (used for javascript array definition). This contains the system settings
0422: * which users (who are not administrators) <em>cannot</em> change.</p>
0423: *
0424: * @return Comma separated string with quotes (used for javascript
0425: * array definition)
0426: */
0427: public String getSettingTabSystemFields() {
0428: if (logger.isDebugEnabled()) {
0429: logger.debug("getSettingTabSystemFields() - start");
0430: }
0431:
0432: String returnString = "";
0433: assert (settingTabActiveTab != null);
0434: int index = settingTabActiveTab.intValue();
0435: assert (index < systemSettingTabs.size()) : index
0436: + " is out of range for system settings (max. "
0437: + (systemSettingTabs.size() - 1) + ")";
0438:
0439: for (Iterator i = ((List) systemSettingTabs.get(index))
0440: .iterator(); i.hasNext();) {
0441: returnString += ("'" + (String) i.next() + "'");
0442: if (i.hasNext()) {
0443: returnString += ",";
0444: }
0445: }
0446:
0447: if (logger.isDebugEnabled()) {
0448: logger
0449: .debug("getSettingTabSystemFields() - end - return value = "
0450: + returnString);
0451: }
0452: return returnString;
0453: }
0454:
0455: /**
0456: * <p>Get the names of user-specific settings to be shown on the current
0457: * tab. The setting names are returned as comma-separated string with
0458: * quotes (used for javascript array definition). This contains the user's
0459: * own settings - not the system-wide ones which require administrator
0460: * privileges.</p>
0461: *
0462: * @return Comma separated string with quotes (used for javascript
0463: * array definition)
0464: */
0465: public String getSettingTabUserFields() {
0466: if (logger.isDebugEnabled()) {
0467: logger.debug("getSettingTabUserFields() - start");
0468: }
0469:
0470: String returnString = "";
0471: assert (settingTabActiveTab != null);
0472: int index = settingTabActiveTab.intValue();
0473: assert (index < userSettingTabs.size()) : index
0474: + " is out of range for user settings (max. "
0475: + (userSettingTabs.size() - 1) + ")";
0476:
0477: for (Iterator i = ((List) userSettingTabs.get(index))
0478: .iterator(); i.hasNext();) {
0479: returnString += ("'" + (String) i.next() + "'");
0480: if (i.hasNext()) {
0481: returnString += ",";
0482: }
0483: }
0484:
0485: if (logger.isDebugEnabled()) {
0486: logger
0487: .debug("getSettingTabUserFields() - end - return value = "
0488: + returnString);
0489: }
0490: return returnString;
0491: }
0492:
0493: /**
0494: * <p>Get the type of a setting.</p>
0495: *
0496: * @param key The name of the setting.
0497: * @return The type of the setting as a constant defined in
0498: * <code>SettingConstants</code>.
0499: */
0500: public Integer getSettingType(final String key) {
0501: if (logger.isDebugEnabled()) {
0502: logger.debug("getSettingType(String key = " + key
0503: + ") - start");
0504: }
0505:
0506: Integer returnInteger = (Integer) settingTypes.get(key);
0507: if (logger.isDebugEnabled()) {
0508: logger
0509: .debug("getSettingType(String) - end - return value = "
0510: + returnInteger);
0511: }
0512: return returnInteger;
0513: }
0514:
0515: /**
0516: * <p>Get the value of a system-wide setting (requiring administrator
0517: * privileges to change it).</p>
0518: *
0519: * @param key The name of the setting.
0520: * @return The value of the setting.
0521: */
0522: public Object getSystemSetting(final String key) {
0523: if (logger.isDebugEnabled()) {
0524: logger.debug("getSystemSetting(String key = " + key
0525: + ") - start");
0526: }
0527:
0528: Object returnObject = systemSettings.get(key);
0529: if (logger.isDebugEnabled()) {
0530: logger
0531: .debug("getSystemSetting(String) - end - return value = "
0532: + returnObject);
0533: }
0534: return returnObject;
0535: }
0536:
0537: /**
0538: * <p>stores the names of system settings for individual tabs as
0539: * <code>List</code>s of <code>String</code>s.</p>
0540: *
0541: * @return the current value of systemSettingTabs.
0542: */
0543: public List getSystemSettingTabs() {
0544: if (logger.isDebugEnabled()) {
0545: logger.debug("getSystemSettingTabs() - start");
0546: }
0547:
0548: if (logger.isDebugEnabled()) {
0549: logger
0550: .debug("getSystemSettingTabs() - end - return value = "
0551: + systemSettingTabs);
0552: }
0553: return systemSettingTabs;
0554: }
0555:
0556: /**
0557: * Message resource strings for the tab labels.
0558: * @return Returns the tabKeys.
0559: */
0560: public List getTabKeys() {
0561: if (logger.isDebugEnabled()) {
0562: logger.debug("getTabKeys() - start");
0563: }
0564:
0565: if (logger.isDebugEnabled()) {
0566: logger.debug("getTabKeys() - end - return value = "
0567: + tabKeys);
0568: }
0569: return tabKeys;
0570: }
0571:
0572: /**
0573: * <p>Get the value of the user's personal setting.</p>
0574: *
0575: * @param key The name of the setting.
0576: * @return The value of the setting.
0577: */
0578: public Object getUserSetting(final String key) {
0579: if (logger.isDebugEnabled()) {
0580: logger.debug("getUserSetting(String key = " + key
0581: + ") - start");
0582: }
0583:
0584: Object returnObject = userSettings.get(key);
0585: if (logger.isDebugEnabled()) {
0586: logger
0587: .debug("getUserSetting(String) - end - return value = "
0588: + returnObject);
0589: }
0590: return returnObject;
0591: }
0592:
0593: /**
0594: * <p>stores the names of user settings for individual tabs as
0595: * <code>List</code>s of <code>String</code>s.</p>
0596: *
0597: * @return the current value of userSettingTabs.
0598: */
0599: public List getUserSettingTabs() {
0600: if (logger.isDebugEnabled()) {
0601: logger.debug("getUserSettingTabs() - start");
0602: }
0603:
0604: if (logger.isDebugEnabled()) {
0605: logger.debug("getUserSettingTabs() - end - return value = "
0606: + userSettingTabs);
0607: }
0608: return userSettingTabs;
0609: }
0610:
0611: /**
0612: * <p>Reset appopriate bean properties to their default state. This
0613: * method
0614: * is called before the properties are repopulated by the controller
0615: * servlet.</p>
0616: *
0617: * @param mapping The mapping used to select this instance
0618: * @param request The servlet request we are processing
0619: */
0620: public void reset(final ActionMapping mapping,
0621: final HttpServletRequest request) {
0622: if (logger.isDebugEnabled()) {
0623: logger.debug("reset(ActionMapping mapping = " + mapping
0624: + ", HttpServletRequest request = " + request
0625: + ") - start");
0626: }
0627:
0628: if (!StringHandling.isNullOrEmpty(settingTabActiveTabKey)
0629: && (settingTabActiveTab != null)) {
0630:
0631: // reset the override checkboxes from the last tab to
0632: // <code>false</code> because they're specific, only the
0633: // 'successful' ones are set
0634: int activeTab = settingTabActiveTab.intValue();
0635: Iterator i = ((List) getSystemSettingTabs().get(activeTab))
0636: .iterator();
0637: while (i.hasNext()) {
0638: String currentKey = (String) i.next();
0639: settingOverride.put(currentKey, new Boolean(false));
0640: }
0641: i = ((List) getUserSettingTabs().get(activeTab)).iterator();
0642: while (i.hasNext()) {
0643: String currentKey = (String) i.next();
0644: settingOverride.put(currentKey, new Boolean(false));
0645: }
0646: // reset the checkboxes on email tab
0647: if ("index.tab.email".equals(settingTabActiveTabKey)) {
0648: if (settingTypes.containsKey("emailSignatureForward")) {
0649: userSettings.put("emailSignatureForward", "false");
0650: systemSettings
0651: .put("emailSignatureForward", "false");
0652: }
0653: if (settingTypes.containsKey("emailSignatureReply")) {
0654: userSettings.put("emailSignatureReply", "false");
0655: systemSettings.put("emailSignatureReply", "false");
0656: }
0657: }
0658: }
0659:
0660: // buttons
0661: setApply(null);
0662: setOk(null);
0663:
0664: if (logger.isDebugEnabled()) {
0665: logger
0666: .debug("reset(ActionMapping, HttpServletRequest) - end");
0667: }
0668: }
0669:
0670: /**
0671: * <copyDoc>Refer to {@link #isAdministrator()}.</copyDoc>
0672: *
0673: * @param administratorParam
0674: * <copyDoc>Refer to {@link #isAdministrator()}.</copyDoc>
0675: */
0676: public void setAdministrator(final boolean administratorParam) {
0677: if (logger.isDebugEnabled()) {
0678: logger.debug("setAdministrator(boolean administrator = "
0679: + administratorParam + ") - start");
0680: }
0681:
0682: this .administrator = administratorParam;
0683:
0684: if (logger.isDebugEnabled()) {
0685: logger.debug("setAdministrator(String) - end");
0686: }
0687: }
0688:
0689: /**
0690: * <p>
0691: * Contains extra info about the field, as a map of {@link FieldInfo}
0692: * instances, keyed by field name. If the map returns empty, a standard
0693: * text field is shown.
0694: * </p>
0695: *
0696: * @param settingName name of the setting for which to set the extra
0697: * info.
0698: * @param value extra field info for this field, or <code>null</code> if
0699: * this field has no extra field info.
0700: */
0701: public void setFieldInfo(final String settingName,
0702: final FieldInfo value) {
0703: if (logger.isDebugEnabled()) {
0704: logger.debug("setFieldInfo(String settingName = "
0705: + settingName + ", FieldInfo value = " + value
0706: + ") - start");
0707: }
0708:
0709: if (value == null) {
0710: fieldInfo.remove(settingName);
0711: } else {
0712: fieldInfo.put(settingName, value);
0713: }
0714:
0715: if (logger.isDebugEnabled()) {
0716: logger.debug("setFieldInfo(String, FieldInfo) - end");
0717: }
0718: }
0719:
0720: /**
0721: * <p>
0722: * Set the last value from the visible field of a setting, but
0723: * this value is not used, {@see getSetting(String).
0724: * </p>
0725: * @param key The name of the setting.
0726: * @param value The value of the visible field of a setting.
0727: */
0728: public void setSetting(final String key, final String value) {
0729: if (logger.isDebugEnabled()) {
0730: logger.debug("setSetting(String key = " + key
0731: + ", String value = " + value + ") - start");
0732: }
0733:
0734: if (logger.isDebugEnabled()) {
0735: logger.debug("setSetting(String, String) - end");
0736: }
0737: }
0738:
0739: /**
0740: * <copyDoc>Refer to {@link #getSettingArea()}.</copyDoc>
0741: * @param settingAreaParam
0742: * <copyDoc>Refer to {@link #getSettingArea()}.</copyDoc>
0743: */
0744: public void setSettingArea(final String settingAreaParam) {
0745: if (logger.isDebugEnabled()) {
0746: logger.debug("setSettingArea(String settingArea = "
0747: + settingAreaParam + ") - start");
0748: }
0749:
0750: this .settingArea = settingAreaParam;
0751:
0752: if (logger.isDebugEnabled()) {
0753: logger.debug("setSettingArea(String) - end");
0754: }
0755: }
0756:
0757: /**
0758: * <p>
0759: * Set the override status for a setting. This is only used when the
0760: * current user has rights to change system settings.
0761: * </p>
0762: *
0763: * @param key the name of the setting
0764: * @param overridable <code>true</code> when this setting can be
0765: * overridden by users, <code>false</code> otherwise.
0766: */
0767: public void setSettingOverride(final String key,
0768: final boolean overridable) {
0769: if (logger.isDebugEnabled()) {
0770: logger.debug("setSettingOverride(String key = " + key
0771: + ", boolean overridable = " + overridable
0772: + ") - start");
0773: }
0774:
0775: settingOverride.put(key, new Boolean(overridable));
0776:
0777: if (logger.isDebugEnabled()) {
0778: logger.debug("setSettingOverride(String, boolean) - end");
0779: }
0780: }
0781:
0782: /**
0783: * <p>
0784: * Sets a value of the system settings (only set as administrator).
0785: * </p>
0786: * @param key the name of the setting
0787: * @param value the value of the setting
0788: */
0789: public void setSystemSetting(final String key, final Object value) {
0790: if (logger.isDebugEnabled()) {
0791: logger.debug("setSystemSetting(String key = " + key
0792: + ", Object value = " + value + ") - start");
0793: }
0794:
0795: if ((key.equals("emailSignatureReply") || key
0796: .equals("emailSignatureForward"))
0797: && !(value instanceof Boolean)
0798: && StringHandling.isNullOrEmpty((String) value)) {
0799: if (logger.isDebugEnabled()) {
0800: logger.debug("setSystemSetting(String, Object) - end");
0801: }
0802: return;
0803: } else {
0804: systemSettings.put(key, value);
0805: }
0806:
0807: if (logger.isDebugEnabled()) {
0808: logger.debug("setSystemSetting(String, Object) - end");
0809: }
0810: }
0811:
0812: /**
0813: * <copyDoc>Refer to {@link #getSettingTabActiveTab()}.</copyDoc>
0814: * @param settingTabActiveTabParam
0815: * <copyDoc>Refer to {@link #getSettingTabActiveTab()}.</copyDoc>
0816: */
0817: public void setSettingTabActiveTab(
0818: final Integer settingTabActiveTabParam) {
0819: if (logger.isDebugEnabled()) {
0820: logger
0821: .debug("setSettingTabActiveTab(Integer settingTabActiveTab = "
0822: + settingTabActiveTabParam + ") - start");
0823: }
0824:
0825: this .settingTabActiveTab = settingTabActiveTabParam;
0826:
0827: if (logger.isDebugEnabled()) {
0828: logger.debug("setSettingTabActiveTab(Integer) - end");
0829: }
0830: }
0831:
0832: /**
0833: * <copyDoc>Refer to {@link #getSettingTabActiveTabKey()}.</copyDoc>
0834: * @param settingTabActiveTabKeyParam
0835: * <copyDoc>Refer to {@link #getSettingTabActiveTabKey()}.</copyDoc>
0836: */
0837: public void setSettingTabActiveTabKey(
0838: final String settingTabActiveTabKeyParam) {
0839: if (logger.isDebugEnabled()) {
0840: logger.debug("setSettingTabActiveTabKey("
0841: + "String settingTabActiveTabKeyParam = "
0842: + settingTabActiveTabKeyParam + ") - start");
0843: }
0844:
0845: settingTabActiveTabKey = settingTabActiveTabKeyParam;
0846:
0847: if (logger.isDebugEnabled()) {
0848: logger.debug("setSettingTabActiveTabKey(String) - end");
0849: }
0850: }
0851:
0852: /**
0853: * <copyDoc>Refer to {@link #getSettingTabs}.</copyDoc>
0854: * @param settingTabsParam
0855: * <copyDoc>Refer to {@link #getSettingTabs}.</copyDoc>
0856: */
0857: public void setSettingTabs(final List settingTabsParam) {
0858: if (logger.isDebugEnabled()) {
0859: logger.debug("setSettingTabs(List settingTabsParam = "
0860: + settingTabsParam + ") - start");
0861: }
0862:
0863: settingTabs = settingTabsParam;
0864:
0865: if (logger.isDebugEnabled()) {
0866: logger.debug("setSettingTabs(List) - end");
0867: }
0868: }
0869:
0870: /**
0871: * <copyDoc>Refer to {@link #getSettingType(String)}.</copyDoc>
0872: * @param key the name of the setting
0873: * <copyDoc>Refer to {@link #getSettingType(String)}.</copyDoc>
0874: * @param type the type of the setting
0875: * <copyDoc>Refer to {@link #getSettingType(String)}.</copyDoc>
0876: */
0877: public void setSettingType(final String key, final int type) {
0878: if (logger.isDebugEnabled()) {
0879: logger.debug("setSettingType(String key = " + key
0880: + ", int type = " + type + ") - start");
0881: }
0882:
0883: settingTypes.put(key, new Integer(type));
0884:
0885: if (logger.isDebugEnabled()) {
0886: logger.debug("setSettingType(String, int) - end");
0887: }
0888: }
0889:
0890: /**
0891: * <copyDoc>Refer to {@link #getSystemSettingTabs()}.</copyDoc>
0892: * @param settingTabSystemParam
0893: * <copyDoc>Refer to {@link #getSystemSettingTabs()}.</copyDoc>
0894: */
0895: public void setSystemSettingTabs(final List settingTabSystemParam) {
0896: if (logger.isDebugEnabled()) {
0897: logger
0898: .debug("setSystemSettingTabs(List settingTabSystem = "
0899: + settingTabSystemParam + ") - start");
0900: }
0901:
0902: this .systemSettingTabs = settingTabSystemParam;
0903:
0904: if (logger.isDebugEnabled()) {
0905: logger.debug("setSystemSettingTabs(List) - end");
0906: }
0907: }
0908:
0909: /**
0910: * <copyDoc>Refer to {@link #getTabKeys}.</copyDoc>
0911: * @param tabKeysParam <copyDoc>Refer to {@link #getTabKeys}.</copyDoc>
0912: */
0913: public void setTabKeys(final List tabKeysParam) {
0914: if (logger.isDebugEnabled()) {
0915: logger.debug("setTabKeys(List tabKeysParam = "
0916: + tabKeysParam + ") - start");
0917: }
0918:
0919: tabKeys = tabKeysParam;
0920:
0921: if (logger.isDebugEnabled()) {
0922: logger.debug("setTabKeys(List) - end");
0923: }
0924: }
0925:
0926: /**
0927: * <copyDoc>Refer to {@link #getUserSetting(String)}.</copyDoc>
0928: * @param key the name of the setting
0929: * <copyDoc>Refer to {@link #getUserSetting(String)}.</copyDoc>
0930: * @param value the value of the setting
0931: * <copyDoc>Refer to {@link #getUserSetting(String)}.</copyDoc>
0932: */
0933: public void setUserSetting(final String key, final Object value) {
0934: if (logger.isDebugEnabled()) {
0935: logger.debug("setUserSetting(String key = " + key
0936: + ", Object value = " + value + ") - start");
0937: }
0938:
0939: if ((key.equals("emailSignatureReply") || key
0940: .equals("emailSignatureForward"))
0941: && !(value instanceof Boolean)
0942: && StringHandling.isNullOrEmpty((String) value)) {
0943: if (logger.isDebugEnabled()) {
0944: logger.debug("setUserSetting(String, Object) - end");
0945: }
0946: return;
0947: } else {
0948: userSettings.put(key, value);
0949: }
0950:
0951: if (logger.isDebugEnabled()) {
0952: logger.debug("setUserSetting(String, Object) - end");
0953: }
0954: }
0955:
0956: /**
0957: * <copyDoc>Refer to {@link #getUserSettingTabs()}.</copyDoc>
0958: *
0959: * @param settingTabUser
0960: * <copyDoc>Refer to {@link #getUserSettingTabs()}.</copyDoc>
0961: */
0962: public void setUserSettingTabs(final List settingTabUser) {
0963: if (logger.isDebugEnabled()) {
0964: logger.debug("setUserSettingTabs(List settingTabUser = "
0965: + settingTabUser + ") - start");
0966: }
0967:
0968: this .userSettingTabs = settingTabUser;
0969:
0970: if (logger.isDebugEnabled()) {
0971: logger.debug("setUserSettingTabs(List) - end");
0972: }
0973: }
0974:
0975: /**
0976: * Validate the settings for correct input,
0977: * and return an <code>ActionErrors</code> object that encapsulates
0978: * any validation errors that have been found. If no errors are found,
0979: * returns <code>null</code>
0980: *
0981: * @param requestParam The servlet request we are processing
0982: * @param sessionParam Current session from the request.
0983: * @return <code>ValidationErrors</code> instanace containing any errors
0984: * for the settings in this form.
0985: */
0986: public ValidationErrors validate(
0987: final HttpServletRequest requestParam,
0988: final HttpSession sessionParam) {
0989: if (logger.isDebugEnabled()) {
0990: logger.debug("validate(HttpServletRequest requestParam = "
0991: + requestParam + ", HttpSession sessionParam = "
0992: + sessionParam + ") - start");
0993: }
0994: ValidationErrors errors = new ValidationErrors();
0995: SecuritySession securitySession = (SecuritySession) sessionParam
0996: .getAttribute("securitySession");
0997: assert (securitySession != null);
0998: // only do the validation when ok or apply was pressed
0999: if (!(getOk() == null && getApply() == null)) {
1000: // we need these for server-side validation
1001: Locale locale = (Locale) sessionParam
1002: .getAttribute(Globals.LOCALE_KEY);
1003: // these maps will contain only the settings that were ok here
1004: Map settingUserValidation = new HashMap(this .userSettings);
1005: Map settingSystemValidation = new HashMap(
1006: this .systemSettings);
1007:
1008: // setting area identification (user or system) for error messages
1009: ResourceBundle adminBundle = ResourceBundle.getBundle(
1010: "com.ivata.intranet.admin.ApplicationResources",
1011: (Locale) sessionParam
1012: .getAttribute(Globals.LOCALE_KEY));
1013: String settingSystemArea = adminBundle
1014: .getString("setting.system");
1015: String settingUserArea = adminBundle
1016: .getString("setting.user");
1017:
1018: // first go through both the user system map and check the types
1019: for (Iterator i = settingTypes.keySet().iterator(); i
1020: .hasNext();) {
1021: String currentKey = (String) i.next();
1022: String currentFieldName = adminBundle
1023: .getString("setting.field." + currentKey);
1024: Integer currentType = (Integer) settingTypes
1025: .get(currentKey);
1026: // the values are strings when they were set from request or
1027: // correct types when the user hasn't submitted the appropriate
1028: // tab yet
1029: String currentUserValue = userSettings.get(currentKey)
1030: .toString();
1031: String currentSystemValue = systemSettings.get(
1032: currentKey).toString();
1033:
1034: switch (currentType.intValue()) {
1035: default:
1036: throw new RuntimeException(
1037: "Unrecognized setting type " + currentType);
1038: // check and conversion to Boolean
1039: case SettingConstants.DATA_TYPE_BOOLEAN:
1040: if (!(currentUserValue.equalsIgnoreCase("true") || currentUserValue
1041: .equalsIgnoreCase("false"))) {
1042: errors.add(new ValidationError(
1043: "errors.setting.boolean", Arrays
1044: .asList(new String[] {
1045: settingUserArea,
1046: currentFieldName })));
1047: settingUserValidation.remove(currentKey);
1048: } else {
1049: userSettings.put(currentKey, new Boolean(
1050: currentUserValue));
1051: settingUserValidation.put(currentKey,
1052: new Boolean(currentUserValue));
1053: }
1054: if (!(currentSystemValue.equalsIgnoreCase("true") || currentSystemValue
1055: .equalsIgnoreCase("false"))) {
1056: errors.add(new ValidationError(
1057: "errors.setting.boolean", Arrays
1058: .asList(new String[] {
1059: settingSystemArea,
1060: currentFieldName })));
1061: settingSystemValidation.remove(currentKey);
1062: } else {
1063: systemSettings.put(currentKey, new Boolean(
1064: currentSystemValue));
1065: settingSystemValidation.put(currentKey,
1066: new Boolean(currentSystemValue));
1067: }
1068: break;
1069:
1070: //check and conversion to Integer
1071: case SettingConstants.DATA_TYPE_INTEGER:
1072: // check whether the string holds number representation
1073: try {
1074: Integer.parseInt(currentUserValue);
1075: // when we're here it's ok
1076: userSettings.put(currentKey, new Integer(
1077: currentUserValue));
1078: settingUserValidation.put(currentKey,
1079: new Integer(currentUserValue));
1080: } catch (NumberFormatException e) {
1081: // we caught this so the string didn't contain a number
1082: errors.add(new ValidationError(
1083: "errors.setting.integer", Arrays
1084: .asList(new String[] {
1085: settingUserArea,
1086: currentFieldName })));
1087: settingUserValidation.remove(currentKey);
1088: }
1089: try {
1090: Integer.parseInt(currentSystemValue);
1091: // when we're here it's ok
1092: systemSettings.put(currentKey, new Integer(
1093: currentSystemValue));
1094: settingSystemValidation.put(currentKey,
1095: new Integer(currentSystemValue));
1096: } catch (NumberFormatException e) {
1097: // we caught this so the string didn't contain a number
1098: errors.add(new ValidationError(
1099: "errors.setting.integer", Arrays
1100: .asList(new String[] {
1101: settingSystemArea,
1102: currentFieldName })));
1103: }
1104: break;
1105:
1106: // the only other type is string, no special validation here
1107: case SettingConstants.DATA_TYPE_STRING:
1108: break;
1109: }
1110: }
1111: // we checked the types here, the real validation takes place on the
1112: // server, when there are some settings with correct types left
1113: if (!(settingUserValidation.isEmpty() && settingSystemValidation
1114: .isEmpty())) {
1115: ValidationErrors userErrors, systemErrors;
1116: try {
1117: userErrors = settings.validate(securitySession,
1118: settingUserValidation, locale,
1119: SettingConstants.SETTING_USER);
1120: systemErrors = settings.validate(securitySession,
1121: settingSystemValidation, locale,
1122: SettingConstants.SETTING_SYSTEM);
1123: } catch (SystemException e) {
1124: logger
1125: .error(
1126: "validate(HttpServletRequest, HttpSession)",
1127: e);
1128:
1129: throw new RuntimeException(e);
1130: }
1131:
1132: errors.addAll(userErrors);
1133: errors.addAll(systemErrors);
1134: }
1135: }
1136:
1137: if (logger.isDebugEnabled()) {
1138: logger.debug("validate - end - return value = " + errors);
1139: }
1140: return errors;
1141: }
1142:
1143: }
|