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
 *    Release: @release@
11
 *
12
 *   '$Author: jones $'
13
 *     '$Date: 2000-08-14 13:53:34 -0700 (Mon, 14 Aug 2000) $'
14
 * '$Revision: 349 $'
15
 */
16

    
17
package edu.ucsb.nceas.metacat;
18

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

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

    
34
  private Connection	conn = null;
35

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

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

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

    
69
          // Print the reulting root nodes
70
          StringBuffer result = new StringBuffer();
71
          String document = null;
72
          String docid = null;
73
          result.append("<?xml version=\"1.0\"?>\n");
74
          result.append("<resultset>\n");
75
  // following line removed by Dan Higgins to avoid insertion of query XML inside returned XML doc
76
  //        result.append("  <query>" + query + "</query>\n");
77
          Enumeration doclist = nodelist.keys(); 
78
          while (doclist.hasMoreElements()) {
79
            docid = (String)doclist.nextElement();
80
            document = (String)nodelist.get(docid);
81
            result.append("  <document>\n    " + document + 
82
                          "\n  </document>\n");
83
          }
84
          result.append("</resultset>\n");
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
    return this.findDocuments(query, null);
119
  }
120

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

    
130
      PreparedStatement pstmt;
131

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

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

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

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

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

    
175
          StringBuffer document = new StringBuffer();
176
          document.append("<docid>").append(docid).append("</docid>");
177

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

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

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

    
199
    return docListResult;
200
  }
201
}
202

    
203
/**
204
 * '$Log$
205
 * 'Revision 1.17  2000/07/26 20:40:40  higgins
206
 * 'no message
207
 * '
208
 * 'Revision 1.16  2000/06/26 10:35:05  jones
209
 * 'Merged in substantial changes to DBWriter and associated classes and to
210
 * 'the MetaCatServlet in order to accomodate the new UPDATE and DELETE
211
 * 'functions.  The command line tools and the parameters for the
212
 * 'servlet have changed substantially.
213
 * '
214
 * 'Revision 1.15.2.2  2000/06/25 23:38:16  jones
215
 * 'Added RCSfile keyword
216
 * '
217
 * 'Revision 1.15.2.1  2000/06/25 23:34:17  jones
218
 * 'Changed documentation formatting, added log entries at bottom of source files
219
 * ''
220
 */
(11-11/27)