Project

General

Profile

1 27 jones
/**
2
 *        Name: DBSimpleQuery.java
3 31 jones
 *     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 27 jones
 *              each document that satisfies the query
7 35 jones
 *   Copyright: 2000 Regents of the University of California and the
8
 *              National Center for Ecological Analysis and Synthesis
9 27 jones
 *     Authors: Matt Jones
10
 *
11
 *     Version: '$Id$'
12
 */
13
14 51 jones
package edu.ucsb.nceas.metacat;
15
16 27 jones
import java.io.*;
17
import java.net.URL;
18
import java.net.MalformedURLException;
19
import java.sql.*;
20
import java.util.Stack;
21
import java.util.Hashtable;
22
import java.util.Enumeration;
23
24 31 jones
/**
25
 * A Class that searches a relational DB for elements and attributes that
26
 * have free text matches to the query string.  It returns a result set
27
 * consisting of the root nodeid for each document that satisfies the query
28
 */
29 27 jones
public class DBSimpleQuery {
30
31
  static  String 	defaultDB = "jdbc:oracle:thin:@localhost:1521:test";
32
  private Connection	conn = null;
33
34 31 jones
  /**
35
   * the main routine used to test the DBSimpleQuery utility.
36
   *
37
   * Usage: java DBSimpleQuery <query> <user> <password> [dbstring]
38
   *
39
   * @param query the text to search for in the element and attribute content
40
   * @param user the username to use for the database connection
41
   * @param password the password to use for the database connection
42
   * @param dbstring the connection info to use for the database connection
43
   */
44 27 jones
  static public void main(String[] args) {
45
46 154 jones
     if (args.length < 4)
47 27 jones
     {
48
        System.err.println("Wrong number of arguments!!!");
49
        System.err.println("USAGE: java DBSimpleQuery " +
50 154 jones
                           "<query> <doctype> <user> <password> [dbstring]");
51 27 jones
        return;
52
     } else {
53
        try {
54
55 28 jones
          String query    = args[0];
56 154 jones
          String doctype  = args[1];
57
          String user     = args[2];
58
          String password = args[3];
59 27 jones
          String dbstring = null;
60
61 154 jones
          if (args.length <= 4) {
62 27 jones
            dbstring = defaultDB;
63
          } else {
64 154 jones
            dbstring = args[4];
65 27 jones
          }
66
67 50 jones
          // Open a connection to the database
68
          Connection dbconn = MetaCatUtil.openDBConnection(
69
                              "oracle.jdbc.driver.OracleDriver",
70
                              dbstring, user, password);
71 27 jones
          // Execute the simple query
72 50 jones
          DBSimpleQuery rd = new DBSimpleQuery(dbconn);
73 154 jones
          Hashtable nodelist = null;
74
          if (doctype.equals("any") || doctype.equals("ANY")) {
75
            nodelist = rd.findDocuments(query);
76
          } else {
77
            nodelist = rd.findDocuments(query, doctype);
78
          }
79 27 jones
80
          // Print the reulting root nodes
81
          StringBuffer result = new StringBuffer();
82 150 jones
          String document = null;
83
          Long docid = null;
84 27 jones
          result.append("<?xml version=\"1.0\"?>\n");
85
          result.append("<resultset>\n");
86 150 jones
          result.append("  <query>" + query + "</query>\n");
87 86 jones
          Enumeration doclist = nodelist.keys();
88
          while (doclist.hasMoreElements()) {
89 150 jones
            docid = (Long)doclist.nextElement();
90
            document = (String)nodelist.get(docid);
91
            result.append("  <document>\n    " + document +
92
                          "\n  </document>\n");
93 27 jones
          }
94 150 jones
          result.append("</resultset>\n");
95 27 jones
96
          System.out.println(result);
97
98
        } catch (Exception e) {
99
          System.err.println("EXCEPTION HANDLING REQUIRED");
100
          System.err.println(e.getMessage());
101
          e.printStackTrace(System.err);
102
        }
103
     }
104
  }
105
106 31 jones
  /**
107
   * construct an instance of the DBSimpleQuery class
108
   *
109 86 jones
   * <p>Generally, one would call the findDocuments() routine after creating
110 50 jones
   * an instance to specify the search query</p>
111 31 jones
   *
112 50 jones
   * @param conn the JDBC connection that we use for the query
113 31 jones
   */
114 50 jones
  public DBSimpleQuery( Connection conn )
115 27 jones
                  throws IOException,
116
                         SQLException,
117
                         ClassNotFoundException
118
  {
119 50 jones
    this.conn = conn;
120 27 jones
  }
121
122 31 jones
  /**
123
   * routine to search the elements and attributes looking to match query
124
   *
125
   * @param query the text to search for
126
   */
127 86 jones
  public Hashtable findDocuments(String query) {
128 154 jones
    return this.findDocuments(query, null);
129
  }
130
131
  /**
132
   * routine to search the elements and attributes looking to match query
133
   *
134
   * @param query the text to search for
135
   * @param requestedDoctype the type of documents to return from the query
136
   */
137
  public Hashtable findDocuments(String query, String requestedDoctype) {
138 86 jones
      Hashtable	 docListResult = new Hashtable();
139 27 jones
140
      PreparedStatement pstmt;
141
142 150 jones
      // Now look up the document id
143
      long docid = 0;
144
      String docname = null;
145
      String doctype = null;
146
      String doctitle = null;
147
      StringBuffer document = null;
148
149 27 jones
      try {
150 154 jones
        if (requestedDoctype == null ||
151
            requestedDoctype.equals("any") ||
152
            requestedDoctype.equals("ANY")) {
153
          pstmt = conn.prepareStatement(
154 150 jones
                "SELECT docid,docname,doctype,doctitle " +
155
                "FROM xml_documents " +
156
                "WHERE docid IN " +
157
                "(SELECT docid " +
158
                "FROM xml_nodes WHERE nodedata LIKE ? )");
159 27 jones
160 154 jones
                // Bind the values to the query
161
                pstmt.setString(1, query);
162
        } else {
163
          pstmt = conn.prepareStatement(
164
                "SELECT docid,docname,doctype,doctitle " +
165
                "FROM xml_documents " +
166
                "WHERE docid IN " +
167
                "(SELECT docid " +
168
                "FROM xml_nodes WHERE nodedata LIKE ? ) " +
169
                "AND doctype = ?");
170
171
                // Bind the values to the query
172
                pstmt.setString(1, query);
173
                pstmt.setString(2, requestedDoctype);
174
        }
175
176 27 jones
        pstmt.execute();
177 150 jones
        ResultSet rs = pstmt.getResultSet();
178
        boolean tableHasRows = rs.next();
179
        while (tableHasRows) {
180
          docid = rs.getLong(1);
181
          docname = rs.getString(2);
182
          doctype = rs.getString(3);
183
          doctitle = rs.getString(4);
184 27 jones
185 150 jones
          document = new StringBuffer();
186
          document.append("<docid>").append(docid).append("</docid>");
187
          if (docname != null) {
188
            document.append("<docname>" + docname + "</docname>");
189
          }
190
          if (doctype != null) {
191
            document.append("<doctype>" + doctype + "</doctype>");
192
          }
193
          if (doctitle != null) {
194
            document.append("<doctitle>" + doctitle + "</doctitle>");
195
          }
196 27 jones
197 150 jones
          // Store the document id and the root node id
198
          docListResult.put(new Long(docid),(String)document.toString());
199
200
          // Advance to the next record in the cursor
201
          tableHasRows = rs.next();
202 27 jones
        }
203
        pstmt.close();
204
      } catch (SQLException e) {
205
        System.out.println("Error getting id: " + e.getMessage());
206
      }
207
208 150 jones
    return docListResult;
209 27 jones
  }
210
}