Project

General

Profile

1
/**
2
 *  '$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
 *
11
 *   '$Author: jones $'
12
 *     '$Date: 2000-06-26 03:35:05 -0700 (Mon, 26 Jun 2000) $'
13
 * '$Revision: 203 $'
14
 */
15

    
16
package edu.ucsb.nceas.metacat;
17

    
18
import java.io.*;
19
import java.net.URL;
20
import java.net.MalformedURLException;
21
import java.sql.*;
22
import java.util.Stack;
23
import java.util.Hashtable;
24
import java.util.Enumeration;
25

    
26
/** 
27
 * A Class that searches a relational DB for elements and attributes that
28
 * have free text matches to the query string.  It returns a result set 
29
 * consisting of the root nodeid for each document that satisfies the query
30
 */
31
public class DBSimpleQuery {
32

    
33
  private Connection	conn = null;
34

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

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

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

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

    
84
          System.out.println(result);
85

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

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

    
128
      PreparedStatement pstmt;
129

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

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

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

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

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

    
173
          StringBuffer document = new StringBuffer();
174
          document.append("<docid>").append(docid).append("</docid>");
175

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

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

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

    
197
    return docListResult;
198
  }
199
}
200

    
201
/**
202
 * '$Log$
203
 * 'Revision 1.15.2.2  2000/06/25 23:38:16  jones
204
 * 'Added RCSfile keyword
205
 * '
206
 * 'Revision 1.15.2.1  2000/06/25 23:34:17  jones
207
 * 'Changed documentation formatting, added log entries at bottom of source files
208
 * ''
209
 */
(14-14/25)