1
|
/**
|
2
|
* Name: DBSimpleQuery.java
|
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
|
* Version: '$Id: DBSimpleQuery.java 168 2000-06-16 03:20:41Z jones $'
|
12
|
*/
|
13
|
|
14
|
package edu.ucsb.nceas.metacat;
|
15
|
|
16
|
import java.io.*;
|
17
|
import java.net.URL;
|
18
|
import java.net.MalformedURLException;
|
19
|
import java.sql.*;
|
20
|
import java.util.Stack;
|
21
|
import java.util.Hashtable;
|
22
|
import java.util.Enumeration;
|
23
|
|
24
|
/**
|
25
|
* A Class that searches a relational DB for elements and attributes that
|
26
|
* have free text matches to the query string. It returns a result set
|
27
|
* consisting of the root nodeid for each document that satisfies the query
|
28
|
*/
|
29
|
public class DBSimpleQuery {
|
30
|
|
31
|
static String defaultDB = "jdbc:oracle:thin:@localhost:1521:test";
|
32
|
private Connection conn = null;
|
33
|
|
34
|
/**
|
35
|
* the main routine used to test the DBSimpleQuery utility.
|
36
|
*
|
37
|
* Usage: java DBSimpleQuery <query> <user> <password> [dbstring]
|
38
|
*
|
39
|
* @param query the text to search for in the element and attribute content
|
40
|
* @param user the username to use for the database connection
|
41
|
* @param password the password to use for the database connection
|
42
|
* @param dbstring the connection info to use for the database connection
|
43
|
*/
|
44
|
static public void main(String[] args) {
|
45
|
|
46
|
if (args.length < 4)
|
47
|
{
|
48
|
System.err.println("Wrong number of arguments!!!");
|
49
|
System.err.println("USAGE: java DBSimpleQuery " +
|
50
|
"<query> <doctype> <user> <password> [dbstring]");
|
51
|
return;
|
52
|
} else {
|
53
|
try {
|
54
|
|
55
|
String query = args[0];
|
56
|
String doctype = args[1];
|
57
|
String user = args[2];
|
58
|
String password = args[3];
|
59
|
String dbstring = null;
|
60
|
|
61
|
if (args.length <= 4) {
|
62
|
dbstring = defaultDB;
|
63
|
} else {
|
64
|
dbstring = args[4];
|
65
|
}
|
66
|
|
67
|
// Open a connection to the database
|
68
|
Connection dbconn = MetaCatUtil.openDBConnection(
|
69
|
"oracle.jdbc.driver.OracleDriver",
|
70
|
dbstring, user, password);
|
71
|
// Execute the simple query
|
72
|
DBSimpleQuery rd = new DBSimpleQuery(dbconn);
|
73
|
Hashtable nodelist = null;
|
74
|
if (doctype.equals("any") || doctype.equals("ANY")) {
|
75
|
nodelist = rd.findDocuments(query);
|
76
|
} else {
|
77
|
nodelist = rd.findDocuments(query, doctype);
|
78
|
}
|
79
|
|
80
|
// Print the reulting root nodes
|
81
|
StringBuffer result = new StringBuffer();
|
82
|
String document = null;
|
83
|
String docid = null;
|
84
|
result.append("<?xml version=\"1.0\"?>\n");
|
85
|
result.append("<resultset>\n");
|
86
|
result.append(" <query>" + query + "</query>\n");
|
87
|
Enumeration doclist = nodelist.keys();
|
88
|
while (doclist.hasMoreElements()) {
|
89
|
docid = (String)doclist.nextElement();
|
90
|
document = (String)nodelist.get(docid);
|
91
|
result.append(" <document>\n " + document +
|
92
|
"\n </document>\n");
|
93
|
}
|
94
|
result.append("</resultset>\n");
|
95
|
|
96
|
System.out.println(result);
|
97
|
|
98
|
} catch (Exception e) {
|
99
|
System.err.println("EXCEPTION HANDLING REQUIRED");
|
100
|
System.err.println(e.getMessage());
|
101
|
e.printStackTrace(System.err);
|
102
|
}
|
103
|
}
|
104
|
}
|
105
|
|
106
|
/**
|
107
|
* construct an instance of the DBSimpleQuery class
|
108
|
*
|
109
|
* <p>Generally, one would call the findDocuments() routine after creating
|
110
|
* an instance to specify the search query</p>
|
111
|
*
|
112
|
* @param conn the JDBC connection that we use for the query
|
113
|
*/
|
114
|
public DBSimpleQuery( Connection conn )
|
115
|
throws IOException,
|
116
|
SQLException,
|
117
|
ClassNotFoundException
|
118
|
{
|
119
|
this.conn = conn;
|
120
|
}
|
121
|
|
122
|
/**
|
123
|
* routine to search the elements and attributes looking to match query
|
124
|
*
|
125
|
* @param query the text to search for
|
126
|
*/
|
127
|
public Hashtable findDocuments(String query) {
|
128
|
return this.findDocuments(query, null);
|
129
|
}
|
130
|
|
131
|
/**
|
132
|
* routine to search the elements and attributes looking to match query
|
133
|
*
|
134
|
* @param query the text to search for
|
135
|
* @param requestedDoctype the type of documents to return from the query
|
136
|
*/
|
137
|
public Hashtable findDocuments(String query, String requestedDoctype) {
|
138
|
Hashtable docListResult = new Hashtable();
|
139
|
|
140
|
PreparedStatement pstmt;
|
141
|
|
142
|
// Now look up the document id
|
143
|
String docid = null;
|
144
|
String docname = null;
|
145
|
String doctype = null;
|
146
|
String doctitle = null;
|
147
|
//StringBuffer document = null;
|
148
|
|
149
|
try {
|
150
|
if (requestedDoctype == null ||
|
151
|
requestedDoctype.equals("any") ||
|
152
|
requestedDoctype.equals("ANY")) {
|
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
|
|
160
|
// Bind the values to the query
|
161
|
pstmt.setString(1, query);
|
162
|
} else {
|
163
|
pstmt = conn.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
|
"AND doctype = ?");
|
170
|
|
171
|
// Bind the values to the query
|
172
|
pstmt.setString(1, query);
|
173
|
pstmt.setString(2, requestedDoctype);
|
174
|
}
|
175
|
|
176
|
pstmt.execute();
|
177
|
ResultSet rs = pstmt.getResultSet();
|
178
|
boolean tableHasRows = rs.next();
|
179
|
while (tableHasRows) {
|
180
|
docid = rs.getString(1);
|
181
|
docname = rs.getString(2);
|
182
|
doctype = rs.getString(3);
|
183
|
doctitle = rs.getString(4);
|
184
|
|
185
|
StringBuffer document = new StringBuffer();
|
186
|
document.append("<docid>").append(docid).append("</docid>");
|
187
|
|
188
|
if (docname != null) {
|
189
|
document.append("<docname>" + docname + "</docname>");
|
190
|
}
|
191
|
if (doctype != null) {
|
192
|
document.append("<doctype>" + doctype + "</doctype>");
|
193
|
}
|
194
|
if (doctitle != null) {
|
195
|
document.append("<doctitle>" + doctitle + "</doctitle>");
|
196
|
}
|
197
|
|
198
|
// Store the document id and the root node id
|
199
|
docListResult.put(docid,(String)document.toString());
|
200
|
|
201
|
// Advance to the next record in the cursor
|
202
|
tableHasRows = rs.next();
|
203
|
}
|
204
|
pstmt.close();
|
205
|
} catch (SQLException e) {
|
206
|
System.out.println("Error getting id: " + e.getMessage());
|
207
|
}
|
208
|
|
209
|
return docListResult;
|
210
|
}
|
211
|
}
|