KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > roller > ui > authoring > struts > actions > BookmarksAction


1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. The ASF licenses this file to You
4 * under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License. For additional information regarding
15 * copyright in this work, please see the NOTICE file in the top level
16 * directory of this distribution.
17 */

18 /*
19  * Created on Oct 21, 2003
20  */

21 package org.apache.roller.ui.authoring.struts.actions;
22
23 import java.text.MessageFormat JavaDoc;
24 import java.util.Comparator JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.LinkedList JavaDoc;
28 import java.util.Set JavaDoc;
29 import java.util.TreeSet JavaDoc;
30
31 import javax.servlet.http.HttpServletRequest JavaDoc;
32 import javax.servlet.http.HttpServletResponse JavaDoc;
33
34 import org.apache.commons.logging.Log;
35 import org.apache.commons.logging.LogFactory;
36 import org.apache.struts.action.ActionError;
37 import org.apache.struts.action.ActionErrors;
38 import org.apache.struts.action.ActionForm;
39 import org.apache.struts.action.ActionForward;
40 import org.apache.struts.action.ActionMapping;
41 import org.apache.struts.action.ActionMessage;
42 import org.apache.struts.action.ActionMessages;
43 import org.apache.struts.actions.DispatchAction;
44 import org.apache.roller.RollerException;
45 import org.apache.roller.model.BookmarkManager;
46 import org.apache.roller.model.Roller;
47 import org.apache.roller.model.RollerFactory;
48 import org.apache.roller.pojos.BookmarkData;
49 import org.apache.roller.pojos.FolderData;
50 import org.apache.roller.pojos.WebsiteData;
51 import org.apache.roller.ui.core.BasePageModel;
52 import org.apache.roller.ui.core.RollerRequest;
53 import org.apache.roller.ui.core.RollerSession;
54 import org.apache.roller.ui.authoring.struts.formbeans.BookmarksForm;
55 import org.apache.roller.ui.core.RequestConstants;
56 import org.apache.roller.util.cache.CacheManager;
57
58 /**
59  * Actions that are initiated from the BookmarksForm.
60  *
61  * @struts.action name="bookmarksForm" path="/roller-ui/authoring/bookmarks" parameter="method"
62  * @struts.action-forward name="BookmarksForm" path=".BookmarksForm"
63  *
64  * @author Dave Johnson
65  */

