001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. The ASF licenses this file to You
004: * under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License. For additional information regarding
015: * copyright in this work, please see the NOTICE file in the top level
016: * directory of this distribution.
017: */
018:
019: package org.apache.roller.business.hibernate;
020:
021: import org.hibernate.Criteria;
022: import org.hibernate.HibernateException;
023: import org.hibernate.Session;
024: import org.hibernate.criterion.Expression;
025: import org.hibernate.criterion.Order;
026: import org.apache.commons.logging.Log;
027: import org.apache.commons.logging.LogFactory;
028: import org.apache.roller.RollerException;
029: import org.apache.roller.pojos.AutoPingData;
030: import org.apache.roller.pojos.PingQueueEntryData;
031: import java.sql.Timestamp;
032: import java.util.List;
033: import org.apache.roller.business.pings.PingQueueManager;
034:
035: /**
036: * Hibernate implementation of the PingQueueManager.
037: *
038: * @author <a href="mailto:anil@busybuddha.org">Anil Gangolli</a>
039: */
040: public class HibernatePingQueueManagerImpl implements PingQueueManager {
041:
042: static final long serialVersionUID = -7660638707453106615L;
043:
044: private static Log log = LogFactory
045: .getLog(HibernatePingQueueManagerImpl.class);
046:
047: private HibernatePersistenceStrategy strategy = null;
048:
049: public HibernatePingQueueManagerImpl(
050: HibernatePersistenceStrategy strat) {
051: this .strategy = strat;
052: }
053:
054: public PingQueueEntryData getQueueEntry(String id)
055: throws RollerException {
056: return (PingQueueEntryData) strategy.load(id,
057: PingQueueEntryData.class);
058: }
059:
060: public void saveQueueEntry(PingQueueEntryData pingQueueEntry)
061: throws RollerException {
062: log.debug("Storing ping queue entry: " + pingQueueEntry);
063: strategy.store(pingQueueEntry);
064: }
065:
066: public void removeQueueEntry(PingQueueEntryData pingQueueEntry)
067: throws RollerException {
068: log.debug("Removing ping queue entry: " + pingQueueEntry);
069: strategy.remove(pingQueueEntry);
070: }
071:
072: public void addQueueEntry(AutoPingData autoPing)
073: throws RollerException {
074: log
075: .debug("Creating new ping queue entry for auto ping configuration: "
076: + autoPing);
077:
078: // First check if there is an existing ping queue entry for the same target and website
079: if (isAlreadyQueued(autoPing)) {
080: log
081: .debug("A ping queue entry is already present for this ping target and website: "
082: + autoPing);
083: return;
084: }
085:
086: Timestamp now = new Timestamp(System.currentTimeMillis());
087: PingQueueEntryData pingQueueEntry = new PingQueueEntryData(
088: null, now, autoPing.getPingTarget(), autoPing
089: .getWebsite(), 0);
090: this .saveQueueEntry(pingQueueEntry);
091: }
092:
093: public List getAllQueueEntries() throws RollerException {
094: try {
095: Session session = ((HibernatePersistenceStrategy) strategy)
096: .getSession();
097: Criteria criteria = session
098: .createCriteria(PingQueueEntryData.class);
099: criteria.addOrder(Order.asc("entryTime"));
100:
101: return criteria.list();
102: } catch (HibernateException e) {
103: throw new RollerException(
104: "ERROR retrieving queue entries.", e);
105: }
106: }
107:
108: // private helper to determine if an has already been queued for the same website and ping target.
109: private boolean isAlreadyQueued(AutoPingData autoPing)
110: throws RollerException {
111: try {
112: Session session = ((HibernatePersistenceStrategy) strategy)
113: .getSession();
114: Criteria criteria = session
115: .createCriteria(PingQueueEntryData.class);
116: criteria.add(Expression.eq("pingTarget", autoPing
117: .getPingTarget()));
118: criteria.add(Expression
119: .eq("website", autoPing.getWebsite()));
120: return !criteria.list().isEmpty();
121: } catch (HibernateException e) {
122: throw new RollerException(
123: "ERROR determining if preexisting queue entry is present.",
124: e);
125: }
126: }
127:
128: public void release() {
129: }
130:
131: }
|