KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ivata > groupware > admin > setting > struts > SettingAction


1 // Source file: h:/cvslocal/ivata groupware/src/com/ivata/intranet/admin/setting/struts/SettingAction.java
2

3 /*
4  * Copyright (c) 2001 - 2005 ivata limited.
5  * All rights reserved.
6  * -----------------------------------------------------------------------------
7  * ivata groupware may be redistributed under the GNU General Public
8  * License as published by the Free Software Foundation;
9  * version 2 of the License.
10  *
11  * These programs are free software; you can redistribute them and/or
12  * modify them under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; version 2 of the License.
14  *
15  * These programs are distributed in the hope that they will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18  *
19  * See the GNU General Public License in the file LICENSE.txt for more
20  * details.
21  *
22  * If you would like a copy of the GNU General Public License write to
23  *
24  * Free Software Foundation, Inc.
25  * 59 Temple Place - Suite 330
26  * Boston, MA 02111-1307, USA.
27  *
28  *
29  * To arrange commercial support and licensing, contact ivata at
30  * http://www.ivata.com/contact.jsp
31  * -----------------------------------------------------------------------------
32  * $Log: SettingAction.java,v $
33  * Revision 1.3 2005/04/10 18:47:34 colinmacleod
34  * Changed i tag to em and b tag to strong.
35  *
36  * Revision 1.2 2005/04/09 17:19:05 colinmacleod
37  * Changed copyright text to GPL v2 explicitly.
38  *
39  * Revision 1.1.1.1 2005/03/10 17:50:44 colinmacleod
40  * Restructured ivata op around Hibernate/PicoContainer.
41  * Renamed ivata groupware.
42  *
43  * Revision 1.10 2004/12/31 18:27:42 colinmacleod
44  * Added MaskFactory to constructor of MaskAction.
45  *
46  * Revision 1.9 2004/12/23 21:01:25 colinmacleod
47  * Updated Struts to v1.2.4.
48  * Changed base classes to use ivata masks.
49  *
50  * Revision 1.8 2004/11/12 18:19:13 colinmacleod
51  * Change action and form classes to extend MaskAction, MaskForm respectively.
52  *
53  * Revision 1.7 2004/11/12 15:56:46 colinmacleod
54  * Removed dependencies on SSLEXT.
55  * Moved Persistence classes to ivata masks.
56  *
57  * Revision 1.6 2004/11/03 15:31:49 colinmacleod
58  * Change method interfaces to remove log.
59  *
60  * Revision 1.5 2004/07/13 19:41:12 colinmacleod
61  * Moved project to POJOs from EJBs.
62  * Applied PicoContainer to services layer (replacing session EJBs).
63  * Applied Hibernate to persistence layer (replacing entity EJBs).
64  *
65  * Revision 1.4 2004/03/21 21:16:05 colinmacleod
66  * Shortened name to ivata op.
67  *
68  * Revision 1.3 2004/02/10 19:57:21 colinmacleod
69  * Changed email address.
70  *
71  * Revision 1.2 2004/02/01 22:00:32 colinmacleod
72  * Added full names to author tags
73  *
74  * Revision 1.1.1.1 2004/01/27 20:57:47 colinmacleod
75  * Moved ivata openportal to SourceForge..
76  *
77  * Revision 1.3 2003/10/17 12:36:12 jano
78  * fixing problems with building
79  * converting intranet -> portal
80  * Eclipse building
81  *
82  * Revision 1.2 2003/10/15 13:04:21 colin
83  * fixing for XDoclet
84  *
85  * Revision 1.18 2003/03/26 16:52:52 peter
86  * when there was no user setting before and only the system one was changed, new user setting not created
87  *
88  * Revision 1.17 2003/03/04 14:19:35 colin
89  * added emailDefaultFormat
90  *
91  * Revision 1.16 2003/03/03 16:57:08 colin
92  * converted localization to automatic paths
93  * added labels
94  * added mandatory fieldName attribute
95  *
96  * Revision 1.15 2003/02/25 17:00:07 peter
97  * added parent refreshing on close
98  *
99  * Revision 1.14 2003/02/24 18:56:15 colin
100  * added to admin
101  *
102  * Revision 1.13 2003/02/19 18:20:46 peter
103  * fixed validation and input format doubling
104  *
105  * Revision 1.10 2003/02/13 17:24:18 peter
106  * added new tabs: controls, notification
107  *
108  * Revision 1.9 2003/02/12 17:25:34 peter
109  * added library and notification tab
110  *
111  * Revision 1.8 2003/02/12 14:12:50 peter
112  * rights do work
113  *
114  * Revision 1.5 2003/02/05 18:15:07 peter
115  * works on validation, and changed to Map backed form
116  * -----------------------------------------------------------------------------
117  */

