Project

General

Profile

1 27 jones
/**
2 203 jones
 *  '$RCSfile$'
3
 *    Purpose: A Class that searches a relational DB for elements and
4
 *             attributes that have free text matches to the query string.
5
 *             It returns a result set consisting of the root nodeid for
6
 *             each document that satisfies the query
7
 *  Copyright: 2000 Regents of the University of California and the
8
 *             National Center for Ecological Analysis and Synthesis
9
 *    Authors: Matt Jones
10 27 jones
 *
11 203 jones
 *   '$Author$'
12
 *     '$Date$'
13
 * '$Revision$'
14 669 jones
 *
15
 * This program is free software; you can redistribute it and/or modify
16
 * it under the terms of the GNU General Public License as published by
17
 * the Free Software Foundation; either version 2 of the License, or
18
 * (at your option) any later version.
19
 *
20
 * This program is distributed in the hope that it will be useful,
21
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 * GNU General Public License for more details.
24
 *
25
 * You should have received a copy of the GNU General Public License
26
 * along with this program; if not, write to the Free Software
27
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
28 27 jones
 */
29
30 51 jones
package edu.ucsb.nceas.metacat;
31
32 27 jones
import java.io.*;
33
import java.net.URL;
34
import java.net.MalformedURLException;
35
import java.sql.*;
36
import java.util.Stack;
37
import java.util.Hashtable;
38
import java.util.Enumeration;
39
40 5015 daigle
import edu.ucsb.nceas.metacat.database.DBConnection;
41
import edu.ucsb.nceas.metacat.database.DBConnectionPool;
42
43 31 jones
/**
44
 * A Class that searches a relational DB for elements and attributes that
45
 * have free text matches to the query string.  It returns a result set
46
 * consisting of the root nodeid for each document that satisfies the query
47
 */
