KickJava   Java API By Example, From Geeks To Geeks.

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


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

4 package org.roller.business.hibernate;
5
6 import net.sf.hibernate.Criteria;
7 import net.sf.hibernate.HibernateException;
8 import net.sf.hibernate.Session;
9 import net.sf.hibernate.expression.Expression;
10 import net.sf.hibernate.expression.EqExpression;
11 import net.sf.hibernate.expression.Order;
12
13 import org.apache.commons.logging.Log;
14 import org.apache.commons.logging.LogFactory;
15 import org.roller.RollerException;
16 import org.roller.business.PersistenceStrategy;
17 import org.roller.business.UserManagerImpl;
18 import org.roller.model.BookmarkManager;
19 import org.roller.model.RollerFactory;
20 import org.roller.model.WeblogManager;
21 import org.roller.model.AutoPingManager;
22 import org.roller.model.PingTargetManager;
23 import org.roller.model.PingQueueManager;
24 import org.roller.pojos.FolderData;
25 import org.roller.pojos.PageData;
26 import org.roller.pojos.RefererData;
27 import org.roller.pojos.RoleData;
28 import org.roller.pojos.UserCookieData;
29 import org.roller.pojos.UserData;
30 import org.roller.pojos.WeblogCategoryData;
31 import org.roller.pojos.WeblogEntryData;
32 import org.roller.pojos.WebsiteData;
33 import org.roller.util.StringUtils;
34 import org.roller.util.Utilities;
35
36 import java.io.IOException JavaDoc;
37 import java.util.ArrayList JavaDoc;
38 import java.util.Iterator JavaDoc;
39 import java.util.List JavaDoc;
40
41 /**
42  * Hibernate queries.
43  * @author David M Johnson
44  */

