Project

General

Profile

1
/**
2
 *        Name: DBSimpleQuery.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: DBSimpleQuery.java 98 2000-05-13 01:29:51Z 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 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
public class DBSimpleQuery {
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 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
  static public void main(String[] args) {
45
     
46
     if (args.length < 3)
47
     {
48
        System.err.println("Wrong number of arguments!!!");
49
        System.err.println("USAGE: java DBSimpleQuery " +
50
                           "<query> <user> <password> [dbstring]");
51
        return;
52
     } else {
53
        try {
54
                    
55
          String query    = args[0];
56
          String user     = args[1];
57
          String password = args[2];
58
          String dbstring = null;
59

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

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

    
74
          // Print the reulting root nodes
75
          StringBuffer result = new StringBuffer();
76
          long docid = 0;
77
          result.append("<?xml version=\"1.0\"?>\n");
78
          result.append("<resultset>\n");
79
          Enumeration doclist = nodelist.keys(); 
80
          while (doclist.hasMoreElements()) {
81
            docid = ((Long)doclist.nextElement()).longValue();
82
            result.append("  <docid>").append(docid).append("</docid>\n");
83
          }
84
          result.append("</resultset>");
85

    
86
          System.out.println(result);
87

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

    
122
      PreparedStatement pstmt;
123
      long nodeid;
124

    
125
      try {
126
        pstmt =
127
          conn.prepareStatement("SELECT nodeid " +
128
                  "FROM xml_nodes WHERE nodedata LIKE ?");
129
        // Bind the values to the query
130
        pstmt.setString(1, query);
131

    
132
        pstmt.execute();
133
        try {
134
          ResultSet rs = pstmt.getResultSet();
135
          try {
136
            boolean tableHasRows = rs.next();
137
            while (tableHasRows) {
138
              try {
139
                nodeid = rs.getLong(1);
140
                nodeListResult.put(new Long(nodeid),new Long(nodeid));
141

    
142
              } catch (SQLException e) {
143
                System.out.println("Error with getInt: " + e.getMessage());
144
              }
145

    
146
              // Advance to the next record in the cursor
147
              tableHasRows = rs.next();
148
            }
149
          } catch (SQLException e) {
150
            System.out.println("Error with next: " + e.getMessage());
151
          }
152
        } catch (SQLException e) {
153
          System.out.println("Error with getrset: " + e.getMessage());
154
        }
155
        pstmt.close();
156
      } catch (SQLException e) {
157
        System.out.println("Error getting id: " + e.getMessage());
158
      }
159

    
160
      Enumeration nodelist = nodeListResult.keys(); 
161
      while (nodelist.hasMoreElements()) {
162
        nodeid = ((Long)nodelist.nextElement()).longValue();
163

    
164
        try {
165
          pstmt =
166
            conn.prepareStatement("SELECT nodeid " +
167
                    "FROM xml_nodes START WITH nodeid = ? " +
168
                    "CONNECT BY nodeid = PRIOR parentnodeid");
169
          // Bind the values to the query
170
          pstmt.setLong(1, nodeid);
171
  
172
          pstmt.execute();
173
          try {
174
            ResultSet rs = pstmt.getResultSet();
175
            try {
176
              boolean tableHasRows = rs.next();
177
              while (tableHasRows) {
178
                try {
179
                  nodeid = rs.getLong(1);
180
                } catch (SQLException e) {
181
                  System.out.println("Error with getInt: " + e.getMessage());
182
                }
183

    
184
                // Advance to the next record in the cursor
185
                tableHasRows = rs.next();
186
                
187
              }
188
            } catch (SQLException e) {
189
              System.out.println("Error with next: " + e.getMessage());
190
            }
191
          } catch (SQLException e) {
192
            System.out.println("Error with getrset: " + e.getMessage());
193
          }
194
          pstmt.close();
195
        } catch (SQLException e) {
196
          System.out.println("Error getting id: " + e.getMessage());
197
        }
198

    
199
        // Record that the last record should be the root
200
        rootListResult.put(new Long(nodeid),new Long(nodeid));
201
      } 
202

    
203
      // Now look up the document id
204
      long docid = 0;
205
      String docname = null;
206
      String doctype = null;
207
      String doctitle = null;
208
      StringBuffer document = null; 
209
      Enumeration rootlist = rootListResult.keys();
210
      while (rootlist.hasMoreElements()) {
211
        nodeid = ((Long)rootlist.nextElement()).longValue();
212

    
213
        try {
214
          pstmt =
215
            conn.prepareStatement("SELECT docid,docname,doctype,doctitle " +
216
                    "FROM xml_documents " +
217
                    "WHERE rootnodeid = ?");
218
          // Bind the values to the query
219
          pstmt.setLong(1, nodeid);
220
 
221
          pstmt.execute();
222
          try {
223
            ResultSet rs = pstmt.getResultSet();
224
            try {
225
              boolean tableHasRows = rs.next();
226
              while (tableHasRows) {
227
                try {
228
                  docid = rs.getLong(1);
229
                  docname = rs.getString(2);
230
                  doctype = rs.getString(3);
231
                  doctitle = rs.getString(4);
232

    
233
                  document = new StringBuffer();
234
                  document.append("<docid>").append(docid).append("</docid>");
235
                  if (docname != null) {
236
                    document.append("<docname>" + docname + "</docname>");
237
                  }
238
                  if (doctype != null) {
239
                    document.append("<doctype>" + doctype + "</doctype>");
240
                  }
241
                  if (doctitle != null) {
242
                    document.append("<doctitle>" + doctitle + "</doctitle>");
243
                  }
244
                } catch (SQLException e) {
245
                  System.out.println("Error with getLong: " + e.getMessage());
246
                }
247

    
248
                // Advance to the next record in the cursor
249
                tableHasRows = rs.next();
250

    
251
              }
252
            } catch (SQLException e) {
253
              System.out.println("Error with next: " + e.getMessage());
254
            }
255
          } catch (SQLException e) {
256
            System.out.println("Error with getrset: " + e.getMessage());
257
          }
258
          pstmt.close();
259
        } catch (SQLException e) {
260
          System.out.println("Error getting id: " + e.getMessage());
261
        }
262

    
263
        // Store the document id and the root node id
264
        docListResult.put(new Long(docid),(String)document.toString());
265
      }
266

    
267
      return docListResult;
268
  }
269
}
(11-11/30)