1 18 19 package org.apache.roller.ui.rendering.model; 20 21 import java.io.IOException ; 22 import java.util.Date ; 23 import java.util.Map ; 24 import java.util.ResourceBundle ; 25 import java.util.Set ; 26 import java.util.TreeMap ; 27 import java.util.TreeSet ; 28 import org.apache.commons.collections.comparators.ReverseComparator; 29 import org.apache.commons.lang.StringUtils; 30 import org.apache.lucene.document.Document; 31 import org.apache.lucene.search.Hits; 32 import org.apache.roller.RollerException; 33 import org.apache.roller.business.search.FieldConstants; 34 import org.apache.roller.business.search.operations.SearchOperation; 35 import org.apache.roller.config.RollerRuntimeConfig; 36 import org.apache.roller.model.IndexManager; 37 import org.apache.roller.model.Roller; 38 import org.apache.roller.model.RollerFactory; 39 import org.apache.roller.model.WeblogManager; 40 import org.apache.roller.pojos.WeblogEntryData; 41 import org.apache.roller.pojos.WeblogEntryWrapperComparator; 42 import org.apache.roller.pojos.wrapper.WeblogEntryDataWrapper; 43 import org.apache.roller.ui.rendering.pagers.SearchResultsPager; 44 import org.apache.roller.ui.rendering.pagers.WeblogEntriesPager; 45 import org.apache.roller.ui.rendering.util.WeblogSearchRequest; 46 import org.apache.roller.util.DateUtil; 47 48 49 54 public class SearchResultsModel extends PageModel { 55 56 private static final ResourceBundle bundle = 57 ResourceBundle.getBundle("ApplicationResources"); 58 59 public static final int RESULTS_PER_PAGE = 10; 60 61 62 WeblogSearchRequest searchRequest = null; 64 65 private TreeMap results = new TreeMap (new ReverseComparator()); 67 68 private SearchResultsPager pager = null; 70 71 private int hits = 0; 72 private int offset = 0; 73 private int limit = 0; 74 private Set categories = new TreeSet (); 75 private boolean websiteSpecificSearch = true; 76 private String errorMessage = null; 77 78 79 public void init(Map initData) throws RollerException { 80 81 searchRequest = (WeblogSearchRequest) initData.get("searchRequest"); 83 if(searchRequest == null) { 84 throw new RollerException("expected searchRequest from init data"); 85 } 86 87 super.init(initData); 89 90 if(searchRequest.getQuery() == null) { 92 pager = new SearchResultsPager(searchRequest, results, false); 93 return; 94 } 95 96 IndexManager indexMgr = RollerFactory.getRoller().getIndexManager(); 98 99 SearchOperation search = new SearchOperation(indexMgr); 100 search.setTerm(searchRequest.getQuery()); 101 102 if(RollerRuntimeConfig.isSiteWideWeblog(searchRequest.getWeblogHandle())) { 103 this.websiteSpecificSearch = false; 104 } else { 105 search.setWebsiteHandle(searchRequest.getWeblogHandle()); 106 } 107 108 if(StringUtils.isNotEmpty(searchRequest.getWeblogCategoryName())) { 109 search.setCategory(searchRequest.getWeblogCategoryName()); 110 } 111 112 indexMgr.executeIndexOperationNow(search); 114 115 if (search.getResultsCount() == -1) { 116 this.errorMessage = bundle.getString("error.searchProblem"); 118 } else { 119 Hits hits = search.getResults(); 120 this.hits = search.getResultsCount(); 121 122 convertHitsToEntries(hits); 124 } 125 126 pager = new SearchResultsPager(searchRequest, results, (hits > (offset+limit))); 128 } 129 130 133 public boolean isSearchResults() { 134 return true; 135 } 136 137 public WeblogEntriesPager getWeblogEntriesPager() { 139 return pager; 140 } 141 142 public WeblogEntriesPager getWeblogEntriesPager(String category) { 144 return pager; 145 } 146 147 private void convertHitsToEntries(Hits hits) throws RollerException { 148 149 this.offset = searchRequest.getPageNum() * RESULTS_PER_PAGE; 151 if(this.offset >= hits.length()) { 152 this.offset = 0; 153 } 154 155 this.limit = RESULTS_PER_PAGE; 157 if(this.offset + this.limit > hits.length()) { 158 this.limit = hits.length() - this.offset; 159 } 160 161 try { 162 TreeSet categories = new TreeSet (); 163 Roller roller = RollerFactory.getRoller(); 164 WeblogManager weblogMgr = roller.getWeblogManager(); 165 166 WeblogEntryData entry = null; 167 Document doc = null; 168 String handle = null; 169 for(int i = offset; i < offset+limit; i++) { 170 171 entry = null; 173 doc = hits.doc(i); 174 handle = doc.getField(FieldConstants.WEBSITE_HANDLE).stringValue(); 175 176 if(websiteSpecificSearch && 177 handle.equals(searchRequest.getWeblogHandle())) { 178 179 entry = weblogMgr.getWeblogEntry( 180 doc.getField(FieldConstants.ID).stringValue()); 181 } else { 182 183 entry = weblogMgr.getWeblogEntry( 184 doc.getField(FieldConstants.ID).stringValue()); 185 186 if (doc.getField(FieldConstants.CATEGORY) != null) { 187 categories.add( 188 doc.getField(FieldConstants.CATEGORY).stringValue()); 189 } 190 } 191 192 if (entry != null) { 195 addEntryToResults(WeblogEntryDataWrapper.wrap(entry)); 196 } 197 } 198 199 if(categories.size() > 0) { 200 this.categories = categories; 201 } 202 } catch(IOException e) { 203 throw new RollerException(e); 204 } 205 } 206 207 208 private void addEntryToResults(WeblogEntryDataWrapper entry) { 209 210 Date midnight = DateUtil.getStartOfDay(entry.getPubTime()); 212 213 TreeSet set = (TreeSet ) this.results.get(midnight); 216 if (set == null) { 217 set = new TreeSet ( new WeblogEntryWrapperComparator()); 219 this.results.put(midnight, set); 220 } 221 set.add(entry); 222 } 223 224 225 public String getTerm() { 226 return (searchRequest.getQuery() == null) ? "" : searchRequest.getQuery(); 227 } 228 229 public int getHits() { 230 return hits; 231 } 232 233 public int getOffset() { 234 return offset; 235 } 236 237 public int getLimit() { 238 return limit; 239 } 240 241 public TreeMap getResults() { 242 return results; 243 } 244 245 public Set getCategories() { 246 return categories; 247 } 248 249 public boolean isWebsiteSpecificSearch() { 250 return websiteSpecificSearch; 251 } 252 253 public String getErrorMessage() { 254 return errorMessage; 255 } 256 257 } 258 | Popular Tags |