1 18 21 package org.apache.roller.ui.authoring.struts.actions; 22 23 import java.text.MessageFormat ; 24 import java.util.Comparator ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 import java.util.LinkedList ; 28 import java.util.Set ; 29 import java.util.TreeSet ; 30 31 import javax.servlet.http.HttpServletRequest ; 32 import javax.servlet.http.HttpServletResponse ; 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 66 public class BookmarksAction extends DispatchAction 67 { 68 private static Log mLogger = 69 LogFactory.getFactory().getInstance(BookmarksAction.class); 70 71 80 public ActionForward selectFolder( 81 ActionMapping mapping, 82 ActionForm actionForm, 83 HttpServletRequest request, 84 HttpServletResponse 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 110 public ActionForward deleteSelected( 111 ActionMapping mapping, 112 ActionForm actionForm, 113 HttpServletRequest request, 114 HttpServletResponse 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 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); } 136 } 137 138 BookmarkData bookmark = null; 139 String 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 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 173 public ActionForward moveSelected( 174 ActionMapping mapping, 175 ActionForm actionForm, 176 HttpServletRequest request, 177 HttpServletResponse 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 String 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 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 String bookmarks[] = form.getSelectedBookmarks(); 224 if (null != bookmarks) 225 { 226 for (int j = 0; j < bookmarks.length; j++) 227 { 228 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 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 260 { 261 public int compare(Object o1, Object 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 folderPath = null; 280 private TreeSet allFolders = null; 281 private FolderData folder = null; 282 283 public BookmarksPageModel( 284 HttpServletRequest request, 285 HttpServletResponse 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 (new FolderPathComparator()); 296 297 String folderId = (String ) 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 (); 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 Iterator 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); } else { 349 allFolders = null; 350 } 351 request.setAttribute("folder", folder); } 353 354 public String 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 [] {folder.getName()}); 365 } 366 } 367 public List getFolderPath() 368 { 369 return folderPath; 370 } 371 public Set getAllFolders() 372 { 373 return allFolders; 374 } 375 public FolderData getFolder() 376 { 377 return folder; 378 } 379 380 381 } 382 } 383 | Popular Tags |