KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > roller > presentation > website > actions > UserBaseAction


1
2 package org.roller.presentation.website.actions;
3
4 import org.apache.commons.logging.Log;
5 import org.apache.commons.logging.LogFactory;
6 import org.apache.struts.action.ActionError;
7 import org.apache.struts.action.ActionErrors;
8 import org.apache.struts.action.ActionMessages;
9 import org.apache.struts.actions.DispatchAction;
10 import org.roller.RollerException;
11 import org.roller.model.UserManager;
12 import org.roller.pojos.UserData;
13 import org.roller.pojos.WebsiteData;
14 import org.roller.presentation.MainPageAction;
15 import org.roller.presentation.RollerContext;
16 import org.roller.presentation.RollerRequest;
17 import org.roller.presentation.website.formbeans.UserAdminForm;
18 import org.roller.presentation.website.formbeans.UserFormEx;
19 import org.roller.util.Utilities;
20 import java.util.Collection JavaDoc;
21 import java.util.Date JavaDoc;
22 import java.util.Locale JavaDoc;
23 import java.util.TimeZone JavaDoc;
24 import java.util.TreeSet JavaDoc;
25
26 import javax.servlet.ServletContext JavaDoc;
27 import javax.servlet.http.HttpServletRequest JavaDoc;
28
29
30
31 /////////////////////////////////////////////////////////////////////////////
32
/**
33  * Base class for user actions.
34  */

