Project

General

Profile

1
/**
2
 *  '$RCSfile$'
3
 *    Purpose: A Class that searches a relational DB for elements and
4
 *             attributes that have free text matches a query string,
5
 *             or structured query matches to a path specified node in the
6
 *             XML hierarchy.  It returns a result set consisting of the
7
 *             document ID for each document that satisfies the query
8
 *  Copyright: 2000 Regents of the University of California and the
9
 *             National Center for Ecological Analysis and Synthesis
10
 *    Authors: Matt Jones
11
 *
12
 *   '$Author: berkley $'
13
 *     '$Date: 2007-04-02 15:23:45 -0700 (Mon, 02 Apr 2007) $'
14
 * '$Revision: 3219 $'
15
 *
16
 * This program is free software; you can redistribute it and/or modify
17
 * it under the terms of the GNU General Public License as published by
18
 * the Free Software Foundation; either version 2 of the License, or
19
 * (at your option) any later version.
20
 *
21
 * This program is distributed in the hope that it will be useful,
22
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24
 * GNU General Public License for more details.
25
 *
26
 * You should have received a copy of the GNU General Public License
27
 * along with this program; if not, write to the Free Software
28
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
29
 */
30

    
31
package edu.ucsb.nceas.metacat;
32

    
33
import java.io.*;
34
import java.util.*;
35

    
36
import org.w3c.dom.*;
37
import org.apache.xpath.*;
38

    
39
import org.apache.log4j.Logger;
40

    
41
/**
42
 * this class implements a metacat resultset and can be serialized to xml
43
 * for printing to the servlet output stream.
44
 */
45
public class MetacatResultSet
46
{
47
  private Logger log = Logger.getLogger(MetacatResultSet.class);
48
  private Vector results = new Vector();
49
  
50
  /**
51
   * default constructor
52
   */
53
  public MetacatResultSet()
54
  {
55
    
56
  }
57
  
58
  /**
59
   * constructor that can process a dom.  this is very slow.
60
   */
61
  public MetacatResultSet(Document d)
62
    throws Exception
63
  {
64
    log.warn("processing resultset...");
65
    NodeList nl = XPathAPI.selectNodeList(d, "//document");
66
    for(int i=0; i<nl.getLength(); i++)
67
    { //get each of the document nodes
68
      Node docNode = nl.item(i);
69
      Node docidNode = XPathAPI.selectSingleNode(docNode, "./docid");
70
      log.warn("processing " + docidNode.getFirstChild().getNodeValue());
71
      Node docnameNode = XPathAPI.selectSingleNode(docNode, "./docname");
72
      Node doctypeNode = XPathAPI.selectSingleNode(docNode, "./doctype");
73
      Node createdateNode = XPathAPI.selectSingleNode(docNode, "./createdate");
74
      Node updatedateNode = XPathAPI.selectSingleNode(docNode, "./updatedate");
75
      //process the returnfields
76
      NodeList returnfieldNL = XPathAPI.selectNodeList(docNode, "./param");
77
      Hashtable returnfieldHash = new Hashtable();
78
      for(int j=0; j<returnfieldNL.getLength(); j++)
79
      {
80
        Node returnfieldNode = returnfieldNL.item(j);
81
        Node nameNode = XPathAPI.selectSingleNode(returnfieldNode, "@name");
82
        String value = returnfieldNode.getFirstChild().getNodeValue();
83
        String name = nameNode.getNodeValue();
84
        returnfieldHash.put(name, value);
85
      }
86
      
87
      Result r = new Result(docidNode.getFirstChild().getNodeValue(),
88
                            docnameNode.getFirstChild().getNodeValue(),
89
                            doctypeNode.getFirstChild().getNodeValue(),
90
                            createdateNode.getFirstChild().getNodeValue(),
91
                            updatedateNode.getFirstChild().getNodeValue(),
92
                            returnfieldHash);
93
      addResult(r);
94
    }
95
  }
96
  
97
  /**
98
   * add a new result to the resultSet
99
   */
100
  public void addResult(Result r)
101
  {
102
    results.addElement(r);
103
  }
104
  
105
  /**
106
   * returns a vector of the results
107
   */
108
  public Vector getResults()
109
  {
110
    return results;
111
  }
112
  
113
  /**
114
   * serialize a selection of the results.  This will print the results from
115
   * start to end-1.  if end is 0, nothing will be printed.
116
   */
117
  public String serializeToXML(int start, int end)
118
  {
119
    StringBuffer sb = new StringBuffer();
120
    if(start > results.size() || end > results.size())
121
    { //make sure we don't go over the edge of the vector
122
      start = results.size() - 10;
123
      end = results.size();
124
    }
125
    
126
    for(int i=start; i<end; i++)
127
    {
128
      Result r = (Result)results.elementAt(i);
129
      sb.append(r.toString());
130
      sb.append("\n");
131
    }
132
    return sb.toString();
133
  }
134
  
135
  /**
136
   * returns an xml representation of this object
137
   */
138
  public String toString()
139
  {
140
    StringBuffer sb = new StringBuffer();
141
    for(int i=0; i<results.size(); i++)
142
    {
143
      Result r = (Result)results.elementAt(i);
144
      sb.append(r.toString());
145
      sb.append("\n");
146
    }
147
    return sb.toString();
148
  }
149
  
150
  
151
  /**
152
   * a class to store one result
153
   */
154
  public class Result
155
  {
156
    private String docid;
157
    private String docname;
158
    private String doctype;
159
    private String createDate;
160
    private String updateDate;
161
    private Hashtable returnfields;
162
    
163
    /**
164
     * constructor
165
     */
166
    public Result(String docid, String docname, String doctype, 
167
      String createDate, String updateDate, Hashtable returnfields)
168
    {
169
      this.docid = docid;
170
      this.doctype = doctype;
171
      this.createDate = createDate;
172
      this.updateDate = updateDate;
173
      this.returnfields = returnfields;
174
    }
175
    
176
    /**
177
     * returns serialized version of this result
178
     */
179
    public String toString()
180
    {
181
      StringBuffer sb = new StringBuffer();
182
      sb.append("<document>\n");
183
      sb.append("  <docid>" + docid + "</docid>\n");
184
      sb.append("  <docname>" + docname + "</docname>\n");
185
      sb.append("  <doctype>" + doctype + "</doctype>\n");
186
      sb.append("  <createdate>" + createDate + "</createdate>\n");
187
      sb.append("  <updatedate>" + updateDate + "</updatedate>\n");
188
      
189
      Enumeration keys = returnfields.keys();
190
      while(keys.hasMoreElements())
191
      {
192
        String key = (String)keys.nextElement();
193
        String value = (String)returnfields.get(key);
194
        sb.append("  <param name=\"" + key + "\">" + value + "</param>\n");
195
      }
196
      sb.append("</document>");
197
      return sb.toString();
198
    }
199
  }
200
}
(47-47/66)