1 43 package net.jforum.dao.generic; 44 45 import java.sql.Connection ; 46 import java.sql.PreparedStatement ; 47 import java.sql.ResultSet ; 48 import java.sql.SQLException ; 49 import java.sql.Statement ; 50 import java.util.ArrayList ; 51 import java.util.Arrays ; 52 import java.util.HashSet ; 53 import java.util.Iterator ; 54 import java.util.List ; 55 import java.util.Set ; 56 import java.util.StringTokenizer ; 57 import net.jforum.entities.Post; 58 import net.jforum.util.preferences.ConfigKeys; 59 import net.jforum.util.preferences.SystemGlobals; 60 import org.apache.log4j.Logger; 61 62 66 public class GenericSearchIndexerDAO extends AutoKeys implements net.jforum.dao.SearchIndexerDAO 67 { 68 private static final Logger log = Logger.getLogger(GenericSearchIndexerDAO.class); 69 70 private Connection conn; 71 72 75 public void setConnection(Connection conn) 76 { 77 this.conn = conn; 78 } 79 80 83 public void insertSearchWords(List posts) throws Exception 84 { 85 int minWordSize = SystemGlobals.getIntValue(ConfigKeys.SEARCH_MIN_WORD_SIZE); 86 int maxWordSize = SystemGlobals.getIntValue(ConfigKeys.SEARCH_MAX_WORD_SIZE); 87 int searchMaxWordsMessage = SystemGlobals.getIntValue(ConfigKeys.SEARCH_MAX_WORDS_MESSAGE); 88 String wordFilterRegex = SystemGlobals.getValue(ConfigKeys.SEARCH_WORD_FILTER_REGEX); 89 StringBuffer sb = new StringBuffer (512); 90 91 String excludeWordsString = SystemGlobals.getValue("search.exclude.words"); 93 Set excludeWords = new HashSet (); 94 if (excludeWordsString != null) { 95 String [] words = excludeWordsString.split(","); 96 for (int i = 0; words != null && i < words.length; i++) { 97 excludeWords.add(words[i].trim()); 98 } 99 } 100 101 String matchSql = SystemGlobals.getSql("SearchModel.associateWordToPost"); 102 PreparedStatement words = this.conn.prepareStatement(SystemGlobals.getSql("SearchModel.insertWords")); 103 104 for (Iterator iter = posts.iterator(); iter.hasNext(); ) { 105 Post p = (Post)iter.next(); 106 107 String text = new StringBuffer (p.getText()).append(" ") 108 .append(p.getSubject()).toString(); 109 110 text = text.toLowerCase().replaceAll("[\\.\\\\\\/~\\^\\&\\(\\)\\-_+=!@;#\\$%\"\'\\[\\]\\{\\}\\?<\\:>,\\*\n\r\t]", " "); 111 112 Set allWords = new HashSet (); 113 114 sb.delete(0, sb.length()); 115 116 StringTokenizer st = new StringTokenizer (text, " "); 117 118 while (st.hasMoreTokens() && 120 (searchMaxWordsMessage < 1 || allWords.size() < searchMaxWordsMessage)) { 121 String w = st.nextToken().trim(); 122 123 if (w.length() < minWordSize) { 124 continue; 125 } 126 else if (w.length() > maxWordSize) { 127 w = w.substring(0, maxWordSize); 128 } 129 130 if (!allWords.contains(w) && !excludeWords.contains(w) && 131 (wordFilterRegex == null || w.matches(wordFilterRegex))) { 132 allWords.add(w); 133 sb.append('\'').append(w).append('\'').append(","); 134 } 135 } 136 137 String in = sb.substring(0, sb.length() - 1); 138 139 String sql = SystemGlobals.getSql("SearchModel.selectExistingWords"); 140 sql = sql.replaceAll("#IN#", in); 141 142 Statement s = this.conn.createStatement(); 143 ResultSet rs = s.executeQuery(sql); 144 145 List newWords = new ArrayList (); 146 147 while (rs.next()) { 148 newWords.add(rs.getString("word")); 149 } 150 151 rs.close(); 152 s.close(); 153 154 allWords.removeAll(newWords); 155 156 for (Iterator witer = allWords.iterator(); witer.hasNext(); ) { 158 String ww = (String )witer.next(); 159 160 words.setString(1, ww); 161 words.setInt(2, ww.hashCode()); 162 163 try { 164 words.executeUpdate(); 165 } 166 catch (SQLException e) { 167 log.error("Cannot index word: \"" + ww + "\"", e); 168 throw e; 169 } 170 } 171 172 sql = matchSql.replaceAll("#ID#", String.valueOf(p.getId())).replaceAll("#IN#", in); 173 174 Statement match = this.conn.createStatement(); 175 match.executeUpdate(sql); 176 match.close(); 177 } 178 179 words.close(); 180 } 181 182 185 public void insertSearchWords(final Post post) throws Exception 186 { 187 this.insertSearchWords(Arrays.asList(new Post[] { post })); 188 } 189 } 190 | Popular Tags |