1 4 package org.roller.business.hibernate; 5 6 import java.sql.Connection ; 7 import java.sql.PreparedStatement ; 8 import java.sql.ResultSet ; 9 import java.util.ArrayList ; 10 import java.util.Date ; 11 import java.util.Iterator ; 12 import java.util.List ; 13 14 import net.sf.hibernate.Criteria; 15 import net.sf.hibernate.Hibernate; 16 import net.sf.hibernate.HibernateException; 17 import net.sf.hibernate.Session; 18 import net.sf.hibernate.expression.Expression; 19 import net.sf.hibernate.expression.Junction; 20 import net.sf.hibernate.expression.Order; 21 import net.sf.hibernate.type.Type; 22 23 import org.apache.commons.lang.StringUtils; 24 import org.apache.commons.logging.Log; 25 import org.apache.commons.logging.LogFactory; 26 import org.roller.RollerException; 27 import org.roller.business.PersistenceStrategy; 28 import org.roller.business.RefererManagerImpl; 29 import org.roller.config.RollerRuntimeConfig; 30 import org.roller.model.RefererManager; 31 import org.roller.pojos.RefererData; 32 import org.roller.pojos.WebsiteData; 33 import org.roller.pojos.WebsiteDisplayData; 34 35 36 40 public class HibernateRefererManagerImpl extends RefererManagerImpl 41 implements RefererManager 42 { 43 static final long serialVersionUID = -4966091850482256435L; 44 45 private static Log mLogger = 46 LogFactory.getFactory().getInstance(HibernateRefererManagerImpl.class); 47 48 public HibernateRefererManagerImpl(PersistenceStrategy support) 50 throws RollerException 51 { 52 super(); 53 mStrategy = (HibernateStrategy)support; 54 mLogger.debug("Instantiating Referer Manager"); 55 } 56 57 61 public void applyRefererFilters() throws RollerException 62 { 63 try 64 { 65 Session session = ((HibernateStrategy)mStrategy).getSession(); 66 Criteria criteria = session.createCriteria(RefererData.class); 67 68 String spamwords = RollerRuntimeConfig.getProperty("spam.referers.ignorewords"); 69 70 String [] ignoreWords = StringUtils.split( 71 StringUtils.deleteWhitespace(spamwords),","); 72 Junction or = Expression.disjunction(); 73 for (int i=0; i<ignoreWords.length; i++) 74 { 75 String ignoreWord = ignoreWords[i].trim(); 76 or.add(Expression.ilike("refererUrl","%"+ignoreWord+"%")); 77 } 78 criteria.add(Expression.conjunction() 79 .add(Expression.isNull("excerpt")) 80 .add(or) 81 ); 82 83 Iterator referers = criteria.list().iterator(); 84 while (referers.hasNext()) 85 { 86 removeReferer( ((RefererData)referers.next()).getId() ); 87 } 88 } 89 catch (HibernateException e) 90 { 91 throw new RollerException(e); 92 } 93 } 94 95 99 public void applyRefererFilters(WebsiteData website) throws RollerException 100 { 101 if (null == website) throw new RollerException("website is null"); 102 if (null == website.getIgnoreWords()) return; 103 104 try 105 { 106 Session session = ((HibernateStrategy)mStrategy).getSession(); 107 Criteria criteria = session.createCriteria(RefererData.class); 108 109 String [] ignoreWords = StringUtils.split( 110 StringUtils.deleteWhitespace(website.getIgnoreWords()),","); 111 if (ignoreWords.length == 0) return; 112 113 Junction or = Expression.disjunction(); 114 for (int i=0; i<ignoreWords.length; i++) 115 { 116 String ignoreWord = ignoreWords[i].trim(); 117 or.add(Expression.ilike("refererUrl","%"+ignoreWord+"%")); 118 } 119 criteria.add(Expression.conjunction() 120 .add(Expression.isNull("excerpt")) 121 .add(Expression.eq("website",website)) 122 .add(or) 123 ); 124 125 Iterator referers = criteria.list().iterator(); 126 while (referers.hasNext()) 127 { 128 removeReferer( ((RefererData)referers.next()).getId() ); 129 } 130 } 131 catch (HibernateException e) 132 { 133 throw new RollerException(e); 134 } 135 } 136 137 139 142 protected List getExistingReferers(WebsiteData website, String dateString, 143 String permalink) throws RollerException 144 { 145 Session session = ((HibernateStrategy)mStrategy).getSession(); 146 Criteria criteria = session.createCriteria(RefererData.class); 147 criteria.add(Expression.conjunction() 148 .add(Expression.eq("website",website)) 149 .add(Expression.eq("dateString",dateString)) 150 .add(Expression.eq("refererPermalink",permalink))); 151 try 152 { 153 return criteria.list(); 154 } 155 catch (HibernateException e) 156 { 157 throw new RollerException(e); 158 } 159 } 160 161 163 166 protected List getMatchingReferers(WebsiteData website, String requestUrl, 167 String refererUrl) throws RollerException 168 { 169 Session session = ((HibernateStrategy)mStrategy).getSession(); 170 Criteria criteria = session.createCriteria(RefererData.class); 171 criteria.add(Expression.conjunction() 172 .add(Expression.eq("website",website)) 173 .add(Expression.eq("requestUrl",requestUrl)) 174 .add(Expression.eq("refererUrl",refererUrl))); 175 try 176 { 177 return criteria.list(); 178 } 179 catch (HibernateException e) 180 { 181 throw new RollerException(e); 182 } 183 } 184 185 187 190 public List getDaysPopularWebsites(int max) throws RollerException 191 { 192 String msg = "Getting popular websites"; 195 Session ses = null; Connection con = null; 197 try 198 { 199 List list = new ArrayList (); 200 201 ses = ((HibernateStrategy)mStrategy).getSession(); 202 con = ses.connection(); 203 204 final PreparedStatement stmt; 205 if (con.getMetaData().getDriverName().startsWith("HSQL")) 206 { 207 stmt = con.prepareStatement( 209 "select top ? u.username,w.name,w.name,sum(r.dayhits) as s "+ 210 "from rolleruser as u, website as w, referer as r "+ 211 "where r.websiteid=w.id and w.userid=u.id and w.isenabled=? " + 212 "group by u.username,w.name,w.id order by s desc"); 213 stmt.setInt(1, max); 214 stmt.setBoolean(2, true); 215 } 216 else 217 { 218 stmt = con.prepareStatement( 219 "select u.username,w.name,w.name,sum(r.dayhits) as s "+ 220 "from rolleruser as u, website as w, referer as r "+ 221 "where r.websiteid=w.id and w.userid=u.id and w.isenabled= ? " + 222 "group by u.username,w.name,w.id order by s desc limit ?"); 226 stmt.setBoolean(1, true); 228 stmt.setInt(2, max); 229 } 230 ResultSet rs = stmt.executeQuery(); 231 if ( rs.next() ) 232 { 233 do 234 { 235 String userName = rs.getString(1); 236 String name = rs.getString(2); 237 String websiteName = rs.getString(3); 238 Integer hits = new Integer (rs.getInt(4)); 239 list.add(new WebsiteDisplayData( 240 name, 241 userName, 242 websiteName, 243 hits)); 244 } 245 while ( rs.next() ); 246 } 247 return list; 248 } 249 catch (Throwable pe) 250 { 251 mLogger.error(msg, pe); 252 throw new RollerException(msg, pe); 253 } 254 255 268 } 269 270 272 275 protected int getHits(WebsiteData website, String type) 276 throws RollerException 277 { 278 int hits = 0; 279 if (mLogger.isDebugEnabled()) 280 { 281 mLogger.debug("getHits: " + website.getName()); 282 } 283 284 Object [] args = { Boolean.TRUE, website.getUser().getUserName() }; 286 Type[] types = { Hibernate.BOOLEAN, Hibernate.STRING }; 287 288 Session session = ((HibernateStrategy)mStrategy).getSession(); 290 List results; 291 try 292 { 293 results = session.find( 294 "select sum(h.dayHits),sum(h.totalHits) from h in class " + 295 "org.roller.pojos.RefererData " + 296 "where h.website.isEnabled=? and h.website.user.userName=? ", 297 args, types); 298 } 299 catch (HibernateException e) 300 { 301 throw new RollerException(e); 302 } 303 Object [] resultsArray = (Object []) results.get(0); 304 305 if (resultsArray.length > 0 && type.equals(DAYHITS)) 306 { 307 if ( resultsArray[0] != null ) 308 { 309 hits = ((Integer ) resultsArray[0]).intValue(); 310 } 311 } 312 else if ( resultsArray.length > 0 ) 313 { 314 if ( resultsArray[0] != null ) 315 { 316 hits = ((Integer ) resultsArray[1]).intValue(); 317 } 318 } 319 else 320 { 321 hits = 0; 322 } 323 324 return hits; 325 } 326 327 330 public List getReferers(WebsiteData website) throws RollerException 331 { 332 if (website==null ) 333 throw new RollerException("website is null"); 334 335 Session session = ((HibernateStrategy)mStrategy).getSession(); 336 Criteria criteria = session.createCriteria(RefererData.class); 337 criteria.add(Expression.eq("website",website)); 338 criteria.addOrder(Order.desc("totalHits")); 339 try 340 { 341 return criteria.list(); 342 } 343 catch (HibernateException e) 344 { 345 throw new RollerException(e); 346 } 347 } 348 349 351 354 public List getTodaysReferers(WebsiteData website) 355 throws RollerException 356 { 357 if (website==null ) 358 throw new RollerException("website is null"); 359 360 Session session = ((HibernateStrategy)mStrategy).getSession(); 361 Criteria criteria = session.createCriteria(RefererData.class); 362 criteria.add(Expression.eq("website", website)); 363 criteria.add(Expression.gt("dayHits", new Integer (0))); 364 criteria.addOrder(Order.desc("dayHits")); 365 try 366 { 367 return criteria.list(); 368 } 369 catch (HibernateException e) 370 { 371 throw new RollerException(e); 372 } 373 } 374 375 377 383 public List getReferersToDate(WebsiteData website, String date) 384 throws RollerException 385 { 386 if (website==null ) 387 throw new RollerException("website is null"); 388 389 if (date==null ) 390 throw new RollerException("Date is null"); 391 392 Session session = ((HibernateStrategy)mStrategy).getSession(); 393 Criteria criteria = session.createCriteria(RefererData.class); 394 criteria.add(Expression.eq("website", website)); 395 criteria.add(Expression.eq("dateString", date)); 396 criteria.add(Expression.eq("duplicate", Boolean.FALSE)); 397 criteria.addOrder(Order.desc("totalHits")); 398 try 399 { 400 return criteria.list(); 401 } 402 catch (HibernateException e) 403 { 404 throw new RollerException(e); 405 } 406 } 407 408 410 414 public List getReferersToEntry(String entryid) throws RollerException 415 { 416 if (null == entryid) 417 throw new RollerException("entryid is null"); 418 419 try 420 { 421 Session session = ((HibernateStrategy)mStrategy).getSession(); 422 Criteria criteria = session.createCriteria(RefererData.class); 423 criteria.createAlias("weblogEntry","e"); 424 425 criteria.add(Expression.eq("e.id", entryid)); 426 criteria.add(Expression.isNotNull("title")); 427 criteria.add(Expression.isNotNull("excerpt")); 428 429 criteria.addOrder(Order.desc("totalHits")); 430 431 return criteria.list(); 432 } 433 catch (HibernateException e) 434 { 435 throw new RollerException(e); 436 } 437 } 438 439 441 445 public void removeReferersForEntry(String entryid) throws RollerException 446 { 447 if (null == entryid) 448 throw new RollerException("entryid is null"); 449 450 try 451 { 452 Session session = ((HibernateStrategy)mStrategy).getSession(); 453 Criteria criteria = session.createCriteria(RefererData.class); 454 criteria.createAlias("weblogEntry","e"); 455 criteria.add(Expression.eq("e.id", entryid)); 456 457 Iterator referers = criteria.list().iterator(); 458 while (referers.hasNext()) 459 { 460 removeReferer( ((RefererData)referers.next()).getId() ); 461 } 462 } 463 catch (HibernateException e) 464 { 465 throw new RollerException(e); 466 } 467 } 468 469 471 474 protected List getReferersToWebsite(WebsiteData website, String refererUrl) 475 throws RollerException 476 { 477 Session session = ((HibernateStrategy)mStrategy).getSession(); 478 Criteria criteria = session.createCriteria(RefererData.class); 479 criteria.add(Expression.eq("website", website)); 480 criteria.add(Expression.eq("refererUrl", refererUrl)); 481 try 482 { 483 return criteria.list(); 484 } 485 catch (HibernateException e) 486 { 487 throw new RollerException(e); 488 } 489 } 490 491 493 496 protected List getReferersWithSameTitle( 497 WebsiteData website, 498 String requestUrl, 499 String title, 500 String excerpt) 501 throws RollerException 502 { 503 try 504 { 505 Session session = ((HibernateStrategy)mStrategy).getSession(); 506 Criteria criteria = session.createCriteria(RefererData.class); 507 508 Junction conjunction = Expression.conjunction(); 509 conjunction.add(Expression.eq("website", website)); 510 conjunction.add(Expression.eq("requestUrl", requestUrl)); 511 512 Junction disjunction = Expression.conjunction(); 513 disjunction.add(Expression.eq("title", title)); 514 disjunction.add(Expression.eq("excerpt", excerpt)); 515 516 criteria.add(conjunction); 517 criteria.add(disjunction); 518 519 return criteria.list(); 520 } 521 catch (HibernateException e) 522 { 523 throw new RollerException(e); 524 } 525 } 526 527 531 public void checkForTurnover( boolean forceTurnover, String websiteId ) 532 throws RollerException 533 { 534 if (mLogger.isDebugEnabled()) 538 { 539 mLogger.debug("checkForTurnover"); 540 } 541 542 Date now = new Date (); 543 544 if (forceTurnover || 545 !mDateFormat.format(now).equals(mDateFormat.format(mRefDate))) 546 { 547 try 548 { 549 if (websiteId == null) mRefDate = now; 550 551 List refs; 552 try 553 { 554 Session session = ((HibernateStrategy)mStrategy).getSession(); 555 Criteria criteria = session.createCriteria(RefererData.class); 556 criteria.add(Expression.gt("dayHits", new Integer (0))); 557 if (websiteId != null) 558 { 559 criteria.add(Expression.eq("website.id", websiteId)); 560 } 561 refs = criteria.list(); 562 } 563 catch (HibernateException e1) 564 { 565 throw new RollerException(e1); 566 } 567 568 Integer zero = new Integer (0); 569 for (Iterator rdItr = refs.iterator(); rdItr.hasNext();) { 570 RefererData referer = (RefererData) rdItr.next(); 571 572 if ( (referer.getExcerpt() != null) && 573 (referer.getExcerpt().trim().length() > 0)) 574 { 575 referer.setDayHits(zero); 577 storeReferer(referer); 578 } 579 else 580 { 581 removeReferer(referer.getId()); 583 } 584 } 585 } 586 catch (RollerException e) 587 { 588 mLogger.error("EXCEPTION resetting referers",e); 589 } 590 } 591 } 592 593 594 } 595 | Popular Tags |