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