Index: src/edu/ucsb/nceas/metacat/QuerySpecification.java
===================================================================
--- src/edu/ucsb/nceas/metacat/QuerySpecification.java	(revision 452)
+++ src/edu/ucsb/nceas/metacat/QuerySpecification.java	(revision 453)
@@ -296,8 +296,6 @@
    * This method prints sql based upon the &lt;returnfield&gt; tag in the
    * pathquery document.  This allows for customization of the 
    * returned fields
-   * The parameters of the query are changed to upper case before the query
-   * so that givenName is queryied the same as givenname.
    */
   public String printExtendedSQL()
   {  
@@ -331,8 +329,33 @@
     //System.out.println(self.toString());
     return self.toString();
   }
-
+   
   /**
+   *
+   */
+  public static String printPackageSQL()
+  {
+    StringBuffer self = new StringBuffer();
+    self.append("select z.nodedata, x.nodedata, y.nodedata from ");
+    self.append("(select nodeid, parentnodeid from xml_index where path like ");
+    self.append("'package/relation/subject') s, (select nodeid, parentnodeid ");
+    self.append("from xml_index where path like ");
+    self.append("'package/relation/relationship') rel, ");
+    self.append("(select nodeid, parentnodeid from xml_index where path like ");
+    self.append("'package/relation/object') o, ");
+    self.append("xml_nodes x, xml_nodes y, xml_nodes z ");
+    self.append("where s.parentnodeid = rel.parentnodeid ");
+    self.append("and rel.parentnodeid = o.parentnodeid ");
+    self.append("and x.parentnodeid in rel.nodeid ");
+    self.append("and y.parentnodeid in o.nodeid ");
+    self.append("and z.parentnodeid in s.nodeid ");
+    //self.append("and z.nodedata like '%");
+    //self.append(docid);
+    //self.append("%'");
+    return self.toString();
+  }
+  
+  /**
    * create a String description of the query that this instance represents.
    * This should become a way to get the XML serialization of the query.
    */
@@ -555,6 +578,10 @@
 
 /**
  * '$Log$
+ * 'Revision 1.14  2000/08/31 21:20:39  berkley
+ * 'changed xslf for new returnfield scheme.  the returnfields are now returned as <param name="<returnfield>"> tags.
+ * 'hThe sql for the returnfield query was redone to fix a previous problem with slow queries
+ * '
  * 'Revision 1.13  2000/08/23 22:55:38  berkley
  * 'changed the field names to be case-sensitive in the returnfields
  * '
Index: src/edu/ucsb/nceas/metacat/DBQuery.java
===================================================================
--- src/edu/ucsb/nceas/metacat/DBQuery.java	(revision 452)
+++ src/edu/ucsb/nceas/metacat/DBQuery.java	(revision 453)
@@ -121,7 +121,9 @@
    *
    * @param xmlquery the xml serialization of the query (@see pathquery.dtd)
    */
-  public Hashtable findDocuments(Reader xmlquery, String user, String group) {
+  public Hashtable findDocuments(Reader xmlquery, String user, String group)
+         //throws Exception
+  {
       Hashtable	 docListResult = new Hashtable();
       PreparedStatement pstmt;
       String docid = null;
@@ -132,6 +134,7 @@
       String updateDate = null;
       String fieldname = null;
       String fielddata = null;
+      String relation = null;
       StringBuffer document = null; 
 
       try {
@@ -215,6 +218,103 @@
             }
           }
         }
