Project

General

Profile

1 523 berkley
/**
2
 *   '$Author$'
3
 *     '$Date$'
4
 * '$Revision$'
5 669 jones
 *
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 523 berkley
 */
20
21
package edu.ucsb.nceas.metacat;
22
23
import java.sql.*;
24
import java.util.*;
25
import java.lang.Thread;
26 662 berkley
import java.net.*;
27 523 berkley
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 601 berkley
    //this.conn = conn;
44
    try
45
    {
46
      this.conn = util.openDBConnection();
47
    }
48
    catch(Exception e)
49
    {
50 675 berkley
      System.out.println("error opening connection in " +
51
                         "relationHandler.relationHandler: " + e.getMessage());
52 601 berkley
    }
53
54 523 berkley
    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 645 bojilova
    String docid = xmldoc.getDocID();
69
70
    // deletes all of the relations with a docid of @docid.
71
    deleteRelations(docid);
72 523 berkley
    //pseudo-code algorithm
73
    //for each new relation r in xml_nodes
74
    //  put r into xml_relation
75
    //  compare r to each relation already in xml_relation
76
    //  if r isrelatedto a row in xml_relation
77
    //    add a new row to xml_relation that represents this new relation
78
    try
79
    {
80
      PreparedStatement pstmt = conn.prepareStatement(
81
                                QuerySpecification.printPackageSQL(docid));
82
      pstmt.execute();
83 662 berkley
84 523 berkley
      //get the new relations out of xml_nodes
85
      ResultSet rs = pstmt.getResultSet();
86
      boolean hasmorerows = rs.next();
87
      while(hasmorerows)
88
      {
89
        String subject = rs.getString(1);
90
        String subjectDoctype = null;
91 662 berkley
        String paramDocid = null;
92
        URL subjectMurl = null;
93
94
        try
95 523 berkley
        {
96 662 berkley
          subjectMurl = new URL(subject);
97
          subjectDoctype = null;
98
          Hashtable murlParams = util.parseQuery(subjectMurl.getQuery());
99
          paramDocid = (String)murlParams.get("docid");
100 523 berkley
        }
101 662 berkley
        catch(MalformedURLException murle)
102
        { //assume this is just a docid not a url
103
          paramDocid = subject;
104
        }
105
106
        DocumentImpl subDoc = new DocumentImpl(conn, paramDocid);
107
        subjectDoctype = subDoc.getDoctype();
108 523 berkley
        String relationship = rs.getString(2);
109
        String object = rs.getString(3);
110
111
        //compare r to each relation in xml_relation
112 671 berkley
113 523 berkley
        pstmt = conn.prepareStatement("select subject, subdoctype, " +
114
                                      "relationship, object, objdoctype " +
115
                                      "from xml_relation");
116
        pstmt.execute();
117
        //get each relation in xml_relation for comparison
118
        ResultSet relations = pstmt.getResultSet();
119
        boolean hasmorerelations = relations.next();
120
        while(hasmorerelations)
121
        {
122
          String currentSub = relations.getString(1);
123
          String currentSubDoctype = relations.getString(2);
124
          String currentRelationship = relations.getString(3);
125
          String currentObj = relations.getString(4);
126
          String currentObjDoctype = relations.getString(5);
127
128
          if(object.equals(currentObj))
129
          {//there is a transitive relation so add a new relation to the table
130
            StringBuffer insertTransRelation = new StringBuffer();
131 634 berkley
            insertTransRelation.append("insert into xml_relation (docid, ");
132
            insertTransRelation.append(" subject, ");
133 523 berkley
            insertTransRelation.append("subdoctype, relationship, object, ");
134
            insertTransRelation.append("objdoctype) values ('");
135 634 berkley
            insertTransRelation.append(docid).append("', '");
136 523 berkley
            insertTransRelation.append(currentSub).append("', '");
137
            insertTransRelation.append(currentSubDoctype).append("', ");
138
            insertTransRelation.append("'hasTransitiveRelationTo', '");
139
            insertTransRelation.append(subject).append("', '");
140
            insertTransRelation.append(subjectDoctype).append("')");
141 634 berkley
            //System.out.println("sql1: " + insertTransRelation.toString());
142 523 berkley
            pstmt = conn.prepareStatement(insertTransRelation.toString());
143 601 berkley
            pstmt.execute();
144 523 berkley
145
            insertTransRelation = new StringBuffer();
146
            //put the same relation in with the subject and object switched
147 634 berkley
            insertTransRelation.append("insert into xml_relation (docid, ");
148
            insertTransRelation.append(" subject, ");
149 523 berkley
            insertTransRelation.append("subdoctype, relationship, object, ");
150
            insertTransRelation.append("objdoctype) values ('");
151 634 berkley
            insertTransRelation.append(docid).append("', '");
152 523 berkley
            insertTransRelation.append(subject).append("', '");
153
            insertTransRelation.append(subjectDoctype).append("', ");
154
            insertTransRelation.append("'hasTransitiveRelationTo', '");
155
            insertTransRelation.append(currentSub).append("', '");
156
            insertTransRelation.append(currentSubDoctype).append("')");
157 634 berkley
            //System.out.println("sql2: " + insertTransRelation.toString());
158 523 berkley
            pstmt = conn.prepareStatement(insertTransRelation.toString());
159
            pstmt.execute();
160
          }
161
162 662 berkley
          hasmorerelations = relations.next();
163 523 berkley
        }
164
165
        //get the current relations information
166 662 berkley
        String subDocid = null;
167
        String objDocid = null;
168 523 berkley
        String subDoctype = null;
169
        String objDoctype = null;
170 662 berkley
171
        try
172 523 berkley
        {
173 662 berkley
          URL subMurl = new URL(subject);
174
          Hashtable subMurlParams = util.parseQuery(subMurl.getQuery());
175
          subDocid = (String)subMurlParams.get("docid");
176 523 berkley
        }
177 662 berkley
        catch(MalformedURLException murle)
178
        { //assume this is just a docid not a url
179
          subDocid = subject;
180
        }
181
182
        try
183 523 berkley
        {
184 662 berkley
          URL objMurl = new URL(object);
185
          Hashtable objMurlParams = util.parseQuery(objMurl.getQuery());
186
          objDocid = (String)objMurlParams.get("docid");
187 523 berkley
        }
188 662 berkley
        catch(MalformedURLException murle)
189
        { //assume this is just a docid
190
          objDocid = object;
191
        }
192
193
        subDoc = new DocumentImpl(conn, subDocid);
194
        subDoctype = subDoc.getDoctype();
195
        DocumentImpl objDoc = new DocumentImpl(conn, objDocid);
196 671 berkley
        objDoctype = objDoc.getDoctype();
197 662 berkley
198 523 berkley
        //now that the comparisons are done, the new relation can be put
199
        //into xml_relation
200
        StringBuffer insertStmt = new StringBuffer();
201 634 berkley
        insertStmt.append("insert into xml_relation (docid, subject, ");
202
        insertStmt.append("subdoctype, ");
203 523 berkley
        insertStmt.append("relationship, object, objdoctype) values ('");
204 634 berkley
        insertStmt.append(docid).append("', '");
205 523 berkley
        insertStmt.append(subject).append("', '");
206
        insertStmt.append(subDoctype).append("', '");
207
        insertStmt.append(relationship).append("', '");
208
        insertStmt.append(object).append("', '");
209
        insertStmt.append(objDoctype).append("')");
210 671 berkley
211 523 berkley
        pstmt = conn.prepareStatement(insertStmt.toString());
212
        pstmt.execute();
213
214
        hasmorerows = rs.next();
215
      }
216
217
      conn.commit();
218 671 berkley
      pstmt.close();
219 601 berkley
      conn.close();
220 523 berkley
      btThread = null;
221
222 601 berkley
    }
