1 43 package net.jforum.view.forum; 44 45 import java.lang.reflect.Field ; 46 import java.util.ArrayList ; 47 import java.util.Date ; 48 import java.util.HashMap ; 49 import java.util.Iterator ; 50 import java.util.List ; 51 import java.util.Map ; 52 53 import javax.servlet.http.HttpServletResponse ; 54 55 import net.jforum.ActionServletRequest; 56 import net.jforum.Command; 57 import net.jforum.JForumExecutionContext; 58 import net.jforum.dao.DataAccessDriver; 59 import net.jforum.dao.SearchDAO; 60 import net.jforum.dao.SearchData; 61 import net.jforum.entities.Forum; 62 import net.jforum.entities.Topic; 63 import net.jforum.repository.ForumRepository; 64 import net.jforum.util.I18n; 65 import net.jforum.util.preferences.ConfigKeys; 66 import net.jforum.util.preferences.SystemGlobals; 67 import net.jforum.util.preferences.TemplateKeys; 68 import net.jforum.view.forum.common.TopicsCommon; 69 import net.jforum.view.forum.common.ViewCommon; 70 import freemarker.template.SimpleHash; 71 72 76 public class SearchAction extends Command 77 { 78 private String searchTerms; 79 private String forum; 80 private String category; 81 private String sortBy; 82 private String sortDir; 83 private String kw; 84 private String author; 85 private String postTime; 86 87 private static Map fieldsMap = new HashMap (); 88 private static Map sortByMap = new HashMap (); 89 90 static { 91 fieldsMap.put("search_terms", "searchTerms"); 92 fieldsMap.put("search_forum", "forum"); 93 fieldsMap.put("search_cat", "catgory"); 94 fieldsMap.put("sort_by", "sortBy"); 95 fieldsMap.put("sort_dir", "sortDir"); 96 fieldsMap.put("search_keywords", "kw"); 97 fieldsMap.put("search_author", "author"); 98 fieldsMap.put("post_time", "postTime"); 99 100 sortByMap.put("time", "p.post_time"); 101 sortByMap.put("title", "t.topic_title"); 102 sortByMap.put("username", "u.username"); 103 sortByMap.put("forum", "t.forum_id"); 104 } 105 106 public SearchAction() {} 107 108 public SearchAction(ActionServletRequest request, HttpServletResponse response, 109 SimpleHash context) { 110 this.request = request; 111 this.response = response; 112 this.context = context; 113 } 114 115 public void filters() throws Exception 116 { 117 this.setTemplateName(TemplateKeys.SEARCH_FILTERS); 118 this.context.put("categories", ForumRepository.getAllCategories()); 119 this.context.put("pageTitle", I18n.getMessage("ForumBase.search")); 120 } 121 122 private void getSearchFields() 123 { 124 this.searchTerms = this.addSlashes(this.request.getParameter("search_terms")); 125 this.forum = this.addSlashes(this.request.getParameter("search_forum")); 126 this.category = this.addSlashes(this.request.getParameter("search_cat")); 127 128 this.sortBy = (String )sortByMap.get(this.addSlashes(this.request.getParameter("sort_by"))); 129 130 if (this.sortBy == null) { 131 this.sortBy = (String )sortByMap.get("time"); 132 } 133 134 this.sortDir = this.addSlashes(this.request.getParameter("sort_dir")); 135 136 if (!"ASC".equals(this.sortDir) && !"DESC".equals(this.sortDir)) { 137 this.sortDir = "DESC"; 138 } 139 140 this.kw = this.addSlashes(this.request.getParameter("search_keywords")); 141 this.author = this.addSlashes(this.request.getParameter("search_author")); 142 this.postTime = this.addSlashes(this.request.getParameter("post_time")); 143 } 144 145 public void search() throws Exception 146 { 147 this.getSearchFields(); 148 149 SearchData sd = new SearchData(); 150 sd.setKeywords(kw); 151 sd.setAuthor(author); 152 sd.setOrderByField(sortBy); 153 sd.setOrderBy(sortDir); 154 155 if (postTime != null) { 156 sd.setTime(new Date (Long.parseLong(postTime))); 157 } 158 159 if (searchTerms != null) { 160 sd.setUseAllWords(searchTerms.equals("any") ? false : true); 161 } 162 else { 163 sd.setUseAllWords(true); 164 } 165 166 if (forum != null && !forum.equals("")) { 167 sd.setForumId(Integer.parseInt(forum)); 168 } 169 170 if (category != null && !category.equals("")) { 171 sd.setCategoryId(Integer.parseInt(category)); 172 } 173 174 int start = ViewCommon.getStartPage(); 175 int recordsPerPage = SystemGlobals.getIntValue(ConfigKeys.TOPICS_PER_PAGE); 176 177 SearchDAO sm = DataAccessDriver.getInstance().newSearchDAO(); 178 179 if (this.request.getParameter("clean") != null) { 181 sm.cleanSearch(); 182 } 183 else { 184 sd.setSearchStarted(true); 185 } 186 187 List allTopics = this.onlyAllowedData(sm.search(sd)); 188 int totalTopics = allTopics.size(); 189 int sublistLimit = recordsPerPage + start > totalTopics ? totalTopics : recordsPerPage + start; 190 191 this.setTemplateName(TemplateKeys.SEARCH_SEARCH); 192 193 this.context.put("fr", new ForumRepository()); 194 195 this.context.put("topics", TopicsCommon.prepareTopics(allTopics.subList(start, sublistLimit))); 196 this.context.put("categories", ForumRepository.getAllCategories()); 197 198 this.context.put("kw", kw); 199 this.context.put("terms", searchTerms); 200 this.context.put("forum", forum); 201 this.context.put("category", category); 202 this.context.put("orderField", sortBy); 203 this.context.put("orderBy", sortDir); 204 this.context.put("author", author); 205 this.context.put("postTime", postTime); 206 this.context.put("openModeration", "1".equals(this.request.getParameter("openModeration"))); 207 this.context.put("postsPerPage", new Integer (SystemGlobals.getIntValue(ConfigKeys.POST_PER_PAGE))); 208 209 ViewCommon.contextToPagination(start, totalTopics, recordsPerPage); 210 211 this.context.put("pageTitle", I18n.getMessage("ForumBase.search")); 212 TopicsCommon.topicListingBase(); 213 } 214 215 private List onlyAllowedData(List topics) throws Exception 216 { 217 List l = new ArrayList (); 218 219 for (Iterator iter = topics.iterator(); iter.hasNext(); ) { 220 Topic t = (Topic)iter.next(); 221 Forum f = ForumRepository.getForum(t.getForumId()); 222 223 if (f != null && ForumRepository.isCategoryAccessible(f.getCategoryId())) { 224 l.add(t); 225 } 226 } 227 228 return l; 229 } 230 231 public void doModeration() throws Exception 232 { 233 new ModerationHelper().doModeration(this.makeRedirect()); 234 235 if (JForumExecutionContext.getRequest().getParameter("topicMove") != null) { 236 this.setTemplateName(TemplateKeys.MODERATION_MOVE_TOPICS); 237 } 238 } 239 240 public void moveTopic() throws Exception 241 { 242 new ModerationHelper().moveTopicsSave(this.makeRedirect()); 243 } 244 245 public void moderationDone() throws Exception 246 { 247 this.setTemplateName(new ModerationHelper().moderationDone(this.makeRedirect())); 248 } 249 250 private String makeRedirect() throws Exception 251 { 252 String persistData = this.request.getParameter("persistData"); 253 if (persistData == null) { 254 this.getSearchFields(); 255 } 256 else { 257 String [] p = persistData.split("&"); 258 259 for (int i = 0; i < p.length; i++) { 260 String [] v = p[i].split("="); 261 262 String name = (String )fieldsMap.get(v[0]); 263 if (name != null) { 264 Field field = this.getClass().getDeclaredField(name); 265 if (field != null && v[1] != null && !v[1].equals("")) { 266 field.set(this, v[1]); 267 } 268 } 269 } 270 } 271 272 StringBuffer path = new StringBuffer (512); 273 path.append(this.request.getContextPath()).append("/jforum" 274 + SystemGlobals.getValue(ConfigKeys.SERVLET_EXTENSION) 275 + "?module=search&action=search&clean=1"); 276 277 if (this.forum != null) { 278 path.append("&search_forum=").append(this.forum); 279 } 280 281 if (this.searchTerms != null) { 282 path.append("&search_terms=").append(this.searchTerms); 283 } 284 285 if (this.category != null) { 286 path.append("&search_cat=").append(this.category); 287 } 288 289 if (this.sortDir != null) { 290 path.append("&sort_dir=").append(this.sortDir); 291 } 292 293 if (this.sortBy != null) { 294 path.append("&sort_by=").append(this.sortBy); 295 } 296 297 if (this.kw != null) { 298 path.append("&search_keywords=").append(this.kw); 299 } 300 301 if (this.postTime != null) { 302 path.append("&post_time=").append(this.postTime); 303 } 304 305 path.append("&start=").append(ViewCommon.getStartPage()); 306 307 return path.toString(); 308 } 309 310 private String addSlashes(String s) 311 { 312 if (s != null) { 313 s = s.replaceAll("'", "\\'"); 314 s = s.replaceAll("\"", "\\\""); 315 } 316 317 return s; 318 } 319 320 323 public void list() throws Exception 324 { 325 this.filters(); 326 } 327 } 328 | Popular Tags |