1 18 19 20 package org.apache.roller.business.hibernate; 21 22 import org.hibernate.Criteria; 23 import org.hibernate.HibernateException; 24 import org.hibernate.Session; 25 import org.hibernate.criterion.Expression; 26 import org.hibernate.criterion.Order; 27 import org.apache.commons.logging.Log; 28 import org.apache.commons.logging.LogFactory; 29 import org.apache.roller.RollerException; 30 import org.apache.roller.pojos.AutoPingData; 31 import org.apache.roller.pojos.PingQueueEntryData; 32 import java.sql.Timestamp ; 33 import java.util.List ; 34 import org.apache.roller.model.PingQueueManager; 35 36 37 42 public class HibernatePingQueueManagerImpl implements PingQueueManager { 43 44 static final long serialVersionUID = -7660638707453106615L; 45 46 private static Log log = LogFactory.getLog(HibernatePingQueueManagerImpl.class); 47 48 private HibernatePersistenceStrategy strategy = null; 49 50 51 public HibernatePingQueueManagerImpl(HibernatePersistenceStrategy strat) { 52 this.strategy = strat; 53 } 54 55 56 public PingQueueEntryData getQueueEntry(String id) throws RollerException { 57 return (PingQueueEntryData) strategy.load(id, PingQueueEntryData.class); 58 } 59 60 61 public void saveQueueEntry(PingQueueEntryData pingQueueEntry) throws RollerException { 62 log.debug("Storing ping queue entry: " + pingQueueEntry); 63 strategy.store(pingQueueEntry); 64 } 65 66 67 public void removeQueueEntry(PingQueueEntryData pingQueueEntry) throws RollerException { 68 log.debug("Removing ping queue entry: " + pingQueueEntry); 69 strategy.remove(pingQueueEntry); 70 } 71 72 73 public void addQueueEntry(AutoPingData autoPing) throws RollerException { 74 log.debug("Creating new ping queue entry for auto ping configuration: " + autoPing); 75 76 if (isAlreadyQueued(autoPing)) { 78 log.debug("A ping queue entry is already present for this ping target and website: " + autoPing); 79 return; 80 } 81 82 Timestamp now = new Timestamp (System.currentTimeMillis()); 83 PingQueueEntryData pingQueueEntry = 84 new PingQueueEntryData(null, now, autoPing.getPingTarget(), autoPing.getWebsite(), 0); 85 this.saveQueueEntry(pingQueueEntry); 86 } 87 88 89 public List getAllQueueEntries() throws RollerException { 90 try { 91 Session session = ((HibernatePersistenceStrategy) strategy).getSession(); 92 Criteria criteria = session.createCriteria(PingQueueEntryData.class); 93 criteria.addOrder(Order.asc("entryTime")); 94 95 return criteria.list(); 96 } catch (HibernateException e) { 97 throw new RollerException("ERROR retrieving queue entries.", e); 98 } 99 } 100 101 102 private boolean isAlreadyQueued(AutoPingData autoPing) throws RollerException { 104 try { 105 Session session = ((HibernatePersistenceStrategy) strategy).getSession(); 106 Criteria criteria = session.createCriteria(PingQueueEntryData.class); 107 criteria.add(Expression.eq("pingTarget", autoPing.getPingTarget())); 108 criteria.add(Expression.eq("website", autoPing.getWebsite())); 109 return !criteria.list().isEmpty(); 110 } catch (HibernateException e) { 111 throw new RollerException("ERROR determining if preexisting queue entry is present.",e); 112 } 113 } 114 115 116 public void release() {} 117 118 } 119 | Popular Tags |