KickJava   Java API By Example, From Geeks To Geeks.

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


1
2 package org.roller.presentation.website.actions;
3
4 import java.io.IOException JavaDoc;
5 import java.util.List JavaDoc;
6
7 import javax.servlet.ServletException JavaDoc;
8 import javax.servlet.http.HttpServletRequest JavaDoc;
9 import javax.servlet.http.HttpServletResponse JavaDoc;
10
11 import org.apache.commons.logging.Log;
12 import org.apache.commons.logging.LogFactory;
13 import org.apache.struts.action.ActionError;
14 import org.apache.struts.action.ActionErrors;
15 import org.apache.struts.action.ActionForm;
16 import org.apache.struts.action.ActionForward;
17 import org.apache.struts.action.ActionMapping;
18 import org.apache.struts.action.ActionMessage;
19 import org.apache.struts.action.ActionMessages;
20 import org.apache.struts.actions.DispatchAction;
21 import org.roller.RollerException;
22 import org.roller.RollerPermissionsException;
23 import org.roller.model.UserManager;
24 import org.roller.pojos.PageData;
25 import org.roller.pojos.UserData;
26 import org.roller.pojos.WebsiteData;
27 import org.roller.presentation.RollerRequest;
28 import org.roller.presentation.forms.PageForm;
29 import org.roller.presentation.pagecache.PageCacheFilter;
30 import org.roller.util.StringUtils;
31 import org.roller.util.Utilities;
32
33
34 /////////////////////////////////////////////////////////////////////////////
35
/**
36  * Page form action.
37  * @struts.action name="pageForm" path="/editor/page"
38  * scope="session" parameter="method"
39  *
40  * @struts.action-forward name="removePage.page" path="/website/remove-page.jsp"
41  * @struts.action-forward name="editPage.page" path="/website/edit-page.jsp"
42  * @struts.action-forward name="editPages.page" path="/website/edit-pages.jsp"
43  */