118 package com.ivata.groupware.admin.setting.struts;
119
120 import java.util.Arrays JavaDoc;
121 import java.util.Iterator JavaDoc;
122 import java.util.List JavaDoc;
123 import java.util.Vector JavaDoc;
124
125 import javax.servlet.http.HttpServletRequest JavaDoc;
126 import javax.servlet.http.HttpServletResponse JavaDoc;
127 import javax.servlet.http.HttpSession JavaDoc;
128
129 import org.apache.struts.action.ActionErrors;
130 import org.apache.struts.action.ActionForm;
131 import org.apache.struts.action.ActionMapping;
132
133 import com.ivata.groupware.admin.security.server.SecuritySession;
134 import com.ivata.groupware.admin.security.user.UserDO;
135 import com.ivata.groupware.admin.setting.Settings;
136 import com.ivata.groupware.admin.setting.right.SettingsRights;
137 import com.ivata.mask.MaskFactory;
138 import com.ivata.mask.util.SystemException;
139 import com.ivata.mask.web.struts.MaskAction;
140 import com.ivata.mask.web.struts.MaskAuthenticator;
141
142
143 /**
144  * <p><code>Action</code> invoked whenever
145  * <code>/setting.jsp</code> is submitted.</p>
146  *
147  * @since 2003-01-27
148  * @author Peter Illes
149  * @version $Revision: 1.3 $
150  */

