1 8 9 package org.roller.business.hibernate; 10 11 import net.sf.hibernate.Criteria; 12 import net.sf.hibernate.HibernateException; 13 import net.sf.hibernate.Session; 14 import net.sf.hibernate.expression.Expression; 15 import net.sf.hibernate.expression.Order; 16 import org.apache.commons.logging.Log; 17 import org.apache.commons.logging.LogFactory; 18 import org.roller.RollerException; 19 import org.roller.business.PersistenceStrategy; 20 import org.roller.business.PingQueueManagerImpl; 21 import org.roller.pojos.AutoPingData; 22 import org.roller.pojos.PingQueueEntryData; 23 import org.roller.pojos.WebsiteData; 24 import org.roller.pojos.PingTargetData; 25 26 import java.sql.Timestamp ; 27 import java.util.Iterator ; 28 import java.util.List ; 29 import java.util.Collection ; 30 31 public class HibernatePingQueueManagerImpl extends PingQueueManagerImpl 32 { 33 static final long serialVersionUID = -7660638707453106615L; 34 35 private static Log logger = LogFactory.getLog(HibernatePingQueueManagerImpl.class); 36 37 public HibernatePingQueueManagerImpl(PersistenceStrategy persistenceStrategy) 38 { 39 super(persistenceStrategy); 40 } 41 42 public void addQueueEntry(AutoPingData autoPing) throws RollerException 43 { 44 if (logger.isDebugEnabled()) logger.debug("Creating new ping queue entry for auto ping configuration: " + autoPing); 45 46 if (isAlreadyQueued(autoPing)) 48 { 49 if (logger.isDebugEnabled()) logger.debug("A ping queue entry is already present for this ping target and website: " + autoPing); 50 return; 51 } 52 53 Timestamp now = new Timestamp (System.currentTimeMillis()); 54 PingQueueEntryData pingQueueEntry = 55 new PingQueueEntryData(null, now, autoPing.getPingTarget(), autoPing.getWebsite(), 0); 56 storeQueueEntry(pingQueueEntry); 57 } 58 59 public void dropQueue() throws RollerException 60 { 61 logger.info("NOTICE Dropping all ping queue entries."); 62 List queueEntries = getAllQueueEntries(); 63 removeEntries(queueEntries); 64 } 65 66 public List getAllQueueEntries() throws RollerException 67 { 68 Session session = ((HibernateStrategy) persistenceStrategy).getSession(); 69 Criteria criteria = session.createCriteria(PingQueueEntryData.class); 70 criteria.addOrder(Order.asc("entryTime")); 71 try 72 { 73 return criteria.list(); 74 } 75 catch (HibernateException e) 76 { 77 throw new RollerException("ERROR retrieving queue entries.", e); 78 } 79 } 80 81 public void removeQueueEntriesByPingTarget(PingTargetData pingTarget) throws RollerException { 82 try 83 { 84 if (logger.isDebugEnabled()) logger.debug("Removing all ping queue entries for ping target " + pingTarget); 85 Session session = ((HibernateStrategy) persistenceStrategy).getSession(); 86 Criteria criteria = session.createCriteria(PingQueueEntryData.class); 87 criteria.add(Expression.eq("pingTarget", pingTarget)); 88 List queueEntries = criteria.list(); 89 removeEntries(queueEntries); 90 } 91 catch (HibernateException e) 92 { 93 throw new RollerException("ERROR removing queue entries for ping target " + pingTarget, e); 94 } 95 } 96 97 public void removeQueueEntriesByWebsite(WebsiteData website) throws RollerException { 98 try 99 { 100 if (logger.isDebugEnabled()) logger.debug("Removing all ping queue entries for website " + website); 101 Session session = ((HibernateStrategy) persistenceStrategy).getSession(); 102 Criteria criteria = session.createCriteria(PingQueueEntryData.class); 103 criteria.add(Expression.eq("website", website)); 104 List queueEntries = criteria.list(); 105 removeEntries(queueEntries); 106 } 107 catch (HibernateException e) 108 { 109 throw new RollerException("ERROR removing queue entries for website " + website, e); 110 } 111 } 112 113 private boolean isAlreadyQueued(AutoPingData autoPing) throws RollerException 115 { 116 try 117 { 118 Session session = ((HibernateStrategy) persistenceStrategy).getSession(); 119 Criteria criteria = session.createCriteria(PingQueueEntryData.class); 120 criteria.add(Expression.eq("pingTarget", autoPing.getPingTarget())); 121 criteria.add(Expression.eq("website", autoPing.getWebsite())); 122 return !criteria.list().isEmpty(); 123 } 124 catch (HibernateException e) 125 { 126 throw new RollerException("ERROR determining if preexisting queue entry is present.",e); 127 } 128 } 129 130 private void removeEntries(Collection queueEntries) throws RollerException { 132 for (Iterator i = queueEntries.iterator(); i.hasNext();) 133 { 134 PingQueueEntryData pqe = (PingQueueEntryData) i.next(); 135 pqe.remove(); 136 } 137 } 138 } 139 | Popular Tags |