KickJava   Java API By Example, From Geeks To Geeks.

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


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

4 package org.roller.presentation.weblog.actions;
5
6 import java.util.Comparator JavaDoc;
7 import java.util.Iterator JavaDoc;
8 import java.util.LinkedList JavaDoc;
9 import java.util.TreeSet JavaDoc;
10
11 import javax.servlet.http.HttpServletRequest JavaDoc;
12 import javax.servlet.http.HttpServletResponse JavaDoc;
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.action.ActionMessage;
22 import org.apache.struts.action.ActionMessages;
23 import org.apache.struts.actions.DispatchAction;
24 import org.roller.RollerException;
25 import org.roller.model.WeblogManager;
26 import org.roller.pojos.WeblogCategoryData;
27 import org.roller.pojos.WebsiteData;
28 import org.roller.presentation.RollerRequest;
29 import org.roller.presentation.weblog.formbeans.CategoriesForm;
30
31 /**
32  * Actions that are initiated from the CategoriesForm.
33  *
34  * @struts.action name="categoriesForm" path="/editor/categories" parameter="method"
35  * @struts.action-forward name="CategoriesForm" path="/weblog/CategoriesForm.jsp"
36  *
37  * @author Dave Johnson
38  */

39 public class CategoriesAction extends DispatchAction
40 {
41     private static Log mLogger =
42         LogFactory.getFactory().getInstance(CategoriesAction.class);
43
44     /**
45      * Present the CategoriesForm loaded with category specified by request.
46      * @param mapping Action mapping.
47      * @param actionForm Form bean.
48      * @param request Request.
49      * @param response Response.
50      * @return Forward to CategoriesForm or access-denied.
51      * @throws RollerException
52      */

53     public ActionForward selectCategory(
54         ActionMapping mapping,
55         ActionForm actionForm,
56         HttpServletRequest JavaDoc request,
57         HttpServletResponse JavaDoc response)
58         throws RollerException
59     {
60         RollerRequest rreq = RollerRequest.getRollerRequest(request);
61         if (rreq.isUserAuthorizedToEdit())
62         {
63             addModelObjects(request, (CategoriesForm)actionForm);
64             return mapping.findForward("CategoriesForm");
65         }
66         else
67         {
68             return mapping.findForward("access-denied");
69         }
70     }
71
72      /**
73      * Move Categories and bookmarks indicated by CategoriesForm bean.
74      * @param mapping Action mapping.
75      * @param actionForm Form bean.
76      * @param request Request.
77      * @param response Response.
78      * @return Forward to CategoriesForm or access-denied.
79      * @throws RollerException
80      */

81     public ActionForward moveSelected(
82         ActionMapping mapping,
83         ActionForm actionForm,
84         HttpServletRequest JavaDoc request,
85         HttpServletResponse JavaDoc response)
86         throws RollerException
87     {
88         ActionMessages messages = new ActionMessages();
89         ActionForward forward = mapping.findForward("CategoriesForm");
90         RollerRequest rreq = RollerRequest.getRollerRequest(request);
91         if (rreq.isUserAuthorizedToEdit())
92         {
93             try
94             {
95                 WeblogManager wmgr = rreq.getRoller().getWeblogManager();
96                 CategoriesForm form = (CategoriesForm)actionForm;
97     
98                 mLogger.debug("Moving categories to category, id="
99                     + form.getMoveToCategoryId());
100                     
101                 // Move subCategories to new category.
102
String JavaDoc Categories[] = form.getSelectedCategories();
103                 WeblogCategoryData parent =
104                     wmgr.retrieveWeblogCategory(form.getMoveToCategoryId());
105                 if (null != Categories)
106                 {
107                     for (int i = 0; i < Categories.length; i++)
108                     {
109                         WeblogCategoryData cd =
110                             wmgr.retrieveWeblogCategory(Categories[i]);
111                         
112                         // Don't move category into itself.
113
if ( !cd.getId().equals(parent.getId())
114                              && !parent.descendentOf(cd))
115                         {
116                             cd.setParent(parent);
117                             cd.save();
118                         }
119                         else
120                         {
121                             messages.add(null, new ActionMessage(
122                                 "categoriesForm.warn.notMoving",cd.getName()));
123                         }
124                     }
125                 }
126     
127                 rreq.getRoller().commit();
128                 
129                 addModelObjects(request, (CategoriesForm)actionForm);
130                 saveMessages(request, messages);
131             }
132             catch (RollerException e)
133             {
134                 ActionErrors errors = new ActionErrors();
135                 errors.add(null, new ActionError("categoriesForm.error.move"));
136                 saveErrors(request, errors);
137             }
138         }
139         else
140         {
141             forward = mapping.findForward("access-denied");
142         }
143         return forward;
144     }
145
146     /**
147      * Load model objects for display in CategoriesForm.
148      * @param request
149      * @throws RollerException
150      */

151     private void addModelObjects(HttpServletRequest JavaDoc request, CategoriesForm form)
152         throws RollerException
153     {
154         RollerRequest rreq = RollerRequest.getRollerRequest(request);
155         WebsiteData wd = rreq.getWebsite();
156         WeblogManager wmgr = rreq.getRoller().getWeblogManager();
157         
158         TreeSet JavaDoc allCategories = new TreeSet JavaDoc(new CategoryPathComparator());
159         
160         // Find catid wherever it may be
161
String JavaDoc catId = (String JavaDoc)request.getAttribute(RollerRequest.WEBLOGCATEGORYID_KEY);
162         if (null == catId)
163         {
164             catId = request.getParameter(RollerRequest.WEBLOGCATEGORYID_KEY);
165         }
166         if (null == catId)
167         {
168             catId = form.getId();
169         }
170        
171         WeblogCategoryData cat = null;
172         if (null == catId || catId.equals("null"))
173         {
174             cat = wmgr.getRootWeblogCategory(wd);
175         }
176         else
177         {
178             cat = wmgr.retrieveWeblogCategory(catId);
179         }
180         form.setId(cat.getId());
181         
182         request.setAttribute("category", cat);
183         request.setAttribute("categories", cat.getWeblogCategories());
184         
185         if (null != cat.getParent())
186         {
187             LinkedList JavaDoc catPath = new LinkedList JavaDoc();
188             catPath.add(0, cat);
189             WeblogCategoryData parent = cat.getParent();
190             while (parent != null)
191             {
192                 catPath.add(0, parent);
193                 parent = parent.getParent();
194             }
195             request.setAttribute("categoryPath", catPath);
196             
197             request.setAttribute(
198                 RollerRequest.PARENTID_KEY, cat.getParent().getId());
199         }
200     
201         // Build collection of all Categories, except for current one,
202
// sorted by path.
203
Iterator JavaDoc iter = wmgr.getWeblogCategories(wd).iterator();
204         while (iter.hasNext())
205         {
206             WeblogCategoryData cd = (WeblogCategoryData) iter.next();
207             if (!cd.getId().equals(catId))
208             {
209                 allCategories.add(cd);
210             }
211         }
212         request.setAttribute("allCategories", allCategories);
213     }
214
215     private static final class CategoryPathComparator implements Comparator JavaDoc
216     {
217         public int compare(Object JavaDoc o1, Object JavaDoc o2) {
218             WeblogCategoryData f1 = (WeblogCategoryData)o1;
219             WeblogCategoryData f2 = (WeblogCategoryData)o2;
220             return f1.getPath().compareTo(f2.getPath());
221         }
222     }
223 }
224
Popular Tags