45 public class HibernateUserManagerImpl extends UserManagerImpl
46 {
47     static final long serialVersionUID = -5128460637997081121L;
48     
49     private static Log mLogger =
50         LogFactory.getFactory().getInstance(HibernateUserManagerImpl.class);
51     
52     /**
53      * @param strategy
54      */

55     public HibernateUserManagerImpl(PersistenceStrategy strategy)
56     {
57         super(strategy);
58         mLogger.debug("Instantiating User Manager");
59     }
60     
61     /**
62      * Use Hibernate directly because Roller's Query API does too much allocation.
63      */

64     public PageData getPageByLink(WebsiteData website, String JavaDoc pagelink)
65                     throws RollerException
66     {
67         if (website == null)
68             throw new RollerException("userName is null");
69                                    
70         if (pagelink == null)
71             throw new RollerException("Pagelink is null");
72                                                                       
73         Session session = ((HibernateStrategy)mStrategy).getSession();
74         Criteria criteria = session.createCriteria(PageData.class);
75         criteria.add(Expression.eq("website",website));
76         criteria.add(Expression.eq("link",pagelink));
77         criteria.setMaxResults(1);
78         try
79         {
80             List JavaDoc list = criteria.list();
81             return list.size()!=0 ? (PageData)list.get(0) : null;
82         }
83         catch (HibernateException e)
84         {
85             throw new RollerException(e);
86         }
87     }
88     
89     /**
90      * Use Hibernate directly because Roller's Query API does too much allocation.
91      */

92     public WebsiteData getWebsite(String JavaDoc userName, boolean enabledOnly)
93                     throws RollerException
94     {
95         if (userName==null )
96             throw new RollerException("userName is null");
97
98         try
99         {
100             Session session = ((HibernateStrategy)mStrategy).getSession();
101             Criteria criteria = session.createCriteria(WebsiteData.class);
102             criteria.createAlias("user","u");
103     
104             if (enabledOnly)
105             {
106                 criteria.add(
107                    Expression.conjunction()
108                        .add(new EqExpression("u.userName",userName,true))
109                        .add(Expression.eq("isEnabled",Boolean.TRUE)));
110             }
111             else
112             {
113                 criteria.add(
114                     Expression.conjunction()
115                        .add(new EqExpression("u.userName",userName,true)));
116             }
117         
118             List JavaDoc list = criteria.list();
119             return list.size()!=0 ? (WebsiteData)list.get(0) : null;
120         }
121         catch (HibernateException e)
122         {
123             throw new RollerException(e);
124         }
125     }
126     
127     /**
128      * @see org.roller.model.UserManager#removeLoginCookies(java.lang.String)
129      */

130     public void removeLoginCookies(String JavaDoc username) throws RollerException
131     {
132         Session session = ((HibernateStrategy)mStrategy).getSession();
133         Criteria criteria = session.createCriteria(UserCookieData.class);
134         criteria.add(Expression.eq("username", username));
135         List JavaDoc list;
136         try
137         {
138             list = criteria.list();
139         }
140         catch (HibernateException e)
141         {
142             throw new RollerException(e);
143         }
144         for (Iterator JavaDoc it = list.iterator(); it.hasNext();)
145         {
146             String JavaDoc id = ((UserCookieData) it.next()).getId();
147             mStrategy.remove(id, UserCookieData.class);
148         }
149     }
150     
151     /**
152      * @see org.roller.model.UserManager#checkLoginCookie(java.lang.String)
153      */

154     public String JavaDoc checkLoginCookie(String JavaDoc value) throws RollerException
155     {
156         try
157         {
158             value = Utilities.decodeString(value);
159         }
160         catch (IOException JavaDoc io)
161         {
162             mLogger.warn("Failed to decode rememberMe cookieString");
163             return null;
164         }
165         
166         String JavaDoc[] values = StringUtils.split(value, "|");
167
168         if (mLogger.isDebugEnabled())
169         {
170             mLogger.debug("looking up cookieId: " + values[1]);
171         }
172
173         Session session = ((HibernateStrategy)mStrategy).getSession();
174         Criteria criteria = session.createCriteria(UserCookieData.class);
175         criteria.add(Expression.eq("username", values[0]));
176         criteria.add(Expression.eq("cookieId", values[1]));
177                
178         List JavaDoc list;
179         try
180         {
181             list = criteria.list();
182         }
183         catch (HibernateException e)
184         {
185             throw new RollerException(e);
186         }
187         UserCookieData cookie = (list.size() > 0) ? (UserCookieData)list.get(0) : null;
188
189         if (cookie != null)
190         {
191             if (mLogger.isDebugEnabled())
192             {
193                 mLogger.debug("cookieId lookup succeeded, generating new cookieId");
194             }
195             return saveLoginCookie(cookie);
196         }
197         else
198         {
199             if (mLogger.isDebugEnabled())
200             {
201                 mLogger.debug("cookieId lookup failed, returning null");
202             }
203
204             return null;
205         }
206     }
207     
208     //------------------------------------------------------------------------
209
/**
210      * @see org.roller.model.UserManager#getPages(WebsiteData)
211      */

212     public List JavaDoc getPages(WebsiteData website) throws RollerException
213     {
214         if (website == null)
215             throw new RollerException("website is null");
216                                                                       
217         Session session = ((HibernateStrategy)mStrategy).getSession();
218         Criteria criteria = session.createCriteria(PageData.class);
219         criteria.add(Expression.eq("website",website));
220         criteria.addOrder(Order.asc("name"));
221         try
222         {
223             return criteria.list();
224         }
225         catch (HibernateException e)
226         {
227             throw new RollerException(e);
228         }
229     }
230     
231     /**
232      * @see org.roller.model.UserManager#getPageByName(WebsiteData, java.lang.String)
233      */

234     public PageData getPageByName(WebsiteData website, String JavaDoc pagename)
235         throws RollerException
236     {
237         if (website == null)
238             throw new RollerException("website is null");
239                                    
240         if (pagename == null)
241             throw new RollerException("Page name is null");
242                                    
243         Session session = ((HibernateStrategy)mStrategy).getSession();
244         Criteria criteria = session.createCriteria(PageData.class);
245         criteria.add(Expression.eq("website", website));
246         criteria.add(Expression.eq("name", pagename));
247         criteria.setMaxResults(1);
248         try
249         {
250             List JavaDoc list = criteria.list();
251             return list.size()!=0 ? (PageData)list.get(0) : null;
252         }
253         catch (HibernateException e)
254         {
255             throw new RollerException(e);
256         }
257     }
258     
259     /*
260      * @see org.roller.business.UserManagerBase#getRoles(org.roller.pojos.UserData)
261      */

262     public List JavaDoc getUserRoles(UserData user) throws RollerException
263     {
264         
265         Session session = ((HibernateStrategy)mStrategy).getSession();
266         Criteria criteria = session.createCriteria(RoleData.class);
267         criteria.add(Expression.eq("user", user));
268         try
269         {
270             return criteria.list();
271         }
272         catch (HibernateException e)
273         {
274             throw new RollerException(e);
275         }
276     }
277
278     public List JavaDoc getUsers(boolean enabledOnly) throws RollerException
279     {
280         Session session = ((HibernateStrategy)mStrategy).getSession();
281         if (enabledOnly)
282         {
283             Criteria criteria = session.createCriteria(WebsiteData.class);
284             criteria.add(Expression.eq("isEnabled", Boolean.TRUE));
285             try
286             {
287                 List JavaDoc users = new ArrayList JavaDoc();
288                 Iterator JavaDoc websites = criteria.list().iterator();
289                 while (websites.hasNext())
290                 {
291                     WebsiteData website = (WebsiteData) websites.next();
292                     users.add(website.getUser());
293                 }
294                 return users;
295             }
296             catch (HibernateException e)
297             {
298                 throw new RollerException(e);
299             }
300         }
301         else
302         {
303             Criteria criteria = session.createCriteria(UserData.class);
304             try
305             {
306                 return criteria.list();
307             }
308             catch (HibernateException e)
309             {
310                 throw new RollerException(e);
311             }
312         }
313     }
314
315     /**
316      * @see org.roller.model.UserManager#removeUserWebsites(org.roller.pojos.UserData)
317      */

318     public void removeUserWebsites(UserData user) throws RollerException
319     {
320         Session session = ((HibernateStrategy)mStrategy).getSession();
321         Criteria criteria = session.createCriteria(WebsiteData.class);
322         criteria.add(Expression.eq("user", user));
323         try
324         {
325             List JavaDoc websites = criteria.list();
326             for (Iterator JavaDoc iter = websites.iterator(); iter.hasNext();)
327             {
328                 WebsiteData website = (WebsiteData)iter.next();
329                 website.remove();
330             }
331         }
332         catch (HibernateException e)
333         {
334             throw new RollerException(e);
335         }
336     }
337
338     /**
339      * @see org.roller.model.UserManager#removeWebsiteContents(org.roller.pojos.WebsiteData)
340      */

341     public void removeWebsiteContents(WebsiteData website)
342         throws RollerException
343     {
344         try
345         {
346             Session session = ((HibernateStrategy)mStrategy).getSession();
347
348             //UserManager umgr = RollerFactory.getRoller().getUserManager();
349
BookmarkManager bmgr = RollerFactory.getRoller().getBookmarkManager();
350             WeblogManager wmgr = RollerFactory.getRoller().getWeblogManager();
351             //PersistenceStrategy pstrat = RollerFactory.getRoller().getPersistenceStrategy();
352
//QueryFactory factory = pstrat.getQueryFactory();
353

354             // remove folders (takes bookmarks with it)
355
FolderData rootFolder = bmgr.getRootFolder(website);
356             if (null != rootFolder)
357             {
358                 rootFolder.remove();
359                 // Still cannot get all Bookmarks cleared!
360
// Iterator allFolders = bmgr.getAllFolders(website).iterator();
361
// while (allFolders.hasNext())
362
// {
363
// FolderData aFolder = (FolderData)allFolders.next();
364
// bmgr.deleteFolderContents(aFolder);
365
// aFolder.remove();
366
// }
367
}
368                         
369             // remove entries
370
Criteria entryQuery = session.createCriteria(WeblogEntryData.class);
371             entryQuery.add(Expression.eq("website", website));
372             List JavaDoc entries = entryQuery.list();
373             for (Iterator JavaDoc iter = entries.iterator(); iter.hasNext();)
374             {
375                 WeblogEntryData entry = (WeblogEntryData) iter.next();
376                 entry.remove();
377             }
378             
379             // remove associated pages
380
Criteria pageQuery = session.createCriteria(PageData.class);
381             pageQuery.add(Expression.eq("website", website));
382             List JavaDoc pages = pageQuery.list();
383             for (Iterator JavaDoc iter = pages.iterator(); iter.hasNext();)
384             {
385                 PageData page = (PageData) iter.next();
386                 page.remove();
387             }
388             
389             // remove associated referers
390
Criteria refererQuery = session.createCriteria(RefererData.class);
391             refererQuery.add(Expression.eq("website", website));
392             List JavaDoc referers = refererQuery.list();
393             for (Iterator JavaDoc iter = referers.iterator(); iter.hasNext();)
394             {
395                 RefererData referer = (RefererData) iter.next();
396                 referer.remove();
397             }
398             
399             // remove categories
400
WeblogCategoryData rootCat = wmgr.getRootWeblogCategory(website);
401             if (null != rootCat)
402             {
403                 rootCat.remove();
404                 Iterator JavaDoc it = wmgr.getWeblogCategories(website).iterator();
405                 while (it.hasNext())
406                 {
407                      ((WeblogCategoryData)it.next()).remove();
408                 }
409             }
410
411             // Remove the website's ping queue entries
412
PingQueueManager pingQueueMgr = RollerFactory.getRoller().getPingQueueManager();
413             pingQueueMgr.removeQueueEntriesByWebsite(website);
414
415             // Remove the website's custom ping targets
416
PingTargetManager pingTargetMgr = RollerFactory.getRoller().getPingTargetManager();
417             pingTargetMgr.removeCustomPingTargets(website);
418
419             // Remove the website's auto ping configurations
420
AutoPingManager autoPingMgr = RollerFactory.getRoller().getAutopingManager();
421             List JavaDoc autopings = autoPingMgr.getAutoPingsByWebsite(website);
422             autoPingMgr.removeAutoPings(autopings);
423
424         }
425         catch (HibernateException e)
426         {
427             throw new RollerException(e);
428         }
429     }
430
431 }
432
433
Popular Tags