1 package org.roller.business; 2 3 import java.io.StringReader ; 4 import java.util.Iterator ; 5 import java.util.LinkedList ; 6 7 import org.apache.commons.logging.Log; 8 import org.apache.commons.logging.LogFactory; 9 import org.jdom.Document; 10 import org.jdom.Element; 11 import org.jdom.input.SAXBuilder; 12 import org.roller.RollerException; 13 import org.roller.model.BookmarkManager; 14 import org.roller.model.Roller; 15 import org.roller.pojos.Assoc; 16 import org.roller.pojos.BookmarkData; 17 import org.roller.pojos.FolderAssoc; 18 import org.roller.pojos.FolderData; 19 import org.roller.pojos.WebsiteData; 20 import org.roller.util.Utilities; 21 22 23 28 public abstract class BookmarkManagerImpl implements BookmarkManager 29 { 30 protected PersistenceStrategy mStrategy; 31 32 private static Log mLogger = 33 LogFactory.getFactory().getInstance(BookmarkManagerImpl.class); 34 35 public BookmarkManagerImpl(PersistenceStrategy pstrategy) 36 { 37 mStrategy = pstrategy; 38 } 39 40 42 public BookmarkData createBookmark() 43 { 44 BookmarkData bd = new BookmarkData(); 45 return bd; 46 } 47 48 public BookmarkData createBookmark( 49 FolderData parent, 50 String name, 51 String desc, 52 String url, 53 String feedUrl, 54 Integer weight, 55 Integer priority, 56 String image) 57 { 58 BookmarkData bd = new BookmarkData( 59 parent, name, desc, url, feedUrl, weight, priority, image); 60 return bd; 61 } 62 63 public BookmarkData retrieveBookmark(String id) throws RollerException 64 { 65 BookmarkData bd = (BookmarkData) 66 mStrategy.load(id, BookmarkData.class); 67 if (bd != null) bd.setBookmarkManager(this); 68 return bd; 69 } 70 71 76 public void removeBookmark(String id) throws RollerException 77 { 78 mStrategy.remove(id, BookmarkData.class); 79 } 80 81 83 86 public FolderData createFolder() 87 { 88 FolderData fd = new FolderData(); 89 return fd; 90 } 91 92 95 public FolderData createFolder( 96 FolderData parent, 97 String name, 98 String desc, 99 WebsiteData website) 100 { 101 FolderData fd = new FolderData(parent, name, desc, website); 102 return fd; 103 } 104 105 108 public FolderData retrieveFolder(String id) throws RollerException 109 { 110 return (FolderData)mStrategy.load(id, FolderData.class); 111 } 112 113 115 public void importBookmarks( 116 WebsiteData website, String folderName, String opml) 117 throws RollerException 118 { 119 String msg = "importBookmarks"; 120 try 121 { 122 FolderData newFolder = getFolder(website, folderName); 123 if (newFolder == null) 124 { 125 newFolder = createFolder( 126 getRootFolder(website), folderName, folderName, website); 127 newFolder.save(); 128 } 129 130 SAXBuilder builder = new SAXBuilder(); 132 StringReader reader = new StringReader ( opml ); 133 Document doc = builder.build( reader ); 134 135 Element body = doc.getRootElement().getChild("body"); 137 Iterator iter = body.getChildren().iterator(); 138 while (iter.hasNext()) 139 { 140 Element elem = (Element)iter.next(); 141 importOpmlElement( website, elem, newFolder ); 142 } 143 } 144 catch (Exception e) 145 { 146 mLogger.error(msg,e); 147 throw new RollerException(msg,e); 148 } 149 } 150 151 private void importOpmlElement( 153 WebsiteData website, Element elem, FolderData parent) 154 throws RollerException 155 { 156 String text = elem.getAttributeValue("text"); 157 String title = elem.getAttributeValue("title"); 158 String desc = elem.getAttributeValue("description"); 159 String url = elem.getAttributeValue("url"); 160 String xmlUrl = elem.getAttributeValue("xmlUrl"); 162 String htmlUrl = elem.getAttributeValue("htmlUrl"); 163 164 title = null!=title ? title : text; 165 desc = null!=desc ? desc : title; 166 xmlUrl = null!=xmlUrl ? xmlUrl : url; 167 url = null!=htmlUrl ? htmlUrl : url; 168 169 if (elem.getChildren().size()==0) 170 { 171 if (null != title && null != url) 177 { 178 BookmarkData bd = createBookmark(parent, 179 title, 180 desc, 181 url, 182 xmlUrl, 183 new Integer (0), 184 new Integer (100), 185 null); 186 parent.addBookmark(bd); 187 } 188 } 189 else 190 { 191 FolderData fd = createFolder( 193 parent, 194 title, 195 desc, 196 parent.getWebsite()); 197 fd.save(); 198 199 Iterator iter = elem.getChildren("outline").iterator(); 201 while ( iter.hasNext() ) 202 { 203 Element subelem = (Element)iter.next(); 204 importOpmlElement( website, subelem, fd ); 205 } 206 } 207 } 208 209 public void moveFolderContents(FolderData src, FolderData dest) 211 throws RollerException 212 { 213 if (dest.descendentOf(src)) 214 { 215 throw new RollerException( 216 "ERROR cannot move parent folder into it's own child"); 217 } 218 219 LinkedList deleteList = new LinkedList (); 221 Iterator srcBookmarks = src.getBookmarks().iterator(); 222 while (srcBookmarks.hasNext()) 223 { 224 BookmarkData bd = (BookmarkData)srcBookmarks.next(); 225 deleteList.add(bd); 226 227 BookmarkData movedBd = new BookmarkData(); 228 movedBd.setData(bd); 229 movedBd.setId(null); 230 231 dest.addBookmark(movedBd); 232 } 233 234 Iterator deleteIter = deleteList.iterator(); 236 while (deleteIter.hasNext()) 237 { 238 BookmarkData bd = (BookmarkData)deleteIter.next(); 239 src.removeBookmark(bd); 240 removeBookmark(bd.getId()); 241 } 242 } 243 244 public void deleteFolderContents(FolderData src) 246 throws RollerException 247 { 248 Iterator srcBookmarks = src.getBookmarks().iterator(); 249 while (srcBookmarks.hasNext()) 250 { 251 BookmarkData bd = (BookmarkData)srcBookmarks.next(); 252 removeBookmark(bd.getId()); 253 254 } 255 } 256 257 259 public FolderData getFolder(WebsiteData website, String folderPath) 260 throws RollerException 261 { 262 return getFolderByPath(website, null, folderPath); 263 } 264 265 public String getPath(FolderData folder) throws RollerException 266 { 267 if (null == folder.getParent()) 268 { 269 return "/"; 270 } 271 else 272 { 273 String parentPath = getPath(folder.getParent()); 274 parentPath = "/".equals(parentPath) ? "" : parentPath; 275 return parentPath + "/" + folder.getName(); 276 } 277 } 278 279 public FolderData getFolderByPath( 280 WebsiteData website, FolderData folder, String path) 281 throws RollerException 282 { 283 final Iterator folders; 284 final String [] pathArray = Utilities.stringToStringArray(path, "/"); 285 286 if (folder == null && (null == path || "".equals(path.trim()))) 287 { 288 throw new RollerException("Bad arguments."); 289 } 290 291 if (path.trim().equals("/")) 292 { 293 return getRootFolder(website); 294 } 295 else if (folder == null || path.trim().startsWith("/")) 296 { 297 folders = getRootFolder(website).getFolders().iterator(); 298 } 299 else 300 { 301 folders = folder.getFolders().iterator(); 302 } 303 304 while (folders.hasNext()) 305 { 306 FolderData possibleMatch = (FolderData)folders.next(); 307 if (possibleMatch.getName().equals(pathArray[0])) 308 { 309 if (pathArray.length == 1) 310 { 311 return possibleMatch; 312 } 313 else 314 { 315 String [] subpath = new String [pathArray.length - 1]; 316 System.arraycopy(pathArray, 1, subpath, 0, subpath.length); 317 318 String pathString= Utilities.stringArrayToString(subpath,"/"); 319 return getFolderByPath(website, possibleMatch, pathString); 320 } 321 } 322 } 323 324 return null; 326 } 327 328 330 public Assoc createFolderAssoc() 331 { 332 return new FolderAssoc(); 333 } 334 335 public Assoc createFolderAssoc( 336 FolderData folder, 337 FolderData ancestor, 338 String relation) throws RollerException 339 { 340 return new FolderAssoc(null, folder, ancestor, relation); 341 } 342 343 public FolderAssoc retrieveFolderAssoc(String id) throws RollerException 344 { 345 return (FolderAssoc)mStrategy.load(id, FolderAssoc.class); 346 } 347 348 public void release() 349 { 350 352 } 353 } 354 355 356 | Popular Tags |