1 18 package org.apache.roller.ui.authoring.struts.actions; 19 20 import java.io.IOException ; 21 import java.util.List ; 22 23 import javax.servlet.ServletException ; 24 import javax.servlet.http.HttpServletRequest ; 25 import javax.servlet.http.HttpServletResponse ; 26 27 import org.apache.commons.logging.Log; 28 import org.apache.commons.logging.LogFactory; 29 import org.apache.struts.action.ActionErrors; 30 import org.apache.struts.action.ActionForm; 31 import org.apache.struts.action.ActionForward; 32 import org.apache.struts.action.ActionMapping; 33 import org.apache.struts.action.ActionMessage; 34 import org.apache.struts.actions.DispatchAction; 35 import org.apache.roller.RollerException; 36 import org.apache.roller.ThemeNotFoundException; 37 import org.apache.roller.config.RollerRuntimeConfig; 38 import org.apache.roller.model.Roller; 39 import org.apache.roller.model.RollerFactory; 40 import org.apache.roller.model.ThemeManager; 41 import org.apache.roller.model.UserManager; 42 import org.apache.roller.pojos.Theme; 43 import org.apache.roller.pojos.WebsiteData; 44 import org.apache.roller.ui.core.BasePageModel; 45 import org.apache.roller.ui.core.RollerRequest; 46 import org.apache.roller.ui.core.RollerSession; 47 import org.apache.roller.util.cache.CacheManager; 48 49 50 60 public class ThemeEditorAction extends DispatchAction { 61 62 private static Log mLogger = 63 LogFactory.getFactory().getInstance(ThemeEditorAction.class); 64 65 66 69 public ActionForward unspecified( 70 ActionMapping mapping, 71 ActionForm actionForm, 72 HttpServletRequest request, 73 HttpServletResponse response) 74 throws IOException , ServletException { 75 76 return this.edit(mapping, actionForm, request, response); 78 } 79 80 81 86 public ActionForward edit( 87 ActionMapping mapping, 88 ActionForm form, 89 HttpServletRequest request, 90 HttpServletResponse response) 91 throws IOException , ServletException { 92 93 ActionErrors errors = new ActionErrors(); 94 ActionForward forward = mapping.findForward("editTheme.page"); 95 try { 96 RollerSession rses = RollerSession.getRollerSession(request); 97 RollerRequest rreq = RollerRequest.getRollerRequest(request); 98 WebsiteData website = rreq.getWebsite(); 99 if ( rses.isUserAuthorizedToAdmin(website) ) { 100 101 BasePageModel pageModel = new BasePageModel( 102 "themeEditor.title", request, response, mapping); 103 request.setAttribute("model",pageModel); 104 105 Roller roller = RollerFactory.getRoller(); 107 ThemeManager themeMgr = roller.getThemeManager(); 108 109 String username = rses.getAuthenticatedUser().getUserName(); 110 String currentTheme = website.getEditorTheme(); 111 List themes = themeMgr.getEnabledThemesList(); 112 113 boolean allowCustomTheme = true; 116 if(website.getDefaultPageId() == null 117 || website.getDefaultPageId().equals("dummy") 118 || website.getDefaultPageId().trim().equals("")) 119 allowCustomTheme = false; 120 121 if(RollerRuntimeConfig.getBooleanProperty("themes.customtheme.allowed") 123 && allowCustomTheme) 124 themes.add(Theme.CUSTOM); 125 126 request.setAttribute("previewTheme", currentTheme); 128 request.setAttribute("currentTheme", currentTheme); 129 request.setAttribute("themesList", themes); 130 131 mLogger.debug("Previewing theme "+currentTheme+" to "+username); 132 133 } else { 134 forward = mapping.findForward("access-denied"); 135 } 136 137 } catch (Exception e) { 138 mLogger.error("ERROR in action",e); 139 throw new ServletException (e); 140 } 141 142 return forward; 143 } 144 145 146 152 public ActionForward preview( 153 ActionMapping mapping, 154 ActionForm form, 155 HttpServletRequest request, 156 HttpServletResponse response) 157 throws IOException , ServletException { 158 159 ActionErrors errors = new ActionErrors(); 160 ActionForward forward = mapping.findForward("editTheme.page"); 161 try { 162 RollerSession rses = RollerSession.getRollerSession(request); 163 RollerRequest rreq = RollerRequest.getRollerRequest(request); 164 WebsiteData website = rreq.getWebsite(); 165 if ( rses.isUserAuthorizedToAdmin(website)) { 166 167 Roller roller = RollerFactory.getRoller(); 169 ThemeManager themeMgr = roller.getThemeManager(); 170 171 172 BasePageModel pageModel = new BasePageModel( 173 "themeEditor.title", request, response, mapping); 174 request.setAttribute("model",pageModel); 175 176 String username = rses.getAuthenticatedUser().getUserName(); 177 String currentTheme = website.getEditorTheme(); 178 List themes = themeMgr.getEnabledThemesList(); 179 180 boolean allowCustomTheme = true; 183 if(website.getDefaultPageId() == null 184 || website.getDefaultPageId().equals("dummy") 185 || website.getDefaultPageId().trim().equals("")) 186 allowCustomTheme = false; 187 188 if(RollerRuntimeConfig.getBooleanProperty("themes.customtheme.allowed") 190 && allowCustomTheme) 191 themes.add(Theme.CUSTOM); 192 193 request.setAttribute("currentTheme", currentTheme); 195 request.setAttribute("themesList", themes); 196 197 String theme = request.getParameter("theme"); 198 try { 199 Theme previewTheme = themeMgr.getTheme(theme); 200 201 if(previewTheme.isEnabled()) { 202 request.setAttribute("previewTheme", previewTheme.getName()); 204 205 mLogger.debug("Previewing theme "+previewTheme.getName()+ 206 " to "+username); 207 } else { 208 request.setAttribute("previewTheme", currentTheme); 209 errors.add(null, new ActionMessage("Theme not enabled")); 210 saveErrors(request, errors); 211 } 212 213 } catch(ThemeNotFoundException tnfe) { 214 if(theme != null && theme.equals(Theme.CUSTOM)) { 216 request.setAttribute("previewTheme", Theme.CUSTOM); 217 } else { 218 request.setAttribute("previewTheme", currentTheme); 220 errors.add(null, new ActionMessage("Theme not found")); 221 saveErrors(request, errors); 222 } 223 } 224 225 } else { 226 forward = mapping.findForward("access-denied"); 227 } 228 229 } catch (Exception e) { 230 mLogger.error("ERROR in action",e); 231 throw new ServletException (e); 232 } 233 234 return forward; 235 } 236 237 238 245 public ActionForward save( 246 ActionMapping mapping, 247 ActionForm form, 248 HttpServletRequest request, 249 HttpServletResponse response) 250 throws IOException , ServletException { 251 252 ActionErrors errors = new ActionErrors(); 253 ActionForward forward = mapping.findForward("editTheme.page"); 254 try { 255 RollerSession rses = RollerSession.getRollerSession(request); 256 RollerRequest rreq = RollerRequest.getRollerRequest(request); 257 WebsiteData website = rreq.getWebsite(); 258 if ( rses.isUserAuthorizedToAdmin(website) ) { 259 260 BasePageModel pageModel = new BasePageModel( 261 "themeEditor.title", request, response, mapping); 262 request.setAttribute("model",pageModel); 263 264 String newTheme = null; 265 266 String theme = request.getParameter("theme"); 268 try { 269 Roller roller = RollerFactory.getRoller(); 270 ThemeManager themeMgr = roller.getThemeManager(); 271 Theme previewTheme = themeMgr.getTheme(theme); 272 273 if(previewTheme.isEnabled()) { 274 newTheme = previewTheme.getName(); 275 } else { 276 errors.add(null, new ActionMessage("Theme not enabled")); 277 saveErrors(request, errors); 278 } 279 280 } catch(ThemeNotFoundException tnfe) { 281 if(theme != null && theme.equals(Theme.CUSTOM)) { 283 newTheme = Theme.CUSTOM; 284 } else { 285 mLogger.warn(tnfe); 287 errors.add(null, new ActionMessage("Theme not found")); 288 saveErrors(request, errors); 289 } 290 } 291 292 if(newTheme != null) { 294 try { 295 String username = rses.getAuthenticatedUser().getUserName(); 296 297 website.setEditorTheme(newTheme); 298 299 UserManager userMgr = RollerFactory.getRoller().getUserManager(); 300 userMgr.saveWebsite(website); 301 RollerFactory.getRoller().flush(); 302 303 mLogger.debug("Saved theme "+newTheme+" for "+username); 304 305 CacheManager.invalidate(website); 307 308 return this.edit(mapping, form, request, response); 310 311 } catch(RollerException re) { 312 mLogger.error(re); 313 errors.add(null, new ActionMessage("Error setting theme")); 314 saveErrors(request, errors); 315 } 316 } 317 318 return this.preview(mapping, form, request, response); 321 322 } else { 323 forward = mapping.findForward("access-denied"); 324 } 325 } catch (Exception e) { 326 mLogger.error("ERROR in action",e); 327 throw new ServletException (e); 328 } 329 return forward; 330 } 331 332 333 341 public ActionForward customize( 342 ActionMapping mapping, 343 ActionForm form, 344 HttpServletRequest request, 345 HttpServletResponse response) 346 throws IOException , ServletException { 347 348 ActionErrors errors = new ActionErrors(); 349 ActionForward forward = mapping.findForward("editTheme.page"); 350 try { 351 RollerSession rses = RollerSession.getRollerSession(request); 352 RollerRequest rreq = RollerRequest.getRollerRequest(request); 353 WebsiteData website = rreq.getWebsite(); 354 if ( rses.isUserAuthorizedToAdmin(website) ) { 355 356 BasePageModel pageModel = new BasePageModel( 357 "themeEditor.title", request, response, mapping); 358 request.setAttribute("model",pageModel); 359 360 Roller roller = RollerFactory.getRoller(); 362 ThemeManager themeMgr = roller.getThemeManager(); 363 364 String username = rses.getAuthenticatedUser().getUserName(); 365 366 try { 367 Theme usersTheme = themeMgr.getTheme(website.getEditorTheme()); 368 369 if(RollerRuntimeConfig.getBooleanProperty("themes.customtheme.allowed")) { 371 try { 372 themeMgr.saveThemePages(website, usersTheme); 373 RollerFactory.getRoller().flush(); 374 } catch(RollerException re) { 375 mLogger.error(re); 376 errors.add(null, new ActionMessage("Error customizing theme")); 377 saveErrors(request, errors); 378 } 379 380 CacheManager.invalidate(website); 383 } 384 385 } catch(ThemeNotFoundException tnfe) { 386 391 mLogger.warn(tnfe); 393 394 errors.add(null, new ActionMessage("Oops! You already have a custom theme.")); 397 } 398 399 return this.edit(mapping, form, request, response); 401 402 } else { 403 forward = mapping.findForward("access-denied"); 404 } 405 } catch (Exception e) { 406 mLogger.error("ERROR in action",e); 407 throw new ServletException (e); 408 } 409 return forward; 410 } 411 412 413 } 414 | Popular Tags |