KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > roller > presentation > bookmarks > actions > BookmarksAction


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

4 package org.roller.presentation.bookmarks.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.BookmarkManager;
26 import org.roller.pojos.BookmarkData;
27 import org.roller.pojos.FolderData;
28 import org.roller.pojos.WebsiteData;
29 import org.roller.presentation.RollerRequest;
30 import org.roller.presentation.bookmarks.formbeans.BookmarksForm;
31
32 /**
33  * Actions that are initiated from the BookmarksForm.
34  *
35  * @struts.action name="bookmarksForm" path="/editor/bookmarks" parameter="method"
36  * @struts.action-forward name="BookmarksForm" path="/bookmarks/BookmarksForm.jsp"
37  *
38  * @author Dave Johnson
39  */

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

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

82     public ActionForward deleteSelected(
83         ActionMapping mapping,
84         ActionForm actionForm,
85         HttpServletRequest JavaDoc request,
86         HttpServletResponse JavaDoc response)
87         throws RollerException
88     {
89         RollerRequest rreq = RollerRequest.getRollerRequest(request);
90         if (rreq.isUserAuthorizedToEdit())
91         {
92             BookmarkManager bmgr = rreq.getRoller().getBookmarkManager();
93             BookmarksForm form = (BookmarksForm)actionForm;
94
95             mLogger.debug("Deleting folders and bookmarks.");
96
97             String JavaDoc folders[] = form.getSelectedFolders();
98             if (null != folders)
99             {
100                 for (int i = 0; i < folders.length; i++)
101                 {
102                     FolderData fd = bmgr.retrieveFolder(folders[i]);
103                     fd.remove(); // removes child folders and bookmarks too
104
}
105             }
106
107             String JavaDoc bookmarks[] = form.getSelectedBookmarks();
108             if (null != bookmarks)
109             {
110                 for (int j = 0; j < bookmarks.length; j++)
111                 {
112                     bmgr.removeBookmark(bookmarks[j]);
113                 }
114             }
115             rreq.getRoller().commit();
116
117             addModelObjects(request, (BookmarksForm)actionForm);
118             return mapping.findForward("BookmarksForm");
119         }
120         else
121         {
122             return mapping.findForward("access-denied");
123         }
124     }
125
126     /**
127      * Move folders and bookmarks indicated by BookmarksForm bean.
128      * @param mapping Action mapping.
129      * @param actionForm Form bean.
130      * @param request Request.
131      * @param response Response.
132      * @return Forward to BookmarksForm or access-denied.
133      * @throws RollerException
134      */

135     public ActionForward moveSelected(
136         ActionMapping mapping,
137         ActionForm actionForm,
138         HttpServletRequest JavaDoc request,
139         HttpServletResponse JavaDoc response)
140         throws RollerException
141     {
142         ActionMessages messages = new ActionMessages();
143         ActionForward forward = mapping.findForward("BookmarksForm");
144         RollerRequest rreq = RollerRequest.getRollerRequest(request);
145         if (rreq.isUserAuthorizedToEdit())
146         {
147             try
148             {
149                 BookmarkManager bmgr = rreq.getRoller().getBookmarkManager();
150                 BookmarksForm form = (BookmarksForm)actionForm;
151     
152                 mLogger.debug("Moving folders and bookmarks to folder, id="
153                     + form.getMoveToFolderId());
154     
155                 // Move subfolders to new folder.
156
String JavaDoc folders[] = form.getSelectedFolders();
157                 FolderData parent = bmgr.retrieveFolder(form.getMoveToFolderId());
158                 if (null != folders)
159                 {
160                     for (int i = 0; i < folders.length; i++)
161                     {
162                         FolderData fd = bmgr.retrieveFolder(folders[i]);
163     
164                         // Don't move folder into itself.
165
if ( !fd.getId().equals(parent.getId())
166                              && !parent.descendentOf(fd))
167                         {
168                             fd.setParent(parent);
169                             fd.save();
170                         }
171                         else
172                         {
173                             messages.add(null, new ActionMessage(
174                                 "bookmarksForm.warn.notMoving",fd.getName()));
175                         }
176                     }
177                 }
178     
179                 // Move bookmarks.
180
String JavaDoc bookmarks[] = form.getSelectedBookmarks();
181                 if (null != bookmarks)
182                 {
183                     for (int j = 0; j < bookmarks.length; j++)
184                     {
185                         BookmarkData bd = bmgr.retrieveBookmark(bookmarks[j]);
186                         bd.setFolder(parent);
187                         bd.save();
188                     }
189                 }
190                 rreq.getRoller().commit();
191     
192                 addModelObjects(request, (BookmarksForm)actionForm);
193                 saveMessages(request, messages);
194             }
195             catch (RollerException e)
196             {
197                 ActionErrors errors = new ActionErrors();
198                 errors.add(null, new ActionError("bookmarksForm.error.move"));
199                 saveErrors(request, errors);
200             }
201         }
202         else
203         {
204             forward = mapping.findForward("access-denied");
205         }
206         return forward;
207     }
208
209     /**
210      * Load model objects for display in BookmarksForm.
211      * @param request
212      * @throws RollerException
213      */

