001: package org.tigris.scarab.feeds;
002:
003: import java.util.ArrayList;
004: import java.util.Date;
005: import java.util.Iterator;
006: import java.util.List;
007:
008: import org.apache.fulcrum.security.util.TurbineSecurityException;
009: import org.apache.torque.TorqueException;
010: import org.tigris.scarab.om.Issue;
011: import org.tigris.scarab.om.IssueManager;
012: import org.tigris.scarab.om.Query;
013: import org.tigris.scarab.om.RModuleUserAttribute;
014: import org.tigris.scarab.util.IteratorWithSize;
015: import org.tigris.scarab.util.ScarabLink;
016: import org.tigris.scarab.util.ScarabUtil;
017: import org.tigris.scarab.util.word.QueryResult;
018:
019: import com.sun.syndication.feed.synd.SyndContent;
020: import com.sun.syndication.feed.synd.SyndContentImpl;
021: import com.sun.syndication.feed.synd.SyndEntry;
022: import com.sun.syndication.feed.synd.SyndEntryImpl;
023: import com.sun.syndication.feed.synd.SyndFeed;
024: import com.sun.syndication.feed.synd.SyndFeedImpl;
025: import com.workingdogs.village.DataSetException;
026:
027: /**
028: * Converts a query to an RSS feed. The private methods are mostly ripped off
029: * of ScarabRequestTool and there should be some refactoring done here.
030: *
031: * @author Eric Pugh
032: *
033: */
034: public class QueryFeed implements Feed {
035:
036: private Query query;
037: private IteratorWithSize results;
038: private ScarabLink scarabLink;
039: private String format;
040:
041: /**
042: * Constructs the query feed with the search results.
043: * @param query The searched query
044: * @param results The iterator containing the search-results
045: * @param scarabToolManager
046: * @param scarabLink
047: * @param format
048: */
049: public QueryFeed(Query query, IteratorWithSize results,
050: ScarabLink scarabLink, String format) {
051: this .query = query;
052: this .results = results;
053: this .scarabLink = scarabLink;
054: this .format = format;
055: }
056:
057: public SyndFeed getFeed() throws Exception, TorqueException,
058: DataSetException, TurbineSecurityException {
059: boolean showModuleName = !query.getMITList().isSingleModule();
060: boolean showIssueType = !query.getMITList().isSingleIssueType();
061:
062: SyndFeed feed = new SyndFeedImpl();
063: feed.setTitle(query.getName());
064: String link = scarabLink.setAction("Search").addPathInfo("go",
065: query.getQueryId()).toString();
066: feed.setLink(link);
067: feed.setDescription(query.getDescription());
068: List entries = new ArrayList();
069: while (results.hasNext()) {
070: SyndEntry entry = new SyndEntryImpl();
071: SyndContent description = new SyndContentImpl();
072: QueryResult queryResult = (QueryResult) results.next();
073: String title = queryResult.getUniqueId();
074: if (showModuleName) {
075: title = title + " ("
076: + queryResult.getModule().getRealName() + ")";
077: }
078: if (showIssueType) {
079: title = title
080: + " ("
081: + queryResult.getRModuleIssueType()
082: .getDisplayName() + ")";
083: }
084: entry.setTitle(title);
085:
086: Issue issue = IssueManager.getInstance(Long
087: .valueOf(queryResult.getIssueId()));
088:
089: link = scarabLink.getIssueIdAbsoluteLink(issue).toString();
090: entry.setLink(link);
091:
092: Date publishedDate = null;
093: if (issue.getModifiedDate() != null) {
094: publishedDate = issue.getModifiedDate();
095: } else {
096: publishedDate = issue.getCreatedDate();
097: }
098: entry.setPublishedDate(publishedDate);
099:
100: description = new SyndContentImpl();
101: description.setType("text/html");
102: String desc = "";
103: Iterator avIteratorCSV = queryResult
104: .getAttributeValuesAsCSV().iterator();
105: Iterator avIterator = query.getMITList()
106: .getAllRModuleUserAttributes().iterator();
107: while (avIterator.hasNext()) {
108: String value = (String) avIteratorCSV.next();
109: RModuleUserAttribute av = (RModuleUserAttribute) avIterator
110: .next();
111: desc = desc + "<b>" + av.getAttribute().getName()
112: + ":</b>" + value + "<br/>";
113: }
114: description.setValue(ScarabUtil.filterNonXml(desc));
115:
116: entry.setDescription(description);
117: entries.add(entry);
118: }
119: feed.setEntries(entries);
120: feed.setFeedType(format);
121: return feed;
122: }
123:
124: }
|