KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.apache.roller.business.hibernate;
20
21 import org.hibernate.Criteria;
22 import org.hibernate.HibernateException;
23 import org.hibernate.Session;
24 import org.hibernate.criterion.Expression;
25 import org.apache.roller.RollerException;
26 import org.apache.roller.pojos.AutoPingData;
27 import org.apache.roller.pojos.PingTargetData;
28 import org.apache.roller.pojos.WeblogEntryData;
29 import org.apache.roller.pojos.WebsiteData;
30 import java.util.Collection JavaDoc;
31 import java.util.Collections JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import java.util.List JavaDoc;
34 import org.apache.commons.logging.Log;
35 import org.apache.commons.logging.LogFactory;
36 import org.apache.roller.config.PingConfig;
37 import org.apache.roller.model.AutoPingManager;
38 import org.apache.roller.model.PingQueueManager;
39 import org.apache.roller.model.RollerFactory;
40
41
42 /**
43  * Hibernate implementation of the AutoPingManager.
44  *
45  * @author <a HREF="mailto:anil@busybuddha.org">Anil Gangolli</a>
46  */

47 public class HibernateAutoPingManagerImpl implements AutoPingManager {
48     
49     static final long serialVersionUID = 5420615676256979199L;
50     
51     private static Log log = LogFactory.getLog(HibernateAutoPingManagerImpl.class);
52     
53     private HibernatePersistenceStrategy strategy = null;
54     
55     
56     public HibernateAutoPingManagerImpl(HibernatePersistenceStrategy strat) {
57         this.strategy = strat;
58     }
59     
60     
61     public AutoPingData getAutoPing(String JavaDoc id) throws RollerException {
62         return (AutoPingData) strategy.load(id, AutoPingData.class);
63     }
64     
65     
66     public void saveAutoPing(AutoPingData autoPing) throws RollerException {
67         strategy.store(autoPing);
68     }
69     
70     
71     public void removeAutoPing(AutoPingData autoPing) throws RollerException {
72         //TODO: first remove all related category restrictions (category restrictions are not yet implemented)
73
strategy.remove(autoPing);
74     }
75     
76     
77     public void removeAutoPing(PingTargetData pingTarget, WebsiteData website) throws RollerException {
78         try {
79             Session session = strategy.getSession();
80             Criteria criteria = session.createCriteria(AutoPingData.class);
81             
82             // Currently category restrictions are not yet implemented, so we
83
// return all auto ping configs for the website.
84
criteria.add(Expression.eq("pingTarget", pingTarget));
85             criteria.add(Expression.eq("website", website));
86             List JavaDoc matches = criteria.list();
87             
88             // This should have at most one element, but we remove them all regardless.
89
this.removeAutoPings(matches);
90         } catch (HibernateException e) {
91             throw new RollerException(e);
92         }
93     }
94     
95     
96     public void removeAutoPings(Collection JavaDoc autopings) throws RollerException {
97         
98         // just go through the list and remove each auto ping
99
Iterator JavaDoc pings = autopings.iterator();
100         while (pings.hasNext()) {
101             this.strategy.remove((AutoPingData) pings.next());
102         }
103     }
104     
105     
106     public void removeAllAutoPings() throws RollerException {
107         try {
108             Session session = ((HibernatePersistenceStrategy) strategy).getSession();
109             Criteria criteria = session.createCriteria(AutoPingData.class);
110             List JavaDoc allAutoPings = criteria.list();
111             this.removeAutoPings(allAutoPings);
112         } catch (HibernateException e) {
113             throw new RollerException(e);
114         }
115     }
116     
117     
118     public void queueApplicableAutoPings(WeblogEntryData changedWeblogEntry) throws RollerException {
119         if (PingConfig.getSuspendPingProcessing()) {
120             if (log.isDebugEnabled()) log.debug("Ping processing is suspended. No auto pings will be queued.");
121             return;
122         }
123         
124         // TODO: new manager method for addQueueEntries(list)?
125
PingQueueManager pingQueueMgr = RollerFactory.getRoller().getPingQueueManager();
126         List JavaDoc applicableAutopings = getApplicableAutoPings(changedWeblogEntry);
127         for (Iterator JavaDoc i = applicableAutopings.iterator(); i.hasNext(); ) {
128             AutoPingData autoPing = (AutoPingData) i.next();
129             pingQueueMgr.addQueueEntry(autoPing);
130         }
131     }
132     
133     
134     public List JavaDoc getAutoPingsByWebsite(WebsiteData website) throws RollerException {
135         try {
136             Session session = ((HibernatePersistenceStrategy) strategy).getSession();
137             Criteria criteria = session.createCriteria(AutoPingData.class);
138             // Currently category restrictions are not yet implemented, so we return all auto ping configs for the
139
// website.
140
criteria.add(Expression.eq("website", website));
141             return criteria.list();
142         } catch (HibernateException e) {
143             throw new RollerException(e);
144         }
145     }
146     
147     
148     public List JavaDoc getAutoPingsByTarget(PingTargetData pingTarget) throws RollerException {
149         try {
150             Session session = ((HibernatePersistenceStrategy) strategy).getSession();
151             Criteria criteria = session.createCriteria(AutoPingData.class);
152             // Currently category restrictions are not yet implemented, so we return all auto ping configs for the
153
// website.
154
criteria.add(Expression.eq("pingTarget", pingTarget));
155             return criteria.list();
156         } catch (HibernateException e) {
157             throw new RollerException(e);
158         }
159     }
160     
161     
162     public List JavaDoc getApplicableAutoPings(WeblogEntryData changedWeblogEntry) throws RollerException {
163         try {
164             Session session = ((HibernatePersistenceStrategy) strategy).getSession();
165             Criteria criteria = session.createCriteria(AutoPingData.class);
166             // Currently category restrictions are not yet implemented, so we return all auto ping configs for the
167
// website.
168
criteria.add(Expression.eq("website", changedWeblogEntry.getWebsite()));
169             return criteria.list();
170         } catch (HibernateException e) {
171             throw new RollerException(e);
172         }
173     }
174     
175     
176     public List JavaDoc getCategoryRestrictions(AutoPingData autoPing) throws RollerException {
177         return Collections.EMPTY_LIST;
178     }
179     
180     
181     public void setCategoryRestrictions(AutoPingData autoPing, Collection JavaDoc newCategories) {
182         // NOT YET IMPLEMENTED
183
return;
184     }
185     
186     
187     public void release() {}
188     
189 }
190
Popular Tags