KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > roller > business > hibernate > HibernateBookmarkManagerImpl


1 /*
2  * Created on Jun 18, 2004
3  */

4 package org.roller.business.hibernate;
5 import net.sf.hibernate.Criteria;
6 import net.sf.hibernate.HibernateException;
7 import net.sf.hibernate.Session;
8 import net.sf.hibernate.expression.Expression;
9 import org.roller.RollerException;
10 import org.roller.business.BookmarkManagerImpl;
11 import org.roller.business.PersistenceStrategy;
12 import org.roller.pojos.Assoc;
13 import org.roller.pojos.BookmarkData;
14 import org.roller.pojos.FolderAssoc;
15 import org.roller.pojos.FolderData;
16 import org.roller.pojos.WebsiteData;
17 import java.util.Iterator JavaDoc;
18 import java.util.LinkedList JavaDoc;
19 import java.util.List JavaDoc;
20 import org.apache.commons.logging.Log;
21 import org.apache.commons.logging.LogFactory;
22
23 /**
24  * Hibernate queries.
25  *
26  * @author David M Johnson
27  */

28 public class HibernateBookmarkManagerImpl extends BookmarkManagerImpl
29 {
30     static final long serialVersionUID = 5286654557062382772L;
31
32     private static Log mLogger =
33         LogFactory.getFactory().getInstance(HibernateBookmarkManagerImpl.class);
34     
35     /**
36      * @param pstrategy
37      * @param roller
38      */

39     public HibernateBookmarkManagerImpl(PersistenceStrategy pstrategy)
40     {
41         super(pstrategy);
42         mLogger.debug("Instantiating Bookmark Manager");
43     }
44
45     /**
46      * @see org.roller.model.BookmarkManager#retrieveBookmarks(
47      * org.roller.pojos.FolderData, boolean)
48      */

49     public List JavaDoc retrieveBookmarks(FolderData folder, boolean subfolders)
50                     throws RollerException
51     {
52         try
53         {
54             Session session = ((HibernateStrategy) mStrategy).getSession();
55             List JavaDoc bookmarks = new LinkedList JavaDoc();
56             if (subfolders)
57             {
58                 // get bookmarks in subfolders
59
Criteria assocsQuery = session
60                                 .createCriteria(FolderAssoc.class);
61                 assocsQuery.add(Expression.eq("ancestorFolder", folder));
62                 Iterator JavaDoc assocs = assocsQuery.list().iterator();
63                 while (assocs.hasNext())
64                 {
65                     FolderAssoc assoc = (FolderAssoc) assocs.next();
66                     Criteria bookmarksQuery = session
67                                     .createCriteria(BookmarkData.class);
68                     bookmarksQuery.add(Expression.eq("folder", assoc
69                                     .getFolder()));
70                     Iterator JavaDoc bookmarkIter = bookmarksQuery.list().iterator();
71                     while (bookmarkIter.hasNext())
72                     {
73                         BookmarkData entry = (BookmarkData) bookmarkIter.next();
74                         bookmarks.add(entry);
75                     }
76                 }
77             }
78             // get bookmarks in folder
79
Criteria bookmarksQuery = session
80                             .createCriteria(BookmarkData.class);
81             bookmarksQuery.add(Expression.eq("folder", folder));
82             Iterator JavaDoc bookmarkIter = bookmarksQuery.list().iterator();
83             while (bookmarkIter.hasNext())
84             {
85                 BookmarkData bookmark = (BookmarkData) bookmarkIter.next();
86                 bookmarks.add(bookmark);
87             }
88             return bookmarks;
89         }
90         catch (HibernateException e)
91         {
92             throw new RollerException(e);
93         }
94     }
95
96     public FolderData getRootFolder(WebsiteData website) throws RollerException
97     {
98         if (website == null)
99             throw new RollerException("website is null");
100         try
101         {
102             Session session = ((HibernateStrategy) mStrategy).getSession();
103             Criteria criteria = session.createCriteria(FolderAssoc.class);
104             criteria.createAlias("folder", "f");
105             criteria.add(Expression.eq("f.website", website));
106             criteria.add(Expression.isNull("ancestorFolder"));
107             criteria.add(Expression.eq("relation", FolderAssoc.PARENT));
108             List JavaDoc results = criteria.list();
109             if (results.size() > 1)
110             {
111                 // Should not have more than one root
112
throw new RollerException(
113                                 "More than one root folder found for website "
114                                                 + website.getId());
115             }
116             else if (results.size() == 1)
117             {
118                 // Return root
119
return ((FolderAssoc) results.get(0)).getFolder();
120             }
121             return null;
122         }
123         catch (HibernateException e)
124         {
125             throw new RollerException(e);
126         }
127     }
128
129     public List JavaDoc getAllFolders(WebsiteData website) throws RollerException
130     {
131         if (website == null)
132             throw new RollerException("Website is null");
133         
134         try
135         {
136             Session session = ((HibernateStrategy) mStrategy).getSession();
137             Criteria criteria = session.createCriteria(FolderData.class);
138             criteria.add(Expression.eq("website", website));
139             return criteria.list();
140         }
141         catch (HibernateException e)
142         {
143             throw new RollerException(e);
144         }
145
146     }
147
148     /**
149      * @see org.roller.model.BookmarkManager#isDuplicateFolderName(org.roller.pojos.FolderData)
150      */

151     public boolean isDuplicateFolderName(FolderData folder) throws RollerException
152     {
153         // ensure that no sibling folders share the same name
154
boolean isNewFolder = (folder.getId() == null);
155         FolderData parent =
156             isNewFolder ? (FolderData)folder.getNewParent() : folder.getParent();
157         
158         if (null != parent)
159         {
160             List JavaDoc sameNames;
161             try
162             {
163                 Session session = ((HibernateStrategy) mStrategy).getSession();
164                 Criteria criteria = session.createCriteria(FolderAssoc.class);
165                 criteria.createAlias("folder", "f");
166                 criteria.add(Expression.eq("f.name", folder.getName()));
167                 criteria.add(Expression.eq("ancestorFolder", parent));
168                 criteria.add(Expression.eq("relation", Assoc.PARENT));
169                 sameNames = criteria.list();
170             }
171             catch (HibernateException e)
172             {
173                 throw new RollerException(e);
174             }
175             // If we got some matches
176
if (sameNames.size() > 0)
177             {
178                 // if we're saving a new folder, any matches are dups
179
if (isNewFolder) return true;
180                 // otherwise it's a dup it isn't the same one (one match with the same id).
181
if (!(sameNames.size() == 1 && folder.getId().equals(((FolderAssoc)sameNames.get(0)).getFolder().getId())))
182                     return true;
183             }
184         }
185         return false;
186     }
187
188     /**
189      * @see org.roller.model.BookmarkManager#getFolderParentAssoc(
190      * org.roller.pojos.FolderData)
191      */

192     public Assoc getFolderParentAssoc(FolderData folder) throws RollerException
193     {
194         try
195         {
196             Session session = ((HibernateStrategy)mStrategy).getSession();
197             Criteria criteria = session.createCriteria(FolderAssoc.class);
198             criteria.add(Expression.eq("folder", folder));
199             criteria.add(Expression.eq("relation", Assoc.PARENT));
200             List JavaDoc parents = criteria.list();
201             if (parents.size() > 1)
202             {
203                 throw new RollerException("ERROR: more than one parent");
204             }
205             else if (parents.size() == 1)
206             {
207                 return (Assoc) parents.get(0);
208             }
209             else
210             {
211                 return null;
212             }
213         }
214         catch (HibernateException e)
215         {
216             throw new RollerException(e);
217         }
218     }
219
220     /**
221      * @see org.roller.model.BookmarkManager#getFolderChildAssocs(
222      * org.roller.pojos.FolderData)
223      */

224     public List JavaDoc getFolderChildAssocs(FolderData folder) throws RollerException
225     {
226         try
227         {
228             Session session = ((HibernateStrategy)mStrategy).getSession();
229             Criteria criteria = session.createCriteria(FolderAssoc.class);
230             criteria.add(Expression.eq("ancestorFolder", folder));
231             criteria.add(Expression.eq("relation", Assoc.PARENT));
232             return criteria.list();
233         }
234         catch (HibernateException e)
235         {
236             throw new RollerException(e);
237         }
238     }
239
240     /**
241      * @see org.roller.model.BookmarkManager#getAllFolderDecscendentAssocs(
242      * org.roller.pojos.FolderData)
243      */

244     public List JavaDoc getAllFolderDecscendentAssocs(FolderData folder) throws RollerException
245     {
246         try
247         {
248             Session session = ((HibernateStrategy)mStrategy).getSession();
249             Criteria criteria = session.createCriteria(FolderAssoc.class);
250             criteria.add(Expression.eq("ancestorFolder", folder));
251             return criteria.list();
252         }
253         catch (HibernateException e)
254         {
255             throw new RollerException(e);
256         }
257     }
258
259     /**
260      * @see org.roller.model.BookmarkManager#getFolderAncestorAssocs(
261      * org.roller.pojos.FolderData)
262      */

263     public List JavaDoc getFolderAncestorAssocs(FolderData folder) throws RollerException
264     {
265         try
266         {
267             Session session = ((HibernateStrategy)mStrategy).getSession();
268             Criteria criteria = session.createCriteria(FolderAssoc.class);
269             criteria.add(Expression.eq("folder", folder));
270             return criteria.list();
271         }
272         catch (HibernateException e)
273         {
274             throw new RollerException(e);
275         }
276     }
277
278     /**
279      * @see org.roller.model.BookmarkManager#isFolderInUse(org.roller.pojos.FolderData)
280      */

281     public boolean isFolderInUse(FolderData folder) throws RollerException
282     {
283         try
284         {
285             // We consider a folder to be "in use" if it contains any bookmarks or has
286
// any children.
287

288             // We first determine the number of bookmark entries.
289
// NOTE: This seems to be an attempt to optimize, rather than just use getBookmarks(),
290
// but I'm not sure that this optimization is really worthwhile, and it ignores
291
// caching in the case that the (lazy) getBookmarks has been done already. --agangolli
292
// TODO: condider changing to just use getBookmarks().size()
293
Session session = ((HibernateStrategy)mStrategy).getSession();
294             Criteria criteria = session.createCriteria(BookmarkData.class);
295             criteria.add(Expression.eq("folder", folder));
296             criteria.setMaxResults(1);
297             int entryCount = criteria.list().size();
298
299             // Return true if we have bookmarks or (, failing that, then checking) if we have children
300
return (entryCount > 0 || folder.getFolders().size() > 0);
301         }
302         catch (HibernateException e)
303         {
304             throw new RollerException(e);
305         }
306     }
307
308     public boolean isDescendentOf(FolderData child, FolderData ancestor)
309         throws RollerException
310     {
311         boolean ret = false;
312         try
313         {
314             Session session = ((HibernateStrategy)mStrategy).getSession();
315             Criteria criteria = session.createCriteria(FolderAssoc.class);
316             criteria.add(Expression.eq("folder", child));
317             criteria.add(Expression.eq("ancestorFolder", ancestor));
318             ret = criteria.list().size() > 0;
319         }
320         catch (HibernateException e)
321         {
322             throw new RollerException(e);
323         }
324         return ret;
325     }
326 }
327
Popular Tags