KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > roller > ui > core > tasks > SyncWebsitesTask


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.ui.core.tasks;
20
21 import java.util.ArrayList JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.TimerTask JavaDoc;
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.apache.roller.RollerException;
28 import org.apache.roller.config.RollerRuntimeConfig;
29 import org.apache.roller.model.PlanetManager;
30 import org.apache.roller.model.Roller;
31 import org.apache.roller.model.RollerFactory;
32 import org.apache.roller.model.ScheduledTask;
33 import org.apache.roller.model.UserManager;
34 import org.apache.roller.pojos.PlanetGroupData;
35 import org.apache.roller.pojos.PlanetSubscriptionData;
36 import org.apache.roller.pojos.WebsiteData;
37 import org.apache.roller.util.URLUtilities;
38
39
40 /**
41  * Ensure that every weblog has a subscription in Planet Roller database.
42  */

43 public class SyncWebsitesTask extends TimerTask JavaDoc implements ScheduledTask {
44     
45     private static Log log = LogFactory.getLog(SyncWebsitesTask.class);
46     
47     
48     /**
49      * Task may be run from the command line
50      */

51     public static void main(String JavaDoc[] args) {
52         try {
53             RollerFactory.setRoller(
54                     "org.apache.roller.business.hibernate.HibernateRollerImpl");
55             SyncWebsitesTask task = new SyncWebsitesTask();
56             task.init(RollerFactory.getRoller(), "dummy");
57             task.run();
58             System.exit(0);
59         } catch (Throwable JavaDoc t) {
60             t.printStackTrace();
61             System.exit(-1);
62         }
63     }
64     
65     
66     public void init(Roller roller, String JavaDoc realPath) throws RollerException {
67         // no-op
68
}
69     
70     
71     public void run() {
72         syncWebsites();
73     }
74     
75     
76     /**
77      * Ensure there's a subscription in the "all" group for every Roller weblog.
78      */

79     private void syncWebsites() {
80         
81         // make sure we have an absolute url value
82
String JavaDoc absUrl = RollerRuntimeConfig.getProperty("site.absoluteurl");
83         if(absUrl == null || absUrl.trim().length() == 0) {
84             log.error("ERROR: cannot sync websites with Planet Roller - "
85                     +"absolute URL not specified in Roller Config");
86             return;
87         }
88         
89         try {
90             PlanetManager planet = RollerFactory.getRoller().getPlanetManager();
91             UserManager userManager = RollerFactory.getRoller().getUserManager();
92             
93             // first, make sure there is an "all" planet group
94
PlanetGroupData group = planet.getGroup("all");
95             if(group == null) {
96                 group = new PlanetGroupData();
97                 group.setHandle("all");
98                 group.setTitle("all");
99                 planet.saveGroup(group);
100             }
101             
102             // walk through all enable weblogs and add/update subs as needed
103
List JavaDoc liveUserFeeds = new ArrayList JavaDoc();
104             Iterator JavaDoc websites =
105                     userManager.getWebsites(null, Boolean.TRUE, Boolean.TRUE, null, null, 0, -1).iterator();
106             while(websites.hasNext()) {
107                 WebsiteData weblog = (WebsiteData) websites.next();
108                 
109                 String JavaDoc siteUrl = URLUtilities.getWeblogURL(weblog, null, true);
110                 String JavaDoc feedUrl = URLUtilities.getWeblogFeedURL(weblog, null, "entries", "rss", null, false, true);
111                 
112                 // add feed url to the "live" list
113
liveUserFeeds.add(feedUrl);
114                 
115                 // if sub already exists then update it, otherwise add it
116
PlanetSubscriptionData sub = planet.getSubscription(feedUrl);
117                 if (sub == null) {
118                     log.info("ADDING feed: "+feedUrl);
119                     
120                     sub = new PlanetSubscriptionData();
121                     sub.setTitle(weblog.getName());
122                     sub.setFeedURL(feedUrl);
123                     sub.setSiteURL(siteUrl);
124                     sub.setAuthor(weblog.getHandle());
125                     
126                     planet.saveSubscription(sub);
127                     group.addSubscription(sub);
128                 } else {
129                     sub.setTitle(weblog.getName());
130                     sub.setAuthor(weblog.getHandle());
131                     
132                     planet.saveSubscription(sub);
133                 }
134             }
135             
136             // new subs added, existing subs updated, now delete old subs
137
Iterator JavaDoc subs = group.getSubscriptions().iterator();
138             while(subs.hasNext()) {
139                 PlanetSubscriptionData sub =
140                         (PlanetSubscriptionData) subs.next();
141                 if (!liveUserFeeds.contains(sub.getFeedURL())) {
142                     log.info("DELETING feed: "+sub.getFeedURL());
143                     planet.deleteSubscription(sub);
144                     group.removeSubscription(sub);
145                 }
146             }
147             
148             // all done, lets save
149
planet.saveGroup(group);
150             RollerFactory.getRoller().flush();
151             
152         } catch (RollerException e) {
153             log.error("ERROR refreshing entries", e);
154         } finally {
155             // don't forget to release
156
RollerFactory.getRoller().release();
157         }
158     }
159     
160 }
161
Popular Tags