Project

General

Profile

1
/**
2
 *  '$RCSfile$'
3
 *    Purpose: class that implements the MARINE interface to the 
4
 *             metadata database.
5
 *  Copyright: 2000 Regents of the University of California and the
6
 *             National Center for Ecological Analysis and Synthesis
7
 *    Authors: Chad Berkley
8
 * 
9
 *   '$Author: berkley $'
10
 *     '$Date: 2000-08-07 08:13:15 -0700 (Mon, 07 Aug 2000) $'
11
 * '$Revision: 323 $'
12
 */
13
 
14
package edu.ucsb.nceas.metacat.marine;
15

    
16

    
17
import java.io.*;
18
import java.util.*;
19
import javax.servlet.*;
20
import javax.servlet.http.*;
21
import java.net.*;
22
import java.sql.*;
23
import org.xml.sax.*;
24
import org.xml.sax.helpers.*;
25
import edu.ucsb.nceas.metacat.*;
26
import oracle.jdbc.driver.*;
27
import oracle.xml.parser.v2.*;
28
import oracle.xml.parser.v2.XMLParser;
29

    
30
public class marineServlet extends edu.ucsb.nceas.metacat.MetaCatServlet 
31
{
32
	private marineUtil util;
33
	private String resultStyleURL;
34
	private String docStyleURL;
35
	private String xmlcatalogfile;
36
	private String saxparser;
37
	private String defaultdatapath;
38
	private String executescript;
39
	private DBQuery queryobj;
40
	private DBReader docreader;
41
	private DBTransform dbt;
42
	private Connection conn;
43
	private PropertyResourceBundle options;
44
	private String msg=null;
45
	private String queryname;
46
	private ServletConfig		config = null;
47
        private ServletContext	context = null;
48
	
49
	/**
50
	  Initializes the servlet and creates a connection to 
51
	  the database
52
 	**/
53

    
54
	public void init(PrintWriter out) 
55
	{
56
		util = new marineUtil();
57
		docStyleURL = util.getOption("docStyleURL");
58
		resultStyleURL = util.getOption("resultStyleURL");
59
		xmlcatalogfile = util.getOption("xmlcatalogfile");
60
      		saxparser = util.getOption("saxparser");
61
      		defaultdatapath = util.getOption("defaultdatapath");
62
      		executescript = util.getOption("executescript");
63

    
64
      		try
65
      		{
66
      		    conn = util.openDBConnection();  
67
      			queryobj = new DBQuery(conn,saxparser);
68
        		docreader = new DBReader(conn);
69
        		dbt = new DBTransform(conn);
70
      		}
71
      		catch(Exception e)
72
      		{
73
      			System.err.println("marineServlet: Error in init:");
74
      		    e.printStackTrace(System.err);
75
      		}
76
	}
77

    
78
	/**
79
	  Handles any GET requests 
80
	**/
81
	public void doGet (HttpServletRequest request, 
82
	                   HttpServletResponse response)
83
    			throws ServletException, IOException 
84
    	{
85
    		handleGetOrPost(request, response);
86
    	}
87
    	
88
    	/**
89
    	  Handles and POST requests
90
    	**/
91
    	public void doPost (HttpServletRequest request, 
92
    	                    HttpServletResponse response)
93
    			throws ServletException, IOException 
94
    	{
95
    		handleGetOrPost(request, response);
96
    	}
97
    	
98
    	/**
99
    	  This method is called from doGet and doPost.  It handles
100
    	  each request and sends back the response. It also handles
101
    	  the XSL tranformations
102
    	**/
103
    	private void handleGetOrPost(HttpServletRequest request, 
104
    			HttpServletResponse response) 
105
    			throws ServletException, IOException 
106
    	{
107
    		
108
    		response.setContentType("text/html");
109
    		PrintWriter out = response.getWriter();
110
    		
111
    		try
112
    		{
113
    			init(out);
114
		}
115
    		catch(Exception e)
116
      		{
117
      			System.err.println("marineServlet: Exception in init in handleGetOrPost (try1): ");
118
      			e.printStackTrace(System.err);
119
      		}
120
    		
121
    		String name = null;
122
    		String[] value = null;
123
    		String[] docid = new String[3];
124
    		Hashtable params = new Hashtable();
125
    		Enumeration paramlist = request.getParameterNames();
126
    		while (paramlist.hasMoreElements()) 
127
    		{
128
      			name = (String)paramlist.nextElement();
129
      			value = request.getParameterValues(name);
130

    
131
      			// Decode the docid and mouse click information
132
      			if (name.endsWith(".y")) 
133
      			{
134
        			docid[0] = name.substring(0,name.length()-2);
135
        			//out.println("docid => " + docid[0]);
136
        			params.put("docid", docid);
137
        			name = "ypos";
138
      			}
139
      			if (name.endsWith(".x")) 
140
      			{
141
        			name = "xpos";
142
      			}
143

    
144
      			//out.println(name + " => " + value[0]);
145
      			params.put(name,value);
146
    		}
147
		
148
		// Determine what type of request the user made
149
    		// if the action parameter is set, use it as a default
150
    		// but if the ypos param is set, calculate the action
151
    		// needed
152
    		String action = ((String[])params.get("action"))[0];
153
    		long ypos = 0;
154
    		try 
155
    		{
156
      			ypos = (new Long(
157
      			((String[])params.get("ypos"))[0]).longValue());
158
      			
159
      			//out.println("<P>YPOS IS " + ypos);
160
      			if (ypos <= 20) 
161
      			{
162
        			action = "viewabstract";
163
      			} 
164
      			else if (ypos > 20 && ypos <= 45) 
165
      			{
166
        			action = "viewdocumentation";
167
      			} 
168
      			else if (ypos > 45) 
169
      			{
170
        			action = "download";
171
      			}
172
    		} 
173
    		catch (Exception npe) 
174
    		{
175
    			System.err.println("marineServlet: Error in handleGetOrPost (try2): ");
176
      			npe.printStackTrace(System.err);
177
    		}
178
    		
179
    		//Jivka's login code should go here!
180
    		
181
    		try{
182
    		   if(action.equals("query") || action.equals("squery")) 
183
    		   {
184
    		      //send any type of query action to handleQueryAction
185
      			   handleQueryAction(out, params, response);
186
    		   }
187
    		   else if(action.equals("init"))
188
    		   {
189
    		   	   handleInitAction(out);
190
    		   }
191
    		   else if(action.equals("viewabstract"))
192
    		   {
193
    		       //when the user clicks the view abstract graphic
194
    			   handleViewAbstractAction(out, params, response);
195
    		   }
196
    		   else if(action.equals("viewdocumentation"))
197
    		   {
198
    		       //when the user clicks the view documentation graphic
199
    		 	   handleViewDocumentationAction(out, params, response);
200
    		   }
201
    		   else if(action.equals("download"))
202
    		   {
203
    		       //when the user clicks the download graphic
204
    			   handleDownloadAction(out, params, response);
205
    		   }
206
    		   else 
207
    		   {
208
      			   out.println("Error: action " + action + " not registered" +
209
      			               "Please report this error.");
210
    		   }
211
    		   
212
    		   conn.close();
213
    		}
214
		catch(Exception e)
215
		{
216
			System.err.println("marineServlet: Error in handleGetOrPost (try3)");
217
			e.printStackTrace(System.err);
218
		}
219
		System.err.println("msg: " + msg);
220
		out.close();
221
    	}
222
	
223
        /**
224
          Handles when the user clicks the View Abstract graphic.
225
        **/
226
	private void handleViewAbstractAction(PrintWriter out, Hashtable params, 
227
               HttpServletResponse response)
228
        {
229
         	PreparedStatement pstatement;
230
         	ResultSet rs = null;
231
        	String markup = null;
232
        	String docid = ((String[])params.get("docid"))[0];
233
        	
234
        	//this query is based on the eml-resource datatype
235
        	//it will NOT work with eml-dataset documents because
236
        	//eml-dataset removes the abstract data an extra level
237
        	//from the <abstract> tag.
238
        	String querysql = "select nodedata from xml_nodes" +
239
                             " where parentnodeid in" +
240
                             " (select nodeid from xml_nodes where" +
241
                             " nodename like 'abstract') and docid like '" +
242
                             docid + "'";
243
                try{
244
                   //create the query, execute it, get the result
245
                   pstatement = conn.prepareStatement(querysql);
246
                   pstatement.execute();
247
                   rs = pstatement.getResultSet();
248
                
249
                   boolean moreRows = rs.next();
250
                   //print out the html.  This does not use any XSL
251
                   markup = "<html><body bgcolor=\"white\"><h1>Abstract</h1>";
252
                   while(moreRows)
253
                   {
254
                	   markup += "<p>" + rs.getString(1) + "</p>";
255
                	   moreRows = rs.next();
256
                   }
257
                   markup += "</body></html>";
258
                   }
259
                catch(SQLException sqle)
260
                {
261
                	
262
                	System.err.println("marineServlet: SQLError " +
263
                	                   "in handleViewAbstractAction");
264
                	sqle.printStackTrace(System.err);
265
                } 
266
                catch(Exception e)
267
                {
268
                	System.err.println("marineServlet: Error " +
269
                	                   "in handleViewAbstractAction");
270
                	e.printStackTrace(System.err);
271
                }
272
                out.println(markup); //send the output to the browser
273
        }
274
        
275
        /**
276
          Handles when the user clicks the View Documentation graphic.
277
        **/
278
        private void handleViewDocumentationAction(PrintWriter out, Hashtable params, 
279
               HttpServletResponse response)
280
        {
281
        	String doc = null;
282
        	String docid = ((String[])params.get("docid"))[0];
283
        	out.println("view documentation not yet implemented");
284
		try{        	
285
        	    doc = docreader.readXMLDocument(docid);
286
    		    
287
        	    XMLDocumentFragment htmldoc = null;
288
        	    XSLStylesheet style = new XSLStylesheet(
289
                                    new URL(docStyleURL), null);
290
          	    htmldoc = (new XSLProcessor()).processXSL(style, 
291
                                   (Reader)(new StringReader
292
                                   (doc)),null);
293
          	    htmldoc.print(out);
294
          	}
295
          	catch(Exception e)
296
          	{
297
          		System.err.println("marineServlet: error in " +
298
          		                   "handleViewDocumentationAction");
299
          		e.printStackTrace(System.err);
300
          	}
301
        }
302
        
303
        private void handleInitAction(PrintWriter out)
304
        {
305
        	try{
306
      			StringBuffer temp = new StringBuffer();
307
      			XMLDocumentFragment html = null;
308
      			
309
      			temp.append("<?xml version=\"1.0\"?>\n");
310
      		    temp.append("<resultset></resultset>\n");
311
      		      
312
      		    //create a null query and run it through the
313
      		    //xsl processor to create the opening page      			
314
      			   
315
      			XSLStylesheet style = new XSLStylesheet(
316
                                 new URL(resultStyleURL), null);
317
           	    html = (new XSLProcessor()).processXSL(style, 
318
                                   (Reader)(new StringReader(temp.toString())), null);
319
          		html.print(out);
320
                }
321
                catch(Exception e)
322
          	{
323
          		System.err.println("marineServlet: error in handleQueryAction (try1)");
324
          		e.printStackTrace(System.err);
325
          	}
326
        }
327
        
328
        /**
329
          Handles when the user clicks the Download graphic.
330
        **/
331
        private void handleDownloadAction(PrintWriter out, Hashtable params, 
332
               HttpServletResponse response)
333
	{
334
		out.println("download not yet implemented");
335
	}
336
	
337
	/**
338
	  This method handles any and all query requests including the 
339
	  init request.  It separates the queries into 3 classes.
340
	  1) query-This executes a free text search.
341
	  2) squery-This executes a structured query.
342
	  3) init-This executes a null query.
343
	  After executing the query, the xml results are sent through
344
	  the XSL processor and returned to the browser as HTML (if
345
	  qformat="html").  If qformat="xml" the xml document is returned
346
	  without transformation.
347
	**/
348
  	private void handleQueryAction(PrintWriter out, Hashtable params, 
349
               HttpServletResponse response)
350
        {
351
        	String query = ((String[])params.get("query"))[0];
352
      		String action = ((String[])params.get("action"))[0];
353
      		Hashtable doclist = null;
354
      		String[] doctypeArr = null;
355
      		String doctype = null;
356
      		Reader xmlquery = null;
357
      		Hashtable extendedQuery = new Hashtable();
358
		
359
      		if(action.equals("query")) 
360
      		{
361
      		     
362
       	 		doctypeArr = (String[])params.get("doctype");
363
        		doctype = null;
364
        		if (doctypeArr != null) 
365
        		{
366
          			doctype = ((String[])params.get("doctype"))[0]; 
367
        		}
368

    
369
        		if (doctype != null) 
370
        		{
371
          			xmlquery = new StringReader(DBQuery.createQuery(query,doctype));
372
        		} 
373
        		else 
374
        		{
375
          			xmlquery = new StringReader(DBQuery.createQuery(query));
376
        		}
377
      		} 
378
      		else if(action.equals("squery")) 
379
      		{ 
380
      			doctypeArr = (String[])params.get("doctype");
381
        		doctype = null;
382
        		if (doctypeArr != null) 
383
        		{
384
          			doctype = ((String[])params.get("doctype"))[0]; 
385
        		}
386
        		// for some reason the doctype="ANY" wildcard is not working
387
        		//correctly. For this reason, I have ommited the doctype
388
        		//here for the time being (7/20). 
389
        		xmlquery = new StringReader(createSQuery(params));
390
      		} 
391
      		else if(action.equals("init"))
392
      		{
393
      			try{
394
      			   StringBuffer temp = new StringBuffer();
395
      			   XMLDocumentFragment html = null;
396
      			
397
      			   temp.append("<?xml version=\"1.0\"?>\n");
398
      		       temp.append("<resultset></resultset>\n");
399
      		       
400
      		       //create a null query and run it through the
401
      		       //xsl processor to create the opening page      			
402
      			   
403
      			   XSLStylesheet style = new XSLStylesheet(
404
                                    new URL(resultStyleURL), null);
405
          		   html = (new XSLProcessor()).processXSL(style, 
406
                                   (Reader)(new StringReader(temp.toString())), null);
407
          		   html.print(out);
408
          		   }
409
          		catch(Exception e)
410
          		{
411
          			System.err.println("marineServlet: error in handleQueryAction (try1)");
412
          			e.printStackTrace(System.err);
413
          		}
414
      		}
415
      		else 
416
      		{
417
        		msg += "marineServlet: Error handling query -- illegal action value";
418
      		}
419
      
420
      		if (queryobj != null) 
421
      		{
422
      			doclist = queryobj.findDocuments(xmlquery);
423
      			//msg += "\n\n" + doclist.toString();
424
      		} 
425
      		else 
426
      		{
427
        		out.println("marineServlet: Query Object Init failed.");
428
        		return;
429
      		}
430
 
431
 		//get the date_created, date_updated, organization
432
 		//and owner information for the query request
433
 		extendedQuery = createExtendedQuery(doclist);
434
 
435
      		// Create a buffer to hold the xml result
436
      		StringBuffer resultset = new StringBuffer();
437
 
438
      		// Print the resulting root nodes
439
      		String docid;
440
      		String document = null;
441
      		resultset.append("<?xml version=\"1.0\"?>\n");
442
      		resultset.append("<resultset>\n");
443
      		//queryname contains the xmlquery.  This sends the query
444
      		//itself back to the page to refresh the text boxes
445
      		//Without the queryname, the text boxes do not retain their
446
      		//values between queries.
447
      		resultset.append("  <query>" + queryname + "</query>");
448
      		Enumeration doclistkeys = doclist.keys(); 
449
      		//add the returned documents to the xml result
450
      		while(doclistkeys.hasMoreElements()) 
451
      		{
452
        		docid = (String)doclistkeys.nextElement();
453
        		document = (String)doclist.get(docid);
454
        		if(extendedQuery.containsKey(docid))
455
        		{
456
        			resultset.append("  <document>" + document +
457
        			          extendedQuery.get(docid).toString() +
458
        			          "</document>");
459
        		}
460
        		else
461
        		{
462
        			resultset.append("  <document>" + document + 
463
        		                 "</document>");
464
        		}
465
      		}
466
      		resultset.append("</resultset>");
467

    
468
      		String qformat = ((String[])params.get("qformat"))[0]; 
469
      		if (qformat.equals("xml")) 
470
      		{
471
        		// set content type and other response 
472
        		// header fields first
473
        		response.setContentType("text/xml");
474
        		out.println(resultset.toString());
475
      		} 
476
      		else if(qformat.equals("html")) 
477
      		{
478
        		// set content type and other response header
479
        		// fields first
480
        		response.setContentType("text/html");
481
			
482
			//transform the xml to html with XSL
483
        		XMLDocumentFragment htmldoc = null;
484
        		
485
        		try  
486
        		{
487
          			XSLStylesheet style = new XSLStylesheet(
488
                                    new URL(resultStyleURL), null);
489
          			htmldoc = (new XSLProcessor()).processXSL(style, 
490
                                   (Reader)(new StringReader
491
                                   (resultset.toString())),null);
492
          			htmldoc.print(out);
493
        		} 
494
        		catch (Exception e) 
495
        		{
496
          			System.err.println("marineServlet: error in handleQueryAction (try2)"); 
497
          	        e.printStackTrace(System.err);
498
        		}
499
      		}
500
      	}
501
      	
502
      	/**
503
      	   creates the sql query for the extended information
504
      	   displayed upon successful query
505
      	**/
506
      	private Hashtable createExtendedQuery(Hashtable docs)
507
      	{
508
      		Hashtable result = new Hashtable();
509
      		ResultSet givenNameResults = null;
510
      		ResultSet surNameResults = null;
511
      		ResultSet organizationNameResults = null;
512
      		ResultSet dateResults = null;
513
      		String docid = null;
514
      		String data = null;
515
      		Object docidObj = null;
516
      		PreparedStatement pstatement;
517
      		
518
      		Enumeration docids = docs.keys();
519
      		
520
      		//find all of the owners first names
521
		String givenNameQuery = "select docid, nodedata from xml_nodes" +
522
		   " where parentnodeid in" +
523
		   " (select nodeid from xml_index where path like 'givenName')" +
524
		   " and parentnodeid in" +
525
 		   " (select nodeid from xml_nodes where nodename like 'givenName'" +
526
  		   " and parentnodeid in" +
527
                   " (select nodeid from xml_nodes where nodename like 'individualName'" +
528
                   " and parentnodeid in" +
529
                   " (select parentnodeid from xml_nodes where nodename like 'roleCode'" +
530
                   " and nodeid in" +
531
                   " (select parentnodeid from xml_nodes where" +
532
                   " nodedata = 'Owner'" +
533
                   " and parentnodeid in" +
534
                   " (select nodeid from xml_index where path like 'roleCode')))))";
535
                
536
                //find all of the owners last names
537
		String surNameQuery = "select docid, nodedata from xml_nodes" +
538
		   " where parentnodeid in" +
539
		   " (select nodeid from xml_index where path like 'surName')" +
540
		   " and parentnodeid in" +
541
 		   " (select nodeid from xml_nodes where nodename like 'surName'" +
542
  		   " and parentnodeid in" +
543
                   " (select nodeid from xml_nodes where nodename like 'individualName'" +
544
                   " and parentnodeid in" +
545
                   " (select parentnodeid from xml_nodes where nodename like 'roleCode'" +
546
                   " and nodeid in" +
547
                   " (select parentnodeid from xml_nodes where" +
548
                   " nodedata = 'Owner'" +
549
                   " and parentnodeid in" +
550
                   " (select nodeid from xml_index where path like 'roleCode')))))";
551

    
552
		//find all of the organizations
553
		String organizationNameQuery = "select docid, nodedata from xml_nodes" +
554
		   " where parentnodeid in" +
555
		   " (select nodeid from xml_index where path like 'organizationName')";
556
                
557
                //find all of the creation and update dates
558
                String dateQuery = "select docid, date_created, date_updated" +
559
                   " from xml_documents";
560
                   
561
            //execute the queries
562
            try{  
563
                pstatement = conn.prepareStatement(givenNameQuery);
564
                pstatement.execute();
565
                givenNameResults = pstatement.getResultSet();
566
                
567
                pstatement = conn.prepareStatement(surNameQuery);
568
                pstatement.execute();
569
                surNameResults = pstatement.getResultSet();
570
		
571
		pstatement = conn.prepareStatement(organizationNameQuery);
572
                pstatement.execute();
573
                organizationNameResults = pstatement.getResultSet();
574
      		
575
      		pstatement = conn.prepareStatement(dateQuery);
576
                pstatement.execute();
577
                dateResults = pstatement.getResultSet();
578
                }
579
             catch(Exception e)
580
             {
581
                System.err.println("marineServlet: error in createExtendedQuery(try1)");
582
                e.printStackTrace(System.err);
583
             }
584
             
585
             try{
586
                 boolean moreRows = givenNameResults.next();
587
                 //add the givenName information to the xml.
588
                 while(moreRows)
589
                 {
590
                 	docid = givenNameResults.getString(1);
591
             	    data = givenNameResults.getString(2);
592
             	    //check if the original result set contains this docid
593
             	    if(docs.containsKey(docid))
594
             	    {
595
             		    String markup = "<givenName>" + data + "</givenName>";
596
             		    result.put(docid, markup);
597
             	    }
598
             	    moreRows = givenNameResults.next();
599
                 }	
600
             
601
                 moreRows = surNameResults.next();
602
                 //add the surName information to the xml.
603
                 while(moreRows)
604
                 {
605
             	    docid = surNameResults.getString(1);
606
             	    data = surNameResults.getString(2);
607
             	    //check if the original result set contains this docid
608
             	    if(docs.containsKey(docid))
609
             	    {
610
             		    //check if this key is already in the result hashtable
611
             		    //i.e. there was a givenName for that docid.
612
             		    if(result.containsKey(docid))
613
             		    {
614
             			    String markup = result.get(docid).toString();
615
             			    markup += "<surName>" + data + "</surName>";
616
             			    result.remove(docid);
617
             			    result.put(docid, markup);
618
             		    }
619
             		    else
620
             	 	    { 
621
             			    String markup = "<surName>" + data + "</surName>";
622
             			    result.put(docid, markup);
623
             		    }
624
             	    }
625
             	    moreRows = surNameResults.next();
626
                 }
627
                 
628
                 //add the organizationName information to the xml.
629
                 moreRows = organizationNameResults.next();
630
                 while(moreRows)
631
                 {
632
             	    docid = organizationNameResults.getString(1);
633
             	    data = organizationNameResults.getString(2);
634
             	    //check if the original result set contains this docid
635
             	    if(docs.containsKey(docid))
636
             	    {
637
             		    //check if this key is already in the result hashtable
638
             		    //i.e. there was a givenName for that docid.
639
             		    if(result.containsKey(docid))
640
             		    {
641
             			    String markup = result.get(docid).toString();
642
             			    markup += "<organizationName>" + data + "</organizationName>";
643
             			    result.remove(docid);
644
             			    result.put(docid, markup);
645
             		    }
646
             		    else
647
             	 	    { 
648
             			    String markup = "<organizationName>" + data + "</organizationName>";
649
             			    result.put(docid, markup);
650
             		    }
651
             	    }
652
             	    moreRows = organizationNameResults.next();
653
                 }
654
                 
655
                 moreRows = dateResults.next();
656
                 //add the date information to the xml.
657
                 while(moreRows)
658
                 {
659
             	    docid = dateResults.getString(1);
660
             	    data = dateResults.getString(2); //date_created
661
             	    String data2 = dateResults.getString(3); //date_updated
662
             	    //check if the original result set contains this docid
663
             	    if(docs.containsKey(docid))
664
             	    {
665
             		    //check if this key is already in the result hashtable
666
             		    //i.e. there was a givenName for that docid.
667
             		    if(result.containsKey(docid))
668
             		    {
669
             			    String markup = result.get(docid).toString();
670
             			    markup += "<date_created>" + data + "</date_created>";
671
             			    markup += "<date_updated>" + data2 + "</date_updated>";
672
             			    result.remove(docid);
673
             			    result.put(docid, markup);
674
             		    }
675
             		    else
676
             	 	    { 
677
             			    String markup = "<date_created>" + data + "</date_created>";
678
             			    markup += "<date_updated>" + data2 + "</date_updated>";
679
             			    result.put(docid, markup);
680
             		    }
681
             	    }
682
             	    moreRows = dateResults.next();
683
                 }
684
             }
685
             catch(Exception e)
686
             {
687
             	System.err.println("marineServlet: error in createExtendedQuery(try2)");
688
                e.printStackTrace(System.err);
689
             }
690
             
691
             return result;
692
      	}
693
      	
694
      	/**
695
      	   creates the xml query for the browse list
696
      	**/
697
      	private String createBrowseList()
698
      	{
699
      		String query;
700
      		query = "<?xml version=\"1.0\"?>";
701
		query += "<!DOCTYPE pathquery PUBLIC \"-//NCEAS//pathquery-1.0//EN\" " + 
702
          		    "\"http://alpha.nceas.ucsb.edu/xmltodb/lib/pathquery.dtd\" >";
703
    		query += "<pathquery version=\"1.0\">";
704
    		query += "<meta_file_id>unspecified</meta_file_id>";
705
    		query += "<querytitle>unspecified</querytitle>";
706
    		query += "<querygroup operator=\"union\">";
707
    		query += "<queryterm casesensitive=\"false\" " + 
708
    				         "searchmode=\"contains\">";
709
    		query += "<value>";
710
    		query += "%";           //return everything
711
    		query += "</value>";
712
    		query += "</queryterm>";
713
    		query += "</querygroup></pathquery>";
714
    		
715
    		return query;
716
      	}
717
      	
718
      	/** takes in the parameter list from the input source and 
719
      	    a doctype and returns a structured xml query based
720
      	    on the parameters.
721
      	**/
722
      	private String createSQuery(Hashtable params, String doctype)
723
	{
724
    		String query;
725
    		Enumeration elements;
726
    		Enumeration keys;
727
    		Object nextkey;
728
    		Object nextelement;
729
    		queryname = " ";
730
    		query = "<?xml version=\"1.0\"?>";
731
		query += "<!DOCTYPE pathquery PUBLIC \"-//NCEAS//pathquery-1.0//EN\" " + 
732
          		    "\"http://alpha.nceas.ucsb.edu/xmltodb/lib/pathquery.dtd\" >";
733
    		query += "<pathquery version=\"1.0\"><meta_file_id>";
734
    		if(params.containsKey("meta_file_id"))
735
    		{
736
    			query += ((String[])params.get("meta_file_id"))[0];
737
    			query += "</meta_file_id>";
738
    		}
739
    		else
740
    		{
741
    			query += "unspecified</meta_file_id>";
742
    		}
743
    		query += "<querytitle>";
744
    		if(params.containsKey("querytitle"))
745
    		{
746
    			query += ((String[])params.get("querytitle"))[0];
747
    			query += "</querytitle>";
748
    		}
749
    		else
750
    		{
751
    			query += "unspecified</querytitle>";
752
    		}
753
    		if(!doctype.equals(""))
754
    		{
755
    			query += "<returndoctype>" + doctype + "</returndoctype>";
756
    		}
757
    		query += "<querygroup operator=\"" + 
758
    		         ((String[])params.get("operator"))[0] + "\">";
759
    		
760
    		
761
    		//anyfield is a special case because it does a 
762
    		//free text search.  It does not have a <pathexpr>
763
    		//tag.
764
    		if(params.containsKey("anyfield") && 
765
    		   !((String[])params.get("anyfield"))[0].equals(""))
766
    		{
767
    			query += "<queryterm casesensitive=\"false\" " +
768
    			         "searchmode=\"contains\"><value>" +
769
    			         ((String[])params.get("anyfield"))[0] +
770
    			         "</value></queryterm>";
771
    			         queryname += "<anyfield>";
772
    			         queryname += ((String[])params.get("anyfield"))[0];
773
    			         queryname += "</anyfield>";
774
    		}
775
    		
776
    		//this while loop finds the rest of the parameters
777
    		//and attempts to query for the field specified
778
    		//by the parameter.
779
    		elements = params.elements();
780
    		keys = params.keys();
781
    		while(keys.hasMoreElements() && elements.hasMoreElements())
782
    		{
783
    			nextkey = keys.nextElement();
784
    			nextelement = elements.nextElement();
785
    			
786
    			//make sure we aren't querying for any of these
787
    			//parameters.
788
    			if(!nextkey.toString().equals("doctype") && 
789
    			   !nextkey.toString().equals("action")  &&
790
    			   !nextkey.toString().equals("qformat") && 
791
    			   !nextkey.toString().equals("anyfield") &&
792
    			   !nextkey.toString().equals("operator") &&
793
    			   !(((String[])nextelement)[0].equals("")) )
794
    			{
795
    				query += "<queryterm casesensitive=\"false\" " + 
796
    				         "searchmode=\"contains\">";
797
    				query += "<value>";
798
    				//add the query value
799
    				query += ((String[])nextelement)[0];
800
    				query += "</value><pathexpr>" +
801
    				//add the path to query by 
802
    				         nextkey.toString() + 
803
    				         "</pathexpr></queryterm>";
804
    				
805
    				//add the query back into the xml to fill in 
806
    				//the text boxes.
807
    				queryname += "<" + nextkey.toString() + ">";
808
    				queryname += ((String[])nextelement)[0];
809
    				queryname += "</" + nextkey.toString() + ">";
810
    			}
811
    		}
812
    		query += "</querygroup></pathquery>";
813
    		return query;
814
        } 
815
        
816
        /**same as createSQuery(Hashtable, String) except
817
           no doctype is required.
818
        **/
819
        private String createSQuery(Hashtable params)
820
        {
821
        	return createSQuery(params, "");
822
        } 
823

    
824
}
(1-1/2)