48 27 jones
public class DBSimpleQuery {
49
50 1217 tao
  //private Connection	conn = null;
51 27 jones
52 31 jones
  /**
53
   * the main routine used to test the DBSimpleQuery utility.
54 184 jones
   * <p>
55
   * Usage: java DBSimpleQuery <query>
56 31 jones
   *
57
   * @param query the text to search for in the element and attribute content
58
   */
59 27 jones
  static public void main(String[] args) {
60
61 184 jones
     if (args.length < 2)
62 27 jones
     {
63
        System.err.println("Wrong number of arguments!!!");
64 184 jones
        System.err.println("USAGE: java DBSimpleQuery <query> <doctype>");
65 27 jones
        return;
66
     } else {
67
        try {
68
69 28 jones
          String query    = args[0];
70 154 jones
          String doctype  = args[1];
71 27 jones
72 184 jones
          // Open a connection to the database
73 1217 tao
          //Connection dbconn = util.openDBConnection();
74 27 jones
75
          // Execute the simple query
76 1217 tao
          DBSimpleQuery rd = new DBSimpleQuery();
77 154 jones
          Hashtable nodelist = null;
78
          if (doctype.equals("any") || doctype.equals("ANY")) {
79
            nodelist = rd.findDocuments(query);
80
          } else {
81
            nodelist = rd.findDocuments(query, doctype);
82
          }
83 27 jones
84
          // Print the reulting root nodes
85
          StringBuffer result = new StringBuffer();
86 150 jones
          String document = null;
87 162 bojilova
          String docid = null;
88 27 jones
          result.append("<?xml version=\"1.0\"?>\n");
89
          result.append("<resultset>\n");
90 296 higgins
  // following line removed by Dan Higgins to avoid insertion of query XML inside returned XML doc
91
  //        result.append("  <query>" + query + "</query>\n");
92 86 jones
          Enumeration doclist = nodelist.keys();
93
          while (doclist.hasMoreElements()) {
94 162 bojilova
            docid = (String)doclist.nextElement();
95 150 jones
            document = (String)nodelist.get(docid);
96
            result.append("  <document>\n    " + document +
97
                          "\n  </document>\n");
98 27 jones
          }
99 150 jones
          result.append("</resultset>\n");
100 27 jones
101
          System.out.println(result);
102
103
        } catch (Exception e) {
104 675 berkley
          System.err.println("Error in DBSimpleQuery.main");
105 27 jones
          System.err.println(e.getMessage());
106
          e.printStackTrace(System.err);
107
        }
108
     }
109
  }
110
111 31 jones
  /**
112
   * construct an instance of the DBSimpleQuery class
113
   *
114 86 jones
   * <p>Generally, one would call the findDocuments() routine after creating
115 50 jones
   * an instance to specify the search query</p>
116 31 jones
   *
117 50 jones
   * @param conn the JDBC connection that we use for the query
118 31 jones
   */
119 1217 tao
  public DBSimpleQuery()
120 27 jones
                  throws IOException,
121
                         SQLException,
122
                         ClassNotFoundException
123
  {
124 1217 tao
    //this.conn = conn;
125 27 jones
  }
126
127 31 jones
  /**
128
   * routine to search the elements and attributes looking to match query
129
   *
130
   * @param query the text to search for
131
   */
132 86 jones
  public Hashtable findDocuments(String query) {
133 154 jones
    return this.findDocuments(query, null);
134
  }
135
136
  /**
137
   * routine to search the elements and attributes looking to match query
138
   *
139
   * @param query the text to search for
140
   * @param requestedDoctype the type of documents to return from the query
141
   */
142
  public Hashtable findDocuments(String query, String requestedDoctype) {
143 86 jones
      Hashtable	 docListResult = new Hashtable();
144 27 jones
145 1217 tao
      PreparedStatement pstmt = null;
146
      DBConnection dbConn = null;
147
      int serialNumber = -1;
148 27 jones
149 150 jones
      // Now look up the document id
150 162 bojilova
      String docid = null;
151 150 jones
      String docname = null;
152
      String doctype = null;
153
      String doctitle = null;
154 167 jones
      //StringBuffer document = null;
155 150 jones
156 27 jones
      try {
157 1217 tao
        dbConn=DBConnectionPool.
158
                  getDBConnection("DBSimpleQuery.findDocuments");
159
        serialNumber=dbConn.getCheckOutSerialNumber();
160 154 jones
        if (requestedDoctype == null ||
161
            requestedDoctype.equals("any") ||
162
            requestedDoctype.equals("ANY")) {
163 1217 tao
          pstmt = dbConn.prepareStatement(
164 150 jones
                "SELECT docid,docname,doctype,doctitle " +
165
                "FROM xml_documents " +
166
                "WHERE docid IN " +
167
                "(SELECT docid " +
168
                "FROM xml_nodes WHERE nodedata LIKE ? )");
169 27 jones
170 154 jones
                // Bind the values to the query
171
                pstmt.setString(1, query);
172
        } else {
173 1217 tao
          pstmt = dbConn.prepareStatement(
174 154 jones
                "SELECT docid,docname,doctype,doctitle " +
175
                "FROM xml_documents " +
176
                "WHERE docid IN " +
177
                "(SELECT docid " +
178
                "FROM xml_nodes WHERE nodedata LIKE ? ) " +
179
                "AND doctype = ?");
180
181
                // Bind the values to the query
182
                pstmt.setString(1, query);
183
                pstmt.setString(2, requestedDoctype);
184
        }
185
186 27 jones
        pstmt.execute();
187 150 jones
        ResultSet rs = pstmt.getResultSet();
188
        boolean tableHasRows = rs.next();
189
        while (tableHasRows) {
190 162 bojilova
          docid = rs.getString(1);
191 150 jones
          docname = rs.getString(2);
192
          doctype = rs.getString(3);
193
          doctitle = rs.getString(4);
194 27 jones
195 167 jones
          StringBuffer document = new StringBuffer();
196 150 jones
          document.append("<docid>").append(docid).append("</docid>");
197 167 jones
198 150 jones
          if (docname != null) {
199
            document.append("<docname>" + docname + "</docname>");
200
          }
201
          if (doctype != null) {
202
            document.append("<doctype>" + doctype + "</doctype>");
203
          }
204
          if (doctitle != null) {
205
            document.append("<doctitle>" + doctitle + "</doctitle>");
206
          }
207 27 jones
208 150 jones
          // Store the document id and the root node id
209 162 bojilova
          docListResult.put(docid,(String)document.toString());
210 150 jones
211
          // Advance to the next record in the cursor
212
          tableHasRows = rs.next();
213 27 jones
        }
214
        pstmt.close();
215
      } catch (SQLException e) {
216 675 berkley
        System.out.println("Error in DBSimpleQuery.findDocuments: " +
217
                            e.getMessage());
218 27 jones
      }
219 1217 tao
      finally
220
      {
221
        DBConnectionPool.returnDBConnection(dbConn, serialNumber);
222
      }
223 27 jones
224 150 jones
    return docListResult;
225 27 jones
  }
226
}