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 349 jones
 *    Release: @release@
11 27 jones
 *
12 203 jones
 *   '$Author$'
13
 *     '$Date$'
14
 * '$Revision$'
15 669 jones
 *
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 27 jones
 */
30
31 51 jones
package edu.ucsb.nceas.metacat;
32
33 27 jones
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 31 jones
/**
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 27 jones
public class DBSimpleQuery {
47
48 1217 tao
  //private Connection	conn = null;
49 27 jones
50 31 jones
  /**
51
   * the main routine used to test the DBSimpleQuery utility.
52 184 jones
   * <p>
53
   * Usage: java DBSimpleQuery <query>
54 31 jones
   *
55
   * @param query the text to search for in the element and attribute content
56
   */
57 27 jones
  static public void main(String[] args) {
58
59 184 jones
     if (args.length < 2)
60 27 jones
     {
61
        System.err.println("Wrong number of arguments!!!");
62 184 jones
        System.err.println("USAGE: java DBSimpleQuery <query> <doctype>");
63 27 jones
        return;
64
     } else {
65
        try {
66
67 28 jones
          String query    = args[0];
68 154 jones
          String doctype  = args[1];
69 27 jones
70 184 jones
          // Open a connection to the database
71
          MetaCatUtil   util = new MetaCatUtil();
72 1217 tao
          //Connection dbconn = util.openDBConnection();
73 27 jones
74
          // Execute the simple query
75 1217 tao
          DBSimpleQuery rd = new DBSimpleQuery();
76 154 jones
          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 27 jones
83
          // Print the reulting root nodes
84
          StringBuffer result = new StringBuffer();
85 150 jones
          String document = null;
86 162 bojilova
          String docid = null;
87 27 jones
          result.append("<?xml version=\"1.0\"?>\n");
88
          result.append("<resultset>\n");
89 296 higgins
  // following line removed by Dan Higgins to avoid insertion of query XML inside returned XML doc
90
  //        result.append("  <query>" + query + "</query>\n");
91 86 jones
          Enumeration doclist = nodelist.keys();
92
          while (doclist.hasMoreElements()) {
93 162 bojilova
            docid = (String)doclist.nextElement();
94 150 jones
            document = (String)nodelist.get(docid);
95
            result.append("  <document>\n    " + document +
96
                          "\n  </document>\n");
97 27 jones
          }
98 150 jones
          result.append("</resultset>\n");
99 27 jones
100
          System.out.println(result);
101
102
        } catch (Exception e) {
103 675 berkley
          System.err.println("Error in DBSimpleQuery.main");
104 27 jones
          System.err.println(e.getMessage());
105
          e.printStackTrace(System.err);
106
        }
107
     }
108
  }
109
110 31 jones
  /**
111
   * construct an instance of the DBSimpleQuery class
112
   *
113 86 jones
   * <p>Generally, one would call the findDocuments() routine after creating
114 50 jones
   * an instance to specify the search query</p>
115 31 jones
   *
116 50 jones
   * @param conn the JDBC connection that we use for the query
117 31 jones
   */
118 1217 tao
  public DBSimpleQuery()
119 27 jones
                  throws IOException,
120
                         SQLException,
121
                         ClassNotFoundException
122
  {
123 1217 tao
    //this.conn = conn;
124 27 jones
  }
125
126 31 jones
  /**
127
   * routine to search the elements and attributes looking to match query
128
   *
129
   * @param query the text to search for
130
   */
131 86 jones
  public Hashtable findDocuments(String query) {
132 154 jones
    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 86 jones
      Hashtable	 docListResult = new Hashtable();
143 27 jones
144 1217 tao
      PreparedStatement pstmt = null;
145
      DBConnection dbConn = null;
146
      int serialNumber = -1;
147 27 jones
148 150 jones
      // Now look up the document id
149 162 bojilova
      String docid = null;
150 150 jones
      String docname = null;
151
      String doctype = null;
152
      String doctitle = null;
153 167 jones
      //StringBuffer document = null;
154 150 jones
155 27 jones
      try {
156 1217 tao
        dbConn=DBConnectionPool.
157
                  getDBConnection("DBSimpleQuery.findDocuments");
158
        serialNumber=dbConn.getCheckOutSerialNumber();
159 154 jones
        if (requestedDoctype == null ||
160
            requestedDoctype.equals("any") ||
161
            requestedDoctype.equals("ANY")) {
162 1217 tao
          pstmt = dbConn.prepareStatement(
163 150 jones
                "SELECT docid,docname,doctype,doctitle " +
164
                "FROM xml_documents " +
165
                "WHERE docid IN " +
166
                "(SELECT docid " +
167
                "FROM xml_nodes WHERE nodedata LIKE ? )");
168 27 jones
169 154 jones
                // Bind the values to the query
170
                pstmt.setString(1, query);
171
        } else {
172 1217 tao
          pstmt = dbConn.prepareStatement(
173 154 jones
                "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 27 jones
        pstmt.execute();
186 150 jones
        ResultSet rs = pstmt.getResultSet();
187
        boolean tableHasRows = rs.next();
188
        while (tableHasRows) {
189 162 bojilova
          docid = rs.getString(1);
190 150 jones
          docname = rs.getString(2);
191
          doctype = rs.getString(3);
192
          doctitle = rs.getString(4);
193 27 jones
194 167 jones
          StringBuffer document = new StringBuffer();
195 150 jones
          document.append("<docid>").append(docid).append("</docid>");
196 167 jones
197 150 jones
          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 27 jones
207 150 jones
          // Store the document id and the root node id
208 162 bojilova
          docListResult.put(docid,(String)document.toString());
209 150 jones
210
          // Advance to the next record in the cursor
211
          tableHasRows = rs.next();
212 27 jones
        }
213
        pstmt.close();
214
      } catch (SQLException e) {
215 675 berkley
        System.out.println("Error in DBSimpleQuery.findDocuments: " +
216
                            e.getMessage());
217 27 jones
      }
218 1217 tao
      finally
219
      {
220
        DBConnectionPool.returnDBConnection(dbConn, serialNumber);
221
      }
222 27 jones
223 150 jones
    return docListResult;
224 27 jones
  }
225
}