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

Quick Search    Search Deep

Source code: com/RuntimeCollective/webapps/SearchResults.java


1   /* $Header: /home/CVS/rjp/src/com/RuntimeCollective/webapps/SearchResults.java,v 1.7 2003/09/30 15:13:04 joe Exp $
2    * $Revision: 1.7 $
3    * $Date: 2003/09/30 15:13:04 $
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.webapps;
31  
32  import com.RuntimeCollective.webapps.bean.EntityBean;
33  import com.RuntimeCollective.webapps.bean.SearchResult;
34  
35  import java.io.Serializable;
36  import java.util.ArrayList;
37  import java.util.Collection;
38  import java.util.Enumeration;
39  import java.util.List;
40  import java.util.Iterator;
41  import java.util.Vector;
42  
43  /** A wrapper for holding a vector of search results.
44   *
45   * @version $Id: SearchResults.java,v 1.7 2003/09/30 15:13:04 joe Exp $
46   */
47  public class SearchResults extends Vector implements Serializable {
48  
49    /** Alternative `get' method that returns object as a SearchResult. */
50    public SearchResult getResult( int index ) {
51      return (SearchResult) super.get(index);
52    }
53  
54    public String toString() {
55      String str = "Search Results, length = " + this.size() + "\n";
56       Enumeration e = elements();
57       while(e.hasMoreElements()) {
58         SearchResult result = (SearchResult) e.nextElement();
59         str.concat(result + "\n");
60      }
61  
62      RuntimeParameters.logDebug(this, "[SearchResults:toString] This = " + super.toString());
63  
64      return str;
65    }
66  
67    /**
68     * Converts a <code>SearchResults</code> into a <code>Collection</code>
69     * containing the full objects found in the search.
70     *
71     * @param sr The <code>SearchResults</code>.
72     * @param type The class of the objects that were searched for.
73     * @return The <code>Collection</code> containing the objects. An empty 
74     *         collection is returned it there are no results or sr is null.
75     */
76    public static Collection toFullObjectCollection(SearchResults sr, 
77                                                    String type) {
78      if (null == sr) { 
79        return new Vector(0);
80      }
81  
82      if (null == type || "".equals(type)) {
83        throw new IllegalArgumentException("You must specify a type!");
84      }
85  
86      Collection results = new Vector(sr.size());
87      
88      for (Iterator it = sr.iterator(); it.hasNext(); ) {
89        SearchResult item = (SearchResult) it.next();
90        Object o = RuntimeParameters.getStore().get(type, item.getId());
91  
92        if (o != null) {
93          results.add(o);
94        }
95      }
96  
97      return results;
98    }
99  
100     /** FR: No you don't need to specify a type. */
101     public List getEntityBeanList() {
102         List result = new ArrayList(size());
103         EntityBean bean;
104         for (int i=0; i<size(); i++) {
105             bean = RuntimeParameters.getStore().get(EntityBean.class.getName(), ((SearchResult) get(i)).getId());
106             if (bean != null)
107                 result.add(bean);
108         }
109         return result;
110     }
111 }