Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/RuntimeCollective/search/bean/Search.java


1   /* $Header: /home/CVS/rjp/src/com/RuntimeCollective/search/bean/Search.java,v 1.10 2003/09/30 15:12:57 joe Exp $
2    * $Revision: 1.10 $
3    * $Date: 2003/09/30 15:12:57 $
4    *
5    * ====================================================================
6    *
7    * Josephine : http://www.runtime-collective.com/josephine/index.html
8    *
9    * Copyright (C) 2003 Runtime Collective
10   * 
11   * This product includes software developed by the
12   * Apache Software Foundation (http://www.apache.org/).
13   *
14   * This library is free software; you can redistribute it and/or
15   * modify it under the terms of the GNU Lesser General Public
16   * License as published by the Free Software Foundation; either
17   * version 2.1 of the License, or (at your option) any later version.
18   *
19   * This library is distributed in the hope that it will be useful,
20   * but WITHOUT ANY WARRANTY; without even the implied warranty of
21   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
22   * Lesser General Public License for more details.
23   *
24   * You should have received a copy of the GNU Lesser General Public
25   * License along with this library; if not, write to the Free Software
26   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
27   *
28   */
29  
30  package com.RuntimeCollective.search.bean;
31  
32  import com.RuntimeCollective.search.SearchException;
33  import com.RuntimeCollective.search.bean.SearchCriterion;
34  import com.RuntimeCollective.search.SearchIndex;
35  import com.RuntimeCollective.search.bean.SearchResults;
36  import com.RuntimeCollective.webapps.RuntimeParameters;
37  import com.RuntimeCollective.webapps.RuntimeDataSource;
38  
39  import java.util.Iterator;
40  import java.util.Vector;
41  import java.util.Calendar;
42  import java.sql.SQLException;
43  
44  /**
45   * Represents a search query made by a user.
46   * A search is performed by creating this <code>Search</code> object, adding criteria
47   * using <code>addCriterion()<code>, then calling <code>getResults()</code>.
48   *
49   * @author Joe Holmberg
50   * @version $Id: Search.java,v 1.10 2003/09/30 15:12:57 joe Exp $
51   */
52  public class Search {
53  
54      private Vector criteria;
55      private SearchIndex searchIndex;
56  
57      /**
58       * Make a new search.
59       */
60      public Search() {
61    criteria = new Vector();
62    searchIndex = (SearchIndex)RuntimeParameters.getSearchIndex();
63      }
64  
65      /**
66       * Adds a criterion to this search.
67       */
68      public void addCriterion(SearchCriterion sc) {
69    criteria.add(sc);
70      }
71  
72      /**
73       * Removes a criterion from this search.
74       */
75      public void removeCriterion(SearchCriterion sc) {
76    criteria.remove(sc);
77      }
78  
79      /**
80       * Gets an Iterator of <code>SearchCriterion</code> objects, that have been added to this search
81       */
82      public Iterator getCriteria() {
83    return criteria.iterator();
84      }
85  
86      public SearchCriterion[] getCriteriaAsArray() {
87    SearchCriterion[] sc = new SearchCriterion[criteria.size()];
88    return (SearchCriterion[])criteria.toArray(sc);
89      }
90  
91  
92  
93      /** When no max number of results is set. */
94      public static int NO_MAX_NO_RESULTS = -1;
95  
96      /** The max number of results that you want to get, NO_MAX_NO_RESULTS if no limit. */
97      protected int maxNoResults = NO_MAX_NO_RESULTS;
98  
99      /** Set the max number of results. */
100     public void setMaxNoResults(int no) {
101   maxNoResults = no;
102     }
103 
104     /** Get the max number of results. */
105     public int getMaxNoResults() {
106   return maxNoResults;
107     }
108 
109     /** Say we don't want to have any max number of results for this query. */
110     public void setNoMaxNoResults() {
111   maxNoResults = NO_MAX_NO_RESULTS;
112     }
113 
114 
115 
116     /** Whether to use AND (the default) or OR to join the criteria. */
117     protected boolean useAndNotOr = true;
118 
119     /** Set the max number of results. */
120     public void setUseAndNotOr(boolean uano) {
121         useAndNotOr = uano;
122     }
123 
124     /** Get the max number of results. */
125     public boolean getUseAndNotOr() {
126         return useAndNotOr;
127     }
128 
129 
130     /*
131      * Returns all the matching Results for the criteria that have been set
132      */
133     public SearchResults getResults() throws SearchException {
134   boolean doLog = true;
135   try {
136       if (RuntimeParameters.get("logSearchCriteria").equals("false"))
137     doLog = false;
138   } catch (Exception e) {
139       // parameter probably not set
140   }
141   if (doLog)
142       logCriteria();
143   return searchIndex.performSearch(this, getMaxNoResults());
144     }
145 
146   /** 
147    * Log the search criteria in the database
148    */
149   private void logCriteria() throws SearchException {
150     
151     // Concatenate the criteria
152     StringBuffer cc = new StringBuffer();
153     boolean hadCriteria = false;
154 
155     for (Iterator allCriteria = getCriteria(); allCriteria.hasNext(); ) {
156       hadCriteria = true;
157 
158       SearchCriterion sc = (SearchCriterion) allCriteria.next();
159       cc.append(sc.getCriterion()).append(" ");
160     }
161 
162     if (hadCriteria) {
163       cc = cc.deleteCharAt(cc.length()-1);
164     } // end of if ()
165     
166     // Save this string to the database, with today's date
167     StringBuffer updateStringBuffer = (new StringBuffer(115)).append("insert into search_keyword (keyword, performed_on) values ('").append(RuntimeDataSource.escape(cc.toString())).append("', ").append(RuntimeDataSource.toSqlString(Calendar.getInstance().getTime())).append(")");
168     //RuntimeParameter.logDebug(this, "LOGGING SEARCH TERMS with: "+updateStringBuffer.toString());
169     try {
170       RuntimeDataSource.update(updateStringBuffer.toString());
171     } catch (SQLException e) {
172       throw new SearchException("Couldn't update search logging table search_keyword: "+e);
173     }
174   }
175 }