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

    
30
package edu.ucsb.nceas.metacat;
31

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

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

    
47
  //private Connection	conn = null;
48

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

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

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

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

    
99
          System.out.println(result);
100

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

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

    
143
      PreparedStatement pstmt = null;
144
      DBConnection dbConn = null;
145
      int serialNumber = -1;
146

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

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

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

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

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

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

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

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

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

    
222
    return docListResult;
223
  }
224
}
(24-24/66)