| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
package net.sourceforge.pebble.event.blog; |
| 33 | |
|
| 34 | |
import net.sourceforge.pebble.domain.*; |
| 35 | |
import net.sourceforge.pebble.webservice.PebbleAPIHandler; |
| 36 | |
import net.sourceforge.pebble.PluginProperties; |
| 37 | |
import net.sourceforge.pebble.api.event.blog.BlogEvent; |
| 38 | |
import net.sourceforge.pebble.api.event.blog.BlogListener; |
| 39 | |
import org.apache.commons.logging.Log; |
| 40 | |
import org.apache.commons.logging.LogFactory; |
| 41 | |
import org.apache.xmlrpc.XmlRpcClient; |
| 42 | |
|
| 43 | |
import java.util.*; |
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | 0 | public abstract class PebbleAPIBlogEntryAggregator extends TimerTask implements BlogListener { |
| 53 | |
|
| 54 | |
private static final int ONE_HOUR = 1000 * 60 * 60; |
| 55 | 0 | private static final Log log = LogFactory.getLog(PebbleAPIBlogEntryAggregator.class); |
| 56 | |
|
| 57 | |
public static final String XMLRPC_URL_KEY = ".xmlrpcUrl"; |
| 58 | |
public static final String BLOG_KEY = ".blog"; |
| 59 | |
public static final String USERNAME_KEY = ".username"; |
| 60 | |
public static final String PASSWORD_KEY = ".password"; |
| 61 | |
public static final String BLOG_ENTRIES_KEY = ".blogEntries"; |
| 62 | |
|
| 63 | |
private Blog blog; |
| 64 | 0 | private Timer timer = new Timer(); |
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
public void blogStarted(BlogEvent event) { |
| 72 | 0 | this.blog = event.getBlog(); |
| 73 | 0 | timer.schedule(this, 0, ONE_HOUR); |
| 74 | 0 | } |
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
public void blogStopped(BlogEvent event) { |
| 82 | 0 | timer.cancel(); |
| 83 | 0 | } |
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 88 | |
public void run() { |
| 89 | |
try { |
| 90 | 0 | PluginProperties props = blog.getPluginProperties(); |
| 91 | 0 | XmlRpcClient xmlrpc = new XmlRpcClient(props.getProperty(getPropertyPrefix() + XMLRPC_URL_KEY)); |
| 92 | 0 | Vector<Object> params = new Vector<Object>(); |
| 93 | 0 | params.add(props.getProperty(getPropertyPrefix() + BLOG_KEY)); |
| 94 | 0 | params.add(props.getProperty(getPropertyPrefix() + USERNAME_KEY)); |
| 95 | 0 | params.add(props.getProperty(getPropertyPrefix() + PASSWORD_KEY)); |
| 96 | |
|
| 97 | 0 | int numberOfBlogEntries = 10; |
| 98 | |
try { |
| 99 | 0 | numberOfBlogEntries = Integer.parseInt(props.getProperty(getPropertyPrefix() + BLOG_ENTRIES_KEY)); |
| 100 | 0 | } catch (NumberFormatException nfe) { |
| 101 | |
|
| 102 | 0 | } |
| 103 | 0 | params.add(numberOfBlogEntries); |
| 104 | |
|
| 105 | |
|
| 106 | 0 | Vector<Hashtable> remoteBlogEntries = (Vector<Hashtable>)xmlrpc.execute("pebble.getRecentBlogEntries", params); |
| 107 | |
|
| 108 | |
|
| 109 | 0 | for (Hashtable remoteBlogEntry : remoteBlogEntries) { |
| 110 | 0 | String id = (String)remoteBlogEntry.get(PebbleAPIHandler.ID); |
| 111 | 0 | String title = (String)remoteBlogEntry.get(PebbleAPIHandler.TITLE); |
| 112 | 0 | String subtitle = (String)remoteBlogEntry.get(PebbleAPIHandler.SUBTITLE); |
| 113 | 0 | String excerpt = (String)remoteBlogEntry.get(PebbleAPIHandler.EXCERPT); |
| 114 | 0 | String body = (String)remoteBlogEntry.get(PebbleAPIHandler.BODY); |
| 115 | 0 | Date date = (Date)remoteBlogEntry.get(PebbleAPIHandler.DATE); |
| 116 | 0 | String author = (String)remoteBlogEntry.get(PebbleAPIHandler.AUTHOR); |
| 117 | 0 | String permalink = (String)remoteBlogEntry.get(PebbleAPIHandler.PERMALINK); |
| 118 | 0 | Vector<String> categories = (Vector<String>)remoteBlogEntry.get(PebbleAPIHandler.CATEGORIES); |
| 119 | 0 | Vector<String> tags = (Vector<String>)remoteBlogEntry.get(PebbleAPIHandler.TAGS); |
| 120 | 0 | Hashtable attachment = (Hashtable)remoteBlogEntry.get(PebbleAPIHandler.ATTACHMENT); |
| 121 | |
|
| 122 | 0 | if (!preAggregate(remoteBlogEntry)) { |
| 123 | 0 | continue; |
| 124 | |
} |
| 125 | |
|
| 126 | 0 | log.info("Aggregating " + title + " [ " + id + " | " + permalink + " ]"); |
| 127 | |
|
| 128 | 0 | BlogService service = new BlogService(); |
| 129 | 0 | BlogEntry blogEntry = service.getBlogEntry(blog, id); |
| 130 | 0 | if (blogEntry == null) { |
| 131 | |
|
| 132 | 0 | blogEntry = new BlogEntry(blog); |
| 133 | 0 | blogEntry.setDate(new Date(Long.parseLong(id))); |
| 134 | 0 | service.putBlogEntry(blogEntry); |
| 135 | |
} |
| 136 | |
|
| 137 | |
|
| 138 | 0 | blogEntry.setTitle(title); |
| 139 | 0 | blogEntry.setSubtitle(subtitle); |
| 140 | 0 | blogEntry.setBody(body); |
| 141 | 0 | blogEntry.setExcerpt(excerpt); |
| 142 | 0 | blogEntry.setAuthor(author); |
| 143 | 0 | blogEntry.setOriginalPermalink(permalink); |
| 144 | 0 | blogEntry.setCommentsEnabled(false); |
| 145 | 0 | blogEntry.setTrackBacksEnabled(false); |
| 146 | |
|
| 147 | 0 | if (categories != null) { |
| 148 | 0 | for (String categoryId : categories) { |
| 149 | 0 | blogEntry.addCategory(blog.getCategory(categoryId)); |
| 150 | |
} |
| 151 | |
} |
| 152 | |
|
| 153 | 0 | if (tags != null) { |
| 154 | 0 | StringBuffer buf = new StringBuffer(); |
| 155 | 0 | Iterator it = tags.iterator(); |
| 156 | 0 | while (it.hasNext()) { |
| 157 | 0 | String tag = (String)it.next(); |
| 158 | 0 | buf.append(tag); |
| 159 | 0 | if (it.hasNext()) { |
| 160 | 0 | buf.append(" "); |
| 161 | |
} |
| 162 | 0 | } |
| 163 | 0 | blogEntry.setTags(buf.toString()); |
| 164 | |
} |
| 165 | |
|
| 166 | 0 | if (attachment != null) { |
| 167 | 0 | Attachment a = new Attachment(); |
| 168 | 0 | a.setUrl((String)attachment.get(PebbleAPIHandler.ATTACHMENT_URL)); |
| 169 | 0 | a.setSize((Long)attachment.get(PebbleAPIHandler.ATTACHMENT_SIZE)); |
| 170 | 0 | a.setType((String)attachment.get(PebbleAPIHandler.ATTACHMENT_TYPE)); |
| 171 | 0 | blogEntry.setAttachment(a); |
| 172 | |
} |
| 173 | |
|
| 174 | 0 | postAggregate(blogEntry); |
| 175 | |
|
| 176 | 0 | service.putBlogEntry(blogEntry); |
| 177 | 0 | } |
| 178 | 0 | } catch (Exception e) { |
| 179 | 0 | log.error("Exception encountered", e); |
| 180 | 0 | } |
| 181 | 0 | } |
| 182 | |
|
| 183 | |
|
| 184 | |
|
| 185 | |
|
| 186 | |
|
| 187 | |
|
| 188 | |
|
| 189 | |
|
| 190 | |
protected abstract boolean preAggregate(Hashtable blogEntry); |
| 191 | |
|
| 192 | |
|
| 193 | |
|
| 194 | |
|
| 195 | |
|
| 196 | |
|
| 197 | |
|
| 198 | |
protected abstract void postAggregate(BlogEntry blogEntry); |
| 199 | |
|
| 200 | |
|
| 201 | |
|
| 202 | |
|
| 203 | |
|
| 204 | |
|
| 205 | |
protected String getPropertyPrefix() { |
| 206 | 0 | return this.getClass().getSimpleName(); |
| 207 | |
} |
| 208 | |
|
| 209 | |
} |