Project

General

Profile

1 27 jones
/**
2 203 jones
 *  '$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 27 jones
 *
11 203 jones
 *   '$Author$'
12
 *     '$Date$'
13
 * '$Revision$'
14 27 jones
 */
15
16 51 jones
package edu.ucsb.nceas.metacat;
17
18 27 jones
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 31 jones
/**
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 27 jones
public class DBSimpleQuery {
32
33
  private Connection	conn = null;
34
35 31 jones
  /**
36
   * the main routine used to test the DBSimpleQuery utility.
37 184 jones
   * <p>
38
   * Usage: java DBSimpleQuery <query>
39 31 jones
   *
40
   * @param query the text to search for in the element and attribute content
41
   */
42 27 jones
  static public void main(String[] args) {
43
44 184 jones
     if (args.length < 2)
45 27 jones
     {
46
        System.err.println("Wrong number of arguments!!!");
47 184 jones
        System.err.println("USAGE: java DBSimpleQuery <query> <doctype>");
48 27 jones
        return;
49
     } else {
50
        try {
51
52 28 jones
          String query    = args[0];
53 154 jones
          String doctype  = args[1];
54 27 jones
55 184 jones
          // Open a connection to the database
56
          MetaCatUtil   util = new MetaCatUtil();
57
          Connection dbconn = util.openDBConnection();
58 27 jones
59
          // Execute the simple query
60 50 jones
          DBSimpleQuery rd = new DBSimpleQuery(dbconn);
61 154 jones
          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 27 jones
68
          // Print the reulting root nodes
69
          StringBuffer result = new StringBuffer();
70 150 jones
          String document = null;
71 162 bojilova
          String docid = null;
72 27 jones
          result.append("<?xml version=\"1.0\"?>\n");
73
          result.append("<resultset>\n");
74 296 higgins
  // following line removed by Dan Higgins to avoid insertion of query XML inside returned XML doc
75
  //        result.append("  <query>" + query + "</query>\n");
76 86 jones
          Enumeration doclist = nodelist.keys();
77
          while (doclist.hasMoreElements()) {
78 162 bojilova
            docid = (String)doclist.nextElement();
79 150 jones
            document = (String)nodelist.get(docid);
80
            result.append("  <document>\n    " + document +
81
                          "\n  </document>\n");
82 27 jones
          }
83 150 jones
          result.append("</resultset>\n");
84 27 jones
85
          System.out.println(result);
86
87
        } catch (Exception e) {
88
          System.err.println("EXCEPTION HANDLING REQUIRED");
89
          System.err.println(e.getMessage());
90
          e.printStackTrace(System.err);
91
        }
92
     }
93
  }
94
95 31 jones
  /**
96
   * construct an instance of the DBSimpleQuery class
97
   *
98 86 jones
   * <p>Generally, one would call the findDocuments() routine after creating
99 50 jones
   * an instance to specify the search query</p>
100 31 jones
   *
101 50 jones
   * @param conn the JDBC connection that we use for the query
102 31 jones
   */
103 50 jones
  public DBSimpleQuery( Connection conn )
104 27 jones
                  throws IOException,
105
                         SQLException,
106
                         ClassNotFoundException
107
  {
108 50 jones
    this.conn = conn;
109 27 jones
  }
110
111 31 jones
  /**
112
   * routine to search the elements and attributes looking to match query
113
   *
114
   * @param query the text to search for
115
   */
116 86 jones
  public Hashtable findDocuments(String query) {
117 154 jones
    return this.findDocuments(query, null);
118
  }
119
120
  /**
121
   * routine to search the elements and attributes looking to match query
122
   *
123
   * @param query the text to search for
124
   * @param requestedDoctype the type of documents to return from the query
125
   */
126
  public Hashtable findDocuments(String query, String requestedDoctype) {
127 86 jones
      Hashtable	 docListResult = new Hashtable();
128 27 jones
129
      PreparedStatement pstmt;
130
131 150 jones
      // Now look up the document id
132 162 bojilova
      String docid = null;
133 150 jones
      String docname = null;
134
      String doctype = null;
135
      String doctitle = null;
136 167 jones
      //StringBuffer document = null;
137 150 jones
138 27 jones
      try {
139 154 jones
        if (requestedDoctype == null ||
140
            requestedDoctype.equals("any") ||
141
            requestedDoctype.equals("ANY")) {
142
          pstmt = conn.prepareStatement(
143 150 jones
                "SELECT docid,docname,doctype,doctitle " +
144
                "FROM xml_documents " +
145
                "WHERE docid IN " +
146
                "(SELECT docid " +
147
                "FROM xml_nodes WHERE nodedata LIKE ? )");
148 27 jones
149 154 jones
                // Bind the values to the query
150
                pstmt.setString(1, query);
151
        } else {
152
          pstmt = conn.prepareStatement(
153
                "SELECT docid,docname,doctype,doctitle " +
154
                "FROM xml_documents " +
155
                "WHERE docid IN " +
156
                "(SELECT docid " +
157
                "FROM xml_nodes WHERE nodedata LIKE ? ) " +
158
                "AND doctype = ?");
159
160
                // Bind the values to the query
161
                pstmt.setString(1, query);
162
                pstmt.setString(2, requestedDoctype);
163
        }
164
165 27 jones
        pstmt.execute();
166 150 jones
        ResultSet rs = pstmt.getResultSet();
167
        boolean tableHasRows = rs.next();
168
        while (tableHasRows) {
169 162 bojilova
          docid = rs.getString(1);
170 150 jones
          docname = rs.getString(2);
171
          doctype = rs.getString(3);
172
          doctitle = rs.getString(4);
173 27 jones
174 167 jones
          StringBuffer document = new StringBuffer();
175 150 jones
          document.append("<docid>").append(docid).append("</docid>");
176 167 jones
177 150 jones
          if (docname != null) {
178
            document.append("<docname>" + docname + "</docname>");
179
          }
180
          if (doctype != null) {
181
            document.append("<doctype>" + doctype + "</doctype>");
182
          }
183
          if (doctitle != null) {
184
            document.append("<doctitle>" + doctitle + "</doctitle>");
185
          }
186 27 jones
187 150 jones
          // Store the document id and the root node id
188 162 bojilova
          docListResult.put(docid,(String)document.toString());
189 150 jones
190
          // Advance to the next record in the cursor
191
          tableHasRows = rs.next();
192 27 jones
        }
193
        pstmt.close();
194
      } catch (SQLException e) {
195
        System.out.println("Error getting id: " + e.getMessage());
196
      }
197
198 150 jones
    return docListResult;
199 27 jones
  }
200
}
201 203 jones
202
/**
203
 * '$Log$
204 296 higgins
 * 'Revision 1.16  2000/06/26 10:35:05  jones
205
 * 'Merged in substantial changes to DBWriter and associated classes and to
206
 * 'the MetaCatServlet in order to accomodate the new UPDATE and DELETE
207
 * 'functions.  The command line tools and the parameters for the
208
 * 'servlet have changed substantially.
209
 * '
210 203 jones
 * 'Revision 1.15.2.2  2000/06/25 23:38:16  jones
211
 * 'Added RCSfile keyword
212
 * '
213
 * 'Revision 1.15.2.1  2000/06/25 23:34:17  jones
214
 * 'Changed documentation formatting, added log entries at bottom of source files
215
 * ''
216
 */