151 public class SettingAction extends MaskAction {
152     private Settings settings;
153     private SettingsRights settingsRights;
154     /**
155      * <p>
156      * TODO
157      * </p>
158      *
159      * @param settings
160      * @param settingsRights
161      * @param maskFactory This factory is needed to access the masks and groups
162      * of masks.
163      * @param authenticator used to confirm whether or not the
164      * user should be allowed to continue, in the <code>execute</code> method.
165      */

166     public SettingAction(Settings settings, SettingsRights settingsRights,
167             MaskFactory maskFactory, MaskAuthenticator authenticator) {
168         super(maskFactory, authenticator);
169         this.settings = settings;
170         this.settingsRights = settingsRights;
171     }
172
173     /**
174      * <p>Called when the clear button is pressed, or after an ok or
175      * delete button, where the session should be restored to its default
176      * state.</p>
177      *
178      * @param mapping The ActionMapping used to select this instance.
179      * @param log valid logging object to write messages to.
180      * @param errors valid errors object to append errors to. If there are
181      * any errors, the action will return to the input.
182      * @param form optional ActionForm bean for this request (if any)
183      * @param request non-HTTP request we are processing
184      * @param response The non-HTTP response we are creating
185      * @param session returned from the <code>request</code> parameter.
186      * @param userName valid, non-null user name from session.
187      * @param settings valid, non-null settings from session.
188      * @exception SystemException if there is any problem which
189      * prevents processing. It will result in the webapp being forwarded
190      * to
191      * the standard error page.
192      */

193     public void clear(final ActionMapping mapping,
194             final ActionErrors errors,
195             final ActionForm form,
196             final HttpServletRequest JavaDoc request,
197             final HttpServletResponse JavaDoc response,
198             final HttpSession JavaDoc session) throws SystemException {
199         session.removeAttribute("settingTab_activeTab");
200         session.removeAttribute("settingForm");
201     }
202
203     /**
204      * <p>Called from the other <code>execute</code> method, this can
205      * be overridden by each subclass to provide the <em>ivata</em>-specific
206      * processing required.</p>
207      *
208      * @param mapping The ActionMapping used to select this instance.
209      * @param log valid logging object to write messages to.
210      * @param errors valid errors object to append errors to. If there are
211      * any errors, the action will return to the input.
212      * @param form optional ActionForm bean for this request (if any)
213      * @param request non-HTTP request we are processing
214      * @param response The non-HTTP response we are creating
215      * @param session returned from the <code>request</code> parameter.
216      * @param userName valid, non-null user name from session.
217      * @param settings valid, non-null settings from session.
218      * @exception SystemException if there is any problem which
219      * prevents processing. It will result in the webapp being forwarded
220      * to
221      * the standard error page.
222      * @return this method returns the string used to identify the correct
223      * <em>Struts</em> <code>ActionForward</code> which should follow this
224      * page, or <code>null</code> if it should return to the input.
225      */

226     public String JavaDoc execute(final ActionMapping mapping,
227             final ActionErrors errors,
228             final ActionForm form,
229             final HttpServletRequest JavaDoc request,
230             final HttpServletResponse JavaDoc response,
231             final HttpSession JavaDoc session) throws SystemException {
232         SettingForm settingForm = (SettingForm) form;
233         List JavaDoc tabName = settingForm.getTabName();
234         SecuritySession securitySession = (SecuritySession) session.getAttribute("securitySession");
235
236         Integer JavaDoc activeTab = settingForm.getSettingTab_activeTab();
237
238         // if the tab is null, it should be the first call, INITIALISE:
239
if (settingForm.getSettingTab_activeTab() == null) {
240             // these two Lists will contain setting names for individual tabs
241
Vector JavaDoc settingTabUser = new Vector JavaDoc();
242             Vector JavaDoc settingTabSystem = new Vector JavaDoc();
243
244             //very important, the administrator rights:
245
if (settingsRights.canAmendSystemSettings(securitySession)) {
246                 settingForm.setAdministrator("true");
247             }
248
249             // the jsp will start in user mode
250
settingForm.setSettingArea("user");
251             // the default jsp tab
252
activeTab = new Integer JavaDoc(0);
253             settingForm.setSettingTab_activeTab(activeTab);
254             // create the vector of tab names, only add tabs with at least
255
// one setting enabled (or allowed to change)
256
tabName = new Vector JavaDoc();
257             // these two work Lists will hold the names of succesful settings
258
List JavaDoc settingNameUser;
259             List JavaDoc settingNameSystem;
260
261             // --- the <strong>general</strong> tab
262
settingNameUser = initialiseSettings(securitySession,
263                         new String JavaDoc[]
264                         {"siteDefaultForward"},
265                         settingForm);
266             settingNameSystem = initialiseSettings(securitySession,
267                         new String JavaDoc[]
268                         {"siteTitle", "siteName", "siteWelcomeText"},
269                         settingForm);
270             if (!(settingNameUser.isEmpty() && settingNameSystem.isEmpty())) {
271                 tabName.add("general");
272                 settingTabUser.add(settingNameUser);
273                 settingTabSystem.add(settingNameSystem);
274             }
275
276             // --- the <strong>controls</strong> tab
277
settingNameUser = initialiseSettings(securitySession,
278                         new String JavaDoc[]
279                         {"guiShortcutStyle", "guiShortcutPause"},
280                         settingForm);
281             if (!settingNameUser.isEmpty()) {
282                 tabName.add("controls");
283                 settingTabUser.add(settingNameUser);
284                 settingTabSystem.add(new Vector JavaDoc());
285             }
286
287             // --- the <strong>email</strong> tab
288
settingNameUser = initialiseSettings(securitySession,
289                     new String JavaDoc[] {
290                 "emailDefaultFormat",
291                 "emailReplyIndent", "emailFolderInbox", "emailFolderSent",
292                 "emailFolderTrash", "emailFolderDrafts", "emailSignatureHTML",
293                 "emailSignatureText", "emailSignatureForward", "emailSignatureReply"},
294                         settingForm);
295             settingNameSystem = initialiseSettings(securitySession,
296                     new String JavaDoc[] {
297                 "emailHost", "emailPort", "emailHostSmtp"},
298                         settingForm);
299             if (!(settingNameUser.isEmpty() && settingNameSystem.isEmpty())) {
300                 tabName.add("email");
301                 settingTabUser.add(settingNameUser);
302                 settingTabSystem.add(settingNameSystem);
303             }
304
305             // --- the <strong>library</strong> tab
306
settingNameUser = initialiseSettings(securitySession,
307                     new String JavaDoc[] {
308                 "libraryHome", "libraryRecent", "libraryCommentSpacer"},
309                         settingForm);
310             if (!settingNameUser.isEmpty()) {
311                 tabName.add("library");
312                 settingTabUser.add(settingNameUser);
313                 settingTabSystem.add(new Vector JavaDoc());
314             }
315
316             // --- the <strong>notification</strong> tab
317
settingNameSystem = initialiseSettings(securitySession,
318                     new String JavaDoc[] {
319                 "libraryNotificationContentComment",
320                 "libraryNotificationContentCommentAmend",
321                 "libraryNotificationContentItem",
322                 "libraryNotificationContentItemAmend",
323                 "libraryNotificationContentItemReply",
324                 "libraryNotificationContentItemReplyAmend"},
325                         settingForm);
326             if (!settingNameSystem.isEmpty()) {
327                 tabName.add("notification");
328                 settingTabUser.add(new Vector JavaDoc());
329                 settingTabSystem.add(settingNameSystem);
330             }
331
332             // --- the <strong>dateTime</strong> tab
333
settingNameUser = initialiseSettings(securitySession,
334                     new String JavaDoc[] {
335                 "i18nTimeZone","calendarDefaultView","calendarFirstWeekDay",
336
337                 "i18nDateInputDisplay", "i18nTimeInputDisplay",
338
339                 "i18nDateLong", "i18nDateLongDay", "i18nDateLongYear",
340                 "i18nDateShort","i18nDateShortYear", "i18nDateWeekDay",
341                 "i18nTimeLong", "i18nTimeShort",
342
343                 "i18nDateShortToday", "i18nDateShortYesterday",
344                 "i18nDateThisAfternoon", "i18nDateThisMorning",
345                 "i18nDateThisEvening", "i18nDateYesterdayMorning",
346                 "i18nDateYesterdayAfternoon", "i18nDateYesterdayEvening"},
347                 settingForm);
348             if (!settingNameUser.isEmpty()) {
349                 tabName.add("dateTime");
350                 settingTabUser.add(settingNameUser);
351                 settingTabSystem.add(new Vector JavaDoc());
352             }
353
354             // when there's no tab with settings to show, go to page with
355
// this info and a close button
356
if (tabName.isEmpty()) {
357                 clear(mapping, errors, form, request, response, session);
358                 return "noSettings";
359             }
360
361             // set visible tab info to the form
362
settingForm.setTabName(tabName);
363             settingForm.setSettingTabUser(settingTabUser);
364             settingForm.setSettingTabSystem(settingTabSystem);
365         }
366         return null;
367     }
368
369     /**
370      * <p>helper method that sets the initial values of setting to the
371      * <code>settingForm</code>
372      * @param name the names of the settings
373      * @param form the <code>SettingForm</code> to fill
374      * @param userName the current user
375      * @param settings the <code>SettingsRemote</code>
376      * @param rights the <code>SettingsRightsRemote</code>
377      * @return List of settings from the input array <code>name</code>,
378      * that are enabled or allowed to update
379      */

380     private List JavaDoc initialiseSettings(final SecuritySession securitySession,
381             final String JavaDoc[] names,
382             final SettingForm form)
383             throws SystemException {
384         Vector JavaDoc returnVector = new Vector JavaDoc();
385         for (int i = 0; i < names.length; i++) {
386             String JavaDoc name = names[i];
387
388             // first check if this setting is enabled right now
389
if (settings.isSettingEnabled(securitySession, name)) {
390                 // now check if this user can change this setting on system
391
// or user level
392
if (settingsRights.canAmendSystemSettings(securitySession) ||
393                         settingsRights.canAmendSetting(securitySession, name)) {
394                     form.setSettingUser(name, settings.getSetting(securitySession,
395                         name, securitySession.getUser()));
396                     form.setSettingSystem(name, settings.getSetting(securitySession,
397                         name, null));
398                     form.setSettingType(name, new Integer JavaDoc(settings.getSettingType(securitySession, name)));
399                     form.setSettingOverride(name, settingsRights.canAmendSetting(securitySession,
400                         name));
401
402                     returnVector.add(name);
403                 }
404             }
405         }
406         return returnVector;
407     }
408
409     /**
410      * <p>This method is called if the ok or apply buttons are pressed.</p>
411      *
412      * @param mapping The ActionMapping used to select this instance.
413      * @param log valid logging object to write messages to.
414      * @param errors valid errors object to append errors to. If there are
415      * any errors, the action will return to the input.
416      * @param form optional ActionForm bean for this request (if any)
417      * @param request non-HTTP request we are processing
418      * @param response The non-HTTP response we are creating
419      * @param session returned from the <code>request</code> parameter.
420      * @param userName valid, non-null user name from session.
421      * @param settings valid, non-null settings from session.
422      * @param ok <code>true</code> if the ok button was pressed, otherwise
423      * <code>false</code> if the apply button was pressed.
424      * @exception SystemException if there is any problem which
425      * prevents processing. It will result in the webapp being forwarded
426      * to
427      * the standard error page.
428      * @return this method returns the string used to identify the correct
429      * <em>Struts</em> <code>ActionForward</code> which should follow this
430      * page, or <code>null</code> if it should return to the input.
431      */

432     public String JavaDoc onConfirm(final ActionMapping mapping,
433             final ActionErrors errors,
434             final ActionForm form,
435             final HttpServletRequest JavaDoc request,
436             final HttpServletResponse JavaDoc response,
437             final HttpSession JavaDoc session,
438             final String JavaDoc defaultForward) throws SystemException {
439         SettingForm settingForm = (SettingForm) form;
440
441         // store the settings, also manage revert-to-system cases
442
SecuritySession securitySession = (SecuritySession) session.getAttribute("securitySession");
443         saveSettings(securitySession, settingForm);
444
445         request.setAttribute("refreshOpener", "1");
446
447         return defaultForward;
448     }
449
450
451     /**
452      * <p>the method handles saving all settings in
453      * <code>settingForm</code> , removes the user
454      * setting when the values of the system and user settings are the
455      * same</p>
456      * @param form the <code>SettingForm</code> with the request parameter
457      * values
458      * @param settings the <code>SettingsRemote</code>
459      */

460     private void saveSettings(final SecuritySession securitySession,
461             final SettingForm form)
462             throws SystemException {
463         UserDO user = securitySession.getUser();
464
465         boolean administrator = (form.getAdministrator()!=null);
466         for (Iterator JavaDoc i = form.getSettingName().iterator(); i.hasNext();) {
467             String JavaDoc name = (String JavaDoc) i.next();
468             Object JavaDoc userValue = form.getSettingUser(name);
469             Object JavaDoc systemValue = form.getSettingSystem(name);
470
471             Object JavaDoc oldSystemValue = settings.getSetting(securitySession, name, null);
472
473             // when the user can change system settings and override status
474
// for settings
475
if(administrator) {
476                 settings.amendSetting(securitySession, name, systemValue, null);
477
478                 // these two settings have to be treated in a special way,
479
// as they have their simpliefied couples (without 'Display')
480
// to allow less strict user input
481
if (name.equals("i18nDateInputDisplay") ||
482                         name.equals("i18nTimeInputDisplay")) {
483                     settings.amendSetting(securitySession, name, userValue, user);
484                 }
485
486                 // override checkbox for this setting
487
if (form.getSettingOverride(name)) {
488                     settingsRights.addAmendRightForSetting(securitySession, name);
489                 } else {
490                     settingsRights.removeAmendRightForSetting(securitySession, name);
491                 }
492             }
493
494             // when the user value copies the system one (old or new), remove it
495
if (userValue.equals(systemValue) || userValue.equals(oldSystemValue)) {
496                 settings.revertSetting(securitySession, name, user);
497                 // these two settings have to be treated in a special way,
498
// as they have their simpliefied couples (without 'Display')
499
// to allow less strict user input, remove them also
500
if (name.equals("i18nDateInputDisplay") ||
501                         name.equals("i18nTimeInputDisplay")) {
502                     String JavaDoc displaySettingName = name.substring(0,name.indexOf("Display"));
503                     settings.revertSetting(securitySession, displaySettingName, user);
504                 }
505                 // set the user setting as a copy of the new system one to
506
// the form, as this could be an apply only
507
form.setSettingUser(name, systemValue);
508
509                 // otherwise save the new value of the user setting
510
} else {
511                 settings.amendSetting(securitySession, name, userValue, user);
512                 // these two settings have to be treated in a special way,
513
// as they have their simpliefied couples (without 'Display')
514
// to allow less strict user input
515
if (name.equals("i18nDateInputDisplay") ||
516                         name.equals("i18nTimeInputDisplay")) {
517                     String JavaDoc displaySettingName = name.substring(0,name.indexOf("Display"));
518                     settings.amendSetting(securitySession, displaySettingName, userValue, user);
519                 }
520
521             }
522         }
523     }
524
525     /**
526      * <p>The method takes a date / time input format and converts it to a
527      * less strict one</p>
528      * @param inputFormat the <code>String</code> with the date/time
529      * format pattern
530      * @return a less strict form of the provided format
531      */

532     private String JavaDoc simplifyInputFormat(final String JavaDoc inputFormat) {
533         String JavaDoc returnFormat = inputFormat;
534         String JavaDoc[] patterns = new String JavaDoc[] {"MM","dd","hh","KK","HH","kk","mm","ss"};
535
536         for (Iterator JavaDoc i = Arrays.asList(patterns).iterator(); i.hasNext(); ) {
537             String JavaDoc currentPattern = (String JavaDoc) i.next();
538
539             // find the pattern
540
int patternPosition = returnFormat.indexOf(currentPattern);
541
542             if (patternPosition != -1) {
543                 int longPatternPosition = returnFormat.indexOf(currentPattern, patternPosition + 1);
544                 if (!(currentPattern.equals("MM") && longPatternPosition == (patternPosition + 1))) {
545                     // replace the double pattern with a single char of it
546
returnFormat = returnFormat.substring(0, patternPosition) +
547                         currentPattern.substring(1) +
548                         returnFormat.substring(patternPosition + 2);
549
550                 }
551             }
552         }
553         return returnFormat;
554     }
555 }
556
Popular Tags