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: higgins $'
12
 *     '$Date: 2000-07-26 13:40:41 -0700 (Wed, 26 Jul 2000) $'
13
 * '$Revision: 296 $'
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
  // following line removed by Dan Higgins to avoid insertion of query XML inside returned XML doc
75
  //        result.append("  <query>" + query + "</query>\n");
76
          Enumeration doclist = nodelist.keys(); 
77
          while (doclist.hasMoreElements()) {
78
            docid = (String)doclist.nextElement();
79
            document = (String)nodelist.get(docid);
80
            result.append("  <document>\n    " + document + 
81
                          "\n  </document>\n");
82
          }
83
          result.append("</resultset>\n");
84

    
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
  /**
96
   * construct an instance of the DBSimpleQuery class 
97
   *
98
   * <p>Generally, one would call the findDocuments() routine after creating 
99
   * an instance to specify the search query</p>
100
   *
101
   * @param conn the JDBC connection that we use for the query
102
   */
103
  public DBSimpleQuery( Connection conn ) 
104
                  throws IOException, 
105
                         SQLException, 
106
                         ClassNotFoundException
107
  {
108
    this.conn = conn;
109
  }
110
  
111
  /** 
112
   * routine to search the elements and attributes looking to match query
113
   *
114
   * @param query the text to search for
115
   */
116
  public Hashtable findDocuments(String query) {
117
    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
      Hashtable	 docListResult = new Hashtable();
128

    
129
      PreparedStatement pstmt;
130

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

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

    
149
                // 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
        pstmt.execute();
166
        ResultSet rs = pstmt.getResultSet();
167
        boolean tableHasRows = rs.next();
168
        while (tableHasRows) {
169
          docid = rs.getString(1);
170
          docname = rs.getString(2);
171
          doctype = rs.getString(3);
172
          doctitle = rs.getString(4);
173

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

    
177
          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

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

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

    
198
    return docListResult;
199
  }
200
}
201

    
202
/**
203
 * '$Log$
204
 * '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
 * '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
 */
(14-14/25)