KickJava   Java API By Example, From Geeks To Geeks.

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


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.commons.collections.map.LinkedMap;
13 import org.apache.fulcrum.parser.StringValueParser;
14 import org.apache.torque.TorqueException;
15 import org.tigris.scarab.om.AttributeValue;
16 import org.tigris.scarab.om.Issue;
17 import org.tigris.scarab.om.IssueManager;
18 import org.tigris.scarab.om.IssueType;
19 import org.tigris.scarab.om.MITList;
20 import org.tigris.scarab.om.Module;
21 import org.tigris.scarab.om.Query;
22 import org.tigris.scarab.om.RModuleUserAttribute;
23 import org.tigris.scarab.om.ScarabUser;
24 import org.tigris.scarab.tools.ScarabToolManager;
25 import org.tigris.scarab.util.IteratorWithSize;
26 import org.tigris.scarab.util.ScarabLink;
27 import org.tigris.scarab.util.word.IssueSearch;
28 import org.tigris.scarab.util.word.IssueSearchFactory;
29 import org.tigris.scarab.util.word.MaxConcurrentSearchException;
30 import org.tigris.scarab.util.word.QueryResult;
31
32 import com.sun.syndication.feed.synd.SyndContent;
33 import com.sun.syndication.feed.synd.SyndContentImpl;
34 import com.sun.syndication.feed.synd.SyndEntry;
35 import com.sun.syndication.feed.synd.SyndEntryImpl;
36 import com.sun.syndication.feed.synd.SyndFeed;
37 import com.sun.syndication.feed.synd.SyndFeedImpl;
38 import com.sun.syndication.io.FeedException;
39
40 /**
41  * Converts a query to an RSS feed. The private methods are mostly ripped off
42  * of ScarabRequestTool and there should be some refactoring done here.
43  *
44  * @author Eric Pugh
45  *
46  */

