KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > roller > ui > authoring > struts > actions > ThemeEditorAction


1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. The ASF licenses this file to You
4 * under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License. For additional information regarding
15 * copyright in this work, please see the NOTICE file in the top level
16 * directory of this distribution.
17 */

18 package org.apache.roller.ui.authoring.struts.actions;
19
20 import java.io.IOException JavaDoc;
21 import java.util.List JavaDoc;
22
23 import javax.servlet.ServletException JavaDoc;
24 import javax.servlet.http.HttpServletRequest JavaDoc;
25 import javax.servlet.http.HttpServletResponse JavaDoc;
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 /**
51  * Struts Action class that handles the website theme chooser page.
52  *
53  * @author Allen Gilliland
54  *
55  * @struts.action name="themeEditorForm" path="/roller-ui/authoring/themeEditor"
56  * scope="session" parameter="method"
57  *
58  * @struts.action-forward name="editTheme.page" path=".theme-editor"
59  */

60 public class ThemeEditorAction extends DispatchAction {
61     
62     private static Log mLogger =
63             LogFactory.getFactory().getInstance(ThemeEditorAction.class);
64     
65     
66     /**
67      * Default action method.
68      */

69     public ActionForward unspecified(
70             ActionMapping mapping,
71             ActionForm actionForm,
72             HttpServletRequest JavaDoc request,
73             HttpServletResponse JavaDoc response)
74             throws IOException JavaDoc, ServletException JavaDoc {
75         
76         // make "edit" our default action
77
return this.edit(mapping, actionForm, request, response);
78     }
79     
80     
81     /**
82      * Base action method.
83      *
84      * Shows the theme chooser page with this users current theme selected.
85      **/

86     public ActionForward edit(
87             ActionMapping mapping,
88             ActionForm form,
89             HttpServletRequest JavaDoc request,
90             HttpServletResponse JavaDoc response)
91             throws IOException JavaDoc, ServletException JavaDoc {
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                 // get users current theme and our themes list
106
Roller roller = RollerFactory.getRoller();
107                 ThemeManager themeMgr = roller.getThemeManager();
108                 
109                 String JavaDoc username = rses.getAuthenticatedUser().getUserName();
110                 String JavaDoc currentTheme = website.getEditorTheme();
111                 List JavaDoc themes = themeMgr.getEnabledThemesList();
112                 
113                 // this checks if the website has a default page template
114
// if not then we don't allow for a custom theme
115
boolean allowCustomTheme = true;
116                 if(website.getDefaultPageId() == null
117                         || website.getDefaultPageId().equals("dummy")
118                         || website.getDefaultPageId().trim().equals(""))
119                     allowCustomTheme = false;
120                 
121                 // if we allow custom themes then add it to the end of the list
122
if(RollerRuntimeConfig.getBooleanProperty("themes.customtheme.allowed")
123                         && allowCustomTheme)
124                     themes.add(Theme.CUSTOM);
125                 
126                 // on the first pass just show a preview of the current theme
127
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 JavaDoc e) {
138             mLogger.error("ERROR in action",e);
139             throw new ServletException JavaDoc(e);
140         }
141         
142         return forward;
143     }
144     
145
146     /**
147      * Preview action method.
148      *
149      * Happens when the user selects a new preview theme from the dropdown menu.
150      * Shows a new preview theme.
151      */

