KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > roller > business > hibernate > HibernatePingQueueManagerImpl


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. The ASF licenses this file to You
4  * under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License. For additional information regarding
15  * copyright in this work, please see the NOTICE file in the top level
16  * directory of this distribution.
17  */

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 JavaDoc;
33 import java.util.List JavaDoc;
34 import org.apache.roller.model.PingQueueManager;
35
36
37 /**
38  * Hibernate implementation of the PingQueueManager.
39  *
40  * @author <a HREF="mailto:anil@busybuddha.org">Anil Gangolli</a>
41  */

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 JavaDoc 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         // First check if there is an existing ping queue entry for the same target and website
77
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 JavaDoc now = new Timestamp JavaDoc(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 JavaDoc 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 helper to determine if an has already been queued for the same website and ping target.
103
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