Project

General

Profile

1
/**
2
 *  '$RCSfile$'
3
 *    Purpose: A class to asyncronously build the package relation index
4
 *             in the database table xml_relation.
5
 *  Copyright: 2000 Regents of the University of California and the
6
 *             National Center for Ecological Analysis and Synthesis
7
 *    Authors: Chad Berkley
8
 *    Release: @release@
9
 *
10
 *   '$Author: berkley $'
11
 *     '$Date: 2000-10-03 14:23:59 -0700 (Tue, 03 Oct 2000) $'
12
 * '$Revision: 486 $'
13
 */
14
 
15
package edu.ucsb.nceas.metacat;
16

    
17
import java.sql.*;
18
import java.util.*;
19
import java.lang.Thread; 
20

    
21
public class relationHandler implements Runnable
22
{
23
  private Thread btThread = null;
24
  private Connection conn = null;
25
  private DocumentImpl xmldoc = null;
26
  MetaCatUtil util = new MetaCatUtil();
27
  
28
  /** 
29
   * Constructor for this class.  finds all of the relations to a single xml
30
   * document and writes them to the database.  This includes transitive
31
   * relations.
32
   * @param xmldoc the xml document to index.
33
   */
34
  public relationHandler(DocumentImpl xmldoc, Connection con)
35
  {
36
    try
37
    {
38
      //conn = util.getConnection();
39
      if(conn == null)
40
      {
41
        conn = util.getConnection();
42
      }
43
      else
44
      {
45
        this.conn = con;
46
      }
47
    }
48
    catch(Exception e)
49
    {
50
      System.out.println("error getting db connection in relationhandler:");
51
      System.out.println(e.getMessage());
52
    }
53
    
54
    this.xmldoc = xmldoc;
55
    if(xmldoc.getDoctype().equals(util.getOption("packagedoctype")))
56
    { //make sure this doctype is a package document then run the thread
57
      btThread = new Thread(this);
58
      btThread.setPriority(Thread.MIN_PRIORITY);
59
      btThread.start();
60
    }
61
  }
62
  
63
  /**
64
   * The thread handler
65
   */
66
  public void run()
67
  {
68
    //pseudo-code algorithm
69
    //for each new relation r in xml_nodes
70
    //  put r into xml_relation
71
    //  compare r to each relation already in xml_relation
72
    //  if r isrelatedto a row in xml_relation
73
    //    add a new row to xml_relation that represents this new relation
74
    try
75
    {
76
      String docid = xmldoc.getDocID();
77
      PreparedStatement pstmt = conn.prepareStatement(
78
                                QuerySpecification.printPackageSQL(docid));
79
      pstmt.execute();
80
      //get the new relations out of xml_nodes
81
      ResultSet rs = pstmt.getResultSet();
82
      boolean hasmorerows = rs.next();
83
      while(hasmorerows)
84
      {
85
        String subject = rs.getString(1);
86
        metacatURL subjectMurl = new metacatURL(subject);
87
        String subjectDoctype = null;
88
        if(subjectMurl.getURLType().equals("metacat"))
89
        {
90
          DocumentImpl subDoc = new DocumentImpl(conn, 
91
                                                 subjectMurl.getParam(0)[1]);
92
          subjectDoctype = subDoc.getDoctype();
93
        }
94
        String relationship = rs.getString(2);
95
        String object = rs.getString(3);
96
      
97
        //compare r to each relation in xml_relation
98
        pstmt = conn.prepareStatement("select subject, subdoctype, " +
99
                                      "relationship, object, objdoctype " +
100
                                      "from xml_relation");
101
        pstmt.execute();
102
        //get each relation in xml_relation for comparison
103
        ResultSet relations = pstmt.getResultSet();
104
        boolean hasmorerelations = relations.next();
105
        while(hasmorerelations)
106
        {
107
          String currentSub = relations.getString(1);
108
          String currentSubDoctype = relations.getString(2);
109
          String currentRelationship = relations.getString(3);
110
          String currentObj = relations.getString(4);
111
          String currentObjDoctype = relations.getString(5);
112
         
113
          if(object.equals(currentObj))
114
          {//there is a transitive relation so add a new relation to the table
115
            StringBuffer insertTransRelation = new StringBuffer();
116
            insertTransRelation.append("insert into xml_relation (subject, ");
117
            insertTransRelation.append("subdoctype, relationship, object, ");
118
            insertTransRelation.append("objdoctype) values ('");
119
            insertTransRelation.append(currentSub).append("', '");
120
            insertTransRelation.append(currentSubDoctype).append("', ");
121
            insertTransRelation.append("'hasTransitiveRelationTo', '");
122
            insertTransRelation.append(subject).append("', '");
123
            insertTransRelation.append(subjectDoctype).append("')");
124
            pstmt = conn.prepareStatement(insertTransRelation.toString());
125
            pstmt.execute();
126
            
127
            insertTransRelation = new StringBuffer();
128
            //put the same relation in with the subject and object switched
129
            insertTransRelation.append("insert into xml_relation (subject, ");
130
            insertTransRelation.append("subdoctype, relationship, object, ");
131
            insertTransRelation.append("objdoctype) values ('");
132
            insertTransRelation.append(subject).append("', '");
133
            insertTransRelation.append(subjectDoctype).append("', ");
134
            insertTransRelation.append("'hasTransitiveRelationTo', '");
135
            insertTransRelation.append(currentSub).append("', '");
136
            insertTransRelation.append(currentSubDoctype).append("')");
137
            pstmt = conn.prepareStatement(insertTransRelation.toString());
138
            pstmt.execute();
139
          }
140
          
141
          hasmorerelations = relations.next();
142
        }
143
        
144
        //get the current relations information
145
        metacatURL subMurl = new metacatURL(subject);
146
        metacatURL objMurl = new metacatURL(object);
147
        String subDoctype = null;
148
        String objDoctype = null;
149
        if(subMurl.getURLType().equals("metacat"))
150
        {
151
          DocumentImpl subDoc = new DocumentImpl(conn, subMurl.getParam(0)[1]);
152
          subDoctype = subDoc.getDoctype();
153
        }
154
        if(objMurl.getURLType().equals("metacat"))
155
        {
156
          DocumentImpl objDoc = new DocumentImpl(conn, objMurl.getParam(0)[1]);
157
          objDoc.getDoctype();
158
        }
159
        //now that the comparisons are done, the new relation can be put
160
        //into xml_relation
161
        StringBuffer insertStmt = new StringBuffer();
162
        insertStmt.append("insert into xml_relation (subject, subdoctype, ");
163
        insertStmt.append("relationship, object, objdoctype) values ('");
164
        insertStmt.append(subject).append("', '");
165
        insertStmt.append(subDoctype).append("', '");
166
        insertStmt.append(relationship).append("', '");
167
        insertStmt.append(object).append("', '");
168
        insertStmt.append(objDoctype).append("')");
169
        pstmt = conn.prepareStatement(insertStmt.toString());
170
        pstmt.execute(); 
171
        
172
        hasmorerows = rs.next();
173
      }
174
      
175
      conn.commit();
176
      util.returnConnection(conn);
177
      btThread = null;
178
    }
179
    catch(Exception e)
180
    {
181
      System.out.println("Error in relationHandler: " + e.getMessage());
182
      util.debugMessage("Error in relationHandler: " + e.getMessage());
183
      e.printStackTrace(System.out);
184
      btThread = null;
185
    }
186
  }
187
}
(32-32/33)