Project

General

Profile

1
/**
2
 *   '$Author: bojilova $'
3
 *     '$Date: 2001-07-17 19:58:29 -0700 (Tue, 17 Jul 2001) $'
4
 * '$Revision: 797 $'
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.net.*;
26

    
27
public class RelationHandler //implements Runnable
28
{
29
  private Thread btThread = null;
30
  private Connection conn = null;
31
  private String docid = null;
32
  MetaCatUtil util = new MetaCatUtil();
33
  
34
  /** 
35
   * Constructor for this class.  finds all of the relations to a single xml
36
   * document and writes them to the database.  This includes transitive
37
   * relations.
38
   * @param docid the ID of the XML document to index.
39
   */
40
  public RelationHandler(String docid, Connection conn)
41
  {
42
    this.conn = conn;
43
    this.docid = docid;
44
    putRelations();
45
  }
46
  
47
  /**
48
   * insert the relations specified in the triples into xml_relation table
49
   */ 
50
  public void putRelations() {
51
    
52
    MetaCatUtil.debugMessage("Running relation handler!");
53

    
54
    // deletes all of the relations with a docid of @docid.
55
    //pseudo-code algorithm
56
    //for each new relation r in xml_nodes
57
    //  put r into xml_relation
58
    //  compare r to each relation already in xml_relation
59
    //  if r isrelatedto a row in xml_relation
60
    //    add a new row to xml_relation that represents this new relation
61
    try {
62

    
63
      DocumentImpl xmldoc = new DocumentImpl(conn);
64
      xmldoc.getDocumentInfo(docid);
65
      String packagetype = xmldoc.getDoctype();
66

    
67
      // first delete the relations for this package document if any
68
      deleteRelations(docid);
69
      // put the new relations
70
      PreparedStatement pstmt = conn.prepareStatement(
71
                                QuerySpecification.printPackageSQL(docid));
72
      pstmt.execute();
73
      
74
      //get the new relations out of xml_nodes
75
      ResultSet rs = pstmt.getResultSet();
76
      boolean hasmorerows = rs.next();
77
      while(hasmorerows) {
78
        String subject = rs.getString(1);
79
        String relationship = rs.getString(2);
80
        String object = rs.getString(3);
81

    
82
        String subjectDoctype = null;
83
        String paramDocid = null;
84
        URL subjectMurl = null;
85

    
86
        //get the current relations information
87
        String subDocid = null;
88
        String objDocid = null;
89
        String subDoctype = null;
90
        String objDoctype = null;
91
        
92
        try {
93
          URL subMurl = new URL(subject);
94
          if(subMurl.getQuery() != null) {
95
            Hashtable subMurlParams = util.parseQuery(subMurl.getQuery());
96
            subDocid = (String)subMurlParams.get("docid");
97
            if(subMurl.getProtocol().equals("metacat")) {
98
              DocumentImpl subDoc = new DocumentImpl(conn, subDocid);
99
              subDoctype = subDoc.getDoctype();
100
            }
101
          } else {
102
            subDocid = subject;
103
          }
104
        } catch(MalformedURLException murle) { 
105
          //assume this is just a docid not a url
106
          subDocid = subject;
107
        }
108
        
109
        try {
110
          URL objMurl = new URL(object); 
111
          if(objMurl.getQuery() != null) {
112
            Hashtable objMurlParams = util.parseQuery(objMurl.getQuery());
113
            objDocid = (String)objMurlParams.get("docid");
114
            if(objMurl.getProtocol().equals("metacat")) {
115
              DocumentImpl objDoc = new DocumentImpl(conn, objDocid);
116
              objDoctype = objDoc.getDoctype();
117
            }
118
          } else {
119
            objDocid = object;
120
          }
121
        } catch(MalformedURLException murle) {
122
          //assume this is just a docid
123
          objDocid = object;
124
        }
125
        
126
        
127
        //now that the comparisons are done, the new relation can be put
128
        //into xml_relation
129
        StringBuffer insertStmt = new StringBuffer();
130
        insertStmt.append("insert into xml_relation (docid, packagetype, ");
131
        insertStmt.append("subject, subdoctype, ");
132
        insertStmt.append("relationship, object, objdoctype) values ('");
133
        insertStmt.append(docid).append("', '");
134
        insertStmt.append(packagetype).append("', '");
135
        insertStmt.append(subject).append("', '");
136
        insertStmt.append(subDoctype).append("', '");
137
        insertStmt.append(relationship).append("', '");
138
        insertStmt.append(object).append("', '");
139
        insertStmt.append(objDoctype).append("')");
140
        
141
        pstmt = conn.prepareStatement(insertStmt.toString());
142
        pstmt.execute(); 
143
        
144
        hasmorerows = rs.next();
145
      }
146
      
147
      pstmt.close();
148
      conn.commit();
149

    
150
    } catch(Exception e) { 
151
      MetaCatUtil.debugMessage("Error in RelationHandler.run(): " + 
152
                               e.getMessage());
153
      System.out.println("Error in RelationHandler.run(): " + 
154
                               e.getMessage());
155
      try { 
156
        conn.rollback();
157
      } catch (SQLException sqle) {
158
        System.out.println("Error in RelationHandler.run(): " + 
159
                           sqle.getMessage());
160
      }
161
    }
162
  }
163
  
164
  /**
165
   * Deletes all of the relations with a docid of 'docid'.
166
   * @param docid the docid to delete.
167
   */
168
  public void deleteRelations(String docid) throws SQLException
169
  {
170
    try {
171
      PreparedStatement pstmt = conn.prepareStatement("delete from " +
172
                             "xml_relation where docid like '" + docid + "'");
173
      pstmt.execute();
174
      pstmt.close();
175
    } catch(SQLException e) {
176
      MetaCatUtil.debugMessage("error in RelationHandler.deleteRelations(): " + 
177
                          e.getMessage());
178
      System.out.println("error in RelationHandler.deleteRelations(): " + 
179
                          e.getMessage());
180
      throw e;
181
    }
182
  }
183
}
(40-40/43)