KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > roller > pojos > PlanetEntryData


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 package org.apache.roller.pojos;
19
20 import java.io.Serializable JavaDoc;
21 import java.sql.Timestamp JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.List JavaDoc;
25
26 import org.apache.roller.config.RollerRuntimeConfig;
27 import org.apache.roller.util.rome.ContentModule;
28
29 import org.apache.roller.util.Utilities;
30 import org.apache.commons.lang.StringUtils;
31 import com.sun.syndication.feed.module.DCModule;
32 import com.sun.syndication.feed.synd.SyndCategory;
33 import com.sun.syndication.feed.synd.SyndContent;
34 import com.sun.syndication.feed.synd.SyndEntry;
35 import com.sun.syndication.feed.synd.SyndFeed;
36
37 import java.util.Map JavaDoc;
38 import org.apache.commons.lang.StringEscapeUtils;
39 import org.apache.roller.RollerException;
40 import org.apache.roller.model.PluginManager;
41 import org.apache.roller.model.Roller;
42 import org.apache.roller.model.RollerFactory;
43
44 /**
45  * Represents a planet entry, i.e. an entry that was parsed out of an RSS or
46  * Atom newsfeed by Roller's built-in planet aggregator.
47  * <p>
48  * The model coded in this class simple, perhaps too simple, and in the future
49  * it should be replaced by more complete model that can fully represent all
50  * forms of RSS and Atom.
51  *
52  * @hibernate.class lazy="false" table="rag_entry"
53  */

