KickJava   Java API By Example, From Geeks To Geeks.

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


1
2 package org.roller.presentation.website.actions;
3
4 import java.io.IOException JavaDoc;
5 import java.util.Arrays JavaDoc;
6 import java.util.List JavaDoc;
7
8 import javax.servlet.ServletContext JavaDoc;
9 import javax.servlet.ServletException JavaDoc;
10 import javax.servlet.http.HttpServletRequest JavaDoc;
11 import javax.servlet.http.HttpServletResponse JavaDoc;
12 import org.apache.commons.lang.StringUtils;
13
14 import org.apache.commons.logging.Log;
15 import org.apache.commons.logging.LogFactory;
16 import org.apache.struts.action.ActionError;
17 import org.apache.struts.action.ActionErrors;
18 import org.apache.struts.action.ActionForm;
19 import org.apache.struts.action.ActionForward;
20 import org.apache.struts.action.ActionMapping;
21 import org.apache.struts.actions.DispatchAction;
22 import org.roller.RollerException;
23 import org.roller.RollerPermissionsException;
24 import org.roller.config.RollerRuntimeConfig;
25 import org.roller.model.UserManager;
26 import org.roller.model.WeblogManager;
27 import org.roller.pojos.UserData;
28 import org.roller.pojos.WeblogCategoryData;
29 import org.roller.pojos.WebsiteData;
30 import org.roller.presentation.RollerRequest;
31 import org.roller.presentation.RollerSession;
32 import org.roller.presentation.pagecache.PageCacheFilter;
33 import org.roller.presentation.forms.WebsiteForm;
34 import org.roller.presentation.website.formbeans.WebsiteFormEx;
35
36
37 /////////////////////////////////////////////////////////////////////////////
38
/**
39  * Website Settings action.
40  *
41  * @struts.action name="websiteFormEx" path="/editor/website"
42  * scope="session" parameter="method"
43  *
44  * @struts.action-forward name="editWebsite.page" path="/website/edit-website.jsp"
45  */

