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 ; 21 import java.util.Date ; 22 import java.util.Locale ; 23 import java.util.TimeZone ; 24 import java.util.TreeSet ; 25 26 import javax.servlet.ServletContext ; 27 import javax.servlet.http.HttpServletRequest ; 28 29 30 31 35 public class UserBaseAction extends DispatchAction 36 { 37 private static Log mLogger = 38 LogFactory.getFactory().getInstance(UserBaseAction.class); 39 40 protected static Collection locales = null; 41 protected static Collection timezones = null; 42 43 45 protected ActionMessages validate( UserFormEx form, ActionMessages errors ) { 46 47 String 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 80 protected void loadRequestObjects( 81 HttpServletRequest request, 82 RollerRequest rreq, 83 UserData ud, 84 UserFormEx form) 85 throws RollerException 86 { 87 ServletContext ctx = rreq.getServletContext(); 89 RollerContext rollerContext = RollerContext.getRollerContext(ctx); 90 String [] themes = rollerContext.getThemeNames(); 91 request.setAttribute( "themes", themes ); 92 93 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 119 protected void loadOptionLists(HttpServletRequest request) 120 { 121 if (UserBaseAction.locales == null) 123 { 124 loadLocaleCollection(); 125 } 126 request.setAttribute("roller.locales", UserBaseAction.locales); 127 128 if (UserBaseAction.timezones == null) 130 { 131 loadTimeZoneCollection(); 132 } 133 request.setAttribute("roller.timezones", UserBaseAction.timezones); 134 } 135 136 147 protected void loadLocaleCollection() 148 { 149 java.util.ArrayList myLocales = new java.util.ArrayList (); 150 TreeSet locTree = new TreeSet (new org.roller.util.LocaleComparator()); 151 Locale [] localeArray = Locale.getAvailableLocales(); 152 for (int i=0; i<localeArray.length; i++) 153 { 154 locTree.add(localeArray[i]); 155 } 156 java.util.Iterator it = locTree.iterator(); 157 while (it.hasNext()) 158 { 159 Locale loc = (Locale )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 174 protected void loadTimeZoneCollection() 175 { 176 Date today = new Date (); 177 java.util.ArrayList myZones = new java.util.ArrayList (); 178 TreeSet zoneTree = new TreeSet (new org.roller.util.TimeZoneComparator()); 179 String [] zoneArray = TimeZone.getAvailableIDs(); 180 for (int i=0; i<zoneArray.length; i++) 181 { 182 zoneTree.add((TimeZone )TimeZone.getTimeZone(zoneArray[i])); 183 } 184 java.util.Iterator it = zoneTree.iterator(); 185 while (it.hasNext()) 186 { 187 StringBuffer sb = new StringBuffer (); 188 TimeZone zone = (TimeZone )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 209 protected void refreshIndexCache(HttpServletRequest 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 if (uaf.getUserEnabled() == null 219 || website.getIsEnabled().booleanValue()) 220 { 221 uaf.setUserEnabled(Boolean.FALSE); 222 refreshIndexCache = true; 223 } 224 } 225 else 226 { 227 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 website.setIsEnabled(uaf.getUserEnabled()); 240 website.save(); 241 242 MainPageAction.flushMainPageCache(); 244 } 245 } 246 } 247 248 249 250 251 252 253 | Popular Tags |