1 41 package com.mvnforum.search.company; 42 43 import java.io.IOException ; 44 import java.sql.Timestamp ; 45 import java.util.ArrayList ; 46 import java.util.Collection ; 47 48 import net.myvietnam.mvncore.exception.DatabaseException; 49 import net.myvietnam.mvncore.exception.ObjectNotFoundException; 50 51 import org.apache.commons.logging.Log; 52 import org.apache.commons.logging.LogFactory; 53 import org.apache.lucene.analysis.Analyzer; 54 import org.apache.lucene.document.Document; 55 import org.apache.lucene.queryParser.ParseException; 56 import org.apache.lucene.queryParser.QueryParser; 57 import org.apache.lucene.search.*; 58 59 import com.mvnforum.MVNForumConfig; 60 import com.mvnforum.db.CompanyBean; 61 import com.mvnforum.db.DAOFactory; 62 63 76 public class CompanySearchQuery 77 { 78 public static final int SEARCH_ANY_DATE = 0; 79 80 public static final int SEARCH_NEWER = 1; 81 public static final int SEARCH_OLDER = 2; 82 83 private static Log log = LogFactory.getLog(CompanySearchQuery.class); 84 85 private int companyID = -1; 86 private String companyNameKey = null; 87 private String companyAddressKey = null; 88 private Timestamp companyDateKey = null; 89 private Timestamp fromCompanyDateKey = null; 90 private Timestamp toCompanyDateKey = null; 91 92 private int hitCount = 0; 93 private Collection searchResult = null; 94 95 public CompanySearchQuery() { 96 } 97 98 102 public void setCompanyId(int companyId) { 103 this.companyID = companyId; 104 } 105 106 110 public void setCompanyAddressKey(String companyAddressKey) { 111 this.companyAddressKey = companyAddressKey; 112 } 113 114 118 public void setCompanyNameKey(String companyNameKey) { 119 this.companyNameKey = companyNameKey; 120 } 121 122 public void setCompanyDateKey(Timestamp companyDateKey) { 123 this.companyDateKey = companyDateKey; 124 } 125 126 public void setFromCompanyDateKey(Timestamp fromCompanyDateKey) { 127 this.fromCompanyDateKey = fromCompanyDateKey; 128 } 129 130 public void setToCompanyDateKey(Timestamp toCompanyDateKey) { 131 this.toCompanyDateKey = toCompanyDateKey; 132 } 133 134 protected IndexSearcher getSearcher() throws IOException { 135 try { 136 IndexSearcher searcher = new IndexSearcher(MVNForumConfig.getSearchCompanyIndexDir()); 137 return searcher; 138 } catch (IOException ex) { 139 log.error("Cannot access the lucene search index for query. Please check if you have configed mvnForumHome properly. You can also go to Admin Zone to rebuild the Lucene index files.", ex); 142 throw new IOException ("Cannot access the lucene search index. Please report this error to web site Administrator (check mvnForumHome or rebuild Lucene index)."); 143 } 144 } 145 146 public void searchDocuments(int offset, int rowsToReturn) 147 throws IOException , DatabaseException, ObjectNotFoundException { 148 BooleanQuery query = new BooleanQuery(); try { 152 Query companyNameQuery = getCompanyNameQuery(); 153 if (companyNameQuery != null) { 154 query.add(companyNameQuery, true, false); 155 log.debug("companyNameQuery = " + companyNameQuery); 156 } 157 Query companyAddressQuery = getCompanyAddressQuery(); 158 if (companyAddressQuery != null) { 159 query.add(companyAddressQuery, true, false); 160 log.debug("companyAddressQuery = " + companyAddressQuery); 161 } 162 163 167 } catch (ParseException pe) { 168 log.error("Cannot parse the search query", pe); 169 } 170 log.debug("[OK ] booleanQuery = " + query); 171 172 DateFilter dateFilter = null; 173 if (fromCompanyDateKey != null && toCompanyDateKey != null) { 174 dateFilter = new DateFilter(CompanyIndexer.FIELD_CREATION_DATE, fromCompanyDateKey, toCompanyDateKey ); 175 } else if (fromCompanyDateKey != null) { 176 dateFilter = DateFilter.After(CompanyIndexer.FIELD_CREATION_DATE, fromCompanyDateKey); 177 } else if (toCompanyDateKey != null) { 178 dateFilter = DateFilter.Before(CompanyIndexer.FIELD_CREATION_DATE, toCompanyDateKey ); 179 } 180 181 IndexSearcher searcher = null; 183 try { 184 searcher = getSearcher(); 185 186 Hits companyHits = null; 188 if (dateFilter != null) { 190 companyHits = searcher.search(query, dateFilter); 191 } else { 192 companyHits = searcher.search(query); 193 } 194 hitCount = companyHits.length(); 195 log.debug("[ HIT COUNT ]" + hitCount); 196 searchResult = getCompanies(companyHits, offset, rowsToReturn); 197 } catch (IOException ex) { 198 throw ex; 199 } finally { 200 try { 201 if (searcher != null) { 202 searcher.close(); 203 } 204 } catch (Exception ex) {} 205 } 206 } 207 208 public int getHitCount() { 209 return hitCount; 210 } 211 212 public Collection getCompanyResult() { 213 return searchResult; 214 } 215 216 private Collection getCompanies(Hits companyHits, int offset, int rowsToReturn) 217 throws IOException , ObjectNotFoundException, DatabaseException { 218 219 if (offset < 0) throw new IllegalArgumentException ("The offset < 0 is not allowed."); 220 if (rowsToReturn <= 0) throw new IllegalArgumentException ("The rowsToReturn <= 0 is not allowed."); 221 222 ArrayList retValue = new ArrayList (hitCount); 224 225 for (int i = offset; (i < offset + rowsToReturn) && (i < hitCount); i++) { 226 Document companyDocument = companyHits.doc(i); 227 int currentCompanyID = Integer.parseInt(companyDocument.get(CompanyIndexer.FIELD_COMPANY_ID)); 228 CompanyBean companyBean = DAOFactory.getCompanyDAO().getCompany(currentCompanyID); 229 retValue.add(companyBean); 230 } 231 return retValue; 232 } 233 234 private Query getCompanyNameQuery() throws ParseException { 235 if (companyNameKey == null) { 236 return null; 237 } 238 Analyzer analyzer = CompanyIndexer.getAnalyzer(); 239 Query companyNameQuery = QueryParser.parse(companyNameKey, 240 CompanyIndexer.FIELD_COMPANY_NAME, 241 analyzer); 242 return companyNameQuery; 243 } 244 245 private Query getCompanyAddressQuery() throws ParseException { 246 if (companyAddressKey == null || companyAddressKey.equals("")) { 247 return null; 248 } 249 Analyzer analyzer = CompanyIndexer.getAnalyzer(); 250 Query companyAddressQuery = QueryParser.parse(companyAddressKey, 251 CompanyIndexer.FIELD_COMPANY_ADDRESS, 252 analyzer); 253 return companyAddressQuery; 254 } 255 267 } 268 | Popular Tags |