214     private void addModelObjects(HttpServletRequest JavaDoc request, BookmarksForm form)
215         throws RollerException
216     {
217         RollerRequest rreq = RollerRequest.getRollerRequest(request);
218         WebsiteData wd = rreq.getWebsite();
219         BookmarkManager bmgr = rreq.getRoller().getBookmarkManager();
220
221         TreeSet JavaDoc allFolders = new TreeSet JavaDoc(new FolderPathComparator());
222
223         // Find folderid wherever it may be
224
String JavaDoc folderId = (String JavaDoc)request.getAttribute(RollerRequest.FOLDERID_KEY);
225         if (null == folderId)
226         {
227             folderId = request.getParameter(RollerRequest.FOLDERID_KEY);
228         }
229         if (null == folderId)
230         {
231             folderId = form.getFolderId();
232         }
233
234         FolderData folder = null;
235         if (null == folderId || folderId.equals("null"))
236         {
237             folder = bmgr.getRootFolder(wd);
238         }
239         else
240         {
241             folder = bmgr.retrieveFolder(folderId);
242         }
243         form.setFolderId(folder.getId());
244
245         request.setAttribute("folder", folder);
246         request.setAttribute("folders", folder.getFolders());
247         request.setAttribute("bookmarks", folder.getBookmarks());
248
249         if (null != folder.getParent())
250         {
251             LinkedList JavaDoc folderPath = new LinkedList JavaDoc();
252             folderPath.add(0, folder);
253             FolderData parent = folder.getParent();
254             while (parent != null)
255             {
256                 folderPath.add(0, parent);
257                 parent = parent.getParent();
258             }
259             request.setAttribute("folderPath", folderPath);
260
261             request.setAttribute(
262                 RollerRequest.PARENTID_KEY, folder.getParent().getId());
263         }
264
265         // Build list of all folders, except for current one, sorted by path.
266
Iterator JavaDoc iter = bmgr.getAllFolders(wd).iterator();
267
268         // Build list of only children
269
//Iterator iter = folder.getFolders().iterator();
270

271         //int max = 20, count = 0;
272
while (iter.hasNext()) // && count < max)
273
{
274             //count++;
275
FolderData fd = (FolderData) iter.next();
276             if (!fd.getId().equals(folderId))
277             {
278                 allFolders.add(fd);
279             }
280         }
281         request.setAttribute("allFolders", allFolders);
282     }
283
284     private static final class FolderPathComparator implements Comparator JavaDoc
285     {
286         public int compare(Object JavaDoc o1, Object JavaDoc o2) {
287             FolderData f1 = (FolderData)o1;
288             FolderData f2 = (FolderData)o2;
289             int res = 0;
290             try
291             {
292                 res = f1.getPath().compareTo(f2.getPath());
293             }
294             catch (RollerException e)
295             {
296                 mLogger.error("ERROR: sorting folders");
297             }
298             return res;
299         }
300     }
301 }
302
Popular Tags