35 public class UserBaseAction extends DispatchAction
36 {
37     private static Log mLogger =
38         LogFactory.getFactory().getInstance(UserBaseAction.class);
39
40     protected static Collection JavaDoc locales = null;
41     protected static Collection JavaDoc timezones = null;
42
43     //------------------------------------------------------------------------
44
/** Validate user form. TODO: replace with Struts validation. */
45     protected ActionMessages validate( UserFormEx form, ActionMessages errors ) {
46
47         String JavaDoc safe = Utilities.replaceNonAlphanumeric(form.getUserName());
48         if ( "".equals(form.getUserName().trim()))
49         {
50             errors.add( ActionErrors.GLOBAL_ERROR,
51                new ActionError("error.add.user.missingUserName"));
52         }
53         else if ( !safe.equals(form.getUserName()) )
54         {
55             errors.add( ActionErrors.GLOBAL_ERROR,
56                new ActionError("error.add.user.badUserName"));
57         }
58
59         if ( "".equals(form.getEmailAddress().trim()))
60         {
61             errors.add( ActionErrors.GLOBAL_ERROR,
62                new ActionError("error.add.user.missingEmailAddress"));
63         }
64         return errors;
65     }
66
67     //-----------------------------------------------------------------------
68
/**
69      * Load Themes, Timezones, and Locales into the
70      * request for use in the UI. As possible, also
71      * set the User's Website's Timezone and Locale
72      * into the Form object.
73      *
74      * @param request
75      * @param rreq
76      * @param ud
77      * @param form
78      * @throws RollerException
79      */

80     protected void loadRequestObjects(
81         HttpServletRequest JavaDoc request,
82         RollerRequest rreq,
83         UserData ud,
84         UserFormEx form)
85         throws RollerException
86     {
87         // prepare themes for interface
88
ServletContext JavaDoc ctx = rreq.getServletContext();
89         RollerContext rollerContext = RollerContext.getRollerContext(ctx);
90         String JavaDoc[] themes = rollerContext.getThemeNames();
91         request.setAttribute( "themes", themes );
92
93         // prepare locales & timezones
94
if (ud != null)
95         {
96             UserManager mgr = rreq.getRoller().getUserManager();
97             WebsiteData website = mgr.getWebsite(ud.getUserName());
98             form.setLocale( website.getLocale() );
99             form.setTimezone( website.getTimezone() );
100             form.setTheme( website.getEditorTheme() );
101         }
102         else
103         {
104             form.setLocale( Locale.getDefault().getDisplayName() );
105             form.setTimezone( TimeZone.getDefault().getID() );
106         }
107         loadOptionLists(request);
108     }
109
110     //-----------------------------------------------------------------------
111
/**
112      * If necessary, load the available Locales and Timezones
113      * into the static members.
114      * As a convenience this method places the Collections into
115      * request attributes roller.locales and roller.timezones.
116      *
117      * @author lance.lavandowska
118      */

119     protected void loadOptionLists(HttpServletRequest JavaDoc request)
120     {
121         // load Locales if necessary
122
if (UserBaseAction.locales == null)
123         {
124             loadLocaleCollection();
125         }
126         request.setAttribute("roller.locales", UserBaseAction.locales);
127
128         // load Timezones if necessary
129
if (UserBaseAction.timezones == null)
130         {
131             loadTimeZoneCollection();
132         }
133         request.setAttribute("roller.timezones", UserBaseAction.timezones);
134     }
135
136     //-----------------------------------------------------------------------
137
/**
138      * LabelValueBeans are Comparable but violate the
139      * equals() part of the TreeSet requirements.
140      * And the html:options tag won't recognize
141      * toString as a property. So we have to put the
142      * Locales into a TreeSet to sort them, then convert
143      * them to LabelValueBeans to display them.
144      * Glad we only have to do this once.
145      *
146      */

147     protected void loadLocaleCollection()
148     {
149         java.util.ArrayList JavaDoc myLocales = new java.util.ArrayList JavaDoc();
150         TreeSet JavaDoc locTree = new TreeSet JavaDoc(new org.roller.util.LocaleComparator());
151         Locale JavaDoc[] localeArray = Locale.getAvailableLocales();
152         for (int i=0; i<localeArray.length; i++)
153         {
154             locTree.add(localeArray[i]);
155         }
156         java.util.Iterator JavaDoc it = locTree.iterator();
157         while (it.hasNext())
158         {
159             Locale JavaDoc loc = (Locale JavaDoc)it.next();
160             myLocales.add(new org.apache.struts.util.LabelValueBean(
161                loc.getDisplayName(),
162                loc.toString()));
163         }
164         UserBaseAction.locales = myLocales;
165     }
166
167     //-----------------------------------------------------------------------
168
/**
169      * html:options tag recognizes "ID" as a property
170      * so we don't have to go through all the rigamarole (sp?)
171      * that we did for Locales.
172      *
173      */

174     protected void loadTimeZoneCollection()
175     {
176         Date JavaDoc today = new Date JavaDoc();
177         java.util.ArrayList JavaDoc myZones = new java.util.ArrayList JavaDoc();
178         TreeSet JavaDoc zoneTree = new TreeSet JavaDoc(new org.roller.util.TimeZoneComparator());
179         String JavaDoc[] zoneArray = TimeZone.getAvailableIDs();
180         for (int i=0; i<zoneArray.length; i++)
181         {
182             zoneTree.add((TimeZone JavaDoc)TimeZone.getTimeZone(zoneArray[i]));
183         }
184         java.util.Iterator JavaDoc it = zoneTree.iterator();
185         while (it.hasNext())
186         {
187             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
188             TimeZone JavaDoc zone = (TimeZone JavaDoc)it.next();
189             sb.append(zone.getDisplayName(zone.inDaylightTime(today), TimeZone.SHORT));
190             sb.append(" - ");
191             sb.append(zone.getID());
192             myZones.add(new org.apache.struts.util.LabelValueBean(
193                sb.toString(),
194                zone.getID()));
195         }
196         UserBaseAction.timezones = myZones;
197     }
198     
199     //-----------------------------------------------------------------------
200
/**
201      * Check to see if the value of "userEnabled" has changed.
202      * If so, update User's Website and save, and refresh the Index page cache.
203      *
204      * @param request
205      * @param rreq
206      * @param uaf
207      * @throws RollerException
208      */

209     protected void refreshIndexCache(HttpServletRequest JavaDoc request,
210             RollerRequest rreq, UserAdminForm uaf) throws RollerException
211     {
212         WebsiteData website = rreq.getRoller().getUserManager().getWebsite(
213                 uaf.getUserName(), false);
214         boolean refreshIndexCache = false;
215         if (request.getParameter("userEnabled") == null)
216         {
217             // only change it if it is a change
218
if (uaf.getUserEnabled() == null
219                     || website.getIsEnabled().booleanValue())
220             {
221                 uaf.setUserEnabled(Boolean.FALSE);
222                 refreshIndexCache = true;
223             }
224         }
225         else
226         {
227             // only change it if it is a change
228
if (uaf.getUserEnabled() == null
229                     || website.getIsEnabled().booleanValue() == false)
230             {
231                 uaf.setUserEnabled(Boolean.TRUE);
232                 refreshIndexCache = true;
233             }
234         }
235
236         if (refreshIndexCache)
237         {
238             // set Website.isEnabled to match uaf.getUserEnabled()
239
website.setIsEnabled(uaf.getUserEnabled());
240             website.save();
241
242             // refresh the front page cache
243
MainPageAction.flushMainPageCache();
244         }
245     }
246 }
247
248
249
250
251
252
253
Popular Tags