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: tao $'
13
 *     '$Date: 2002-06-13 11:54:51 -0700 (Thu, 13 Jun 2002) $'
14
 * '$Revision: 1217 $'
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();
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() 
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 = null;
145
      DBConnection dbConn = null;
146
      int serialNumber = -1;
147

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

    
155
      try {
156
        dbConn=DBConnectionPool.
157
                  getDBConnection("DBSimpleQuery.findDocuments");
158
        serialNumber=dbConn.getCheckOutSerialNumber();
159
        if (requestedDoctype == null || 
160
            requestedDoctype.equals("any") || 
161
            requestedDoctype.equals("ANY")) {
162
          pstmt = dbConn.prepareStatement(
163
                "SELECT docid,docname,doctype,doctitle " +
164
                "FROM xml_documents " +
165
                "WHERE docid IN " +
166
                "(SELECT docid " +
167
                "FROM xml_nodes WHERE nodedata LIKE ? )");
168

    
169
                // Bind the values to the query
170
                pstmt.setString(1, query);
171
        } else {
172
          pstmt = dbConn.prepareStatement(
173
                "SELECT docid,docname,doctype,doctitle " +
174
                "FROM xml_documents " +
175
                "WHERE docid IN " +
176
                "(SELECT docid " +
177
                "FROM xml_nodes WHERE nodedata LIKE ? ) " +
178
                "AND doctype = ?");
179

    
180
                // Bind the values to the query
181
                pstmt.setString(1, query);
182
                pstmt.setString(2, requestedDoctype);
183
        }
184

    
185
        pstmt.execute();
186
        ResultSet rs = pstmt.getResultSet();
187
        boolean tableHasRows = rs.next();
188
        while (tableHasRows) {
189
          docid = rs.getString(1);
190
          docname = rs.getString(2);
191
          doctype = rs.getString(3);
192
          doctitle = rs.getString(4);
193

    
194
          StringBuffer document = new StringBuffer();
195
          document.append("<docid>").append(docid).append("</docid>");
196

    
197
          if (docname != null) {
198
            document.append("<docname>" + docname + "</docname>");
199
          }
200
          if (doctype != null) {
201
            document.append("<doctype>" + doctype + "</doctype>");
202
          }
203
          if (doctitle != null) {
204
            document.append("<doctitle>" + doctitle + "</doctitle>");
205
          }
206

    
207
          // Store the document id and the root node id
208
          docListResult.put(docid,(String)document.toString());
209

    
210
          // Advance to the next record in the cursor
211
          tableHasRows = rs.next();
212
        }
213
        pstmt.close();
214
      } catch (SQLException e) {
215
        System.out.println("Error in DBSimpleQuery.findDocuments: " + 
216
                            e.getMessage());
217
      }
218
      finally
219
      {
220
        DBConnectionPool.returnDBConnection(dbConn, serialNumber);
221
      }
222

    
223
    return docListResult;
224
  }
225
}
(25-25/63)