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 662 berkley
import java.net.*;
26 523 berkley
27 797 bojilova
public class RelationHandler //implements Runnable
28 523 berkley
{
29
  private Thread btThread = null;
30
  private Connection conn = null;
31 797 bojilova
  private String docid = null;
32 523 berkley
  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 797 bojilova
   * @param docid the ID of the XML document to index.
39 523 berkley
   */
40 797 bojilova
  public RelationHandler(String docid, Connection conn)
41 523 berkley
  {
42 797 bojilova
    this.conn = conn;
43
    this.docid = docid;
44
    putRelations();
45 523 berkley
  }
46
47
  /**
48 797 bojilova
   * insert the relations specified in the triples into xml_relation table
49 683 berkley
   */
50 797 bojilova
  public void putRelations() {
51
52 743 jones
    MetaCatUtil.debugMessage("Running relation handler!");
53 797 bojilova
54 645 bojilova
    // deletes all of the relations with a docid of @docid.
55 523 berkley
    //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 797 bojilova
    try {
62 743 jones
63 797 bojilova
      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 523 berkley
                                QuerySpecification.printPackageSQL(docid));
72
      pstmt.execute();
73 662 berkley
74 523 berkley
      //get the new relations out of xml_nodes
75
      ResultSet rs = pstmt.getResultSet();
76
      boolean hasmorerows = rs.next();
77 797 bojilova
      while(hasmorerows) {
78 523 berkley
        String subject = rs.getString(1);
79 743 jones
        String relationship = rs.getString(2);
80
        String object = rs.getString(3);
81
82 523 berkley
        String subjectDoctype = null;
83 662 berkley
        String paramDocid = null;
84
        URL subjectMurl = null;
85
86 523 berkley
        //get the current relations information
87 662 berkley
        String subDocid = null;
88
        String objDocid = null;
89 523 berkley
        String subDoctype = null;
90
        String objDoctype = null;
91 662 berkley
92 797 bojilova
        try {
93 662 berkley
          URL subMurl = new URL(subject);
94 797 bojilova
          if(subMurl.getQuery() != null) {
95 679 berkley
            Hashtable subMurlParams = util.parseQuery(subMurl.getQuery());
96
            subDocid = (String)subMurlParams.get("docid");
97 797 bojilova
            if(subMurl.getProtocol().equals("metacat")) {
98
              DocumentImpl subDoc = new DocumentImpl(conn, subDocid);
99 679 berkley
              subDoctype = subDoc.getDoctype();
100
            }
101 797 bojilova
          } else {
102 679 berkley
            subDocid = subject;
103
          }
104 797 bojilova
        } catch(MalformedURLException murle) {
105
          //assume this is just a docid not a url
106 662 berkley
          subDocid = subject;
107
        }
108
109 797 bojilova
        try {
110 662 berkley
          URL objMurl = new URL(object);
111 797 bojilova
          if(objMurl.getQuery() != null) {
112 679 berkley
            Hashtable objMurlParams = util.parseQuery(objMurl.getQuery());
113
            objDocid = (String)objMurlParams.get("docid");
114 797 bojilova
            if(objMurl.getProtocol().equals("metacat")) {
115
              DocumentImpl objDoc = new DocumentImpl(conn, objDocid);
116 679 berkley
              objDoctype = objDoc.getDoctype();
117
            }
118 797 bojilova
          } else {
119 679 berkley
            objDocid = object;
120
          }
121 797 bojilova
        } catch(MalformedURLException murle) {
122
          //assume this is just a docid
123 662 berkley
          objDocid = object;
124
        }
125
126
127 523 berkley
        //now that the comparisons are done, the new relation can be put
128
        //into xml_relation
129
        StringBuffer insertStmt = new StringBuffer();
130 743 jones
        insertStmt.append("insert into xml_relation (docid, packagetype, ");
131
        insertStmt.append("subject, subdoctype, ");
132 523 berkley
        insertStmt.append("relationship, object, objdoctype) values ('");
133 634 berkley
        insertStmt.append(docid).append("', '");
134 743 jones
        insertStmt.append(packagetype).append("', '");
135 523 berkley
        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 671 berkley
141 797 bojilova
        pstmt = conn.prepareStatement(insertStmt.toString());
142 523 berkley
        pstmt.execute();
143
144
        hasmorerows = rs.next();
145
      }
146
147 671 berkley
      pstmt.close();
148 797 bojilova
      conn.commit();
149 523 berkley
150 797 bojilova
    } catch(Exception e) {
151
      MetaCatUtil.debugMessage("Error in RelationHandler.run(): " +
152 743 jones
                               e.getMessage());
153 797 bojilova
      System.out.println("Error in RelationHandler.run(): " +
154
                               e.getMessage());
155
      try {
156 523 berkley
        conn.rollback();
157 797 bojilova
      } catch (SQLException sqle) {
158
        System.out.println("Error in RelationHandler.run(): " +
159
                           sqle.getMessage());
160
      }
161 523 berkley
    }
162
  }
163 634 berkley
164
  /**
165
   * Deletes all of the relations with a docid of 'docid'.
166
   * @param docid the docid to delete.
167
   */
168 797 bojilova
  public void deleteRelations(String docid) throws SQLException
169 634 berkley
  {
170 797 bojilova
    try {
171
      PreparedStatement pstmt = conn.prepareStatement("delete from " +
172 634 berkley
                             "xml_relation where docid like '" + docid + "'");
173
      pstmt.execute();
174 645 bojilova
      pstmt.close();
175 797 bojilova
    } catch(SQLException e) {
176 743 jones
      MetaCatUtil.debugMessage("error in RelationHandler.deleteRelations(): " +
177 683 berkley
                          e.getMessage());
178 797 bojilova
      System.out.println("error in RelationHandler.deleteRelations(): " +
179
                          e.getMessage());
180
      throw e;
181 634 berkley
    }
182
  }
183 523 berkley
}