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: 2008-07-06 21:25:34 -0700 (Sun, 06 Jul 2008) $'
13
 * '$Revision: 4080 $'
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
          //Connection dbconn = util.openDBConnection();
71

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

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

    
98
          System.out.println(result);
99

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

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

    
142
      PreparedStatement pstmt = null;
143
      DBConnection dbConn = null;
144
      int serialNumber = -1;
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
        dbConn=DBConnectionPool.
155
                  getDBConnection("DBSimpleQuery.findDocuments");
156
        serialNumber=dbConn.getCheckOutSerialNumber();
157
        if (requestedDoctype == null || 
158
            requestedDoctype.equals("any") || 
159
            requestedDoctype.equals("ANY")) {
160
          pstmt = dbConn.prepareStatement(
161
                "SELECT docid,docname,doctype,doctitle " +
162
                "FROM xml_documents " +
163
                "WHERE docid IN " +
164
                "(SELECT docid " +
165
                "FROM xml_nodes WHERE nodedata LIKE ? )");
166

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

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

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

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

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

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

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

    
221
    return docListResult;
222
  }
223
}
(23-23/67)