66 public class BookmarksAction extends DispatchAction
67 {
68     private static Log mLogger =
69         LogFactory.getFactory().getInstance(BookmarksAction.class);
70
71     /**
72      * Present the BookmarksForm loaded with folder specified by request.
73      * @param mapping Action mapping.
74      * @param actionForm Form bean.
75      * @param request Request.
76      * @param response Response.
77      * @return Forward to BookmarksForm or access-denied.
78      * @throws RollerException
79      */

80     public ActionForward selectFolder(
81         ActionMapping mapping,
82         ActionForm actionForm,
83         HttpServletRequest JavaDoc request,
84         HttpServletResponse JavaDoc response)
85         throws RollerException
86     {
87         BookmarksPageModel pageModel = new BookmarksPageModel(
88             request, response, mapping, (BookmarksForm)actionForm);
89         if (RollerSession.getRollerSession(request).isUserAuthorizedToAuthor(
90                 pageModel.getFolder().getWebsite()))
91         {
92             request.setAttribute("model", pageModel);
93             return mapping.findForward("BookmarksForm");
94         }
95         else
96         {
97             return mapping.findForward("access-denied");
98         }
99     }
100
101     /**
102      * Delete folders and bookmarks indicated by BookmarksForm bean.
103      * @param mapping Action mapping.
104      * @param actionForm Form bean.
105      * @param request Request.
106      * @param response Response.
107      * @return Forward to BookmarksForm or access-denied.
108      * @throws RollerException
109      */

110     public ActionForward deleteSelected(
111         ActionMapping mapping,
112         ActionForm actionForm,
113         HttpServletRequest JavaDoc request,
114         HttpServletResponse JavaDoc response)
115         throws RollerException
116     {
117         Roller roller = RollerFactory.getRoller();
118         BookmarksPageModel pageModel = new BookmarksPageModel(
119             request, response, mapping, (BookmarksForm)actionForm);
120         WebsiteData website = pageModel.getFolder().getWebsite();
121         if (RollerSession.getRollerSession(request).isUserAuthorizedToAuthor(website))
122         {
123             BookmarkManager bmgr = roller.getBookmarkManager();
124             BookmarksForm form = (BookmarksForm)actionForm;
125
126             mLogger.debug("Deleting folders and bookmarks.");
127
128             String JavaDoc folders[] = form.getSelectedFolders();
129             if (null != folders)
130             {
131                 for (int i = 0; i < folders.length; i++)
132                 {
133                     FolderData fd = bmgr.getFolder(folders[i]);
134                     bmgr.removeFolder(fd); // removes child folders and bookmarks too
135
}
136             }
137             
138             BookmarkData bookmark = null;
139             String JavaDoc bookmarks[] = form.getSelectedBookmarks();
140             if (null != bookmarks)
141             {
142                 for (int j = 0; j < bookmarks.length; j++)
143                 {
144                     bookmark = bmgr.getBookmark(bookmarks[j]);
145                     bmgr.removeBookmark(bookmark);
146                 }
147             }
148             RollerFactory.getRoller().flush();
149                 
150             CacheManager.invalidate(website);
151
152             // recreate model now that folder is deleted
153
pageModel = new BookmarksPageModel(
154                 request, response, mapping, (BookmarksForm)actionForm);
155             request.setAttribute("model", pageModel);
156             return mapping.findForward("BookmarksForm");
157         }
158         else
159         {
160             return mapping.findForward("access-denied");
161         }
162     }
163
164     /**
165      * Move folders and bookmarks indicated by BookmarksForm bean.
166      * @param mapping Action mapping.
167      * @param actionForm Form bean.
168      * @param request Request.
169      * @param response Response.
170      * @return Forward to BookmarksForm or access-denied.
171      * @throws RollerException
172      */

173     public ActionForward moveSelected(
174         ActionMapping mapping,
175         ActionForm actionForm,
176         HttpServletRequest JavaDoc request,
177         HttpServletResponse JavaDoc response)
178         throws RollerException
179     {
180         ActionMessages messages = new ActionMessages();
181         ActionForward forward = mapping.findForward("BookmarksForm");
182         Roller roller = RollerFactory.getRoller();
183         BookmarksPageModel pageModel = new BookmarksPageModel(
184             request, response, mapping, (BookmarksForm)actionForm);
185         request.setAttribute("model", pageModel);
186         WebsiteData website = pageModel.getFolder().getWebsite();
187
188         if (RollerSession.getRollerSession(request).isUserAuthorizedToAuthor(website))
189         {
190             try
191             {
192                 BookmarkManager bmgr = roller.getBookmarkManager();
193                 BookmarksForm form = (BookmarksForm)actionForm;
194     
195                 mLogger.debug("Moving folders and bookmarks to folder, id="
196                     + form.getMoveToFolderId());
197     
198                 // Move subfolders to new folder.
199
String JavaDoc folders[] = form.getSelectedFolders();
200                 FolderData parent = bmgr.getFolder(form.getMoveToFolderId());
201                 if (null != folders)
202                 {
203                     for (int i = 0; i < folders.length; i++)
204                     {
205                         FolderData fd = bmgr.getFolder(folders[i]);
206     
207                         // Don't move folder into itself.
208
if ( !fd.getId().equals(parent.getId())
209                              && !parent.descendentOf(fd))
210                         {
211                             fd.setParent(parent);
212                             bmgr.saveFolder(fd);
213                         }
214                         else
215                         {
216                             messages.add(null, new ActionMessage(
217                                 "bookmarksForm.warn.notMoving",fd.getName()));
218                         }
219                     }
220                 }
221     
222                 // Move bookmarks.
223
String JavaDoc bookmarks[] = form.getSelectedBookmarks();
224                 if (null != bookmarks)
225                 {
226                     for (int j = 0; j < bookmarks.length; j++)
227                     {
228                         // maybe we should be using folder.addBookmark()?
229
BookmarkData bd = bmgr.getBookmark(bookmarks[j]);
230                         bd.setFolder(parent);
231                         bmgr.saveBookmark(bd);
232                     }
233                 }
234                 RollerFactory.getRoller().flush();
235
236                 CacheManager.invalidate(website);
237                 
238                 saveMessages(request, messages);
239                 
240                 // recreate model now that folder is altered
241
pageModel = new BookmarksPageModel(
242                         request, response, mapping, (BookmarksForm)actionForm);
243                 request.setAttribute("model", pageModel);
244             }
245             catch (RollerException e)
246             {
247                 ActionErrors errors = new ActionErrors();
248                 errors.add(null, new ActionError("bookmarksForm.error.move"));
249                 saveErrors(request, errors);
250             }
251         }
252         else
253         {
254             forward = mapping.findForward("access-denied");
255         }
256         return forward;
257     }
258
259     private static final class FolderPathComparator implements Comparator JavaDoc
260     {
261         public int compare(Object JavaDoc o1, Object JavaDoc o2) {
262             FolderData f1 = (FolderData)o1;
263             FolderData f2 = (FolderData)o2;
264             int res = 0;
265             try
266             {
267                 res = f1.getPath().compareTo(f2.getPath());
268             }
269             catch (RollerException e)
270             {
271                 mLogger.error("ERROR: sorting folders");
272             }
273             return res;
274         }
275     }
276     
277     public class BookmarksPageModel extends BasePageModel
278     {
279         private List JavaDoc folderPath = null;
280         private TreeSet JavaDoc allFolders = null;
281         private FolderData folder = null;
282         
283         public BookmarksPageModel(
284                 HttpServletRequest JavaDoc request,
285                 HttpServletResponse JavaDoc response,
286                 ActionMapping mapping,
287                 BookmarksForm form) throws RollerException
288         {
289             super("", request, response, mapping);
290             
291             RollerRequest rreq = RollerRequest.getRollerRequest(request);
292             RollerSession rollerSession = RollerSession.getRollerSession(request);
293             BookmarkManager bmgr = RollerFactory.getRoller().getBookmarkManager();
294
295             allFolders = new TreeSet JavaDoc(new FolderPathComparator());
296
297             // Find folderid wherever it may be
298
String JavaDoc folderId = (String JavaDoc)
299                 request.getAttribute(RequestConstants.FOLDER_ID);
300             if (null == folderId)
301             {
302                 folderId = request.getParameter(RequestConstants.FOLDER_ID);
303             }
304             if (null == folderId)
305             {
306                 folderId = form.getFolderId();
307             }
308
309             if (null == folderId || folderId.equals("null"))
310             {
311                 website = rreq.getWebsite();
312                 folder = bmgr.getRootFolder(website);
313                 folderId = folder.getId();
314             }
315             else
316             {
317                 folder = bmgr.getFolder(folderId);
318                 website = folder.getWebsite();
319             }
320             form.setFolderId(folder.getId());
321
322             if (null != folder.getParent())
323             {
324                 folderPath = new LinkedList JavaDoc();
325                 folderPath.add(0, folder);
326                 FolderData parent = folder.getParent();
327                 while (parent != null)
328                 {
329                     folderPath.add(0, parent);
330                     parent = parent.getParent();
331                 }
332                 request.setAttribute(
333                     RequestConstants.PARENT_ID, folder.getParent().getId());
334             }
335
336             // Build list of all folders, except for current one, sorted by path.
337
Iterator JavaDoc iter = bmgr.getAllFolders(website).iterator();
338             while (iter.hasNext())
339             {
340                 FolderData fd = (FolderData) iter.next();
341                 if (!fd.getId().equals(folderId))
342                 {
343                     allFolders.add(fd);
344                 }
345             }
346             if (allFolders.size() > 0) {
347                 request.setAttribute("allFolders", allFolders); // for Struts tags
348
} else {
349                 allFolders = null;
350             }
351             request.setAttribute("folder", folder); // for Struts tags
352
}
353         
354         public String JavaDoc getTitle()
355         {
356             if (folderPath == null || folderPath.isEmpty())
357             {
358                 return bundle.getString("bookmarksForm.rootTitle");
359             }
360             else
361             {
362                 return MessageFormat.format(
363                     bundle.getString("bookmarksForm.folderTitle"),
364                     new Object JavaDoc[] {folder.getName()});
365             }
366         }
367         public List JavaDoc getFolderPath()
368         {
369             return folderPath;
370         }
371         public Set JavaDoc getAllFolders()
372         {
373             return allFolders;
374         }
375         public FolderData getFolder()
376         {
377             return folder;
378         }
379                 
380
381     }
382 }
383
Popular Tags