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 |
27
|
jones
|
*/
|
16 |
|
|
|
17 |
51
|
jones
|
package edu.ucsb.nceas.metacat;
|
18 |
|
|
|
19 |
27
|
jones
|
import java.io.*;
|
20 |
|
|
import java.net.URL;
|
21 |
|
|
import java.net.MalformedURLException;
|
22 |
|
|
import java.sql.*;
|
23 |
|
|
import java.util.Stack;
|
24 |
|
|
import java.util.Hashtable;
|
25 |
|
|
import java.util.Enumeration;
|
26 |
|
|
|
27 |
31
|
jones
|
/**
|
28 |
|
|
* A Class that searches a relational DB for elements and attributes that
|
29 |
|
|
* have free text matches to the query string. It returns a result set
|
30 |
|
|
* consisting of the root nodeid for each document that satisfies the query
|
31 |
|
|
*/
|
32 |
27
|
jones
|
public class DBSimpleQuery {
|
33 |
|
|
|
34 |
|
|
private Connection conn = null;
|
35 |
|
|
|
36 |
31
|
jones
|
/**
|
37 |
|
|
* the main routine used to test the DBSimpleQuery utility.
|
38 |
184
|
jones
|
* <p>
|
39 |
|
|
* Usage: java DBSimpleQuery <query>
|
40 |
31
|
jones
|
*
|
41 |
|
|
* @param query the text to search for in the element and attribute content
|
42 |
|
|
*/
|
43 |
27
|
jones
|
static public void main(String[] args) {
|
44 |
|
|
|
45 |
184
|
jones
|
if (args.length < 2)
|
46 |
27
|
jones
|
{
|
47 |
|
|
System.err.println("Wrong number of arguments!!!");
|
48 |
184
|
jones
|
System.err.println("USAGE: java DBSimpleQuery <query> <doctype>");
|
49 |
27
|
jones
|
return;
|
50 |
|
|
} else {
|
51 |
|
|
try {
|
52 |
|
|
|
53 |
28
|
jones
|
String query = args[0];
|
54 |
154
|
jones
|
String doctype = args[1];
|
55 |
27
|
jones
|
|
56 |
184
|
jones
|
// Open a connection to the database
|
57 |
|
|
MetaCatUtil util = new MetaCatUtil();
|
58 |
|
|
Connection dbconn = util.openDBConnection();
|
59 |
27
|
jones
|
|
60 |
|
|
// Execute the simple query
|
61 |
50
|
jones
|
DBSimpleQuery rd = new DBSimpleQuery(dbconn);
|
62 |
154
|
jones
|
Hashtable nodelist = null;
|
63 |
|
|
if (doctype.equals("any") || doctype.equals("ANY")) {
|
64 |
|
|
nodelist = rd.findDocuments(query);
|
65 |
|
|
} else {
|
66 |
|
|
nodelist = rd.findDocuments(query, doctype);
|
67 |
|
|
}
|
68 |
27
|
jones
|
|
69 |
|
|
// Print the reulting root nodes
|
70 |
|
|
StringBuffer result = new StringBuffer();
|
71 |
150
|
jones
|
String document = null;
|
72 |
162
|
bojilova
|
String docid = null;
|
73 |
27
|
jones
|
result.append("<?xml version=\"1.0\"?>\n");
|
74 |
|
|
result.append("<resultset>\n");
|
75 |
296
|
higgins
|
// following line removed by Dan Higgins to avoid insertion of query XML inside returned XML doc
|
76 |
|
|
// result.append(" <query>" + query + "</query>\n");
|
77 |
86
|
jones
|
Enumeration doclist = nodelist.keys();
|
78 |
|
|
while (doclist.hasMoreElements()) {
|
79 |
162
|
bojilova
|
docid = (String)doclist.nextElement();
|
80 |
150
|
jones
|
document = (String)nodelist.get(docid);
|
81 |
|
|
result.append(" <document>\n " + document +
|
82 |
|
|
"\n </document>\n");
|
83 |
27
|
jones
|
}
|
84 |
150
|
jones
|
result.append("</resultset>\n");
|
85 |
27
|
jones
|
|
86 |
|
|
System.out.println(result);
|
87 |
|
|
|
88 |
|
|
} catch (Exception e) {
|
89 |
|
|
System.err.println("EXCEPTION HANDLING REQUIRED");
|
90 |
|
|
System.err.println(e.getMessage());
|
91 |
|
|
e.printStackTrace(System.err);
|
92 |
|
|
}
|
93 |
|
|
}
|
94 |
|
|
}
|
95 |
|
|
|
96 |
31
|
jones
|
/**
|
97 |
|
|
* construct an instance of the DBSimpleQuery class
|
98 |
|
|
*
|
99 |
86
|
jones
|
* <p>Generally, one would call the findDocuments() routine after creating
|
100 |
50
|
jones
|
* an instance to specify the search query</p>
|
101 |
31
|
jones
|
*
|
102 |
50
|
jones
|
* @param conn the JDBC connection that we use for the query
|
103 |
31
|
jones
|
*/
|
104 |
50
|
jones
|
public DBSimpleQuery( Connection conn )
|
105 |
27
|
jones
|
throws IOException,
|
106 |
|
|
SQLException,
|
107 |
|
|
ClassNotFoundException
|
108 |
|
|
{
|
109 |
50
|
jones
|
this.conn = conn;
|
110 |
27
|
jones
|
}
|
111 |
|
|
|
112 |
31
|
jones
|
/**
|
113 |
|
|
* routine to search the elements and attributes looking to match query
|
114 |
|
|
*
|
115 |
|
|
* @param query the text to search for
|
116 |
|
|
*/
|
117 |
86
|
jones
|
public Hashtable findDocuments(String query) {
|
118 |
154
|
jones
|
return this.findDocuments(query, null);
|
119 |
|
|
}
|
120 |
|
|
|
121 |
|
|
/**
|
122 |
|
|
* routine to search the elements and attributes looking to match query
|
123 |
|
|
*
|
124 |
|
|
* @param query the text to search for
|
125 |
|
|
* @param requestedDoctype the type of documents to return from the query
|
126 |
|
|
*/
|
127 |
|
|
public Hashtable findDocuments(String query, String requestedDoctype) {
|
128 |
86
|
jones
|
Hashtable docListResult = new Hashtable();
|
129 |
27
|
jones
|
|
130 |
|
|
PreparedStatement pstmt;
|
131 |
|
|
|
132 |
150
|
jones
|
// Now look up the document id
|
133 |
162
|
bojilova
|
String docid = null;
|
134 |
150
|
jones
|
String docname = null;
|
135 |
|
|
String doctype = null;
|
136 |
|
|
String doctitle = null;
|
137 |
167
|
jones
|
//StringBuffer document = null;
|
138 |
150
|
jones
|
|
139 |
27
|
jones
|
try {
|
140 |
154
|
jones
|
if (requestedDoctype == null ||
|
141 |
|
|
requestedDoctype.equals("any") ||
|
142 |
|
|
requestedDoctype.equals("ANY")) {
|
143 |
|
|
pstmt = conn.prepareStatement(
|
144 |
150
|
jones
|
"SELECT docid,docname,doctype,doctitle " +
|
145 |
|
|
"FROM xml_documents " +
|
146 |
|
|
"WHERE docid IN " +
|
147 |
|
|
"(SELECT docid " +
|
148 |
|
|
"FROM xml_nodes WHERE nodedata LIKE ? )");
|
149 |
27
|
jones
|
|
150 |
154
|
jones
|
// Bind the values to the query
|
151 |
|
|
pstmt.setString(1, query);
|
152 |
|
|
} else {
|
153 |
|
|
pstmt = conn.prepareStatement(
|
154 |
|
|
"SELECT docid,docname,doctype,doctitle " +
|
155 |
|
|
"FROM xml_documents " +
|
156 |
|
|
"WHERE docid IN " +
|
157 |
|
|
"(SELECT docid " +
|
158 |
|
|
"FROM xml_nodes WHERE nodedata LIKE ? ) " +
|
159 |
|
|
"AND doctype = ?");
|
160 |
|
|
|
161 |
|
|
// Bind the values to the query
|
162 |
|
|
pstmt.setString(1, query);
|
163 |
|
|
pstmt.setString(2, requestedDoctype);
|
164 |
|
|
}
|
165 |
|
|
|
166 |
27
|
jones
|
pstmt.execute();
|
167 |
150
|
jones
|
ResultSet rs = pstmt.getResultSet();
|
168 |
|
|
boolean tableHasRows = rs.next();
|
169 |
|
|
while (tableHasRows) {
|
170 |
162
|
bojilova
|
docid = rs.getString(1);
|
171 |
150
|
jones
|
docname = rs.getString(2);
|
172 |
|
|
doctype = rs.getString(3);
|
173 |
|
|
doctitle = rs.getString(4);
|
174 |
27
|
jones
|
|
175 |
167
|
jones
|
StringBuffer document = new StringBuffer();
|
176 |
150
|
jones
|
document.append("<docid>").append(docid).append("</docid>");
|
177 |
167
|
jones
|
|
178 |
150
|
jones
|
if (docname != null) {
|
179 |
|
|
document.append("<docname>" + docname + "</docname>");
|
180 |
|
|
}
|
181 |
|
|
if (doctype != null) {
|
182 |
|
|
document.append("<doctype>" + doctype + "</doctype>");
|
183 |
|
|
}
|
184 |
|
|
if (doctitle != null) {
|
185 |
|
|
document.append("<doctitle>" + doctitle + "</doctitle>");
|
186 |
|
|
}
|
187 |
27
|
jones
|
|
188 |
150
|
jones
|
// Store the document id and the root node id
|
189 |
162
|
bojilova
|
docListResult.put(docid,(String)document.toString());
|
190 |
150
|
jones
|
|
191 |
|
|
// Advance to the next record in the cursor
|
192 |
|
|
tableHasRows = rs.next();
|
193 |
27
|
jones
|
}
|
194 |
|
|
pstmt.close();
|
195 |
|
|
} catch (SQLException e) {
|
196 |
|
|
System.out.println("Error getting id: " + e.getMessage());
|
197 |
|
|
}
|
198 |
|
|
|
199 |
150
|
jones
|
return docListResult;
|
200 |
27
|
jones
|
}
|
201 |
|
|
}
|
202 |
203
|
jones
|
|
203 |
|
|
/**
|
204 |
|
|
* '$Log$
|
205 |
349
|
jones
|
* 'Revision 1.17 2000/07/26 20:40:40 higgins
|
206 |
|
|
* 'no message
|
207 |
|
|
* '
|
208 |
296
|
higgins
|
* 'Revision 1.16 2000/06/26 10:35:05 jones
|
209 |
|
|
* 'Merged in substantial changes to DBWriter and associated classes and to
|
210 |
|
|
* 'the MetaCatServlet in order to accomodate the new UPDATE and DELETE
|
211 |
|
|
* 'functions. The command line tools and the parameters for the
|
212 |
|
|
* 'servlet have changed substantially.
|
213 |
|
|
* '
|
214 |
203
|
jones
|
* 'Revision 1.15.2.2 2000/06/25 23:38:16 jones
|
215 |
|
|
* 'Added RCSfile keyword
|
216 |
|
|
* '
|
217 |
|
|
* 'Revision 1.15.2.1 2000/06/25 23:34:17 jones
|
218 |
|
|
* 'Changed documentation formatting, added log entries at bottom of source files
|
219 |
|
|
* ''
|
220 |
|
|
*/
|