KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > roller > presentation > weblog > actions > CategoryDeleteAction


1 /*
2  * Created on Oct 21, 2003
3  */

4 package org.roller.presentation.weblog.actions;
5
6 import org.apache.struts.action.Action;
7 import org.apache.struts.action.ActionForm;
8 import org.apache.struts.action.ActionForward;
9 import org.apache.struts.action.ActionMapping;
10 import org.roller.model.WeblogManager;
11 import org.roller.pojos.WeblogCategoryData;
12 import org.roller.pojos.WebsiteData;
13 import org.roller.presentation.RollerRequest;
14 import org.roller.presentation.weblog.formbeans.CategoryDeleteForm;
15
16 import java.util.Iterator JavaDoc;
17 import java.util.LinkedList JavaDoc;
18 import java.util.List JavaDoc;
19
20 import javax.servlet.http.HttpServletRequest JavaDoc;
21 import javax.servlet.http.HttpServletResponse JavaDoc;
22
23 /**
24  * @struts.action path="/editor/categoryDelete" name="categoryDeleteForm"
25  * @struts.action-forward name="Categories" path="/editor/categories.do?method=selectCategory"
26  * @struts.action-forward name="CategoryDeleteOK" path="/weblog/CategoryDeleteOK.jsp"
27  *
28  * @author Dave Johnson
29  */

30 public class CategoryDeleteAction extends Action
31 {
32     public ActionForward execute(
33         ActionMapping mapping,
34         ActionForm actionForm,
35         HttpServletRequest JavaDoc request,
36         HttpServletResponse JavaDoc response)
37         throws Exception JavaDoc
38     {
39         ActionForward forward = null;
40         CategoryDeleteForm form = (CategoryDeleteForm)actionForm;
41         RollerRequest rreq = RollerRequest.getRollerRequest(request);
42         WeblogManager wmgr = rreq.getRoller().getWeblogManager();
43         
44         if (rreq.isUserAuthorizedToEdit())
45         {
46             String JavaDoc catid = request.getParameter("catid");
47             WeblogCategoryData catToDelete =
48                 wmgr.retrieveWeblogCategory(catid);
49             String JavaDoc returnId = null;
50             if (catToDelete.getParent() != null)
51             {
52                 returnId = catToDelete.getParent().getId();
53             }
54             if (form.isDelete() == null)
55             {
56                 // Present CategoryDeleteOK? page to user
57
WebsiteData website = rreq.getWebsite();
58                 WeblogCategoryData theCat = wmgr.retrieveWeblogCategory(catid);
59                 Iterator JavaDoc allCats =
60                     wmgr.getWeblogCategories(website).iterator();
61                 List JavaDoc destCats = new LinkedList JavaDoc();
62                 while (allCats.hasNext())
63                 {
64                     WeblogCategoryData cat = (WeblogCategoryData)allCats.next();
65                     // Allow entries to be moved to any other category except
66
// root and the sub-cats of the category being deleted.
67
if (!cat.getId().equals(catid)
68                         && cat.getParent()!=null
69                         && !cat.descendentOf(catToDelete))
70                     {
71                         destCats.add(cat);
72                     }
73                 }
74                 if (destCats.size() > 0)
75                 {
76                     form.setName(theCat.getName());
77                     form.setCatid(catid);
78                     form.setCats(destCats);
79                     form.setInUse(Boolean.valueOf(catToDelete.isInUse()));
80                     forward = mapping.findForward("CategoryDeleteOK");
81                 }
82                 else
83                 {
84                     // Can't delete last category, send 'em back!
85
if (null != returnId)
86                     {
87                         request.setAttribute(
88                                 RollerRequest.WEBLOGCATEGORYID_KEY, returnId);
89                     }
90                     forward = mapping.findForward("Categories");
91                 }
92             }
93             else if (form.isDelete().booleanValue())
94             {
95                 // User clicked YES to delete
96
WeblogCategoryData destCat = null;
97                 if (form.getMoveToWeblogCategoryId() != null)
98                 {
99                     destCat = wmgr.retrieveWeblogCategory(form.getMoveToWeblogCategoryId());
100                 }
101                   
102                 // move entries to destCat and remove catToDelete
103
catToDelete.remove(destCat);
104                 
105                 rreq.getRoller().commit();
106                 
107                 if (null != returnId)
108                 {
109                     request.setAttribute(
110                         RollerRequest.WEBLOGCATEGORYID_KEY, returnId);
111                 }
112                 forward = mapping.findForward("Categories");
113             }
114             else
115             {
116                 // User clicked NO to delete, do back to categories form
117
if (null != returnId)
118                 {
119                     request.setAttribute(
120                        RollerRequest.WEBLOGCATEGORYID_KEY, returnId);
121                 }
122                 forward = mapping.findForward("Categories");
123             }
124         }
125         else
126         {
127             forward = mapping.findForward("access-denied");
128         }
129         return forward;
130     }
131
132 }
133
Popular Tags