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 31 jones
/**
41
 * A Class that searches a relational DB for elements and attributes that
42
 * have free text matches to the query string.  It returns a result set
43
 * consisting of the root nodeid for each document that satisfies the query
44
 */
45 27 jones
public class DBSimpleQuery {
46
47 1217 tao
  //private Connection	conn = null;
48 27 jones
49 31 jones
  /**
50
   * the main routine used to test the DBSimpleQuery utility.
51 184 jones
   * <p>
52
   * Usage: java DBSimpleQuery <query>
53 31 jones
   *
54
   * @param query the text to search for in the element and attribute content
55
   */
56 27 jones
  static public void main(String[] args) {
57
58 184 jones
     if (args.length < 2)
59 27 jones
     {
60
        System.err.println("Wrong number of arguments!!!");
61 184 jones
        System.err.println("USAGE: java DBSimpleQuery <query> <doctype>");
62 27 jones
        return;
63
     } else {
64
        try {
65
66 28 jones
          String query    = args[0];
67 154 jones
          String doctype  = args[1];
68 27 jones
69 184 jones
          // Open a connection to the database
70 1217 tao
          //Connection dbconn = util.openDBConnection();
71 27 jones
72
          // Execute the simple query
73 1217 tao
          DBSimpleQuery rd = new DBSimpleQuery();
74 154 jones
          Hashtable nodelist = null;
75
          if (doctype.equals("any") || doctype.equals("ANY")) {
76
            nodelist = rd.findDocuments(query);
77
          } else {
78
            nodelist = rd.findDocuments(query, doctype);
79
          }
80 27 jones
81
          // Print the reulting root nodes
82
          StringBuffer result = new StringBuffer();
83 150 jones
          String document = null;
84 162 bojilova
          String docid = null;
85 27 jones
          result.append("<?xml version=\"1.0\"?>\n");
86
          result.append("<resultset>\n");
87 296 higgins
  // following line removed by Dan Higgins to avoid insertion of query XML inside returned XML doc
88
  //        result.append("  <query>" + query + "</query>\n");
89 86 jones
          Enumeration doclist = nodelist.keys();
90
          while (doclist.hasMoreElements()) {
91 162 bojilova
            docid = (String)doclist.nextElement();
92 150 jones
            document = (String)nodelist.get(docid);
93
            result.append("  <document>\n    " + document +
94
                          "\n  </document>\n");
95 27 jones
          }
96 150 jones
          result.append("</resultset>\n");
97 27 jones
98
          System.out.println(result);
99
100
        } catch (Exception e) {
101 675 berkley
          System.err.println("Error in DBSimpleQuery.main");
102 27 jones
          System.err.println(e.getMessage());
103
          e.printStackTrace(System.err);
104
        }
105
     }
106
  }
107
108 31 jones
  /**
109
   * construct an instance of the DBSimpleQuery class
110
   *
111 86 jones
   * <p>Generally, one would call the findDocuments() routine after creating
112 50 jones
   * an instance to specify the search query</p>
113 31 jones
   *
114 50 jones
   * @param conn the JDBC connection that we use for the query
115 31 jones
   */
116 1217 tao
  public DBSimpleQuery()
117 27 jones
                  throws IOException,
118
                         SQLException,
119
                         ClassNotFoundException
120
  {
121 1217 tao
    //this.conn = conn;
122 27 jones
  }
123
124 31 jones
  /**
125
   * routine to search the elements and attributes looking to match query
126
   *
127
   * @param query the text to search for
128
   */
129 86 jones
  public Hashtable findDocuments(String query) {
130 154 jones
    return this.findDocuments(query, null);
131
  }
132
133
  /**
134
   * routine to search the elements and attributes looking to match query
135
   *
136
   * @param query the text to search for
137
   * @param requestedDoctype the type of documents to return from the query
138
   */
139
  public Hashtable findDocuments(String query, String requestedDoctype) {
140 86 jones
      Hashtable	 docListResult = new Hashtable();
141 27 jones
142 1217 tao
      PreparedStatement pstmt = null;
143
      DBConnection dbConn = null;
144
      int serialNumber = -1;
145 27 jones
146 150 jones
      // Now look up the document id
147 162 bojilova
      String docid = null;
148 150 jones
      String docname = null;
149
      String doctype = null;
150
      String doctitle = null;
151 167 jones
      //StringBuffer document = null;
152 150 jones
153 27 jones
      try {
154 1217 tao
        dbConn=DBConnectionPool.
155
                  getDBConnection("DBSimpleQuery.findDocuments");
156
        serialNumber=dbConn.getCheckOutSerialNumber();
157 154 jones
        if (requestedDoctype == null ||
158
            requestedDoctype.equals("any") ||
159
            requestedDoctype.equals("ANY")) {
160 1217 tao
          pstmt = dbConn.prepareStatement(
161 150 jones
                "SELECT docid,docname,doctype,doctitle " +
162
                "FROM xml_documents " +
163
                "WHERE docid IN " +
164
                "(SELECT docid " +
165
                "FROM xml_nodes WHERE nodedata LIKE ? )");
166 27 jones
167 154 jones
                // Bind the values to the query
168
                pstmt.setString(1, query);
169
        } else {
170 1217 tao
          pstmt = dbConn.prepareStatement(
171 154 jones
                "SELECT docid,docname,doctype,doctitle " +
172
                "FROM xml_documents " +
173
                "WHERE docid IN " +
174
                "(SELECT docid " +
175
                "FROM xml_nodes WHERE nodedata LIKE ? ) " +
176
                "AND doctype = ?");
177
178
                // Bind the values to the query
179
                pstmt.setString(1, query);
180
                pstmt.setString(2, requestedDoctype);
181
        }
182
183 27 jones
        pstmt.execute();
184 150 jones
        ResultSet rs = pstmt.getResultSet();
185
        boolean tableHasRows = rs.next();
186
        while (tableHasRows) {
187 162 bojilova
          docid = rs.getString(1);
188 150 jones
          docname = rs.getString(2);
189
          doctype = rs.getString(3);
190
          doctitle = rs.getString(4);
191 27 jones
192 167 jones
          StringBuffer document = new StringBuffer();
193 150 jones
          document.append("<docid>").append(docid).append("</docid>");
194 167 jones
195 150 jones
          if (docname != null) {
196
            document.append("<docname>" + docname + "</docname>");
197
          }
198
          if (doctype != null) {
199
            document.append("<doctype>" + doctype + "</doctype>");
200
          }
201
          if (doctitle != null) {
202
            document.append("<doctitle>" + doctitle + "</doctitle>");
203
          }
204 27 jones
205 150 jones
          // Store the document id and the root node id
206 162 bojilova
          docListResult.put(docid,(String)document.toString());
207 150 jones
208
          // Advance to the next record in the cursor
209
          tableHasRows = rs.next();
210 27 jones
        }
211
        pstmt.close();
212
      } catch (SQLException e) {
213 675 berkley
        System.out.println("Error in DBSimpleQuery.findDocuments: " +
214
                            e.getMessage());
215 27 jones
      }
216 1217 tao
      finally
217
      {
218
        DBConnectionPool.returnDBConnection(dbConn, serialNumber);
219
      }
220 27 jones
221 150 jones
    return docListResult;
222 27 jones
  }
223
}