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-12-12 14:32:24 -0800 (Tue, 12 Dec 2000) $'
12
 * '$Revision: 601 $'
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 conn)
35
  {
36
    //this.conn = conn;
37
    try
38
    {
39
      this.conn = util.openDBConnection();
40
    }
41
    catch(Exception e)
42
    {
43
      System.out.println("error opening connection in relationHandler");
44
    }
45
    
46
    this.xmldoc = xmldoc;
47
    if(xmldoc.getDoctype().equals(util.getOption("packagedoctype")))
48
    { //make sure this doctype is a package document then run the thread
49
      btThread = new Thread(this);
50
      btThread.setPriority(Thread.MIN_PRIORITY);
51
      btThread.start();
52
    }
53
  }
54
  
55
  /**
56
   * The thread handler
57
   */
58
  public void run()
59
  {
60
    //pseudo-code algorithm
61
    //for each new relation r in xml_nodes
62
    //  put r into xml_relation
63
    //  compare r to each relation already in xml_relation
64
    //  if r isrelatedto a row in xml_relation
65
    //    add a new row to xml_relation that represents this new relation
66
    try
67
    {
68
      String docid = xmldoc.getDocID();
69
      PreparedStatement pstmt = conn.prepareStatement(
70
                                QuerySpecification.printPackageSQL(docid));
71
      pstmt.execute();
72
      //get the new relations out of xml_nodes
73
      ResultSet rs = pstmt.getResultSet();
74
      boolean hasmorerows = rs.next();
75
      while(hasmorerows)
76
      {
77
        String subject = rs.getString(1);
78
        MetacatURL subjectMurl = new MetacatURL(subject);
79
        String subjectDoctype = null;
80
        if(subjectMurl.getProtocol().equals("metacat"))
81
        {
82
          DocumentImpl subDoc = new DocumentImpl(conn, 
83
                                                 subjectMurl.getParam(0)[1]);
84
          subjectDoctype = subDoc.getDoctype();
85
        }
86
        String relationship = rs.getString(2);
87
        String object = rs.getString(3);
88
      
89
        //compare r to each relation in xml_relation
90
        pstmt = conn.prepareStatement("select subject, subdoctype, " +
91
                                      "relationship, object, objdoctype " +
92
                                      "from xml_relation");
93
        pstmt.execute();
94
        //get each relation in xml_relation for comparison
95
        ResultSet relations = pstmt.getResultSet();
96
        boolean hasmorerelations = relations.next();
97
        while(hasmorerelations)
98
        {
99
          String currentSub = relations.getString(1);
100
          String currentSubDoctype = relations.getString(2);
101
          String currentRelationship = relations.getString(3);
102
          String currentObj = relations.getString(4);
103
          String currentObjDoctype = relations.getString(5);
104
         
105
          if(object.equals(currentObj))
106
          {//there is a transitive relation so add a new relation to the table
107
            StringBuffer insertTransRelation = new StringBuffer();
108
            insertTransRelation.append("insert into xml_relation (subject, ");
109
            insertTransRelation.append("subdoctype, relationship, object, ");
110
            insertTransRelation.append("objdoctype) values ('");
111
            insertTransRelation.append(currentSub).append("', '");
112
            insertTransRelation.append(currentSubDoctype).append("', ");
113
            insertTransRelation.append("'hasTransitiveRelationTo', '");
114
            insertTransRelation.append(subject).append("', '");
115
            insertTransRelation.append(subjectDoctype).append("')");
116
            pstmt = conn.prepareStatement(insertTransRelation.toString());
117
            pstmt.execute(); 
118
            
119
            insertTransRelation = new StringBuffer();
120
            //put the same relation in with the subject and object switched
121
            insertTransRelation.append("insert into xml_relation (subject, ");
122
            insertTransRelation.append("subdoctype, relationship, object, ");
123
            insertTransRelation.append("objdoctype) values ('");
124
            insertTransRelation.append(subject).append("', '");
125
            insertTransRelation.append(subjectDoctype).append("', ");
126
            insertTransRelation.append("'hasTransitiveRelationTo', '");
127
            insertTransRelation.append(currentSub).append("', '");
128
            insertTransRelation.append(currentSubDoctype).append("')");
129
            pstmt = conn.prepareStatement(insertTransRelation.toString());
130
            pstmt.execute();
131
          }
132
          
133
          hasmorerelations = relations.next();
134
        }
135
        
136
        //get the current relations information
137
        MetacatURL subMurl = new MetacatURL(subject);
138
        MetacatURL objMurl = new MetacatURL(object);
139
        String subDoctype = null;
140
        String objDoctype = null;
141
        if(subMurl.getProtocol().equals("metacat"))
142
        {
143
          DocumentImpl subDoc = new DocumentImpl(conn, subMurl.getParam(0)[1]);
144
          subDoctype = subDoc.getDoctype();
145
        }
146
        if(objMurl.getProtocol().equals("metacat"))
147
        {
148
          DocumentImpl objDoc = new DocumentImpl(conn, objMurl.getParam(0)[1]);
149
          objDoc.getDoctype();
150
        }
151
        //now that the comparisons are done, the new relation can be put
152
        //into xml_relation
153
        StringBuffer insertStmt = new StringBuffer();
154
        insertStmt.append("insert into xml_relation (subject, subdoctype, ");
155
        insertStmt.append("relationship, object, objdoctype) values ('");
156
        insertStmt.append(subject).append("', '");
157
        insertStmt.append(subDoctype).append("', '");
158
        insertStmt.append(relationship).append("', '");
159
        insertStmt.append(object).append("', '");
160
        insertStmt.append(objDoctype).append("')");
161
        pstmt = conn.prepareStatement(insertStmt.toString());
162
        pstmt.execute(); 
163
        
164
        hasmorerows = rs.next();
165
      }
166
      
167
      conn.commit();
168
      conn.close();
169
      btThread = null;
170

    
171
    } 
172
    catch(Exception e) 
173
    { 
174
      try 
175
      { 
176
        conn.rollback();
177
      } 
178
      catch (SQLException sqle) {}
179
      System.out.println("Error in relationHandler: " + e.getMessage());
180
      util.debugMessage("Error in relationHandler: " + e.getMessage());
181
      e.printStackTrace(System.out);
182
      btThread = null;
183
    }
184
  }
185
}
(35-35/38)