001: package org.tigris.scarab.feeds;
002:
003: import java.io.IOException;
004: import java.util.ArrayList;
005: import java.util.Date;
006: import java.util.Iterator;
007: import java.util.List;
008:
009: import org.apache.torque.TorqueException;
010: import org.tigris.scarab.om.Activity;
011: import org.tigris.scarab.om.ActivitySet;
012: import org.tigris.scarab.om.Issue;
013: import org.tigris.scarab.tools.ScarabLocalizationTool;
014: import org.tigris.scarab.tools.ScarabToolManager;
015: import org.tigris.scarab.util.ScarabLink;
016:
017: import com.sun.syndication.feed.synd.SyndContent;
018: import com.sun.syndication.feed.synd.SyndContentImpl;
019: import com.sun.syndication.feed.synd.SyndEntry;
020: import com.sun.syndication.feed.synd.SyndEntryImpl;
021: import com.sun.syndication.feed.synd.SyndFeed;
022: import com.sun.syndication.feed.synd.SyndFeedImpl;
023: import com.sun.syndication.io.FeedException;
024:
025: /**
026: * Converts a Issue to an RSS feed.
027: *
028: * @todo improve what is shown to a user
029: * @author Eric Pugh
030: *
031: */
032: public class IssueFeed implements Feed {
033:
034: private Issue issue;
035:
036: private ScarabLink scarabLink;
037: private ScarabToolManager scarabToolManager;
038: private ScarabLocalizationTool l10nTool;
039:
040: public IssueFeed(Issue issue, ScarabLink scarabLink,
041: ScarabToolManager scarabToolManager,
042: ScarabLocalizationTool l10nTool) {
043: this .issue = issue;
044: this .scarabLink = scarabLink;
045: this .scarabToolManager = scarabToolManager;
046: this .l10nTool = l10nTool;
047: }
048:
049: public SyndFeed getFeed() throws IOException, FeedException,
050: TorqueException, Exception {
051:
052: SyndFeed feed = new SyndFeedImpl();
053:
054: String title = issue.getUniqueId() + ": "
055: + issue.getDefaultText();
056: feed.setTitle(title);
057: String link = scarabLink.getIssueIdAbsoluteLink(issue)
058: .toString();
059: feed.setLink(link);
060: feed.setDescription(title);
061:
062: List entries = new ArrayList();
063: List allActivities = issue.getActivity(true);
064:
065: for (Iterator i = allActivities.iterator(); i.hasNext();) {
066: SyndEntry entry;
067: SyndContent description;
068:
069: Activity activity = (Activity) i.next();
070:
071: ActivitySet activitySet = activity.getActivitySet();
072: Date date = activitySet.getCreatedDate();
073: entry = new SyndEntryImpl();
074:
075: entry.setPublishedDate(date);
076:
077: description = new SyndContentImpl();
078: description.setType("text/html");
079:
080: StringBuffer desc = new StringBuffer();
081: String activityDesc = activity
082: .getDescription(this .l10nTool);
083: desc.append("<b>Description:</b>" + activityDesc + "<br/>");
084: desc
085: .append("<b>Reason:</b>"
086: + activitySet.getActivityReason(l10nTool)
087: + "<br/>");
088: entry.setAuthor(activitySet.getCreator().getName());
089:
090: description.setValue(desc.toString());
091:
092: String entryTitle = createEntryTitle(activity);
093: entry.setTitle(entryTitle);
094:
095: entry.setDescription(description);
096:
097: //The hashCode is a cheap trick to have a unique link tag in the RSS feed
098: //to help those reader as ThunderBird that use the link to check for new items
099: entry.setLink(link + "?hash=" + entry.hashCode());
100:
101: entries.add(entry);
102: }
103:
104: feed.setEntries(entries);
105:
106: return feed;
107: }
108:
109: /**
110: * Just a method to isolate the logic for the entry naming.
111: * It is just a substring of the activity description
112: * @param activity the activity we want to create the title for
113: * @return the entry title
114: */
115: private String createEntryTitle(Activity activity) {
116: String activityDesc = activity.getDescription(this .l10nTool);
117: String entryTitle = null;
118: int maxTitleLength = 64;
119: int activityLength = activityDesc.length();
120: entryTitle = activityDesc.substring(0,
121: (activityLength >= maxTitleLength) ? maxTitleLength
122: : activityLength);
123: return entryTitle;
124: }
125:
126: }
|