Project

General

Profile

1
/**
2
 *      Name: DBQuery.java
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
 *
11
 *   Version: '$Id: DBQuery.java 155 2000-06-14 20:54:13Z jones $'
12
 */
13

    
14
package edu.ucsb.nceas.metacat;
15

    
16
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
/** 
25
 * A Class that searches a relational DB for elements and attributes that
26
 * have structured 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
public class DBQuery {
30

    
31
  static  String 	defaultDB = "jdbc:oracle:thin:@localhost:1521:test";
32
  private Connection	conn = null;
33

    
34
  /**
35
   * the main routine used to test the DBQuery utility.
36
   *
37
   * Usage: java DBQuery <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
  static public void main(String[] args) {
45
     
46
     if (args.length < 4)
47
     {
48
        System.err.println("Wrong number of arguments!!!");
49
        System.err.println("USAGE: java DBQuery " +
50
                           "<query> <doctype> <user> <password> [dbstring]");
51
        return;
52
     } else {
53
        try {
54
                    
55
          String query    = args[0];
56
          String doctype  = args[1];
57
          String user     = args[2];
58
          String password = args[3];
59
          String dbstring = null;
60

    
61
          if (args.length <= 4) {
62
            dbstring = defaultDB;
63
          } else {
64
            dbstring = args[4];
65
          }
66

    
67
          // Open a connection to the database
68
          Connection dbconn = MetaCatUtil.openDBConnection( 
69
                              "oracle.jdbc.driver.OracleDriver",
70
                              dbstring, user, password);
71
          // Execute the simple query
72
          DBQuery rd = new DBQuery(dbconn);
73
          Hashtable nodelist = null;
74
          nodelist = rd.findDocuments(query);
75

    
76
          // Print the reulting root nodes
77
          StringBuffer result = new StringBuffer();
78
          String document = null;
79
          Long docid = null;
80
          result.append("<?xml version=\"1.0\"?>\n");
81
          result.append("<resultset>\n");
82
          result.append("  <query>" + query + "</query>\n");
83
          Enumeration doclist = nodelist.keys(); 
84
          while (doclist.hasMoreElements()) {
85
            docid = (Long)doclist.nextElement();
86
            document = (String)nodelist.get(docid);
87
            result.append("  <document>\n    " + document + 
88
                          "\n  </document>\n");
89
          }
90
          result.append("</resultset>\n");
91

    
92
          System.out.println(result);
93

    
94
        } catch (Exception e) {
95
          System.err.println("EXCEPTION HANDLING REQUIRED");
96
          System.err.println(e.getMessage());
97
          e.printStackTrace(System.err);
98
        }
99
     }
100
  }
101
  
102
  /**
103
   * construct an instance of the DBQuery class 
104
   *
105
   * <p>Generally, one would call the findDocuments() routine after creating 
106
   * an instance to specify the search query</p>
107
   *
108
   * @param conn the JDBC connection that we use for the query
109
   */
110
  public DBQuery( Connection conn ) 
111
                  throws IOException, 
112
                         SQLException, 
113
                         ClassNotFoundException
114
  {
115
    this.conn = conn;
116
  }
117
  
118
  /** 
119
   * routine to search the elements and attributes looking to match query
120
   *
121
   * @param queryspec the xml serialization of the query
122
   * @param requestedDoctype the type of documents to return from the query
123
   */
124
  public Hashtable findDocuments(String queryspec) {
125
      Hashtable	 docListResult = new Hashtable();
126

    
127

    
128
      //QuerySpecification qspec = new QuerySpecification(queryspec);
129

    
130
 
131
/*
132
      PreparedStatement pstmt;
133

    
134
      // Now look up the document id
135
      long docid = 0;
136
      String docname = null;
137
      String doctype = null;
138
      String doctitle = null;
139
      StringBuffer document = null; 
140

    
141
      try {
142
        if (requestedDoctype == null || 
143
            requestedDoctype.equals("any") || 
144
            requestedDoctype.equals("ANY")) {
145
          pstmt = conn.prepareStatement(
146
                "SELECT docid,docname,doctype,doctitle " +
147
                "FROM xml_documents " +
148
                "WHERE docid IN " +
149
                "(SELECT docid " +
150
                "FROM xml_nodes WHERE nodedata LIKE ? )");
151

    
152
                // Bind the values to the query
153
                pstmt.setString(1, query);
154
        } else {
155
          pstmt = conn.prepareStatement(
156
                "SELECT docid,docname,doctype,doctitle " +
157
                "FROM xml_documents " +
158
                "WHERE docid IN " +
159
                "(SELECT docid " +
160
                "FROM xml_nodes WHERE nodedata LIKE ? ) " +
161
                "AND doctype = ?");
162

    
163
                // Bind the values to the query
164
                pstmt.setString(1, query);
165
                pstmt.setString(2, requestedDoctype);
166
        }
167

    
168
        pstmt.execute();
169
        ResultSet rs = pstmt.getResultSet();
170
        boolean tableHasRows = rs.next();
171
        while (tableHasRows) {
172
          docid = rs.getLong(1);
173
          docname = rs.getString(2);
174
          doctype = rs.getString(3);
175
          doctitle = rs.getString(4);
176

    
177
          document = new StringBuffer();
178
          document.append("<docid>").append(docid).append("</docid>");
179
          if (docname != null) {
180
            document.append("<docname>" + docname + "</docname>");
181
          }
182
          if (doctype != null) {
183
            document.append("<doctype>" + doctype + "</doctype>");
184
          }
185
          if (doctitle != null) {
186
            document.append("<doctitle>" + doctitle + "</doctitle>");
187
          }
188

    
189
          // Store the document id and the root node id
190
          docListResult.put(new Long(docid),(String)document.toString());
191

    
192
          // Advance to the next record in the cursor
193
          tableHasRows = rs.next();
194
        }
195
        pstmt.close();
196
      } catch (SQLException e) {
197
        System.out.println("Error getting id: " + e.getMessage());
198
      }
199
*/
200
    return docListResult;
201
  }
202
}
(5-5/20)