Project

General

Profile

1
/**
2
 *   '$Author: berkley $'
3
 *     '$Date: 2001-01-18 12:27:15 -0800 (Thu, 18 Jan 2001) $'
4
 * '$Revision: 671 $'
5
 *
6
 * This program is free software; you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation; either version 2 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
 */
20
 
21
package edu.ucsb.nceas.metacat;
22

    
23
import java.sql.*;
24
import java.util.*;
25
import java.lang.Thread; 
26
import java.net.*;
27

    
28
public class RelationHandler implements Runnable
29
{
30
  private Thread btThread = null;
31
  private Connection conn = null;
32
  private DocumentImpl xmldoc = null;
33
  MetaCatUtil util = new MetaCatUtil();
34
  
35
  /** 
36
   * Constructor for this class.  finds all of the relations to a single xml
37
   * document and writes them to the database.  This includes transitive
38
   * relations.
39
   * @param xmldoc the xml document to index.
40
   */
41
  public RelationHandler(DocumentImpl xmldoc, Connection conn)
42
  {
43
    //this.conn = conn;
44
    try
45
    {
46
      this.conn = util.openDBConnection();
47
    }
48
    catch(Exception e)
49
    {
50
      System.out.println("error opening connection in relationHandler");
51
    }
52
    
53
    this.xmldoc = xmldoc;
54
    if(xmldoc.getDoctype().equals(util.getOption("packagedoctype")))
55
    { //make sure this doctype is a package document then run the thread
56
      btThread = new Thread(this);
57
      btThread.setPriority(Thread.MIN_PRIORITY);
58
      btThread.start();
59
    }
60
  }
61
  
62
  /**
63
   * The thread handler
64
   */
65
  public void run()
66
  {
67
    String docid = xmldoc.getDocID();
68

    
69
    // deletes all of the relations with a docid of @docid.
70
    deleteRelations(docid);
71
    //pseudo-code algorithm
72
    //for each new relation r in xml_nodes
73
    //  put r into xml_relation
74
    //  compare r to each relation already in xml_relation
75
    //  if r isrelatedto a row in xml_relation
76
    //    add a new row to xml_relation that represents this new relation
77
    try
78
    {
79
      PreparedStatement pstmt = conn.prepareStatement(
80
                                QuerySpecification.printPackageSQL(docid));
81
      pstmt.execute();
82
      
83
      //get the new relations out of xml_nodes
84
      ResultSet rs = pstmt.getResultSet();
85
      boolean hasmorerows = rs.next();
86
      while(hasmorerows)
87
      {
88
        String subject = rs.getString(1);
89
        String subjectDoctype = null;
90
        String paramDocid = null;
91
        URL subjectMurl = null;
92

    
93
        try
94
        {
95
          subjectMurl = new URL(subject);
96
          subjectDoctype = null;
97
          Hashtable murlParams = util.parseQuery(subjectMurl.getQuery());
98
          paramDocid = (String)murlParams.get("docid"); 
99
        }
100
        catch(MalformedURLException murle)
101
        { //assume this is just a docid not a url
102
          paramDocid = subject;
103
        }
104
        
105
        DocumentImpl subDoc = new DocumentImpl(conn, paramDocid);
106
        subjectDoctype = subDoc.getDoctype();
107
        String relationship = rs.getString(2);
108
        String object = rs.getString(3);
109
      
110
        //compare r to each relation in xml_relation
111
        
112
        pstmt = conn.prepareStatement("select subject, subdoctype, " +
113
                                      "relationship, object, objdoctype " +
114
                                      "from xml_relation");
115
        pstmt.execute();
116
        //get each relation in xml_relation for comparison
117
        ResultSet relations = pstmt.getResultSet();
118
        boolean hasmorerelations = relations.next();
119
        while(hasmorerelations)
120
        {
121
          String currentSub = relations.getString(1);
122
          String currentSubDoctype = relations.getString(2);
123
          String currentRelationship = relations.getString(3);
124
          String currentObj = relations.getString(4);
125
          String currentObjDoctype = relations.getString(5);
126
         
127
          if(object.equals(currentObj))
128
          {//there is a transitive relation so add a new relation to the table
129
            StringBuffer insertTransRelation = new StringBuffer();
130
            insertTransRelation.append("insert into xml_relation (docid, ");
131
            insertTransRelation.append(" subject, ");
132
            insertTransRelation.append("subdoctype, relationship, object, ");
133
            insertTransRelation.append("objdoctype) values ('");
134
            insertTransRelation.append(docid).append("', '");
135
            insertTransRelation.append(currentSub).append("', '");
136
            insertTransRelation.append(currentSubDoctype).append("', ");
137
            insertTransRelation.append("'hasTransitiveRelationTo', '");
138
            insertTransRelation.append(subject).append("', '");
139
            insertTransRelation.append(subjectDoctype).append("')");
140
            //System.out.println("sql1: " + insertTransRelation.toString());
141
            pstmt = conn.prepareStatement(insertTransRelation.toString());
142
            pstmt.execute(); 
143
            
144
            insertTransRelation = new StringBuffer();
145
            //put the same relation in with the subject and object switched
146
            insertTransRelation.append("insert into xml_relation (docid, ");
147
            insertTransRelation.append(" subject, ");
148
            insertTransRelation.append("subdoctype, relationship, object, ");
149
            insertTransRelation.append("objdoctype) values ('");
150
            insertTransRelation.append(docid).append("', '");
151
            insertTransRelation.append(subject).append("', '");
152
            insertTransRelation.append(subjectDoctype).append("', ");
153
            insertTransRelation.append("'hasTransitiveRelationTo', '");
154
            insertTransRelation.append(currentSub).append("', '");
155
            insertTransRelation.append(currentSubDoctype).append("')");
156
            //System.out.println("sql2: " + insertTransRelation.toString());
157
            pstmt = conn.prepareStatement(insertTransRelation.toString());
158
            pstmt.execute();
159
          }
160
          
161
          hasmorerelations = relations.next(); 
162
        }
163
        
164
        //get the current relations information
165
        String subDocid = null;
166
        String objDocid = null;
167
        String subDoctype = null;
168
        String objDoctype = null;
169
        
170
        try
171
        {
172
          URL subMurl = new URL(subject);
173
          Hashtable subMurlParams = util.parseQuery(subMurl.getQuery());
174
          subDocid = (String)subMurlParams.get("docid");
175
        }
176
        catch(MalformedURLException murle)
177
        { //assume this is just a docid not a url
178
          subDocid = subject;
179
        }
180
        
181
        try
182
        {
183
          URL objMurl = new URL(object); 
184
          Hashtable objMurlParams = util.parseQuery(objMurl.getQuery());
185
          objDocid = (String)objMurlParams.get("docid");
186
        }
187
        catch(MalformedURLException murle)
188
        { //assume this is just a docid
189
          objDocid = object;
190
        }
191
        
192
        subDoc = new DocumentImpl(conn, subDocid);
193
        subDoctype = subDoc.getDoctype();
194
        DocumentImpl objDoc = new DocumentImpl(conn, objDocid);
195
        objDoctype = objDoc.getDoctype();
196
        
197
        //now that the comparisons are done, the new relation can be put
198
        //into xml_relation
199
        StringBuffer insertStmt = new StringBuffer();
200
        insertStmt.append("insert into xml_relation (docid, subject, ");
201
        insertStmt.append("subdoctype, ");
202
        insertStmt.append("relationship, object, objdoctype) values ('");
203
        insertStmt.append(docid).append("', '");
204
        insertStmt.append(subject).append("', '");
205
        insertStmt.append(subDoctype).append("', '");
206
        insertStmt.append(relationship).append("', '");
207
        insertStmt.append(object).append("', '");
208
        insertStmt.append(objDoctype).append("')");
209
        
210
        pstmt = conn.prepareStatement(insertStmt.toString());
211
        pstmt.execute(); 
212
        
213
        hasmorerows = rs.next();
214
      }
215
      
216
      conn.commit();
217
      pstmt.close();
218
      conn.close();
219
      btThread = null;
220

    
221
    } 
222
    catch(Exception e) 
223
    { 
224
      try 
225
      { 
226
        conn.rollback();
227
      } 
228
      catch (SQLException sqle) {}
229
      System.out.println("Error in relationHandler: " + e.getMessage());
230
      util.debugMessage("Error in relationHandler: " + e.getMessage());
231
      e.printStackTrace(System.out);
232
      btThread = null;
233
    }
234
  }
235
  
236
  /**
237
   * Deletes all of the relations with a docid of 'docid'.
238
   * @param docid the docid to delete.
239
   */
240
  public void deleteRelations(String docid)
241
  {
242
    try
243
    {
244
      PreparedStatement pstmt = conn.prepareStatement("delete from " +
245
                             "xml_relation where docid like '" + docid + "'");
246
      pstmt.execute();
247
      pstmt.close();
248
    }
249
    catch(Exception e)
250
    {
251
      try 
252
      { 
253
        conn.rollback();
254
      } 
255
      catch (SQLException sqle) {}
256
      System.out.println("error in deleteRelations(): " + e.getMessage());
257
      e.printStackTrace(System.out);
258
    }
259
    
260
  }
261
}
(40-40/43)