Revision 453
Added by berkley about 24 years ago
src/edu/ucsb/nceas/metacat/QuerySpecification.java | ||
---|---|---|
296 | 296 |
* This method prints sql based upon the <returnfield> tag in the |
297 | 297 |
* pathquery document. This allows for customization of the |
298 | 298 |
* returned fields |
299 |
* The parameters of the query are changed to upper case before the query |
|
300 |
* so that givenName is queryied the same as givenname. |
|
301 | 299 |
*/ |
302 | 300 |
public String printExtendedSQL() |
303 | 301 |
{ |
... | ... | |
331 | 329 |
//System.out.println(self.toString()); |
332 | 330 |
return self.toString(); |
333 | 331 |
} |
334 |
|
|
332 |
|
|
335 | 333 |
/** |
334 |
* |
|
335 |
*/ |
|
336 |
public static String printPackageSQL() |
|
337 |
{ |
|
338 |
StringBuffer self = new StringBuffer(); |
|
339 |
self.append("select z.nodedata, x.nodedata, y.nodedata from "); |
|
340 |
self.append("(select nodeid, parentnodeid from xml_index where path like "); |
|
341 |
self.append("'package/relation/subject') s, (select nodeid, parentnodeid "); |
|
342 |
self.append("from xml_index where path like "); |
|
343 |
self.append("'package/relation/relationship') rel, "); |
|
344 |
self.append("(select nodeid, parentnodeid from xml_index where path like "); |
|
345 |
self.append("'package/relation/object') o, "); |
|
346 |
self.append("xml_nodes x, xml_nodes y, xml_nodes z "); |
|
347 |
self.append("where s.parentnodeid = rel.parentnodeid "); |
|
348 |
self.append("and rel.parentnodeid = o.parentnodeid "); |
|
349 |
self.append("and x.parentnodeid in rel.nodeid "); |
|
350 |
self.append("and y.parentnodeid in o.nodeid "); |
|
351 |
self.append("and z.parentnodeid in s.nodeid "); |
|
352 |
//self.append("and z.nodedata like '%"); |
|
353 |
//self.append(docid); |
|
354 |
//self.append("%'"); |
|
355 |
return self.toString(); |
|
356 |
} |
|
357 |
|
|
358 |
/** |
|
336 | 359 |
* create a String description of the query that this instance represents. |
337 | 360 |
* This should become a way to get the XML serialization of the query. |
338 | 361 |
*/ |
... | ... | |
555 | 578 |
|
556 | 579 |
/** |
557 | 580 |
* '$Log$ |
581 |
* 'Revision 1.14 2000/08/31 21:20:39 berkley |
|
582 |
* 'changed xslf for new returnfield scheme. the returnfields are now returned as <param name="<returnfield>"> tags. |
|
583 |
* 'hThe sql for the returnfield query was redone to fix a previous problem with slow queries |
|
584 |
* ' |
|
558 | 585 |
* 'Revision 1.13 2000/08/23 22:55:38 berkley |
559 | 586 |
* 'changed the field names to be case-sensitive in the returnfields |
560 | 587 |
* ' |
src/edu/ucsb/nceas/metacat/DBQuery.java | ||
---|---|---|
121 | 121 |
* |
122 | 122 |
* @param xmlquery the xml serialization of the query (@see pathquery.dtd) |
123 | 123 |
*/ |
124 |
public Hashtable findDocuments(Reader xmlquery, String user, String group) { |
|
124 |
public Hashtable findDocuments(Reader xmlquery, String user, String group) |
|
125 |
//throws Exception |
|
126 |
{ |
|
125 | 127 |
Hashtable docListResult = new Hashtable(); |
126 | 128 |
PreparedStatement pstmt; |
127 | 129 |
String docid = null; |
... | ... | |
132 | 134 |
String updateDate = null; |
133 | 135 |
String fieldname = null; |
134 | 136 |
String fielddata = null; |
137 |
String relation = null; |
|
135 | 138 |
StringBuffer document = null; |
136 | 139 |
|
137 | 140 |
try { |
... | ... | |
215 | 218 |
} |
216 | 219 |
} |
217 | 220 |
} |
221 |
|
|
222 |
pstmt = conn.prepareStatement(qspec.printPackageSQL()); |
|
223 |
pstmt.execute(); |
|
224 |
rs = pstmt.getResultSet(); |
|
225 |
tableHasRows = rs.next(); |
|
226 |
String[][] relations = new String[2000][3]; |
|
227 |
int relLength=0; |
|
228 |
while(tableHasRows) |
|
229 |
{ |
|
230 |
relations[relLength][0] = rs.getString(1).trim(); |
|
231 |
relations[relLength][1] = rs.getString(2).trim(); |
|
232 |
relations[relLength][2] = rs.getString(3).trim(); |
|
233 |
relLength++; |
|
234 |
|
|
235 |
//this get's direct relations from the data. i.e. those relations |
|
236 |
//where the docid of the current document is in the subject tag. |
|
237 |
|
|
238 |
docid = rs.getString(1); |
|
239 |
metacatURL murl = new metacatURL(docid); |
|
240 |
if(murl.getURLType().equals("metacat")) |
|
241 |
{//we only want to process metacat urls here. |
|
242 |
String[] tempparam = murl.getParam(0); |
|
243 |
if(tempparam[0].equals("docid")) |
|
244 |
{ |
|
245 |
docid = tempparam[1]; |
|
246 |
document = new StringBuffer(); |
|
247 |
document.append("<relation>"); |
|
248 |
document.append("<relationtype>").append(rs.getString(2)); |
|
249 |
document.append("</relationtype>"); |
|
250 |
document.append("<relationdoc>").append(rs.getString(3)); |
|
251 |
document.append("</relationdoc>"); |
|
252 |
document.append("</relation>"); |
|
253 |
tableHasRows = rs.next(); |
|
254 |
if(docListResult.containsKey(docid)) |
|
255 |
{ |
|
256 |
String removedelement = (String)docListResult.remove(docid); |
|
257 |
docListResult.put(docid, removedelement + document.toString()); |
|
258 |
} |
|
259 |
else |
|
260 |
{ |
|
261 |
docListResult.put(docid, document.toString()); |
|
262 |
} |
|
263 |
} |
|
264 |
else |
|
265 |
{ |
|
266 |
//throw new Exception("Error in url. The first parameter must " + |
|
267 |
// "be the docid."); |
|
268 |
System.err.println("DBQuery: error in url"); |
|
269 |
} |
|
270 |
} |
|
271 |
} |
|
272 |
|
|
273 |
//this loop handles transitive relations. i.e. if two relation tags |
|
274 |
//both have the same object then the subject of each are related. |
|
275 |
for(int i=0; i<relLength; i++) |
|
276 |
{ |
|
277 |
for(int j=0; j<relLength; j++) |
|
278 |
{ |
|
279 |
if(i != j) |
|
280 |
{ |
|
281 |
if(relations[i][2].equals(relations[j][2])) |
|
282 |
{//the objects are the same. |
|
283 |
metacatURL murl = new metacatURL(relations[i][0]); |
|
284 |
//relations[i][0] contains the docid of document that |
|
285 |
//the new document is related to. |
|
286 |
String[] tempparam = murl.getParam(0); |
|
287 |
if(tempparam[0].equals("docid")) |
|
288 |
{ |
|
289 |
docid = tempparam[1]; |
|
290 |
document = new StringBuffer(); |
|
291 |
document.append("<relation>"); |
|
292 |
document.append("<relationtype>").append(relations[j][1]); |
|
293 |
document.append("</relationtype>"); |
|
294 |
document.append("<relationdoc>").append(relations[j][0]); |
|
295 |
//the relation is to the subject of the new document |
|
296 |
//instead of the object. |
|
297 |
// direct relation transitive relation |
|
298 |
// |-----------------| |-----------------| |
|
299 |
// subject -> (object = object) -> subject |
|
300 |
document.append("</relationdoc>"); |
|
301 |
document.append("</relation>"); |
|
302 |
if(docListResult.containsKey(docid)) |
|
303 |
{ |
|
304 |
String removedelement = (String)docListResult.remove(docid); |
|
305 |
docListResult.put(docid, removedelement + |
|
306 |
document.toString()); |
|
307 |
} |
|
308 |
else |
|
309 |
{ |
|
310 |
docListResult.put(docid, document.toString()); |
|
311 |
} |
|
312 |
} |
|
313 |
} |
|
314 |
} |
|
315 |
} |
|
316 |
} |
|
317 |
|
|
218 | 318 |
pstmt.close(); |
219 | 319 |
} catch (SQLException e) { |
220 | 320 |
System.err.println("Error getting id: " + e.getMessage()); |
... | ... | |
595 | 695 |
|
596 | 696 |
/** |
597 | 697 |
* '$Log$ |
698 |
* 'Revision 1.19 2000/09/12 17:37:07 bojilova |
|
699 |
* 'added check from "read" permission on "query" and "squery" actions |
|
700 |
* 'for connected user or for "public" connection |
|
701 |
* ' |
|
598 | 702 |
* 'Revision 1.18 2000/09/05 20:50:56 berkley |
599 | 703 |
* 'Added a method called getNodeContent which retrieves the content of a node in a document. If there are more than one nodes with the same name returned, it returns an array with all of the data. |
600 | 704 |
* ' |
src/edu/ucsb/nceas/metacat/MetaCatServlet.java | ||
---|---|---|
20 | 20 |
import java.io.BufferedReader; |
21 | 21 |
import java.io.File; |
22 | 22 |
import java.io.FileInputStream; |
23 |
import java.io.DataInputStream; |
|
23 | 24 |
import java.util.Enumeration; |
24 | 25 |
import java.util.Hashtable; |
25 | 26 |
import java.util.ResourceBundle; |
... | ... | |
31 | 32 |
import java.sql.Connection; |
32 | 33 |
import java.sql.SQLException; |
33 | 34 |
import java.lang.reflect.*; |
35 |
import java.net.*; |
|
34 | 36 |
|
35 | 37 |
import javax.servlet.ServletConfig; |
36 | 38 |
import javax.servlet.ServletContext; |
... | ... | |
274 | 276 |
} catch (SQLException se) { |
275 | 277 |
out.println(se.getMessage()); |
276 | 278 |
} |
277 |
} else if (action.equals("insert") || action.equals("update")) { |
|
279 |
} |
|
280 |
else if (action.equals("getrelateddocument")) { |
|
281 |
try { |
|
282 |
handleGetRelatedDocumentAction(out, params, response); |
|
283 |
} catch (ClassNotFoundException e) { |
|
284 |
out.println(e.getMessage()); |
|
285 |
} catch (SQLException se) { |
|
286 |
out.println(se.getMessage()); |
|
287 |
} |
|
288 |
} |
|
289 |
else if (action.equals("insert") || action.equals("update")) { |
|
278 | 290 |
if ( !username.equals("public") && (username != null) ) { |
279 | 291 |
handleInsertOrUpdateAction(out, params, response, username, groupname); |
280 | 292 |
} else { |
... | ... | |
598 | 610 |
} |
599 | 611 |
|
600 | 612 |
/** |
613 |
* Handle the database getrelateddocument request and return a XML document, |
|
614 |
* possibly transformed from XML into HTML |
|
615 |
*/ |
|
616 |
private void handleGetRelatedDocumentAction(PrintWriter out, Hashtable params, |
|
617 |
HttpServletResponse response) |
|
618 |
throws ClassNotFoundException, IOException, SQLException |
|
619 |
{ |
|
620 |
String docid = null; |
|
621 |
Connection conn = null; |
|
622 |
|
|
623 |
if(params.containsKey("url")) |
|
624 |
{//the identifier for the related document is contained in the URL param |
|
625 |
try |
|
626 |
{ |
|
627 |
DocumentImpl xmldoc=null; |
|
628 |
metacatURL murl = new metacatURL(((String[])params.get("url"))[0]); |
|
629 |
if(murl.getURLType().equals("metacat")) |
|
630 |
{//get the document from the database if it is the right type of url |
|
631 |
String[] murlParams = murl.getParam(0); |
|
632 |
if(murlParams[0].equals("docid")) |
|
633 |
{//the docid should be first |
|
634 |
murl.printParams(); |
|
635 |
docid = murlParams[1]; //get the docid value |
|
636 |
conn = util.getConnection(); |
|
637 |
xmldoc = new DocumentImpl(conn, docid); |
|
638 |
|
|
639 |
//************************************************** |
|
640 |
//the style sheet handling code needs to be put here. |
|
641 |
out.println(xmldoc.toString()); |
|
642 |
//************************************************** |
|
643 |
} |
|
644 |
else |
|
645 |
{ |
|
646 |
//throw new Exception("handleGetDocument: bad URL"); |
|
647 |
System.err.println("handleGetDocument: bad URL"); |
|
648 |
} |
|
649 |
} |
|
650 |
else if(murl.getURLType().equals("http")) |
|
651 |
{//get the document from the internet |
|
652 |
String[] murlParams = murl.getParam(0); |
|
653 |
if(murlParams[0].equals("httpurl")) |
|
654 |
{//httpurl is the param name for an http url. |
|
655 |
URL urlconn = new URL(murlParams[1]); //create a new url obj. |
|
656 |
DataInputStream htmldoc = new DataInputStream(urlconn.openStream()); |
|
657 |
//bind a data stream. |
|
658 |
try |
|
659 |
{ //display the document |
|
660 |
String line=null; |
|
661 |
while((line = htmldoc.readLine()) != null) |
|
662 |
{ |
|
663 |
out.println(line); |
|
664 |
} |
|
665 |
} |
|
666 |
catch(Exception e) |
|
667 |
{ |
|
668 |
util.debugMessage("error viewing html document"); |
|
669 |
} |
|
670 |
} |
|
671 |
} |
|
672 |
} |
|
673 |
catch (McdbException e) { |
|
674 |
response.setContentType("text/xml"); |
|
675 |
e.toXml(out); |
|
676 |
} catch (Throwable t) { |
|
677 |
response.setContentType("text/html"); |
|
678 |
out.println(t.getMessage()); |
|
679 |
} finally { |
|
680 |
util.returnConnection(conn); |
|
681 |
} |
|
682 |
} |
|
683 |
} |
|
684 |
|
|
685 |
/** |
|
601 | 686 |
* Handle the database getdocument request and return a XML document, |
602 | 687 |
* possibly transformed from XML into HTML |
603 | 688 |
*/ |
Also available in: Unified diff
Added functionality for package specifications. metacatservlet now contains a new action called getrelateddocument that handles retrieving related documents using the metacatURL specification (metacatURL.java). DBQuery contains new code in runQuery that embeds relation tags in the returned hashtable describing the documents related to each docid. querySpecification contains a new method which prints the sql that does the relation query.