1 2 package org.tigris.scarab.feeds; 3 4 import java.io.IOException ; 5 import java.text.DateFormat ; 6 import java.text.SimpleDateFormat ; 7 import java.util.ArrayList ; 8 import java.util.Date ; 9 import java.util.Iterator ; 10 import java.util.List ; 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 47 public class QueryFeed implements Feed { 48 private static final String 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 , FeedException, TorqueException, Exception { 64 DateFormat dateParser = new SimpleDateFormat (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 currentQueryString = query.getValue(); 73 IssueSearch search = getPopulatedSearch(currentQueryString,mitList,user); 74 75 IteratorWithSize queryResults = null; 76 77 79 if (search == null) { 80 queryResults = IteratorWithSize.EMPTY; 83 } else { 84 queryResults = search.getQueryResults(); 85 86 } 87 88 feed.setTitle(query.getName()); 89 String link = scarabLink.setAction("Search").addPathInfo("go",query.getQueryId()).toString(); 90 feed.setLink(link); 91 feed.setDescription(query.getDescription()); 92 93 List entries = new ArrayList (); 94 95 for (Iterator i = queryResults; i.hasNext();) { 96 SyndEntry entry; 97 SyndContent description; 98 QueryResult queryResult = (QueryResult)i.next(); 100 101 102 103 entry = new SyndEntryImpl(); 104 String 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 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 desc = ""; 131 Iterator avIteratorCSV = queryResult.getAttributeValuesAsCSV().iterator(); 132 Iterator avIterator = search.getIssueListAttributeColumns().iterator(); 133 for(;avIterator.hasNext();){ 134 String value = (String )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 160 private IssueSearch getPopulatedSearch(String query, MITList mitList, ScarabUser searcher) throws Exception { 161 IssueSearch search = getNewSearch(mitList,searcher); 162 Module module = mitList.getModule(); 163 IssueType issueType = null; 164 if(mitList.isSingleIssueType()){ 166 issueType = mitList.getIssueType(); 167 } 168 else { 169 issueType = mitList.getFirstItem().getIssueType(); 170 } 171 List listUserAttributes = scarabToolManager.getRModuleUserAttributes(user,module,issueType); 172 search.setIssueListAttributeColumns(listUserAttributes); 173 174 176 if (query == null) { 177 throw new Exception ("Query was null"); 178 } 179 180 StringValueParser parser = new StringValueParser(); 182 parser.parse(query, '&', '=', true); 183 String [] userList = parser.getStrings("user_list"); 184 if (userList != null && userList.length > 0) { 185 for (int i = 0; i < userList.length; i++) { 186 String userId = userList[i]; 187 String [] 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 229 LinkedMap avMap = search.getCommonAttributeValuesMap(); 231 Iterator i = avMap.mapIterator(); 232 while (i.hasNext()) { 233 AttributeValue aval = (AttributeValue) avMap.get(i.next()); 234 } 239 240 249 254 return search; 255 } 256 257 262 private IssueSearch getNewSearch(MITList mitList, ScarabUser searcher) throws Exception , 263 MaxConcurrentSearchException { 264 265 IssueSearch issueSearch = IssueSearchFactory.INSTANCE.getInstance(mitList, searcher); 266 268 return issueSearch; 269 } 270 271 } | Popular Tags |