152     public ActionForward preview(
153             ActionMapping mapping,
154             ActionForm form,
155             HttpServletRequest JavaDoc request,
156             HttpServletResponse JavaDoc response)
157             throws IOException JavaDoc, ServletException JavaDoc {
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                 // get users current theme
168
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 JavaDoc username = rses.getAuthenticatedUser().getUserName();
177                 String JavaDoc currentTheme = website.getEditorTheme();
178                 List JavaDoc themes = themeMgr.getEnabledThemesList();
179                 
180                 // this checks if the website has a default page template
181
// if not then we don't allow for a custom theme
182
boolean allowCustomTheme = true;
183                 if(website.getDefaultPageId() == null
184                         || website.getDefaultPageId().equals("dummy")
185                         || website.getDefaultPageId().trim().equals(""))
186                     allowCustomTheme = false;
187                 
188                 // if we allow custom themes then add it to the end of the list
189
if(RollerRuntimeConfig.getBooleanProperty("themes.customtheme.allowed")
190                         && allowCustomTheme)
191                     themes.add(Theme.CUSTOM);
192                 
193                 // set the current theme in the request
194
request.setAttribute("currentTheme", currentTheme);
195                 request.setAttribute("themesList", themes);
196                 
197                 String JavaDoc theme = request.getParameter("theme");
198                 try {
199                     Theme previewTheme = themeMgr.getTheme(theme);
200                     
201                     if(previewTheme.isEnabled()) {
202                         // make sure the view knows what theme to preview
203
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                     // hmm ... maybe they chose "custom"?
215
if(theme != null && theme.equals(Theme.CUSTOM)) {
216                         request.setAttribute("previewTheme", Theme.CUSTOM);
217                     } else {
218                         // we should never get here
219
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 JavaDoc e) {
230             mLogger.error("ERROR in action",e);
231             throw new ServletException JavaDoc(e);
232         }
233         
234         return forward;
235     }
236     
237
238     /**
239      * Save action method.
240      *
241      * Happens when the user clicks the "Save" button to set a new theme.
242      * This method simply updates the WebsiteData.editorTheme property with
243      * the value of the new theme.
244      */

245     public ActionForward save(
246             ActionMapping mapping,
247             ActionForm form,
248             HttpServletRequest JavaDoc request,
249             HttpServletResponse JavaDoc response)
250             throws IOException JavaDoc, ServletException JavaDoc {
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 JavaDoc newTheme = null;
265                 
266                 // lookup what theme the user wants first
267
String JavaDoc 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                     // possibly selected "custom"
282
if(theme != null && theme.equals(Theme.CUSTOM)) {
283                         newTheme = Theme.CUSTOM;
284                     } else {
285                         // hmm ... that's weird
286
mLogger.warn(tnfe);
287                         errors.add(null, new ActionMessage("Theme not found"));
288                         saveErrors(request, errors);
289                     }
290                 }
291                 
292                 // update theme for website and save
293
if(newTheme != null) {
294                     try {
295                         String JavaDoc 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                         // make sure to flush the page cache so ppl can see the change
306
CacheManager.invalidate(website);
307                         
308                         // update complete ... now just send them back to edit
309
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                 // if we got down here then there was an error :(
319
// send the user back to preview page with errors already set
320
return this.preview(mapping, form, request, response);
321                 
322             } else {
323                 forward = mapping.findForward("access-denied");
324             }
325         } catch (Exception JavaDoc e) {
326             mLogger.error("ERROR in action",e);
327             throw new ServletException JavaDoc(e);
328         }
329         return forward;
330     }
331    
332     
333     /**
334      * Customize action method.
335      *
336      * Happens when a user clicks the "Customize" button on their current theme.
337      * This method copies down all the theme templates from the currently
338      * selected theme into the users custom template pages and updates the users
339      * theme to "custom".
340      */

341     public ActionForward customize(
342             ActionMapping mapping,
343             ActionForm form,
344             HttpServletRequest JavaDoc request,
345             HttpServletResponse JavaDoc response)
346             throws IOException JavaDoc, ServletException JavaDoc {
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                 // copy down current theme to weblog templates
361
Roller roller = RollerFactory.getRoller();
362                 ThemeManager themeMgr = roller.getThemeManager();
363                 
364                 String JavaDoc username = rses.getAuthenticatedUser().getUserName();
365                 
366                 try {
367                     Theme usersTheme = themeMgr.getTheme(website.getEditorTheme());
368                     
369                     // only if custom themes are allowed
370
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                         // make sure to flush the page cache so ppl can see the change
381
//PageCacheFilter.removeFromCache(request, website);
382
CacheManager.invalidate(website);
383                     }
384                     
385                 } catch(ThemeNotFoundException tnfe) {
386                     // this catches the potential case where someone customizes
387
// a theme and has their theme as "custom" but then hits the
388
// browser back button and presses the button again, so
389
// they are basically trying to customize a "custom" theme
390

391                     // log this as a warning just in case
392
mLogger.warn(tnfe);
393                     
394                     // show the user an error message and let things go back
395
// to the edit page
396
errors.add(null, new ActionMessage("Oops! You already have a custom theme."));
397                 }
398                 
399                 // just take the user back home to the edit theme page
400
return this.edit(mapping, form, request, response);
401                 
402             } else {
403                 forward = mapping.findForward("access-denied");
404             }
405         } catch (Exception JavaDoc e) {
406             mLogger.error("ERROR in action",e);
407             throw new ServletException JavaDoc(e);
408         }
409         return forward;
410     }
411     
412     
413 }
414
Popular Tags