KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > tigris > scarab > feeds > IssueFeed


1
2 package org.tigris.scarab.feeds;
3
4 import java.io.IOException JavaDoc;
5 import java.text.DateFormat JavaDoc;
6 import java.text.SimpleDateFormat JavaDoc;
7 import java.util.ArrayList JavaDoc;
8 import java.util.Date JavaDoc;
9 import java.util.Iterator JavaDoc;
10 import java.util.List JavaDoc;
11
12 import org.apache.torque.TorqueException;
13 import org.tigris.scarab.om.Activity;
14 import org.tigris.scarab.om.ActivitySet;
15 import org.tigris.scarab.om.Issue;
16 import org.tigris.scarab.tools.ScarabToolManager;
17 import org.tigris.scarab.util.ScarabLink;
18
19 import com.sun.syndication.feed.synd.SyndContent;
20 import com.sun.syndication.feed.synd.SyndContentImpl;
21 import com.sun.syndication.feed.synd.SyndEntry;
22 import com.sun.syndication.feed.synd.SyndEntryImpl;
23 import com.sun.syndication.feed.synd.SyndFeed;
24 import com.sun.syndication.feed.synd.SyndFeedImpl;
25 import com.sun.syndication.io.FeedException;
26
27 /**
28  * Converts a Issue to an RSS feed.
29  *
30  * @todo improve what is shown to a user
31  * @author Eric Pugh
32  *
33  */

34 public class IssueFeed implements Feed{
35
36     private Issue issue;
37
38     private ScarabLink scarabLink;
39     private ScarabToolManager scarabToolManager;
40
41     public IssueFeed(Issue issue,ScarabLink scarabLink,ScarabToolManager scarabToolManager) {
42         this.issue = issue;
43         this.scarabLink = scarabLink;
44         this.scarabToolManager = scarabToolManager;
45     }
46
47     public SyndFeed getFeed() throws IOException JavaDoc, FeedException, TorqueException, Exception JavaDoc {
48
49         SyndFeed feed = new SyndFeedImpl();
50        
51         String JavaDoc title = issue.getUniqueId() + ": " + issue.getDefaultText();
52         feed.setTitle(title);
53         String JavaDoc link = scarabLink.getIssueIdAbsoluteLink(issue).toString();
54         feed.setLink(link);
55         feed.setDescription(title);
56
57         List JavaDoc entries = new ArrayList JavaDoc();
58         List JavaDoc allActivities = issue.getActivity(true);
59         
60
61         for (Iterator JavaDoc i = allActivities.iterator(); i.hasNext();) {
62             SyndEntry entry;
63             SyndContent description;
64             
65             Activity activity = (Activity)i.next();
66             
67             ActivitySet activitySet = activity.getActivitySet();
68             Date JavaDoc date =activitySet.getCreatedDate();
69             entry = new SyndEntryImpl();
70
71             entry.setPublishedDate(date);
72             
73             description = new SyndContentImpl();
74             description.setType("text/html");
75             
76             StringBuffer JavaDoc desc = new StringBuffer JavaDoc();
77             String JavaDoc activityDesc=activity.getDescription();
78             desc.append("<b>Description:</b>" + activityDesc +"<br/>");
79             desc.append("<b>Reason:</b>" + scarabToolManager.getActivityReason(activitySet,activity) +"<br/>");
80
81             description.setValue(desc.toString());
82
83             String JavaDoc entryTitle = createEntryTitle(activity);
84             entry.setTitle(entryTitle);
85             
86             entry.setDescription(description);
87             
88             //The hashCode is a cheap trick to have a unique link tag in the RSS feed
89
//to help those reader as ThunderBird that use the link to check for new items
90
entry.setLink(link+"?id="+entry.hashCode());
91             
92             entries.add(entry);
93         }
94      
95
96         feed.setEntries(entries);
97         
98         return feed;
99     }
100
101     /**
102      * Just a method to isolate the logic for the entry naming.
103      * It is just a substring of the activity description
104      * @param activity the activity we want to create the title for
105      * @return the entry title
106      */

107     private String JavaDoc createEntryTitle(Activity activity) {
108         String JavaDoc activityDesc=activity.getDescription();
109         String JavaDoc entryTitle=null;
110         int maxTitleLength=64;
111         int activityLength=activityDesc.length();
112         entryTitle=activityDesc.substring(0,(activityLength>=maxTitleLength)?maxTitleLength:activityLength);
113         return entryTitle;
114     }
115
116 }
Popular Tags