223
    catch(Exception e)
224
    {
225
      try
226
      {
227 523 berkley
        conn.rollback();
228 601 berkley
      }
229
      catch (SQLException sqle) {}
230 675 berkley
      System.out.println("Error in relationHandler.run: " + e.getMessage());
231
      util.debugMessage("Error in relationHandler.run: " + e.getMessage());
232 523 berkley
      e.printStackTrace(System.out);
233
      btThread = null;
234
    }
235
  }
236 634 berkley
237
  /**
238
   * Deletes all of the relations with a docid of 'docid'.
239
   * @param docid the docid to delete.
240
   */
241 645 bojilova
  public void deleteRelations(String docid)
242 634 berkley
  {
243
    try
244
    {
245
      PreparedStatement pstmt = conn.prepareStatement("delete from " +
246
                             "xml_relation where docid like '" + docid + "'");
247
      pstmt.execute();
248 645 bojilova
      pstmt.close();
249 634 berkley
    }
250
    catch(Exception e)
251
    {
252 645 bojilova
      try
253
      {
254
        conn.rollback();
255
      }
256
      catch (SQLException sqle) {}
257 675 berkley
      System.out.println("error in RelationHandler.deleteRelations(): " +
258
                          e.getMessage());
259 634 berkley
      e.printStackTrace(System.out);
260
    }
261
262
  }
263 523 berkley
}