46 public final class WebsiteFormAction extends DispatchAction
47 {
48     private static Log mLogger =
49         LogFactory.getFactory().getInstance(WebsiteFormAction.class);
50
51     public ActionForward add(
52         ActionMapping mapping,
53         ActionForm actionForm,
54         HttpServletRequest JavaDoc request,
55         HttpServletResponse JavaDoc response)
56         throws IOException JavaDoc, ServletException JavaDoc
57     {
58         return update( mapping, actionForm, request, response );
59     }
60
61     //-----------------------------------------------------------------------
62
public ActionForward edit(
63         ActionMapping mapping,
64         ActionForm actionForm,
65         HttpServletRequest JavaDoc request,
66         HttpServletResponse JavaDoc response)
67         throws IOException JavaDoc, ServletException JavaDoc
68     {
69         ActionForward forward = mapping.findForward("editWebsite.page");
70         try
71         {
72             RollerRequest rreq = RollerRequest.getRollerRequest(request);
73             if ( rreq.isUserAuthorizedToEdit() )
74             {
75                 UserData ud = rreq.getUser();
76                 request.setAttribute("user",ud);
77
78                 WebsiteData hd = rreq.getWebsite();
79                 WebsiteForm wf = (WebsiteFormEx)actionForm;
80                 wf.copyFrom(hd, request.getLocale());
81
82                 List JavaDoc cd = rreq.getRoller().getWeblogManager()
83                    .getWeblogCategories(rreq.getWebsite(), true);
84                 request.setAttribute("categories",cd);
85
86                 List JavaDoc bcd = rreq.getRoller().getWeblogManager()
87                     .getWeblogCategories(rreq.getWebsite(), true);
88                 request.setAttribute("bloggerCategories",bcd);
89
90                 List JavaDoc pages = rreq.getRoller().getUserManager().getPages(rreq.getWebsite());
91                 request.setAttribute("pages",pages);
92
93                 ServletContext JavaDoc ctx = request.getSession().getServletContext();
94                 String JavaDoc editorPages =
95                         RollerRuntimeConfig.getProperty("users.editor.pages");
96                 
97                 List JavaDoc epages = Arrays.asList(StringUtils.split(StringUtils.deleteWhitespace(editorPages), ","));
98                 request.setAttribute("editorPagesList", epages);
99             }
100             else
101             {
102                 forward = mapping.findForward("access-denied");
103             }
104         }
105         catch (Exception JavaDoc e)
106         {
107             throw new ServletException JavaDoc(e);
108         }
109         return forward;
110     }
111
112
113     //-----------------------------------------------------------------------
114
public ActionForward update(
115         ActionMapping mapping,
116         ActionForm actionForm,
117         HttpServletRequest JavaDoc request,
118         HttpServletResponse JavaDoc response)
119         throws IOException JavaDoc, ServletException JavaDoc
120     {
121         ActionForward forward = mapping.findForward("editWebsite");
122         try
123         {
124             RollerRequest rreq = RollerRequest.getRollerRequest(request);
125             WeblogManager wmgr = rreq.getRoller().getWeblogManager();
126             UserManager umgr = rreq.getRoller().getUserManager();
127             if ( rreq.isUserAuthorizedToEdit() )
128             {
129                 WebsiteFormEx form = (WebsiteFormEx)actionForm;
130                 
131                 // checkboxes return no value when not checked
132
if (form.getAllowComments() == null)
133                 {
134                     form.setAllowComments( Boolean.FALSE );
135                 }
136                 if (form.getEmailComments() == null)
137                 {
138                     form.setEmailComments( Boolean.FALSE );
139                 }
140                 if (form.getEnableBloggerApi() == null)
141                 {
142                     form.setEnableBloggerApi( Boolean.FALSE );
143                 }
144
145                 if(!form.getDefaultPageId().equals(form.getWeblogDayPageId()))
146                 {
147                     WebsiteData wd = umgr.retrieveWebsite(form.getId());
148                     wd.save(); // should throw if save not permitted
149

150                     // ensure isEnabled can't be changed
151
form.setIsEnabled(wd.getIsEnabled());
152                     form.copyTo(wd, request.getLocale());
153                     
154                     if (form.getDefaultCategoryId() != null)
155                     {
156                         WeblogCategoryData defaultCat =
157                             wmgr.retrieveWeblogCategory(form.getDefaultCategoryId());
158                         wd.setDefaultCategory(defaultCat);
159                     }
160                     
161                     if (form.getBloggerCategoryId() != null)
162                     {
163                         WeblogCategoryData bloggerCat =
164                             wmgr.retrieveWeblogCategory(form.getBloggerCategoryId());
165                         wd.setBloggerCategory(bloggerCat);
166                     }
167                     
168                     wd.save();
169                     rreq.getRoller().getRefererManager().applyRefererFilters(wd);
170                     rreq.getRoller().commit();
171
172                     request.getSession().setAttribute(
173                         RollerSession.STATUS_MESSAGE,
174                         "Successfully submitted new Weblog templates");
175
176                     request.getSession().setAttribute(
177                         RollerRequest.WEBSITEID_KEY,form.getId());
178
179                     // clear the page cache for this user
180
PageCacheFilter.removeFromCache( request, wd.getUser() );
181
182                     // set the Editor Page list
183
ServletContext JavaDoc ctx = request.getSession().getServletContext();
184                     String JavaDoc editorPages =
185                         RollerRuntimeConfig.getProperty("users.editor.pages");
186                 
187                     List JavaDoc epages = Arrays.asList(StringUtils.split(
188                        StringUtils.deleteWhitespace(editorPages), ","));
189                     request.setAttribute("editorPagesList", epages);
190                 }
191                 else
192                 {
193                     request.getSession().setAttribute(
194                         RollerSession.ERROR_MESSAGE,
195                         "CHANGES REJECTED: Cannot set default page template "
196                         +"and day template to same template");
197                 }
198             }
199             else
200             {
201                 forward = mapping.findForward("access-denied");
202             }
203                     
204             actionForm.reset(mapping,request);
205         }
206         catch (RollerPermissionsException e)
207         {
208             ActionErrors errors = new ActionErrors();
209             errors.add(null, new ActionError("error.permissions.deniedSave"));
210             saveErrors(request, errors);
211             forward = mapping.findForward("access-denied");
212         }
213         catch (RollerException re)
214         {
215             mLogger.error("Unexpected exception",re.getRootCause());
216             throw new ServletException JavaDoc(re);
217         }
218         catch (Exception JavaDoc e)
219         {
220             mLogger.error("Unexpected exception",e);
221             throw new ServletException JavaDoc(e);
222         }
223         return forward;
224     }
225
226 }
227
228
Popular Tags