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: bojilova $'
11
 *     '$Date: 2001-01-08 17:11:26 -0800 (Mon, 08 Jan 2001) $'
12
 * '$Revision: 645 $'
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
    String docid = xmldoc.getDocID();
61

    
62
    // deletes all of the relations with a docid of @docid.
63
    deleteRelations(docid);
64

    
65
    //pseudo-code algorithm
66
    //for each new relation r in xml_nodes
67
    //  put r into xml_relation
68
    //  compare r to each relation already in xml_relation
69
    //  if r isrelatedto a row in xml_relation
70
    //    add a new row to xml_relation that represents this new relation
71
    try
72
    {
73
      PreparedStatement pstmt = conn.prepareStatement(
74
                                QuerySpecification.printPackageSQL(docid));
75
      pstmt.execute();
76
      //get the new relations out of xml_nodes
77
      ResultSet rs = pstmt.getResultSet();
78
      boolean hasmorerows = rs.next();
79
      while(hasmorerows)
80
      {
81
        String subject = rs.getString(1);
82
        MetacatURL subjectMurl = new MetacatURL(subject);
83
        String subjectDoctype = null;
84
        if(subjectMurl.getProtocol().equals("metacat"))
85
        {
86
          DocumentImpl subDoc = new DocumentImpl(conn, 
87
                                                 subjectMurl.getParam(0)[1]);
88
          subjectDoctype = subDoc.getDoctype();
89
        }
90
        String relationship = rs.getString(2);
91
        String object = rs.getString(3);
92
      
93
        //compare r to each relation in xml_relation
94
        pstmt = conn.prepareStatement("select subject, subdoctype, " +
95
                                      "relationship, object, objdoctype " +
96
                                      "from xml_relation");
97
        pstmt.execute();
98
        //get each relation in xml_relation for comparison
99
        ResultSet relations = pstmt.getResultSet();
100
        boolean hasmorerelations = relations.next();
101
        while(hasmorerelations)
102
        {
103
          String currentSub = relations.getString(1);
104
          String currentSubDoctype = relations.getString(2);
105
          String currentRelationship = relations.getString(3);
106
          String currentObj = relations.getString(4);
107
          String currentObjDoctype = relations.getString(5);
108
         
109
          if(object.equals(currentObj))
110
          {//there is a transitive relation so add a new relation to the table
111
            StringBuffer insertTransRelation = new StringBuffer();
112
            insertTransRelation.append("insert into xml_relation (docid, ");
113
            insertTransRelation.append(" subject, ");
114
            insertTransRelation.append("subdoctype, relationship, object, ");
115
            insertTransRelation.append("objdoctype) values ('");
116
            insertTransRelation.append(docid).append("', '");
117
            insertTransRelation.append(currentSub).append("', '");
118
            insertTransRelation.append(currentSubDoctype).append("', ");
119
            insertTransRelation.append("'hasTransitiveRelationTo', '");
120
            insertTransRelation.append(subject).append("', '");
121
            insertTransRelation.append(subjectDoctype).append("')");
122
            //System.out.println("sql1: " + insertTransRelation.toString());
123
            pstmt = conn.prepareStatement(insertTransRelation.toString());
124
            pstmt.execute(); 
125
            
126
            insertTransRelation = new StringBuffer();
127
            //put the same relation in with the subject and object switched
128
            insertTransRelation.append("insert into xml_relation (docid, ");
129
            insertTransRelation.append(" subject, ");
130
            insertTransRelation.append("subdoctype, relationship, object, ");
131
            insertTransRelation.append("objdoctype) values ('");
132
            insertTransRelation.append(docid).append("', '");
133
            insertTransRelation.append(subject).append("', '");
134
            insertTransRelation.append(subjectDoctype).append("', ");
135
            insertTransRelation.append("'hasTransitiveRelationTo', '");
136
            insertTransRelation.append(currentSub).append("', '");
137
            insertTransRelation.append(currentSubDoctype).append("')");
138
            //System.out.println("sql2: " + insertTransRelation.toString());
139
            pstmt = conn.prepareStatement(insertTransRelation.toString());
140
            pstmt.execute();
141
          }
142
          
143
          hasmorerelations = relations.next();
144
        }
145
        
146
        //get the current relations information
147
        MetacatURL subMurl = new MetacatURL(subject);
148
        MetacatURL objMurl = new MetacatURL(object);
149
        String subDoctype = null;
150
        String objDoctype = null;
151
        if(subMurl.getProtocol().equals("metacat"))
152
        {
153
          DocumentImpl subDoc = new DocumentImpl(conn, subMurl.getParam(0)[1]);
154
          subDoctype = subDoc.getDoctype();
155
        }
156
        if(objMurl.getProtocol().equals("metacat"))
157
        {
158
          DocumentImpl objDoc = new DocumentImpl(conn, objMurl.getParam(0)[1]);
159
          objDoc.getDoctype();
160
        }
161
        //now that the comparisons are done, the new relation can be put
162
        //into xml_relation
163
        StringBuffer insertStmt = new StringBuffer();
164
        insertStmt.append("insert into xml_relation (docid, subject, ");
165
        insertStmt.append("subdoctype, ");
166
        insertStmt.append("relationship, object, objdoctype) values ('");
167
        insertStmt.append(docid).append("', '");
168
        insertStmt.append(subject).append("', '");
169
        insertStmt.append(subDoctype).append("', '");
170
        insertStmt.append(relationship).append("', '");
171
        insertStmt.append(object).append("', '");
172
        insertStmt.append(objDoctype).append("')");
173
        pstmt = conn.prepareStatement(insertStmt.toString());
174
        pstmt.execute(); 
175
        
176
        hasmorerows = rs.next();
177
      }
178
      
179
      conn.commit();
180
      conn.close();
181
      btThread = null;
182

    
183
    } 
184
    catch(Exception e) 
185
    { 
186
      try 
187
      { 
188
        conn.rollback();
189
      } 
190
      catch (SQLException sqle) {}
191
      System.out.println("Error in relationHandler: " + e.getMessage());
192
      util.debugMessage("Error in relationHandler: " + e.getMessage());
193
      e.printStackTrace(System.out);
194
      btThread = null;
195
    }
196
  }
197
  
198
  /**
199
   * Deletes all of the relations with a docid of 'docid'.
200
   * @param docid the docid to delete.
201
   */
202
  public void deleteRelations(String docid)
203
  {
204
    try
205
    {
206
      PreparedStatement pstmt = conn.prepareStatement("delete from " +
207
                             "xml_relation where docid like '" + docid + "'");
208
      pstmt.execute();
209
      pstmt.close();
210
    }
211
    catch(Exception e)
212
    {
213
      try 
214
      { 
215
        conn.rollback();
216
      } 
217
      catch (SQLException sqle) {}
218
      System.out.println("error in deleteRelations(): " + e.getMessage());
219
      e.printStackTrace(System.out);
220
    }
221
    
222
  }
223
}
(40-40/43)