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: berkley $'
13
 *     '$Date: 2001-01-18 15:15:21 -0800 (Thu, 18 Jan 2001) $'
14
 * '$Revision: 675 $'
15
 *
16
 * This program is free software; you can redistribute it and/or modify
17
 * it under the terms of the GNU General Public License as published by
18
 * the Free Software Foundation; either version 2 of the License, or
19
 * (at your option) any later version.
20
 *
21
 * This program is distributed in the hope that it will be useful,
22
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24
 * GNU General Public License for more details.
25
 *
26
 * You should have received a copy of the GNU General Public License
27
 * along with this program; if not, write to the Free Software
28
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
29
 */
30

    
31
package edu.ucsb.nceas.metacat;
32

    
33
import java.io.*;
34
import java.net.URL;
35
import java.net.MalformedURLException;
36
import java.sql.*;
37
import java.util.Stack;
38
import java.util.Hashtable;
39
import java.util.Enumeration;
40

    
41
/** 
42
 * A Class that searches a relational DB for elements and attributes that
43
 * have free text matches to the query string.  It returns a result set 
44
 * consisting of the root nodeid for each document that satisfies the query
45
 */
46
public class DBSimpleQuery {
47

    
48
  private Connection	conn = null;
49

    
50
  /**
51
   * the main routine used to test the DBSimpleQuery utility.
52
   * <p>
53
   * Usage: java DBSimpleQuery <query>
54
   *
55
   * @param query the text to search for in the element and attribute content
56
   */
57
  static public void main(String[] args) {
58
     
59
     if (args.length < 2)
60
     {
61
        System.err.println("Wrong number of arguments!!!");
62
        System.err.println("USAGE: java DBSimpleQuery <query> <doctype>");
63
        return;
64
     } else {
65
        try {
66
                    
67
          String query    = args[0];
68
          String doctype  = args[1];
69

    
70
          // Open a connection to the database
71
          MetaCatUtil   util = new MetaCatUtil();
72
          Connection dbconn = util.openDBConnection();
73

    
74
          // Execute the simple query
75
          DBSimpleQuery rd = new DBSimpleQuery(dbconn);
76
          Hashtable nodelist = null;
77
          if (doctype.equals("any") || doctype.equals("ANY")) {
78
            nodelist = rd.findDocuments(query);
79
          } else {
80
            nodelist = rd.findDocuments(query, doctype);
81
          }
82

    
83
          // Print the reulting root nodes
84
          StringBuffer result = new StringBuffer();
85
          String document = null;
86
          String docid = null;
87
          result.append("<?xml version=\"1.0\"?>\n");
88
          result.append("<resultset>\n");
89
  // following line removed by Dan Higgins to avoid insertion of query XML inside returned XML doc
90
  //        result.append("  <query>" + query + "</query>\n");
91
          Enumeration doclist = nodelist.keys(); 
92
          while (doclist.hasMoreElements()) {
93
            docid = (String)doclist.nextElement();
94
            document = (String)nodelist.get(docid);
95
            result.append("  <document>\n    " + document + 
96
                          "\n  </document>\n");
97
          }
98
          result.append("</resultset>\n");
99

    
100
          System.out.println(result);
101

    
102
        } catch (Exception e) {
103
          System.err.println("Error in DBSimpleQuery.main");
104
          System.err.println(e.getMessage());
105
          e.printStackTrace(System.err);
106
        }
107
     }
108
  }
109
  
110
  /**
111
   * construct an instance of the DBSimpleQuery class 
112
   *
113
   * <p>Generally, one would call the findDocuments() routine after creating 
114
   * an instance to specify the search query</p>
115
   *
116
   * @param conn the JDBC connection that we use for the query
117
   */
118
  public DBSimpleQuery( Connection conn ) 
119
                  throws IOException, 
120
                         SQLException, 
121
                         ClassNotFoundException
122
  {
123
    this.conn = conn;
124
  }
125
  
126
  /** 
127
   * routine to search the elements and attributes looking to match query
128
   *
129
   * @param query the text to search for
130
   */
131
  public Hashtable findDocuments(String query) {
132
    return this.findDocuments(query, null);
133
  }
134

    
135
  /** 
136
   * routine to search the elements and attributes looking to match query
137
   *
138
   * @param query the text to search for
139
   * @param requestedDoctype the type of documents to return from the query
140
   */
141
  public Hashtable findDocuments(String query, String requestedDoctype) {
142
      Hashtable	 docListResult = new Hashtable();
143

    
144
      PreparedStatement pstmt;
145

    
146
      // Now look up the document id
147
      String docid = null;
148
      String docname = null;
149
      String doctype = null;
150
      String doctitle = null;
151
      //StringBuffer document = null; 
152

    
153
      try {
154
        if (requestedDoctype == null || 
155
            requestedDoctype.equals("any") || 
156
            requestedDoctype.equals("ANY")) {
157
          pstmt = conn.prepareStatement(
158
                "SELECT docid,docname,doctype,doctitle " +
159
                "FROM xml_documents " +
160
                "WHERE docid IN " +
161
                "(SELECT docid " +
162
                "FROM xml_nodes WHERE nodedata LIKE ? )");
163

    
164
                // Bind the values to the query
165
                pstmt.setString(1, query);
166
        } else {
167
          pstmt = conn.prepareStatement(
168
                "SELECT docid,docname,doctype,doctitle " +
169
                "FROM xml_documents " +
170
                "WHERE docid IN " +
171
                "(SELECT docid " +
172
                "FROM xml_nodes WHERE nodedata LIKE ? ) " +
173
                "AND doctype = ?");
174

    
175
                // Bind the values to the query
176
                pstmt.setString(1, query);
177
                pstmt.setString(2, requestedDoctype);
178
        }
179

    
180
        pstmt.execute();
181
        ResultSet rs = pstmt.getResultSet();
182
        boolean tableHasRows = rs.next();
183
        while (tableHasRows) {
184
          docid = rs.getString(1);
185
          docname = rs.getString(2);
186
          doctype = rs.getString(3);
187
          doctitle = rs.getString(4);
188

    
189
          StringBuffer document = new StringBuffer();
190
          document.append("<docid>").append(docid).append("</docid>");
191

    
192
          if (docname != null) {
193
            document.append("<docname>" + docname + "</docname>");
194
          }
195
          if (doctype != null) {
196
            document.append("<doctype>" + doctype + "</doctype>");
197
          }
198
          if (doctitle != null) {
199
            document.append("<doctitle>" + doctitle + "</doctitle>");
200
          }
201

    
202
          // Store the document id and the root node id
203
          docListResult.put(docid,(String)document.toString());
204

    
205
          // Advance to the next record in the cursor
206
          tableHasRows = rs.next();
207
        }
208
        pstmt.close();
209
      } catch (SQLException e) {
210
        System.out.println("Error in DBSimpleQuery.findDocuments: " + 
211
                            e.getMessage());
212
      }
213

    
214
    return docListResult;
215
  }
216
}
(17-17/43)