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.apache.roller.RollerException;
026: import org.apache.roller.pojos.AutoPingData;
027: import org.apache.roller.pojos.PingTargetData;
028: import org.apache.roller.pojos.WeblogEntryData;
029: import org.apache.roller.pojos.WebsiteData;
030: import java.util.Collection;
031: import java.util.Collections;
032: import java.util.Iterator;
033: import java.util.List;
034: import org.apache.commons.logging.Log;
035: import org.apache.commons.logging.LogFactory;
036: import org.apache.roller.config.PingConfig;
037: import org.apache.roller.business.pings.AutoPingManager;
038: import org.apache.roller.business.pings.PingQueueManager;
039: import org.apache.roller.business.RollerFactory;
040:
041: /**
042: * Hibernate implementation of the AutoPingManager.
043: *
044: * @author <a href="mailto:anil@busybuddha.org">Anil Gangolli</a>
045: */
046: public class HibernateAutoPingManagerImpl implements AutoPingManager {
047:
048: static final long serialVersionUID = 5420615676256979199L;
049:
050: private static Log log = LogFactory
051: .getLog(HibernateAutoPingManagerImpl.class);
052:
053: private HibernatePersistenceStrategy strategy = null;
054:
055: public HibernateAutoPingManagerImpl(
056: HibernatePersistenceStrategy strat) {
057: this .strategy = strat;
058: }
059:
060: public AutoPingData getAutoPing(String id) throws RollerException {
061: return (AutoPingData) strategy.load(id, AutoPingData.class);
062: }
063:
064: public void saveAutoPing(AutoPingData autoPing)
065: throws RollerException {
066: strategy.store(autoPing);
067: }
068:
069: public void removeAutoPing(AutoPingData autoPing)
070: throws RollerException {
071: //TODO: first remove all related category restrictions (category restrictions are not yet implemented)
072: strategy.remove(autoPing);
073: }
074:
075: public void removeAutoPing(PingTargetData pingTarget,
076: WebsiteData website) throws RollerException {
077: try {
078: Session session = strategy.getSession();
079: Criteria criteria = session
080: .createCriteria(AutoPingData.class);
081:
082: // Currently category restrictions are not yet implemented, so we
083: // return all auto ping configs for the website.
084: criteria.add(Expression.eq("pingTarget", pingTarget));
085: criteria.add(Expression.eq("website", website));
086: List matches = criteria.list();
087:
088: // This should have at most one element, but we remove them all regardless.
089: this .removeAutoPings(matches);
090: } catch (HibernateException e) {
091: throw new RollerException(e);
092: }
093: }
094:
095: public void removeAutoPings(Collection autopings)
096: throws RollerException {
097:
098: // just go through the list and remove each auto ping
099: Iterator pings = autopings.iterator();
100: while (pings.hasNext()) {
101: this .strategy.remove((AutoPingData) pings.next());
102: }
103: }
104:
105: public void removeAllAutoPings() throws RollerException {
106: try {
107: Session session = ((HibernatePersistenceStrategy) strategy)
108: .getSession();
109: Criteria criteria = session
110: .createCriteria(AutoPingData.class);
111: List allAutoPings = criteria.list();
112: this .removeAutoPings(allAutoPings);
113: } catch (HibernateException e) {
114: throw new RollerException(e);
115: }
116: }
117:
118: public void queueApplicableAutoPings(
119: WeblogEntryData changedWeblogEntry) throws RollerException {
120: if (PingConfig.getSuspendPingProcessing()) {
121: if (log.isDebugEnabled())
122: log
123: .debug("Ping processing is suspended. No auto pings will be queued.");
124: return;
125: }
126:
127: // TODO: new manager method for addQueueEntries(list)?
128: PingQueueManager pingQueueMgr = RollerFactory.getRoller()
129: .getPingQueueManager();
130: List applicableAutopings = getApplicableAutoPings(changedWeblogEntry);
131: for (Iterator i = applicableAutopings.iterator(); i.hasNext();) {
132: AutoPingData autoPing = (AutoPingData) i.next();
133: pingQueueMgr.addQueueEntry(autoPing);
134: }
135: }
136:
137: public List getAutoPingsByWebsite(WebsiteData website)
138: throws RollerException {
139: try {
140: Session session = ((HibernatePersistenceStrategy) strategy)
141: .getSession();
142: Criteria criteria = session
143: .createCriteria(AutoPingData.class);
144: // Currently category restrictions are not yet implemented, so we return all auto ping configs for the
145: // website.
146: criteria.add(Expression.eq("website", website));
147: return criteria.list();
148: } catch (HibernateException e) {
149: throw new RollerException(e);
150: }
151: }
152:
153: public List getAutoPingsByTarget(PingTargetData pingTarget)
154: throws RollerException {
155: try {
156: Session session = ((HibernatePersistenceStrategy) strategy)
157: .getSession();
158: Criteria criteria = session
159: .createCriteria(AutoPingData.class);
160: // Currently category restrictions are not yet implemented, so we return all auto ping configs for the
161: // website.
162: criteria.add(Expression.eq("pingTarget", pingTarget));
163: return criteria.list();
164: } catch (HibernateException e) {
165: throw new RollerException(e);
166: }
167: }
168:
169: public List getApplicableAutoPings(
170: WeblogEntryData changedWeblogEntry) throws RollerException {
171: try {
172: Session session = ((HibernatePersistenceStrategy) strategy)
173: .getSession();
174: Criteria criteria = session
175: .createCriteria(AutoPingData.class);
176: // Currently category restrictions are not yet implemented, so we return all auto ping configs for the
177: // website.
178: criteria.add(Expression.eq("website", changedWeblogEntry
179: .getWebsite()));
180: return criteria.list();
181: } catch (HibernateException e) {
182: throw new RollerException(e);
183: }
184: }
185:
186: public List getCategoryRestrictions(AutoPingData autoPing)
187: throws RollerException {
188: return Collections.EMPTY_LIST;
189: }
190:
191: public void setCategoryRestrictions(AutoPingData autoPing,
192: Collection newCategories) {
193: // NOT YET IMPLEMENTED
194: return;
195: }
196:
197: public void release() {
198: }
199:
200: }
|