54 public class PlanetEntryData extends PersistentObject
55         implements Serializable JavaDoc, Comparable JavaDoc {
56     protected String JavaDoc id;
57     protected String JavaDoc handle;
58     protected String JavaDoc title;
59     protected String JavaDoc guid;
60     protected String JavaDoc permalink;
61     protected String JavaDoc author;
62     protected String JavaDoc text = "";
63     protected Timestamp JavaDoc published;
64     protected Timestamp JavaDoc updated;
65     
66     private String JavaDoc categoriesString;
67     protected PlanetSubscriptionData subscription = null;
68     
69     /**
70      * Construct empty entry.
71      */

72     public PlanetEntryData() {
73     }
74     
75     /**
76      * Create entry from Rome entry.
77      */

78     public PlanetEntryData(
79             SyndFeed romeFeed, SyndEntry romeEntry, PlanetSubscriptionData sub) {
80         setSubscription(sub);
81         initFromRomeEntry(romeFeed, romeEntry);
82     }
83     
84     /**
85      * Create entry from Rome entry.
86      */

87     public PlanetEntryData(
88             WeblogEntryData rollerEntry,
89             PlanetSubscriptionData sub,
90             Map JavaDoc pagePlugins) throws RollerException {
91         setSubscription(sub);
92         initFromRollerEntry(rollerEntry, pagePlugins);
93     }
94     
95     /**
96      * Init entry from Rome entry
97      */

98     private void initFromRomeEntry(SyndFeed romeFeed, SyndEntry romeEntry) {
99         setTitle(romeEntry.getTitle());
100         setPermalink(romeEntry.getLink());
101         
102         // Play some games to get the author
103
DCModule entrydc = (DCModule)romeEntry.getModule(DCModule.URI);
104         DCModule feeddc = (DCModule)romeFeed.getModule(DCModule.URI);
105         if (romeEntry.getAuthor() != null) {
106             setAuthor(romeEntry.getAuthor());
107         } else {
108             setAuthor(entrydc.getCreator()); // use <dc:creator>
109
}
110         
111         // Play some games to get the date too
112
if (romeEntry.getPublishedDate() != null) {
113             setPubTime(new Timestamp JavaDoc(romeEntry.getPublishedDate().getTime())); // use <pubDate>
114
} else if (entrydc != null) {
115             setPubTime(new Timestamp JavaDoc(entrydc.getDate().getTime())); // use <dc:date>
116
}
117         
118         // get content and unescape if it is 'text/plain'
119
if (romeEntry.getContents().size() > 0) {
120             SyndContent content= (SyndContent)romeEntry.getContents().get(0);
121             if (content != null && content.getType().equals("text/plain")) {
122                 setText(StringEscapeUtils.unescapeHtml(content.getValue()));
123             } else if (content != null) {
124                 setText(content.getValue());
125             }
126         }
127         
128         // no content, then try <content:encoded>
129
if (getText() == null || getText().trim().length() == 0) {
130             ContentModule cm = (ContentModule)romeEntry.getModule(ContentModule.URI);
131             if (cm != null) {
132                 setText(StringEscapeUtils.unescapeHtml(cm.getEncoded()));
133             }
134         }
135         
136         // copy categories
137
if (romeEntry.getCategories().size() > 0) {
138             List JavaDoc list = new ArrayList JavaDoc();
139             Iterator JavaDoc cats = romeEntry.getCategories().iterator();
140             while (cats.hasNext()) {
141                 SyndCategory cat = (SyndCategory)cats.next();
142                 list.add(cat.getName());
143             }
144             setCategoriesString(list);
145         }
146     }
147     
148     /**
149      * Init entry from Roller entry
150      */

151     private void initFromRollerEntry(WeblogEntryData rollerEntry, Map JavaDoc pagePlugins)
152     throws RollerException {
153         Roller roller = RollerFactory.getRoller();
154         PluginManager ppmgr = roller.getPagePluginManager();
155         
156         String JavaDoc content = "";
157         if (!StringUtils.isEmpty(rollerEntry.getText())) {
158             content = rollerEntry.getText();
159         } else {
160             content = rollerEntry.getSummary();
161         }
162         content = ppmgr.applyWeblogEntryPlugins(pagePlugins, rollerEntry, content);
163         
164         setAuthor( rollerEntry.getCreator().getFullName());
165         setTitle( rollerEntry.getTitle());
166         setPermalink( rollerEntry.getLink());
167         setPubTime( rollerEntry.getPubTime());
168         setText( content);
169         
170         setPermalink(RollerRuntimeConfig.getProperty("site.absoluteurl")
171         + rollerEntry.getPermaLink());
172         
173         List JavaDoc cats = new ArrayList JavaDoc();
174         cats.add(rollerEntry.getCategory().getPath());
175         setCategoriesString(cats);
176     }
177     
178     //----------------------------------------------------------- persistent fields
179

180     /**
181      * @hibernate.id column="id"
182      * generator-class="uuid.hex" unsaved-value="null"
183      * @roller.wrapPojoMethod type="simple"
184      */

185     public String JavaDoc getId() {
186         return id;
187     }
188     public void setId(String JavaDoc id) {
189         this.id = id;
190     }
191     /**
192      * @hibernate.property column="categories" non-null="false" unique="false"
193      * @roller.wrapPojoMethod type="simple"
194      */

195     public String JavaDoc getCategoriesString() {
196         return categoriesString;
197     }
198     public void setCategoriesString(String JavaDoc categoriesString) {
199         this.categoriesString = categoriesString;
200     }
201     /**
202      * @hibernate.many-to-one column="subscription_id" cascade="none" not-null="true"
203      */

204     public PlanetSubscriptionData getSubscription() {
205         return subscription;
206     }
207     public void setSubscription(PlanetSubscriptionData subscription) {
208         this.subscription = subscription;
209     }
210     /**
211      * @hibernate.property column="author" non-null="false" unique="false"
212      * @roller.wrapPojoMethod type="simple"
213      */

214     public String JavaDoc getAuthor() {
215         return author;
216     }
217     public void setAuthor(String JavaDoc author) {
218         this.author = author;
219     }
220     /**
221      * @hibernate.property column="content" non-null="false" unique="false"
222      * @roller.wrapPojoMethod type="simple"
223      */

224     public String JavaDoc getText() {
225         return text;
226     }
227     public void setText(String JavaDoc content) {
228         this.text = content;
229     }
230     /**
231      * @hibernate.property column="guid" non-null="false" unique="true"
232      * @roller.wrapPojoMethod type="simple"
233      */

234     public String JavaDoc getGuid() {
235         return guid;
236     }
237     public void setGuid(String JavaDoc guid) {
238         this.guid = guid;
239     }
240     /**
241      * @hibernate.property column="handle" non-null="false" unique="false"
242      * @roller.wrapPojoMethod type="simple"
243      */

244     public String JavaDoc getHandle() {
245         return handle;
246     }
247     public void setHandle(String JavaDoc handle) {
248         this.handle = handle;
249     }
250     /**
251      * @hibernate.property column="published" non-null="true" unique="false"
252      * @roller.wrapPojoMethod type="simple"
253      */

254     public Timestamp JavaDoc getPubTime() {
255         return published;
256     }
257     public void setPubTime(Timestamp JavaDoc published) {
258         this.published = published;
259     }
260     /**
261      * @hibernate.property column="permalink" non-null="true" unique="false"
262      * @roller.wrapPojoMethod type="simple"
263      */

264     public String JavaDoc getPermalink() {
265         return permalink;
266     }
267     public void setPermalink(String JavaDoc permalink) {
268         this.permalink = permalink;
269     }
270     /**
271      * @hibernate.property column="title" non-null="false" unique="false"
272      * @roller.wrapPojoMethod type="simple"
273      */

274     public String JavaDoc getTitle() {
275         return title;
276     }
277     public void setTitle(String JavaDoc title) {
278         this.title = title;
279     }
280     /**
281      * @hibernate.property column="updated" non-null="false" unique="false"
282      * @roller.wrapPojoMethod type="simple"
283      */

284     public Timestamp JavaDoc getUpdateTime() {
285         return updated;
286     }
287     public void setUpdateTime(Timestamp JavaDoc updated) {
288         this.updated = updated;
289     }
290     
291     //----------------------------------------------------------------- convenience
292

293     /**
294      * Returns true if any of entry's categories contain a specific string
295      * (case-insensitive comparison).
296      */

297     public boolean inCategory(String JavaDoc category) {
298         Iterator JavaDoc cats = getCategories().iterator();
299         while (cats.hasNext()) {
300             String JavaDoc catName = ((String JavaDoc)cats.next()).toLowerCase();
301             if (catName.indexOf(category.toLowerCase()) != -1) {
302                 return true;
303             }
304         }
305         return false;
306     }
307     
308     //------------------------------------------------------------- implementation
309

310     /**
311      * Returns categories as list of WeblogCategoryData objects.
312      */

313     public List JavaDoc getCategories() {
314         List JavaDoc list = new ArrayList JavaDoc();
315         if (categoriesString != null) {
316             String JavaDoc[] catArray = Utilities.stringToStringArray(categoriesString,",");
317             for (int i=0; i<catArray.length; i++) {
318                 WeblogCategoryData cat = new WeblogCategoryData();
319                 cat.setName(catArray[i]);
320                 cat.setPath(catArray[i]);
321                 list.add(cat);
322             }
323         }
324         return list;
325     }
326        
327     /**
328      * Return first entry in category collection.
329      * @roller.wrapPojoMethod type="pojo"
330      */

331     public WeblogCategoryData getCategory() {
332         WeblogCategoryData cat = null;
333         List JavaDoc cats = getCategories();
334         if (cats.size() > 0) {
335             cat = (WeblogCategoryData)cats.get(0);
336         }
337         return cat;
338     }
339
340     private void setCategoriesString(List JavaDoc categories) {
341         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
342         Iterator JavaDoc cats = categories.iterator();
343         while (cats.hasNext()) {
344             String JavaDoc cat = (String JavaDoc)cats.next();
345             sb.append(cat);
346             if (cats.hasNext()) sb.append(",");
347         }
348         categoriesString = sb.toString();
349     }
350     
351     /**
352      * Returns creator as a UserData object.
353      * @roller.wrapPojoMethod type="pojo"
354      * TODO: make planet model entry author name, email, and uri
355      */

356     public UserData getCreator() {
357         UserData user = null;
358         if (author != null) {
359             user = new UserData();
360             user.setFullName(author);
361             user.setUserName(author);
362         }
363         return user;
364     }
365     
366     /**
367      * Returns summary (always null for planet entry)
368      * @roller.wrapPojoMethod type="simple"
369      */

370     public String JavaDoc getSummary() {
371         return null;
372     }
373     
374     /**
375      * Compare planet entries by comparing permalinks.
376      */

377     public int compareTo(Object JavaDoc o) {
378         PlanetEntryData other = (PlanetEntryData)o;
379         return getPermalink().compareTo(other.getPermalink());
380     }
381     
382     /**
383      * Compare planet entries by comparing permalinks.
384      */

385     public boolean equals(Object JavaDoc other) {
386         if(this == other) return true;
387         if(!(other instanceof PlanetEntryData)) return false;
388         final PlanetEntryData that = (PlanetEntryData) other;
389         return this.permalink.equals(that.getPermalink());
390     }
391     
392     /**
393      * Generate hash code based on permalink.
394      */

395     public int hashCode() {
396         return this.permalink.hashCode();
397     }
398     
399     public void setData(PersistentObject vo) {}
400
401     /**
402      * Read-only synomym for getSubscription()
403      * @roller.wrapPojoMethod type="pojo"
404      */

405     public PlanetSubscriptionData getWebsite() {
406         return this.subscription;
407     }
408     public void setWebsite() {
409         // noop
410
}
411 }
412
413
414
415
Popular Tags