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