47 public class QueryFeed implements Feed {
48     private static final String JavaDoc DATE_FORMAT = "yyyy-MM-dd";
49
50     private Query query;
51     private ScarabUser user;
52     private ScarabLink scarabLink;
53     private ScarabToolManager scarabToolManager;
54     
55
56     public QueryFeed(Query query, ScarabUser user,ScarabToolManager scarabToolManager, ScarabLink scarabLink) {
57         this.query = query;
58         this.user = user;
59         this.scarabLink = scarabLink;
60         this.scarabToolManager = scarabToolManager;
61     }
62
63     public SyndFeed getFeed() throws IOException JavaDoc, FeedException, TorqueException, Exception JavaDoc {
64         DateFormat JavaDoc dateParser = new SimpleDateFormat JavaDoc(DATE_FORMAT);
65
66
67         SyndFeed feed = new SyndFeedImpl();
68
69         MITList mitList = query.getMITList();
70         boolean showModuleName = !mitList.isSingleModule();
71         boolean showIssueType = !mitList.isSingleIssueType();
72         String JavaDoc currentQueryString = query.getValue();
73         IssueSearch search = getPopulatedSearch(currentQueryString,mitList,user);
74         
75         IteratorWithSize queryResults = null;
76
77         // Do search
78

79         if (search == null) {
80             // an alert message should have been set while attempting
81
// to populate the search.
82
queryResults = IteratorWithSize.EMPTY;
83         } else {
84             queryResults = search.getQueryResults();
85
86         }
87
88         feed.setTitle(query.getName());
89         String JavaDoc link = scarabLink.setAction("Search").addPathInfo("go",query.getQueryId()).toString();
90         feed.setLink(link);
91         feed.setDescription(query.getDescription());
92
93         List JavaDoc entries = new ArrayList JavaDoc();
94
95         for (Iterator JavaDoc i = queryResults; i.hasNext();) {
96             SyndEntry entry;
97             SyndContent description;
98             //MITListItem item = (MITListItem) i.next();
99
QueryResult queryResult = (QueryResult)i.next();
100
101             
102             
103             entry = new SyndEntryImpl();
104             String JavaDoc title = queryResult.getUniqueId();
105             if(showModuleName){
106                 title = title + " ("+ queryResult.getModule().getRealName() + ")";
107             }
108             if(showIssueType){
109                 title = title + " ("+ queryResult.getRModuleIssueType().getDisplayName() + ")";
110             }
111             entry.setTitle(title);
112             
113             Issue issue = IssueManager.getInstance(Long.valueOf(queryResult.getIssueId()));
114
115             link = scarabLink.getIssueIdAbsoluteLink(issue).toString();
116             entry.setLink(link);
117             
118             Date JavaDoc publishedDate = null;
119             if(issue.getModifiedDate()!= null){
120                 publishedDate = issue.getModifiedDate();
121             }
122             else {
123                 publishedDate = issue.getCreatedDate();
124             }
125             entry.setPublishedDate(publishedDate);
126             
127
128             description = new SyndContentImpl();
129             description.setType("text/html");
130             String JavaDoc desc = "";
131             Iterator JavaDoc avIteratorCSV = queryResult.getAttributeValuesAsCSV().iterator();
132             Iterator JavaDoc avIterator = search.getIssueListAttributeColumns().iterator();
133             for(;avIterator.hasNext();){
134                 String JavaDoc value = (String JavaDoc)avIteratorCSV.next();
135                 RModuleUserAttribute av = (RModuleUserAttribute)avIterator.next();
136                 desc = desc + "<b>" + av.getAttribute().getName()+":</b>" + value +"<br/>";
137             }
138             description.setValue(desc);
139
140             entry.setDescription(description);
141             entries.add(entry);
142
143         }
144      
145
146         feed.setEntries(entries);
147         
148         search.close();
149         IssueSearchFactory.INSTANCE.notifyDone();
150
151         return feed;
152     }
153
154     /**
155      * Get an IssueSearch object based on a query string. Copied from
156      * ScarabRequestTool
157      *
158      * @return a <code>Issue</code> value
159      */

160     private IssueSearch getPopulatedSearch(String JavaDoc query, MITList mitList, ScarabUser searcher) throws Exception JavaDoc {
161         IssueSearch search = getNewSearch(mitList,searcher);
162         Module module = mitList.getModule();
163         IssueType issueType = null;
164         // this sucks, but seems required...
165
if(mitList.isSingleIssueType()){
166             issueType = mitList.getIssueType();
167         }
168         else {
169             issueType = mitList.getFirstItem().getIssueType();
170         }
171         List JavaDoc listUserAttributes = scarabToolManager.getRModuleUserAttributes(user,module,issueType);
172         search.setIssueListAttributeColumns(listUserAttributes);
173
174        //Intake intake = null;
175

176         if (query == null) {
177             throw new Exception JavaDoc("Query was null");
178         }
179
180         // If they have entered users to search on, add them to the search
181
StringValueParser parser = new StringValueParser();
182         parser.parse(query, '&', '=', true);
183         String JavaDoc[] userList = parser.getStrings("user_list");
184         if (userList != null && userList.length > 0) {
185             for (int i = 0; i < userList.length; i++) {
186                 String JavaDoc userId = userList[i];
187                 String JavaDoc[] attrIds = parser.getStrings("user_attr_" + userId);
188                 if (attrIds != null) {
189                     for (int j = 0; j < attrIds.length; j++) {
190                         search.addUserCriteria(userId, attrIds[j]);
191                     }
192                 }
193             }
194         }
195
196         // Set intake properties
197
/*
198          * boolean searchSuccess = true; Group searchGroup =
199          * intake.get("SearchIssue", search.getQueryKey());
200          *
201          * Field minDate = searchGroup.get("MinDate"); if (minDate != null &&
202          * minDate.toString().length() > 0) { searchSuccess = checkDate(search,
203          * minDate.toString()); }
204          *
205          * Field maxDate = searchGroup.get("MaxDate"); if (maxDate != null &&
206          * maxDate.toString().length() > 0) { searchSuccess = checkDate(search,
207          * maxDate.toString()); }
208          *
209          * Field stateChangeFromDate = searchGroup.get("StateChangeFromDate");
210          * if (stateChangeFromDate != null &&
211          * stateChangeFromDate.toString().length() > 0) { searchSuccess =
212          * checkDate(search, stateChangeFromDate.toString()); }
213          *
214          * Field stateChangeToDate = searchGroup.get("StateChangeToDate"); if
215          * (stateChangeToDate != null && stateChangeToDate.toString().length() >
216          * 0) { searchSuccess = checkDate(search, stateChangeToDate.toString()); }
217          *
218          * if (!searchSuccess) { setAlertMessage(l10n.format("DateFormatPrompt",
219          * L10NKeySet.ShortDateDisplay)); return null; }
220          *
221          * try { searchGroup.setProperties(search); } catch (Exception e) {
222          * setAlertMessage(l10n.getMessage(e)); return null; }
223          *
224          * Integer oldOptionId = search.getStateChangeFromOptionId(); if
225          * (oldOptionId != null && oldOptionId.intValue() != 0 &&
226          * oldOptionId.equals(search.getStateChangeToOptionId())) {
227          * setAlertMessage(L10NKeySet.StateChangeOldEqualNew); return null; }
228          */

229         // Set attribute values to search on
230
LinkedMap avMap = search.getCommonAttributeValuesMap();
231         Iterator JavaDoc i = avMap.mapIterator();
232         while (i.hasNext()) {
233             AttributeValue aval = (AttributeValue) avMap.get(i.next());
234          // Group group = intake.get("AttributeValue", aval.getQueryKey());
235
// if (group != null) {
236
// group.setProperties(aval);
237
// }
238
}
239
240         // If user is sorting on an attribute, set sort criteria
241
// Do not use intake, since intake parsed from query is not the same
242
// As intake passed from the form
243
// String sortColumn = data.getParameters().getString("sortColumn");
244
// if (sortColumn != null && sortColumn.length() > 0 &&
245
// StringUtils.isNumeric(sortColumn)) {
246
// search.setSortAttributeId(new Integer(sortColumn));
247
// }
248

249         // String sortPolarity = data.getParameters().getString("sortPolarity");
250
// if (sortPolarity != null && sortPolarity.length() > 0) {
251
// search.setSortPolarity(sortPolarity);
252
// }
253

254         return search;
255     }
256
257     /**
258      * Get a new IssueSearch object. Copied from ScarabRequestTool
259      *
260      * @return a <code>Issue</code> value
261      */

262     private IssueSearch getNewSearch(MITList mitList, ScarabUser searcher) throws Exception JavaDoc,
263             MaxConcurrentSearchException {
264
265         IssueSearch issueSearch = IssueSearchFactory.INSTANCE.getInstance(mitList, searcher);
266         // issueSearch.setLocale(getLocalizationTool().getPrimaryLocale());
267

268         return issueSearch;
269     }
270
271 }
Popular Tags