44 public final class PageFormAction extends DispatchAction
45 {
46     private static Log mLogger =
47         LogFactory.getFactory().getInstance(PageFormAction.class);
48         
49     public ActionForward add(
50         ActionMapping mapping,
51         ActionForm actionForm,
52         HttpServletRequest JavaDoc request,
53         HttpServletResponse JavaDoc response)
54         throws IOException JavaDoc, ServletException JavaDoc
55     {
56         ActionForward forward = mapping.findForward("editPages.page");
57         try
58         {
59             RollerRequest rreq = RollerRequest.getRollerRequest(request);
60             if ( rreq.isUserAuthorizedToEdit() )
61             {
62                 PageForm form = (PageForm)actionForm;
63                 PageData data = new PageData();
64                 form.copyTo(data, request.getLocale());
65                 WebsiteData hd = rreq.getWebsite();
66
67                 data.setWebsite( hd );
68                 data.setUpdateTime( new java.util.Date JavaDoc() );
69                 data.setDescription("");
70                 data.setTemplate("");
71                 validateLink( data );
72
73                 UserManager mgr = rreq.getRoller().getUserManager();
74                 mgr.storePage( data );
75                 rreq.getRoller().commit();
76
77                 ActionMessages uiMessages = new ActionMessages();
78                 uiMessages.add(ActionMessages.GLOBAL_MESSAGE,
79                         new ActionMessage("pagesForm.addNewPage.success",
80                                 data.getName()));
81                 saveMessages(request, uiMessages);
82                 
83                 UserData user = rreq.getUser();
84                 PageCacheFilter.removeFromCache( request, user );
85                     
86                 actionForm.reset(mapping,request);
87                 
88                 addModelObjects(rreq);
89             }
90             else
91             {
92                 forward = mapping.findForward("access-denied");
93             }
94         }
95         catch (Exception JavaDoc e)
96         {
97             mLogger.error("ERROR in action",e);
98             throw new ServletException JavaDoc(e);
99         }
100         return forward;
101     }
102
103     //-----------------------------------------------------------------------
104
public ActionForward edit(
105         ActionMapping mapping,
106         ActionForm actionForm,
107         HttpServletRequest JavaDoc request,
108         HttpServletResponse JavaDoc response)
109         throws IOException JavaDoc, ServletException JavaDoc
110     {
111         ActionForward forward = mapping.findForward("editPage.page");
112         try
113         {
114             RollerRequest rreq = RollerRequest.getRollerRequest(request);
115             if ( rreq.isUserAuthorizedToEdit() )
116             {
117                 UserData ud = rreq.getUser();
118                 PageData pd = rreq.getPage();
119                 PageForm pf = (PageForm)actionForm;
120                 pf.copyFrom(pd, request.getLocale());
121
122                 PageCacheFilter.removeFromCache( request,ud );
123                 
124                 addModelObjects(rreq);
125             }
126             else
127             {
128                 forward = mapping.findForward("access-denied");
129             }
130         }
131         catch (Exception JavaDoc e)
132         {
133             mLogger.error("ERROR in action",e);
134             throw new ServletException JavaDoc(e);
135         }
136         return forward;
137     }
138
139     //-----------------------------------------------------------------------
140
public ActionForward editPages(
141         ActionMapping mapping,
142         ActionForm actionForm,
143         HttpServletRequest JavaDoc request,
144         HttpServletResponse JavaDoc response)
145         throws IOException JavaDoc, ServletException JavaDoc
146     {
147         ActionForward forward = mapping.findForward("editPages.page");
148         try
149         {
150             RollerRequest rreq = RollerRequest.getRollerRequest(request);
151             if ( rreq.isUserAuthorizedToEdit() )
152             {
153                 addModelObjects(rreq);
154             }
155             else
156             {
157                 forward = mapping.findForward("access-denied");
158             }
159         }
160         catch (Exception JavaDoc e)
161         {
162             mLogger.error("ERROR in action",e);
163             throw new ServletException JavaDoc(e);
164         }
165         return forward;
166     }
167
168     //-----------------------------------------------------------------------
169
public ActionForward remove(
170         ActionMapping mapping,
171         ActionForm actionForm,
172         HttpServletRequest JavaDoc request,
173         HttpServletResponse JavaDoc response)
174         throws IOException JavaDoc, ServletException JavaDoc
175     {
176         ActionForward forward = mapping.findForward("editPages");
177         try
178         {
179             RollerRequest rreq = RollerRequest.getRollerRequest(request);
180             if ( rreq.isUserAuthorizedToEdit() )
181             {
182                 PageForm form = (PageForm)actionForm;
183                 PageData data = new PageData();
184                 form.copyTo(data, request.getLocale());
185
186                 UserManager mgr = rreq.getRoller().getUserManager();
187                 mgr.removePageSafely( data.getId() );
188                 rreq.getRoller().commit();
189
190                 UserData user = rreq.getUser();
191                 PageCacheFilter.removeFromCache( request,user );
192                     
193                 addModelObjects(rreq);
194
195                 actionForm.reset(mapping,request);
196             }
197             else
198             {
199                 forward = mapping.findForward("access-denied");
200             }
201         }
202         catch (RollerException e)
203         {
204             ActionErrors errors = new ActionErrors();
205             errors.add(null, new ActionError(
206                     "error.internationalized",e.getRootCauseMessage()));
207             saveErrors(request, errors);
208         }
209         catch (Exception JavaDoc e)
210         {
211             mLogger.error("ERROR in action",e);
212             throw new ServletException JavaDoc(e);
213         }
214         return forward;
215     }
216
217     //-----------------------------------------------------------------------
218
public ActionForward removeOk(
219         ActionMapping mapping,
220         ActionForm actionForm,
221         HttpServletRequest JavaDoc request,
222         HttpServletResponse JavaDoc response)
223         throws IOException JavaDoc, ServletException JavaDoc
224     {
225         ActionForward forward = mapping.findForward("removePage.page");
226         try
227         {
228             RollerRequest rreq = RollerRequest.getRollerRequest(request);
229             if ( rreq.isUserAuthorizedToEdit() )
230             {
231                 PageData cd = rreq.getPage();
232                 PageForm pf = (PageForm)actionForm;
233                 pf.copyFrom(cd, request.getLocale());
234
235                 UserData ud = rreq.getUser();
236                 request.setAttribute("user",ud);
237             }
238             else
239             {
240                 forward = mapping.findForward("access-denied");
241             }
242         }
243         catch (Exception JavaDoc e)
244         {
245             mLogger.error("ERROR in action",e);
246             throw new ServletException JavaDoc(e);
247         }
248         return forward;
249     }
250
251     //-----------------------------------------------------------------------
252
public ActionForward update(
253         ActionMapping mapping,
254         ActionForm actionForm,
255         HttpServletRequest JavaDoc request,
256         HttpServletResponse JavaDoc response)
257         throws IOException JavaDoc, ServletException JavaDoc
258     {
259         ActionForward forward = mapping.findForward("editPage.page");
260         try
261         {
262             RollerRequest rreq = RollerRequest.getRollerRequest(request);
263             if ( rreq.isUserAuthorizedToEdit() )
264             {
265                 PageForm form = (PageForm)actionForm;
266                 UserManager mgr = rreq.getRoller().getUserManager();
267                 PageData data = mgr.retrievePage(form.getId());
268                 data.save(); // should through exception if no save permission
269
form.copyTo(data, request.getLocale());
270                 data.setUpdateTime( new java.util.Date JavaDoc() );
271                 data.setWebsite( rreq.getWebsite() );
272
273                 validateLink( data );
274
275                 mgr.storePage( data );
276                 rreq.getRoller().commit();
277
278                 // set the (possibly) new link back into the Form bean
279
((PageForm)actionForm).setLink( data.getLink() );
280
281                 ActionMessages uiMessages = new ActionMessages();
282                 uiMessages.add(ActionMessages.GLOBAL_MESSAGE,
283                         new ActionMessage("pageForm.save.success",
284                                 data.getName()));
285                 saveMessages(request, uiMessages);
286
287                 UserData user = rreq.getUser();
288                 PageCacheFilter.removeFromCache( request,user );
289             }
290             else
291             {
292                 forward = mapping.findForward("access-denied");
293             }
294               
295             // Don't reset this form. Allow user to keep on tweaking.
296
//actionForm.reset(mapping,request);
297
}
298         catch (RollerPermissionsException e)
299         {
300             ActionErrors errors = new ActionErrors();
301             errors.add(null, new ActionError("error.permissions.deniedSave"));
302             saveErrors(request, errors);
303             forward = mapping.findForward("access-denied");
304         }
305         catch (Exception JavaDoc e)
306         {
307             mLogger.error("ERROR in action",e);
308             throw new ServletException JavaDoc(e);
309         }
310         return forward;
311     }
312
313     //-----------------------------------------------------------------------
314
/**
315      * Ensures that the page has a safe value for the link
316      * field. "Safe" is defined as containing no html
317      * or any other non-alphanumeric characters.
318      * While this is overly strict (there are non-alphanum
319      * characters that are web-safe), this is a much easier
320      * test-and-correct. Otherwise we would need a RegEx package.
321      */

322     private void validateLink( PageData data )
323     {
324         // if data.getLink() is null or empty
325
// use the title ( data.getName() )
326
if ( StringUtils.isEmpty( data.getLink() ) )
327         {
328             data.setLink( data.getName() );
329         }
330
331         // if link contains any nonAlphanumeric, strip them
332
// first we must remove any html, as this is
333
// non-instructional markup. Then do a straight
334
// removeNonAlphanumeric.
335
if ( !StringUtils.isAlphanumeric( data.getLink() ) )
336         {
337             String JavaDoc link = Utilities.removeHTML( data.getLink() );
338             link = Utilities.removeNonAlphanumeric( link );
339             data.setLink( link );
340         }
341     }
342     
343     //-----------------------------------------------------------------------
344
public ActionForward cancel(
345         ActionMapping mapping,
346         ActionForm actionForm,
347         HttpServletRequest JavaDoc request,
348         HttpServletResponse JavaDoc response)
349         throws IOException JavaDoc, ServletException JavaDoc
350     {
351         return (mapping.findForward("editPages"));
352     }
353     
354     //-----------------------------------------------------------------------
355
private void addModelObjects( RollerRequest rreq )
356         throws RollerException {
357             
358         HttpServletRequest JavaDoc request = rreq.getRequest();
359             
360         UserManager mgr = rreq.getRoller().getUserManager();
361
362         UserData user = rreq.getUser();
363         request.setAttribute("user",user);
364
365         WebsiteData wd = rreq.getWebsite();
366         request.setAttribute("website", wd);
367
368         List JavaDoc pages = mgr.getPages(wd);
369         request.setAttribute("pages",pages);
370     }
371 }
372
373
Popular Tags