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: daigle $'
12
 *     '$Date: 2009-08-04 14:32:58 -0700 (Tue, 04 Aug 2009) $'
13
 * '$Revision: 5015 $'
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
import edu.ucsb.nceas.metacat.database.DBConnection;
41
import edu.ucsb.nceas.metacat.database.DBConnectionPool;
42

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

    
50
  //private Connection	conn = null;
51

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

    
72
          // Open a connection to the database
73
          //Connection dbconn = util.openDBConnection();
74

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

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

    
101
          System.out.println(result);
102

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

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

    
145
      PreparedStatement pstmt = null;
146
      DBConnection dbConn = null;
147
      int serialNumber = -1;
148

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

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

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

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

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

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

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

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

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

    
224
    return docListResult;
225
  }
226
}
(20-20/62)