KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > roller > business > BookmarkManagerImpl


1 package org.roller.business;
2
3 import java.io.StringReader JavaDoc;
4 import java.util.Iterator JavaDoc;
5 import java.util.LinkedList JavaDoc;
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 /**
24  * Abstract base implementation using PersistenceStrategy.
25  * @author Dave Johnson
26  * @author Lance Lavandowska
27  */

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     //---------------------------------------------------------- Bookmark CRUD
41

42     public BookmarkData createBookmark()
43     {
44        BookmarkData bd = new BookmarkData();
45        return bd;
46     }
47
48     public BookmarkData createBookmark(
49         FolderData parent,
50         String JavaDoc name,
51         String JavaDoc desc,
52         String JavaDoc url,
53         String JavaDoc feedUrl,
54         Integer JavaDoc weight,
55         Integer JavaDoc priority,
56         String JavaDoc 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 JavaDoc 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 // public void storeBookmark(BookmarkData data) throws RollerException
72
// {
73
// mStrategy.store(data);
74
// }
75

76     public void removeBookmark(String JavaDoc id) throws RollerException
77     {
78         mStrategy.remove(id, BookmarkData.class);
79     }
80
81     //------------------------------------------------------------ Folder CRUD
82

83     /**
84      * @see org.roller.model.BookmarkManager#createFolder()
85      */

86     public FolderData createFolder()
87     {
88        FolderData fd = new FolderData();
89        return fd;
90     }
91
92     /**
93      * @see org.roller.model.BookmarkManager#createFolder()
94      */

95     public FolderData createFolder(
96         FolderData parent,
97         String JavaDoc name,
98         String JavaDoc desc,
99         WebsiteData website)
100     {
101        FolderData fd = new FolderData(parent, name, desc, website);
102        return fd;
103     }
104
105     /**
106      * Retrieve folder and lazy-load it's sub-folders and bookmarks.
107      */

108     public FolderData retrieveFolder(String JavaDoc id) throws RollerException
109     {
110         return (FolderData)mStrategy.load(id, FolderData.class);
111     }
112     
113     //------------------------------------------------------------ Operations
114

115     public void importBookmarks(
116         WebsiteData website, String JavaDoc folderName, String JavaDoc opml)
117         throws RollerException
118     {
119         String JavaDoc 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             // Build JDOC document OPML string
131
SAXBuilder builder = new SAXBuilder();
132             StringReader JavaDoc reader = new StringReader JavaDoc( opml );
133             Document doc = builder.build( reader );
134             
135             // Iterate through children of OPML body, importing each
136
Element body = doc.getRootElement().getChild("body");
137             Iterator JavaDoc 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 JavaDoc e)
145         {
146             mLogger.error(msg,e);
147             throw new RollerException(msg,e);
148         }
149     }
150
151     //----------------------------------------------------------------
152
private void importOpmlElement(
153         WebsiteData website, Element elem, FolderData parent)
154         throws RollerException
155     {
156         String JavaDoc text = elem.getAttributeValue("text");
157         String JavaDoc title = elem.getAttributeValue("title");
158         String JavaDoc desc = elem.getAttributeValue("description");
159         String JavaDoc url = elem.getAttributeValue("url");
160         //String type = elem.getAttributeValue("type");
161
String JavaDoc xmlUrl = elem.getAttributeValue("xmlUrl");
162         String JavaDoc 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             // Leaf element. Store a bookmark
172
// Currently bookmarks must have at least a name and HTML url to be stored. Previous logic was
173
// trying to skip invalid ones, but was letting ones with an xml url and no html url through
174
// which could result in a db exception.
175
// TODO: Consider providing error feedback instead of silently skipping the invalid bookmarks here.
176
if (null != title && null != url)
177             {
178                 BookmarkData bd = createBookmark(parent,
179                     title,
180                     desc,
181                     url,
182                     xmlUrl,
183                     new Integer JavaDoc(0),
184                     new Integer JavaDoc(100),
185                     null);
186                 parent.addBookmark(bd);
187             }
188         }
189         else
190         {
191             // Store a folder
192
FolderData fd = createFolder(
193                     parent,
194                     title,
195                     desc,
196                     parent.getWebsite());
197             fd.save();
198             
199             // Import folder's children
200
Iterator JavaDoc 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     //----------------------------------------------------------------
210
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         // Add to destination folder
220
LinkedList JavaDoc deleteList = new LinkedList JavaDoc();
221         Iterator JavaDoc 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         // Remove from source folder
235
Iterator JavaDoc 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     //----------------------------------------------------------------
245
public void deleteFolderContents(FolderData src)
246     throws RollerException
247     {
248         Iterator JavaDoc srcBookmarks = src.getBookmarks().iterator();
249         while (srcBookmarks.hasNext())
250         {
251             BookmarkData bd = (BookmarkData)srcBookmarks.next();
252             removeBookmark(bd.getId());
253
254         }
255     }
256     
257     //---------------------------------------------------------------- Queries
258

259     public FolderData getFolder(WebsiteData website, String JavaDoc folderPath)
260         throws RollerException
261     {
262         return getFolderByPath(website, null, folderPath);
263     }
264     
265     public String JavaDoc getPath(FolderData folder) throws RollerException
266     {
267         if (null == folder.getParent())
268         {
269             return "/";
270         }
271         else
272         {
273             String JavaDoc 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 JavaDoc path)
281         throws RollerException
282     {
283         final Iterator JavaDoc folders;
284         final String JavaDoc[] 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 JavaDoc[] subpath = new String JavaDoc[pathArray.length - 1];
316                     System.arraycopy(pathArray, 1, subpath, 0, subpath.length);
317                 
318                     String JavaDoc pathString= Utilities.stringArrayToString(subpath,"/");
319                     return getFolderByPath(website, possibleMatch, pathString);
320                 }
321             }
322         }
323         
324         // The folder did not match and neither did any subfolders
325
return null;
326     }
327
328     //----------------------------------------------- FolderAssoc CRUD
329

330     public Assoc createFolderAssoc()
331     {
332         return new FolderAssoc();
333     }
334
335     public Assoc createFolderAssoc(
336         FolderData folder,
337         FolderData ancestor,
338         String JavaDoc relation) throws RollerException
339     {
340         return new FolderAssoc(null, folder, ancestor, relation);
341     }
342
343     public FolderAssoc retrieveFolderAssoc(String JavaDoc id) throws RollerException
344     {
345         return (FolderAssoc)mStrategy.load(id, FolderAssoc.class);
346     }
347
348     public void release()
349     {
350         // TODO Auto-generated method stub
351

352     }
353 }
354
355
356
Popular Tags