+
+        pstmt = conn.prepareStatement(qspec.printPackageSQL());
+        pstmt.execute();
+        rs = pstmt.getResultSet();
+        tableHasRows = rs.next();
+        String[][] relations = new String[2000][3];
+        int relLength=0;
+        while(tableHasRows)
+        {
+          relations[relLength][0] = rs.getString(1).trim();
+          relations[relLength][1] = rs.getString(2).trim();
+          relations[relLength][2] = rs.getString(3).trim();
+          relLength++;
+          
+          //this get's direct relations from the data.  i.e. those relations
+          //where the docid of the current document is in the subject tag.
+          
+          docid = rs.getString(1);
+          metacatURL murl = new metacatURL(docid);
+          if(murl.getURLType().equals("metacat"))
+          {//we only want to process metacat urls here.
+            String[] tempparam = murl.getParam(0);
+            if(tempparam[0].equals("docid"))
+            {
+              docid = tempparam[1]; 
+              document = new StringBuffer();
+              document.append("<relation>");
+              document.append("<relationtype>").append(rs.getString(2));
+              document.append("</relationtype>");
+              document.append("<relationdoc>").append(rs.getString(3));
+              document.append("</relationdoc>");
+              document.append("</relation>");
+              tableHasRows = rs.next();
+              if(docListResult.containsKey(docid))
+              {
+                String removedelement = (String)docListResult.remove(docid);
+                docListResult.put(docid, removedelement + document.toString());
+              }
+              else
+              {
+                docListResult.put(docid, document.toString()); 
+              }
+            }
+            else
+            {
+              //throw new Exception("Error in url. The first parameter must " +
+              //                    "be the docid.");
+              System.err.println("DBQuery: error in url");
+            }
+          }
+        }
+        
+        //this loop handles transitive relations. i.e. if two relation tags
+        //both have the same object then the subject of each are related.
+        for(int i=0; i<relLength; i++)
+        {
+          for(int j=0; j<relLength; j++)
+          {
+            if(i != j)
+            {
+              if(relations[i][2].equals(relations[j][2]))
+              {//the objects are the same.
+                metacatURL murl = new metacatURL(relations[i][0]);
+                //relations[i][0] contains the docid of document that 
+                //the new document is related to.
+                String[] tempparam = murl.getParam(0);
+                if(tempparam[0].equals("docid"))
+                {
+                  docid = tempparam[1]; 
+                  document = new StringBuffer();
+                  document.append("<relation>");
+                  document.append("<relationtype>").append(relations[j][1]);
+                  document.append("</relationtype>");
+                  document.append("<relationdoc>").append(relations[j][0]);
+                  //the relation is to the subject of the new document
+                  //instead of the object.
+                  //   direct relation    transitive relation
+                  // |-----------------|  |-----------------|
+                  // subject -> (object = object) -> subject
+                  document.append("</relationdoc>");
+                  document.append("</relation>");
+                  if(docListResult.containsKey(docid))
+                  {
+                    String removedelement = (String)docListResult.remove(docid);
+                    docListResult.put(docid, removedelement + 
+                                      document.toString());
+                  }
+                  else
+                  {
+                    docListResult.put(docid, document.toString()); 
+                  }
+                }
+              }
+            }
+          }
+        }
+
         pstmt.close();
       } catch (SQLException e) {
         System.err.println("Error getting id: " + e.getMessage());
@@ -595,6 +695,10 @@
 
 /**
  * '$Log$
+ * 'Revision 1.19  2000/09/12 17:37:07  bojilova
+ * 'added check from "read" permission on "query" and "squery" actions
+ * 'for connected user or for "public" connection
+ * '
  * 'Revision 1.18  2000/09/05 20:50:56  berkley
  * '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.
  * '
Index: src/edu/ucsb/nceas/metacat/MetaCatServlet.java
===================================================================
--- src/edu/ucsb/nceas/metacat/MetaCatServlet.java	(revision 452)
+++ src/edu/ucsb/nceas/metacat/MetaCatServlet.java	(revision 453)
@@ -20,6 +20,7 @@
 import java.io.BufferedReader;
 import java.io.File;
 import java.io.FileInputStream;
+import java.io.DataInputStream;
 import java.util.Enumeration;
 import java.util.Hashtable;
 import java.util.ResourceBundle; 
@@ -31,6 +32,7 @@
 import java.sql.Connection;
 import java.sql.SQLException;
 import java.lang.reflect.*;
+import java.net.*;
 
 import javax.servlet.ServletConfig;
 import javax.servlet.ServletContext;
@@ -274,7 +276,17 @@
       } catch (SQLException se) {
         out.println(se.getMessage());
       }
-    } else if (action.equals("insert") || action.equals("update")) {
+    } 
+    else if (action.equals("getrelateddocument")) {
+      try {
+        handleGetRelatedDocumentAction(out, params, response);
+      } catch (ClassNotFoundException e) {
+        out.println(e.getMessage());
+      } catch (SQLException se) {
+        out.println(se.getMessage());
+      }
+    }
+    else if (action.equals("insert") || action.equals("update")) {
       if ( !username.equals("public") && (username != null) ) {
         handleInsertOrUpdateAction(out, params, response, username, groupname);
       } else {  
@@ -598,6 +610,79 @@
   }
 
   /** 
+   * Handle the database getrelateddocument request and return a XML document, 
+   * possibly transformed from XML into HTML
+   */
+  private void handleGetRelatedDocumentAction(PrintWriter out, Hashtable params, 
+               HttpServletResponse response) 
+               throws ClassNotFoundException, IOException, SQLException 
+  {
+    String docid = null;
+    Connection conn = null;
+      
+    if(params.containsKey("url"))
+    {//the identifier for the related document is contained in the URL param
+      try
+      {
+        DocumentImpl xmldoc=null;
+        metacatURL murl = new metacatURL(((String[])params.get("url"))[0]);
+        if(murl.getURLType().equals("metacat"))
+        {//get the document from the database if it is the right type of url
+          String[] murlParams = murl.getParam(0);
+          if(murlParams[0].equals("docid"))
+          {//the docid should be first
+            murl.printParams();
+            docid = murlParams[1]; //get the docid value
+            conn = util.getConnection();
+            xmldoc = new DocumentImpl(conn, docid);
+            
+            //**************************************************
+            //the style sheet handling code needs to be put here. 
+            out.println(xmldoc.toString());
+            //**************************************************
+          }
+          else
+          {
+            //throw new Exception("handleGetDocument: bad URL");
+            System.err.println("handleGetDocument: bad URL");
+          }
+        }
+        else if(murl.getURLType().equals("http"))
+        {//get the document from the internet
+          String[] murlParams = murl.getParam(0);
+          if(murlParams[0].equals("httpurl"))
+          {//httpurl is the param name for an http url.
+            URL urlconn = new URL(murlParams[1]);  //create a new url obj.
+            DataInputStream htmldoc = new DataInputStream(urlconn.openStream());
+            //bind a data stream.
+            try
+            { //display the document
+              String line=null;
+              while((line = htmldoc.readLine()) != null)
+              {
+                out.println(line); 
+              }
+            }
+            catch(Exception e)
+            {
+              util.debugMessage("error viewing html document"); 
+            }
+          }
+        }
+      }
+      catch (McdbException e) {
+        response.setContentType("text/xml");
+        e.toXml(out);
+      } catch   (Throwable t) {
+        response.setContentType("text/html");
+        out.println(t.getMessage());
+      } finally {
+        util.returnConnection(conn);
+      }
+    }
+  }   
+  
+  /** 
    * Handle the database getdocument request and return a XML document, 
    * possibly transformed from XML into HTML
    */
