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 184 2000-06-21 02:57:19Z 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
  private Connection	conn = null;
32

    
33
  /**
34
   * the main routine used to test the DBSimpleQuery utility.
35
   * <p>
36
   * Usage: java DBSimpleQuery <query>
37
   *
38
   * @param query the text to search for in the element and attribute content
39
   */
40
  static public void main(String[] args) {
41
     
42
     if (args.length < 2)
43
     {
44
        System.err.println("Wrong number of arguments!!!");
45
        System.err.println("USAGE: java DBSimpleQuery <query> <doctype>");
46
        return;
47
     } else {
48
        try {
49
                    
50
          String query    = args[0];
51
          String doctype  = args[1];
52

    
53
          // Open a connection to the database
54
          MetaCatUtil   util = new MetaCatUtil();
55
          Connection dbconn = util.openDBConnection();
56

    
57
          // Execute the simple query
58
          DBSimpleQuery rd = new DBSimpleQuery(dbconn);
59
          Hashtable nodelist = null;
60
          if (doctype.equals("any") || doctype.equals("ANY")) {
61
            nodelist = rd.findDocuments(query);
62
          } else {
63
            nodelist = rd.findDocuments(query, doctype);
64
          }
65

    
66
          // Print the reulting root nodes
67
          StringBuffer result = new StringBuffer();
68
          String document = null;
69
          String docid = null;
70
          result.append("<?xml version=\"1.0\"?>\n");
71
          result.append("<resultset>\n");
72
          result.append("  <query>" + query + "</query>\n");
73
          Enumeration doclist = nodelist.keys(); 
74
          while (doclist.hasMoreElements()) {
75
            docid = (String)doclist.nextElement();
76
            document = (String)nodelist.get(docid);
77
            result.append("  <document>\n    " + document + 
78
                          "\n  </document>\n");
79
          }
80
          result.append("</resultset>\n");
81

    
82
          System.out.println(result);
83

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

    
117
  /** 
118
   * routine to search the elements and attributes looking to match query
119
   *
120
   * @param query the text to search for
121
   * @param requestedDoctype the type of documents to return from the query
122
   */
123
  public Hashtable findDocuments(String query, String requestedDoctype) {
124
      Hashtable	 docListResult = new Hashtable();
125

    
126
      PreparedStatement pstmt;
127

    
128
      // Now look up the document id
129
      String docid = null;
130
      String docname = null;
131
      String doctype = null;
132
      String doctitle = null;
133
      //StringBuffer document = null; 
134

    
135
      try {
136
        if (requestedDoctype == null || 
137
            requestedDoctype.equals("any") || 
138
            requestedDoctype.equals("ANY")) {
139
          pstmt = conn.prepareStatement(
140
                "SELECT docid,docname,doctype,doctitle " +
141
                "FROM xml_documents " +
142
                "WHERE docid IN " +
143
                "(SELECT docid " +
144
                "FROM xml_nodes WHERE nodedata LIKE ? )");
145

    
146
                // Bind the values to the query
147
                pstmt.setString(1, query);
148
        } else {
149
          pstmt = conn.prepareStatement(
150
                "SELECT docid,docname,doctype,doctitle " +
151
                "FROM xml_documents " +
152
                "WHERE docid IN " +
153
                "(SELECT docid " +
154
                "FROM xml_nodes WHERE nodedata LIKE ? ) " +
155
                "AND doctype = ?");
156

    
157
                // Bind the values to the query
158
                pstmt.setString(1, query);
159
                pstmt.setString(2, requestedDoctype);
160
        }
161

    
162
        pstmt.execute();
163
        ResultSet rs = pstmt.getResultSet();
164
        boolean tableHasRows = rs.next();
165
        while (tableHasRows) {
166
          docid = rs.getString(1);
167
          docname = rs.getString(2);
168
          doctype = rs.getString(3);
169
          doctitle = rs.getString(4);
170

    
171
          StringBuffer document = new StringBuffer();
172
          document.append("<docid>").append(docid).append("</docid>");
173

    
174
          if (docname != null) {
175
            document.append("<docname>" + docname + "</docname>");
176
          }
177
          if (doctype != null) {
178
            document.append("<doctype>" + doctype + "</doctype>");
179
          }
180
          if (doctitle != null) {
181
            document.append("<doctitle>" + doctitle + "</doctitle>");
182
          }
183

    
184
          // Store the document id and the root node id
185
          docListResult.put(docid,(String)document.toString());
186

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

    
195
    return docListResult;
196
  }
197
}
(10-10/20)