Project

General

Profile

1
/**
2
 *   '$Author: jones $'
3
 *     '$Date: 2001-05-21 13:51:10 -0700 (Mon, 21 May 2001) $'
4
 * '$Revision: 743 $'
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
      conn = MetacatReplication.getDBConnection("RelationHandler." +
47
                                                "relationHandler");
48
    }
49
    catch(Exception e)
50
    {
51
      MetaCatUtil.debugMessage("unable to get db connection in " + 
52
                         "relationhandler: " + e.getMessage());
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
    MetaCatUtil.debugMessage("Running relation handler!");
69
    Connection dbconn = null;
70
    String docid = xmldoc.getDocID();
71
    String packagetype = xmldoc.getDoctype();
72
    // deletes all of the relations with a docid of @docid.
73
    deleteRelations(docid);
74
    //pseudo-code algorithm
75
    //for each new relation r in xml_nodes
76
    //  put r into xml_relation
77
    //  compare r to each relation already in xml_relation
78
    //  if r isrelatedto a row in xml_relation
79
    //    add a new row to xml_relation that represents this new relation
80
    try
81
    {
82
      dbconn = MetacatReplication.getDBConnection("RelationHandler." +
83
                                                  "run");
84

    
85
      PreparedStatement pstmt = dbconn.prepareStatement(
86
                                QuerySpecification.printPackageSQL(docid));
87
      pstmt.execute();
88
      
89
      //get the new relations out of xml_nodes
90
      ResultSet rs = pstmt.getResultSet();
91
      boolean hasmorerows = rs.next();
92
      while(hasmorerows)
93
      {
94
        String subject = rs.getString(1);
95
        String relationship = rs.getString(2);
96
        String object = rs.getString(3);
97

    
98
        String subjectDoctype = null;
99
        String paramDocid = null;
100
        URL subjectMurl = null;
101

    
102
        //get the current relations information
103
        String subDocid = null;
104
        String objDocid = null;
105
        String subDoctype = null;
106
        String objDoctype = null;
107
        
108
        try
109
        {
110
          URL subMurl = new URL(subject);
111
          if(subMurl.getQuery() != null)
112
          {
113
            Hashtable subMurlParams = util.parseQuery(subMurl.getQuery());
114
            subDocid = (String)subMurlParams.get("docid");
115
            if(subMurl.getProtocol().equals("metacat"))
116
            {
117
              DocumentImpl subDoc = new DocumentImpl(dbconn, subDocid);
118
              subDoctype = subDoc.getDoctype();
119
            }
120
          }
121
          else
122
          {
123
            subDocid = subject;
124
          }
125
        }
126
        catch(MalformedURLException murle)
127
        { //assume this is just a docid not a url
128
          subDocid = subject;
129
        }
130
        
131
        try
132
        {
133
          URL objMurl = new URL(object); 
134
          if(objMurl.getQuery() != null)
135
          {
136
            Hashtable objMurlParams = util.parseQuery(objMurl.getQuery());
137
            objDocid = (String)objMurlParams.get("docid");
138
            if(objMurl.getProtocol().equals("metacat"))
139
            {
140
              DocumentImpl objDoc = new DocumentImpl(dbconn, objDocid);
141
              objDoctype = objDoc.getDoctype();
142
            }
143
          }
144
          else
145
          {
146
            objDocid = object;
147
          }
148
        }
149
        catch(MalformedURLException murle)
150
        { //assume this is just a docid
151
          objDocid = object;
152
        }
153
        
154
        
155
        //now that the comparisons are done, the new relation can be put
156
        //into xml_relation
157
        StringBuffer insertStmt = new StringBuffer();
158
        insertStmt.append("insert into xml_relation (docid, packagetype, ");
159
        insertStmt.append("subject, subdoctype, ");
160
        insertStmt.append("relationship, object, objdoctype) values ('");
161
        insertStmt.append(docid).append("', '");
162
        insertStmt.append(packagetype).append("', '");
163
        insertStmt.append(subject).append("', '");
164
        insertStmt.append(subDoctype).append("', '");
165
        insertStmt.append(relationship).append("', '");
166
        insertStmt.append(object).append("', '");
167
        insertStmt.append(objDoctype).append("')");
168
        
169
        pstmt = dbconn.prepareStatement(insertStmt.toString());
170
        pstmt.execute(); 
171
        
172
        hasmorerows = rs.next();
173
      }
174
      
175
      dbconn.commit();
176
      pstmt.close();
177
      dbconn.close();
178
      btThread = null;
179

    
180
    } 
181
    catch(Exception e) 
182
    { 
183
      MetaCatUtil.debugMessage("Error in relationHandler.run: " + 
184
                               e.getMessage());
185
      btThread = null;
186
      try 
187
      { 
188
        conn.rollback();
189
      } 
190
      catch (SQLException sqle) {}
191
    }
192
  }
193
  
194
  /**
195
   * Deletes all of the relations with a docid of 'docid'.
196
   * @param docid the docid to delete.
197
   */
198
  public void deleteRelations(String docid)
199
  {
200
    Connection dbconn = null;
201
    try
202
    {
203
      dbconn = MetacatReplication.getDBConnection("RelationHandler."+
204
                                                  "deleteRelations");
205
    }
206
    catch(Exception ee)
207
    {
208
      MetaCatUtil.debugMessage("error in relationHandler.deleteRelations: " +
209
                          ee.getMessage());
210
    }
211
    
212
    try
213
    {
214
      PreparedStatement pstmt = dbconn.prepareStatement("delete from " +
215
                             "xml_relation where docid like '" + docid + "'");
216
      pstmt.execute();
217
      pstmt.close();
218
      dbconn.close();
219
    }
220
    catch(Exception e)
221
    {
222
      MetaCatUtil.debugMessage("error in RelationHandler.deleteRelations(): " + 
223
                          e.getMessage());
224
      try 
225
      { 
226
        dbconn.rollback();
227
        dbconn.close();
228
      } 
229
      catch (SQLException sqle) {}
230
    }
231
  }
232
}
(40-40/43)