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 800 jones
      DocumentImpl xmldoc = new DocumentImpl(conn,docid,false);
64
      //DocumentImpl xmldoc = new DocumentImpl(conn);
65
      //xmldoc.getDocumentInfo(docid);
66 797 bojilova
      String packagetype = xmldoc.getDoctype();
67
68
      // first delete the relations for this package document if any
69
      deleteRelations(docid);
70
      // put the new relations
71
      PreparedStatement pstmt = conn.prepareStatement(
72 523 berkley
                                QuerySpecification.printPackageSQL(docid));
73
      pstmt.execute();
74 662 berkley
75 523 berkley
      //get the new relations out of xml_nodes
76
      ResultSet rs = pstmt.getResultSet();
77
      boolean hasmorerows = rs.next();
78 797 bojilova
      while(hasmorerows) {
79 523 berkley
        String subject = rs.getString(1);
80 743 jones
        String relationship = rs.getString(2);
81
        String object = rs.getString(3);
82
83 523 berkley
        String subjectDoctype = null;
84 662 berkley
        String paramDocid = null;
85
        URL subjectMurl = null;
86
87 523 berkley
        //get the current relations information
88 662 berkley
        String subDocid = null;
89
        String objDocid = null;
90 523 berkley
        String subDoctype = null;
91
        String objDoctype = null;
92 662 berkley
93 797 bojilova
        try {
94 662 berkley
          URL subMurl = new URL(subject);
95 797 bojilova
          if(subMurl.getQuery() != null) {
96 679 berkley
            Hashtable subMurlParams = util.parseQuery(subMurl.getQuery());
97
            subDocid = (String)subMurlParams.get("docid");
98 797 bojilova
            if(subMurl.getProtocol().equals("metacat")) {
99
              DocumentImpl subDoc = new DocumentImpl(conn, subDocid);
100 679 berkley
              subDoctype = subDoc.getDoctype();
101
            }
102 797 bojilova
          } else {
103 679 berkley
            subDocid = subject;
104
          }
105 797 bojilova
        } catch(MalformedURLException murle) {
106
          //assume this is just a docid not a url
107 662 berkley
          subDocid = subject;
108
        }
109
110 797 bojilova
        try {
111 662 berkley
          URL objMurl = new URL(object);
112 797 bojilova
          if(objMurl.getQuery() != null) {
113 679 berkley
            Hashtable objMurlParams = util.parseQuery(objMurl.getQuery());
114
            objDocid = (String)objMurlParams.get("docid");
115 797 bojilova
            if(objMurl.getProtocol().equals("metacat")) {
116
              DocumentImpl objDoc = new DocumentImpl(conn, objDocid);
117 679 berkley
              objDoctype = objDoc.getDoctype();
118
            }
119 797 bojilova
          } else {
120 679 berkley
            objDocid = object;
121
          }
122 797 bojilova
        } catch(MalformedURLException murle) {
123
          //assume this is just a docid
124 662 berkley
          objDocid = object;
125
        }
126
127
128 523 berkley
        //now that the comparisons are done, the new relation can be put
129
        //into xml_relation
130
        StringBuffer insertStmt = new StringBuffer();
131 743 jones
        insertStmt.append("insert into xml_relation (docid, packagetype, ");
132
        insertStmt.append("subject, subdoctype, ");
133 523 berkley
        insertStmt.append("relationship, object, objdoctype) values ('");
134 634 berkley
        insertStmt.append(docid).append("', '");
135 743 jones
        insertStmt.append(packagetype).append("', '");
136 523 berkley
        insertStmt.append(subject).append("', '");
137
        insertStmt.append(subDoctype).append("', '");
138
        insertStmt.append(relationship).append("', '");
139
        insertStmt.append(object).append("', '");
140
        insertStmt.append(objDoctype).append("')");
141 671 berkley
142 797 bojilova
        pstmt = conn.prepareStatement(insertStmt.toString());
143 523 berkley
        pstmt.execute();
144
145
        hasmorerows = rs.next();
146
      }
147
148 671 berkley
      pstmt.close();
149 797 bojilova
      conn.commit();
150 523 berkley
151 797 bojilova
    } catch(Exception e) {
152
      MetaCatUtil.debugMessage("Error in RelationHandler.run(): " +
153 743 jones
                               e.getMessage());
154 797 bojilova
      System.out.println("Error in RelationHandler.run(): " +
155
                               e.getMessage());
156
      try {
157 523 berkley
        conn.rollback();
158 797 bojilova
      } catch (SQLException sqle) {
159
        System.out.println("Error in RelationHandler.run(): " +
160
                           sqle.getMessage());
161
      }
162 523 berkley
    }
163
  }
164 634 berkley
165
  /**
166
   * Deletes all of the relations with a docid of 'docid'.
167
   * @param docid the docid to delete.
168
   */
169 797 bojilova
  public void deleteRelations(String docid) throws SQLException
170 634 berkley
  {
171 797 bojilova
    try {
172
      PreparedStatement pstmt = conn.prepareStatement("delete from " +
173 634 berkley
                             "xml_relation where docid like '" + docid + "'");
174
      pstmt.execute();
175 645 bojilova
      pstmt.close();
176 797 bojilova
    } catch(SQLException e) {
177 743 jones
      MetaCatUtil.debugMessage("error in RelationHandler.deleteRelations(): " +
178 683 berkley
                          e.getMessage());
179 797 bojilova
      System.out.println("error in RelationHandler.deleteRelations(): " +
180
                          e.getMessage());
181
      throw e;
182 634 berkley
    }
183
  }
184 523 berkley
}