1 43 package net.jforum.dao.generic; 44 45 import java.sql.PreparedStatement ; 46 import java.sql.ResultSet ; 47 import java.sql.Timestamp ; 48 import java.util.ArrayList ; 49 import java.util.HashMap ; 50 import java.util.HashSet ; 51 import java.util.Iterator ; 52 import java.util.List ; 53 import java.util.Map ; 54 import java.util.Set ; 55 56 import net.jforum.JForumExecutionContext; 57 import net.jforum.SessionFacade; 58 import net.jforum.dao.SearchData; 59 import net.jforum.util.preferences.ConfigKeys; 60 import net.jforum.util.preferences.SystemGlobals; 61 62 import org.apache.log4j.Logger; 63 64 68 public class GenericSearchDAO implements net.jforum.dao.SearchDAO 69 { 70 private static final Logger log = Logger.getLogger(GenericSearchDAO.class); 71 72 75 public List search(SearchData sd) throws Exception 76 { 77 List l = new ArrayList (); 78 79 if (!sd.getSearchStarted()) { 81 if (sd.getTime() == null) { 82 this.topicsByKeyword(sd); 83 } 84 else { 85 this.topicsByTime(sd); 86 } 87 } 88 89 String sql = SystemGlobals.getSql("SearchModel.searchBase"); 90 StringBuffer criterias = new StringBuffer (512); 91 92 if (sd.getForumId() != 0) { 93 criterias.append(" AND t.forum_id = " + sd.getForumId()); 94 } 95 96 if (sd.getCategoryId() != 0) { 97 sql = sql.replaceAll(":table_category:", ", jforum_forums f"); 98 99 criterias.append(" AND f.categories_id = "+ sd.getCategoryId()); 100 criterias.append(" AND t.forum_id = f.forum_id"); 101 } 102 else { 103 sql = sql.replaceAll(":table_category:", ""); 104 } 105 106 if (sd.getOrderByField() == null || sd.getOrderByField().equals("")) { 107 sd.setOrderByField("p.post_time"); 108 } 109 110 sql = sql.replaceAll(":orderByField:", sd.getOrderByField()); 112 sql = sql.replaceAll(":orderBy:", sd.getOrderBy()); 113 sql = sql.replaceAll(":criterias:", criterias.toString()); 114 115 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(sql); 116 p.setString(1, SessionFacade.getUserSession().getSessionId()); 117 118 return new GenericTopicDAO().fillTopicsData(p); 119 } 120 121 private void topicsByTime(SearchData sd) throws Exception 123 { 124 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("SearchModel.searchByTime")); 125 p.setString(1, SessionFacade.getUserSession().getSessionId()); 126 p.setTimestamp(2, new Timestamp (sd.getTime().getTime())); 127 p.executeUpdate(); 128 p.close(); 129 130 this.selectTopicData(); 131 } 132 133 private void topicsByKeyword(SearchData sd) throws Exception 135 { 136 boolean isLike = "like".equals(SystemGlobals.getValue(ConfigKeys.SEARCH_WORD_MATCHING).trim()); 137 138 String sql = isLike 139 ? SystemGlobals.getSql("SearchModel.searchByLikeWord") 140 : SystemGlobals.getSql("SearchModel.searchByWord"); 141 142 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(sql); 143 144 Map eachWordMap = new HashMap (); 145 146 int maxWordSize = SystemGlobals.getIntValue(ConfigKeys.SEARCH_MAX_WORD_SIZE); 147 148 for (int i = 0; i < sd.getKeywords().length; i++) { 150 String word = sd.getKeywords()[i].toLowerCase(); 151 152 if (word.length() > maxWordSize) { 153 word = word.substring(0, maxWordSize); 155 } 156 157 if (isLike) { 158 p.setString(1, "%" + word + "%"); 159 } 160 else { 161 p.setString(1, word); 162 } 163 164 Set postsIds = new HashSet (); 165 ResultSet rs = p.executeQuery(); 166 167 while (rs.next()) { 168 postsIds.add(new Integer (rs.getInt("post_id"))); 169 } 170 171 if (postsIds.size() > 0) { 172 eachWordMap.put(sd.getKeywords()[i], postsIds); 173 } 174 } 175 176 p.close(); 177 178 180 Set postsIds = null; 183 184 if (sd.getUseAllWords()) { 185 for (Iterator iter = eachWordMap.values().iterator(); iter.hasNext(); ) { 186 if (postsIds == null) { 187 postsIds = new HashSet (eachWordMap.values().size()); 188 postsIds.addAll((HashSet )iter.next()); 189 } 190 else { 191 postsIds.retainAll((HashSet )iter.next()); 192 } 193 } 194 } 195 else { 196 postsIds = new HashSet (); 197 198 for (Iterator iter = eachWordMap.values().iterator(); iter.hasNext(); ) { 199 postsIds.addAll((HashSet )iter.next()); 200 } 201 } 202 203 if (postsIds == null || postsIds.size() == 0) { 204 return; 205 } 206 207 StringBuffer sb = new StringBuffer (1024); 209 for (Iterator iter = postsIds.iterator(); iter.hasNext(); ) { 210 sb.append(iter.next()).append(","); 211 } 212 sb.delete(sb.length() - 1, sb.length()); 213 214 sql = SystemGlobals.getSql("SearchModel.insertTopicsIds"); 216 sql = sql.replaceAll(":posts:", sb.toString()); 217 218 p = JForumExecutionContext.getConnection().prepareStatement(sql); 219 p.setString(1, SessionFacade.getUserSession().getSessionId()); 220 221 p.executeUpdate(); 222 223 this.selectTopicData(); 226 227 p.close(); 228 } 229 230 private void selectTopicData() throws Exception 231 { 232 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("SearchModel.selectTopicData")); 233 p.setString(1, SessionFacade.getUserSession().getSessionId()); 234 p.setString(2, SessionFacade.getUserSession().getSessionId()); 235 p.executeUpdate(); 236 237 p.close(); 238 } 239 240 241 244 public void cleanSearch() throws Exception 245 { 246 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("SearchModel.cleanSearchTopics")); 247 p.setString(1, SessionFacade.getUserSession().getSessionId()); 248 p.executeUpdate(); 249 p.close(); 250 251 p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("SearchModel.cleanSearchResults")); 252 p.setString(1, SessionFacade.getUserSession().getSessionId()); 253 p.executeUpdate(); 254 p.close(); 255 } 256 } 257 | Popular Tags |