1 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 ; 37 import java.util.ArrayList ; 38 import java.util.Iterator ; 39 import java.util.List ; 40 41 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 55 public HibernateUserManagerImpl(PersistenceStrategy strategy) 56 { 57 super(strategy); 58 mLogger.debug("Instantiating User Manager"); 59 } 60 61 64 public PageData getPageByLink(WebsiteData website, String 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 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 92 public WebsiteData getWebsite(String 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 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 130 public void removeLoginCookies(String 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 list; 136 try 137 { 138 list = criteria.list(); 139 } 140 catch (HibernateException e) 141 { 142 throw new RollerException(e); 143 } 144 for (Iterator it = list.iterator(); it.hasNext();) 145 { 146 String id = ((UserCookieData) it.next()).getId(); 147 mStrategy.remove(id, UserCookieData.class); 148 } 149 } 150 151 154 public String checkLoginCookie(String value) throws RollerException 155 { 156 try 157 { 158 value = Utilities.decodeString(value); 159 } 160 catch (IOException io) 161 { 162 mLogger.warn("Failed to decode rememberMe cookieString"); 163 return null; 164 } 165 166 String [] 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 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 212 public List 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 234 public PageData getPageByName(WebsiteData website, String 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 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 262 public List 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 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 users = new ArrayList (); 288 Iterator 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 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 websites = criteria.list(); 326 for (Iterator 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 341 public void removeWebsiteContents(WebsiteData website) 342 throws RollerException 343 { 344 try 345 { 346 Session session = ((HibernateStrategy)mStrategy).getSession(); 347 348 BookmarkManager bmgr = RollerFactory.getRoller().getBookmarkManager(); 350 WeblogManager wmgr = RollerFactory.getRoller().getWeblogManager(); 351 354 FolderData rootFolder = bmgr.getRootFolder(website); 356 if (null != rootFolder) 357 { 358 rootFolder.remove(); 359 } 368 369 Criteria entryQuery = session.createCriteria(WeblogEntryData.class); 371 entryQuery.add(Expression.eq("website", website)); 372 List entries = entryQuery.list(); 373 for (Iterator iter = entries.iterator(); iter.hasNext();) 374 { 375 WeblogEntryData entry = (WeblogEntryData) iter.next(); 376 entry.remove(); 377 } 378 379 Criteria pageQuery = session.createCriteria(PageData.class); 381 pageQuery.add(Expression.eq("website", website)); 382 List pages = pageQuery.list(); 383 for (Iterator iter = pages.iterator(); iter.hasNext();) 384 { 385 PageData page = (PageData) iter.next(); 386 page.remove(); 387 } 388 389 Criteria refererQuery = session.createCriteria(RefererData.class); 391 refererQuery.add(Expression.eq("website", website)); 392 List referers = refererQuery.list(); 393 for (Iterator iter = referers.iterator(); iter.hasNext();) 394 { 395 RefererData referer = (RefererData) iter.next(); 396 referer.remove(); 397 } 398 399 WeblogCategoryData rootCat = wmgr.getRootWeblogCategory(website); 401 if (null != rootCat) 402 { 403 rootCat.remove(); 404 Iterator it = wmgr.getWeblogCategories(website).iterator(); 405 while (it.hasNext()) 406 { 407 ((WeblogCategoryData)it.next()).remove(); 408 } 409 } 410 411 PingQueueManager pingQueueMgr = RollerFactory.getRoller().getPingQueueManager(); 413 pingQueueMgr.removeQueueEntriesByWebsite(website); 414 415 PingTargetManager pingTargetMgr = RollerFactory.getRoller().getPingTargetManager(); 417 pingTargetMgr.removeCustomPingTargets(website); 418 419 AutoPingManager autoPingMgr = RollerFactory.getRoller().getAutopingManager(); 421 List 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 |