1 4 package org.roller.presentation.bookmarks.actions; 5 6 import java.util.Comparator ; 7 import java.util.Iterator ; 8 import java.util.LinkedList ; 9 import java.util.TreeSet ; 10 11 import javax.servlet.http.HttpServletRequest ; 12 import javax.servlet.http.HttpServletResponse ; 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 40 public class BookmarksAction extends DispatchAction 41 { 42 private static Log mLogger = 43 LogFactory.getFactory().getInstance(BookmarksAction.class); 44 45 54 public ActionForward selectFolder( 55 ActionMapping mapping, 56 ActionForm actionForm, 57 HttpServletRequest request, 58 HttpServletResponse 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 82 public ActionForward deleteSelected( 83 ActionMapping mapping, 84 ActionForm actionForm, 85 HttpServletRequest request, 86 HttpServletResponse 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 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(); } 105 } 106 107 String 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 135 public ActionForward moveSelected( 136 ActionMapping mapping, 137 ActionForm actionForm, 138 HttpServletRequest request, 139 HttpServletResponse 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 String 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 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 String 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 214 private void addModelObjects(HttpServletRequest 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 allFolders = new TreeSet (new FolderPathComparator()); 222 223 String folderId = (String )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 folderPath = new LinkedList (); 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 Iterator iter = bmgr.getAllFolders(wd).iterator(); 267 268 271 while (iter.hasNext()) { 274 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 285 { 286 public int compare(Object o1, Object 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 |