KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 2005 Sun Microsystems, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not 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.
15  */

16 package org.roller.pojos;
17
18 import java.io.Serializable JavaDoc;
19 import java.util.ArrayList JavaDoc;
20 import java.util.Date JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.List JavaDoc;
23
24 import org.roller.util.Utilities;
25 import org.roller.util.rome.ContentModule;
26
27 import com.sun.syndication.feed.module.DCModule;
28 import com.sun.syndication.feed.synd.SyndCategory;
29 import com.sun.syndication.feed.synd.SyndContent;
30 import com.sun.syndication.feed.synd.SyndEntry;
31 import com.sun.syndication.feed.synd.SyndFeed;
32
33 /**
34  * A syndication feed entry intended for use in an in-memory or database cache to
35  * speed the creation of aggregations in Planet Roller implementations.
36  *
37  * @hibernate.class table="rag_entry"
38  * @author Dave Johnson
39  */

40 public class PlanetEntryData extends PersistentObject
41                              implements Serializable JavaDoc, Comparable JavaDoc
42 {
43     protected String JavaDoc id;
44     protected String JavaDoc handle;
45     protected String JavaDoc title;
46     protected String JavaDoc guid;
47     protected String JavaDoc permalink;
48     protected String JavaDoc author;
49     protected String JavaDoc content = "";
50     protected Date JavaDoc published;
51     protected Date JavaDoc updated;
52     
53     private String JavaDoc categoriesString;
54     protected PlanetSubscriptionData subscription = null;
55     
56     /**
57      * Construct empty entry.
58      */

59     public PlanetEntryData()
60     {
61     }
62
63     /**
64      * Create entry from Rome entry.
65      */

66     public PlanetEntryData(
67             SyndFeed romeFeed, SyndEntry romeEntry, PlanetSubscriptionData sub)
68     {
69         setSubscription(sub);
70         initFromRomeEntry(romeFeed, romeEntry);
71     }
72     
73     /**
74      * Init entry from Rome entry
75      */

76     private void initFromRomeEntry(SyndFeed romeFeed, SyndEntry romeEntry)
77     {
78         setAuthor(romeEntry.getAuthor());
79         setTitle(romeEntry.getTitle());
80         setPermalink(romeEntry.getLink());
81         
82         // Play some games to get the date
83
DCModule entrydc = (DCModule)romeEntry.getModule(DCModule.URI);
84         DCModule feeddc = (DCModule)romeFeed.getModule(DCModule.URI);
85         if (romeEntry.getPublishedDate() != null)
86         {
87             setPublished(romeEntry.getPublishedDate()); // use <pubDate>
88
}
89         else if (entrydc != null)
90         {
91             setPublished(entrydc.getDate()); // use <dc:date>
92
}
93         // Commented out: preserve knowledge that entry has no date
94
//if (getPublished() == null) // Still no published date?
95
//{
96
//if (romeFeed.getPublishedDate() != null)
97
//{
98
//setPublished(romeFeed.getPublishedDate()); // try feed's <pubDate>
99
//}
100
//else if (feeddc != null)
101
//{
102
//setPublished(feeddc.getDate()); // try feed's <dc:date>
103
//}
104
//}
105

106         // get content and unescape if it is 'text/plain'
107
if (romeEntry.getContents().size() > 0)
108         {
109             SyndContent content= (SyndContent)romeEntry.getContents().get(0);
110             if (content != null && content.getType().equals("text/plain"))
111             {
112                 setContent(Utilities.unescapeHTML(content.getValue()));
113             }
114             else if (content != null)
115             {
116                 setContent(content.getValue());
117             }
118         }
119         
120         // no content, then try <content:encoded>
121
if (getContent() == null || getContent().trim().length() == 0)
122         {
123             ContentModule cm = (ContentModule)romeEntry.getModule(ContentModule.URI);
124             if (cm != null)
125             {
126                 setContent(Utilities.unescapeHTML(cm.getEncoded()));
127             }
128         }
129         
130         // copy categories
131
if (romeEntry.getCategories().size() > 0)
132         {
133             List JavaDoc list = new ArrayList JavaDoc();
134             Iterator JavaDoc cats = romeEntry.getCategories().iterator();
135             while (cats.hasNext())
136             {
137                 SyndCategory cat = (SyndCategory)cats.next();
138                 list.add(cat.getName());
139             }
140             setCategories(list);
141         }
142     }
143     
144     //----------------------------------------------------------- persistent fields
145

146     /**
147      * @hibernate.id column="id" type="string"
148      * generator-class="uuid.hex" unsaved-value="null"
149      */

150     public String JavaDoc getId()
151     {
152         return id;
153     }
154     public void setId(String JavaDoc id)
155     {
156         this.id = id;
157     }
158     /**
159      * @hibernate.property column="categories" non-null="false" unique="false"
160      */

161     public String JavaDoc getCategoriesString()
162     {
163         return categoriesString;
164     }
165     public void setCategoriesString(String JavaDoc categoriesString)
166     {
167         this.categoriesString = categoriesString;
168     }
169     /**
170      * @hibernate.many-to-one column="subscription_id" cascade="save-update" not-null="true"
171      */

172     public PlanetSubscriptionData getSubscription()
173     {
174         return subscription;
175     }
176     public void setSubscription(PlanetSubscriptionData subscription)
177     {
178         this.subscription = subscription;
179     }
180     /**
181      * @hibernate.property column="author" non-null="false" unique="false"
182      */

183     public String JavaDoc getAuthor()
184     {
185         return author;
186     }
187     public void setAuthor(String JavaDoc author)
188     {
189         this.author = author;
190     }
191     /**
192      * @hibernate.property column="content" non-null="false" unique="false"
193      */

194     public String JavaDoc getContent()
195     {
196         return content;
197     }
198     public void setContent(String JavaDoc content)
199     {
200         this.content = content;
201     }
202     /**
203      * @hibernate.property column="guid" non-null="false" unique="true"
204      */

205     public String JavaDoc getGuid()
206     {
207         return guid;
208     }
209     public void setGuid(String JavaDoc guid)
210     {
211         this.guid = guid;
212     }
213     /**
214      * @hibernate.property column="handle" non-null="false" unique="false"
215      */

216     public String JavaDoc getHandle()
217     {
218         return handle;
219     }
220     public void setHandle(String JavaDoc handle)
221     {
222         this.handle = handle;
223     }
224     /**
225      * @hibernate.property column="published" non-null="true" unique="false"
226      */

227     public Date JavaDoc getPublished()
228     {
229         return published;
230     }
231     public void setPublished(Date JavaDoc published)
232     {
233         this.published = published;
234     }
235     /**
236      * @hibernate.property column="permalink" non-null="true" unique="false"
237      */

238     public String JavaDoc getPermalink()
239     {
240         return permalink;
241     }
242     public void setPermalink(String JavaDoc permalink)
243     {
244         this.permalink = permalink;
245     }
246     /**
247      * @hibernate.property column="title" non-null="false" unique="false"
248      */

249     public String JavaDoc getTitle()
250     {
251         return title;
252     }
253     public void setTitle(String JavaDoc title)
254     {
255         this.title = title;
256     }
257     /**
258      * @hibernate.property column="updated" non-null="false" unique="false"
259      */

260     public Date JavaDoc getUpdated()
261     {
262         return updated;
263     }
264     public void setUpdated(Date JavaDoc updated)
265     {
266         this.updated = updated;
267     }
268
269     //----------------------------------------------------------------- convenience
270

271     /**
272      * Returns true if any of entry's categories contain a specific string
273      * (case-insensitive comparison).
274      */

275     public boolean inCategory(String JavaDoc category)
276     {
277         Iterator JavaDoc cats = getCategories().iterator();
278         while (cats.hasNext())
279         {
280             String JavaDoc catName = ((String JavaDoc)cats.next()).toLowerCase();
281             if (catName.indexOf(category.toLowerCase()) != -1)
282             {
283                 return true;
284             }
285         }
286         return false;
287     }
288
289     //------------------------------------------------------------- implemenatation
290
public List JavaDoc getCategories()
291     {
292         List JavaDoc list = new ArrayList JavaDoc();
293         if (categoriesString != null)
294         {
295             String JavaDoc[] catArray = Utilities.stringToStringArray(categoriesString,",");
296             for (int i=0; i<catArray.length; i++)
297             {
298                 list.add(catArray[i]);
299             }
300         }
301         return list;
302     }
303     public void setCategories(List JavaDoc categories)
304     {
305         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
306         Iterator JavaDoc cats = categories.iterator();
307         while (cats.hasNext())
308         {
309             String JavaDoc cat = (String JavaDoc)cats.next();
310             sb.append(cat);
311             if (cats.hasNext()) sb.append(",");
312         }
313         categoriesString = sb.toString();
314     }
315     
316     /**
317      * Return category names as a string, separated by delimeters.
318      */

319     public String JavaDoc getCategoriesAsString(String JavaDoc delim)
320     {
321         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
322         Iterator JavaDoc cats = getCategories().iterator();
323         while (cats.hasNext())
324         {
325             String JavaDoc catName = ((String JavaDoc)cats.next()).toLowerCase();
326             sb.append(catName);
327             if (cats.hasNext()) sb.append(delim);
328         }
329         return sb.toString();
330     }
331     
332     public int compareTo(Object JavaDoc o)
333     {
334         PlanetEntryData other = (PlanetEntryData)o;
335         return getPermalink().compareTo(other.getPermalink());
336     }
337     public void setData(PersistentObject vo)
338     {
339     }
340 }
341
342
Popular Tags