Project

General

Profile

1
/**
2
 *   '$Author: bojilova $'
3
 *     '$Date: 2001-08-08 12:56:35 -0700 (Wed, 08 Aug 2001) $'
4
 * '$Revision: 819 $'
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
              throws McdbException, SQLException, AccessionNumberException
42
  {
43
    this.conn = conn;
44
    this.docid = docid;
45
    putRelations();
46
  }
47
  
48
  /**
49
   * insert the relations specified in the triples into xml_relation table
50
   */ 
51
  private void putRelations() 
52
              throws McdbException, SQLException, AccessionNumberException
53
  {
54
    String packagetype = null;
55
    String subject = null;
56
    String subDoctype = null;
57
    String relationship = null;
58
    String object = null;
59
    String objDoctype = null;
60
    PreparedStatement pstmt = null; // to get the relations from xml_nodes
61
    PreparedStatement tstmt = null; // to insert each relation into xml_relation
62
    MetaCatUtil.debugMessage("Running relation handler!");
63

    
64
    /* 
65
     * PSEUDO-CODE ALGORITHM:
66
     * deletes all of the relations with a docid of @docid.
67
     * for each new relation r in xml_nodes
68
     *   put r into xml_relation
69
     */
70

    
71
    //DocumentImpl xmldoc = new DocumentImpl(conn,docid,false);
72
    packagetype = (new DocumentImpl(conn,docid,false)).getDoctype();
73

    
74
    // first delete the relations for this package document if any
75
    deleteRelations(docid);
76

    
77
    // to put the new relations get them out of xml_nodes
78
    pstmt = conn.prepareStatement(QuerySpecification.printPackageSQL(docid));
79
    pstmt.execute();
80
    ResultSet rs = pstmt.getResultSet();
81
    boolean hasmorerows = rs.next();
82
    if (hasmorerows) {
83
      tstmt = conn.prepareStatement("INSERT INTO xml_relation (" +
84
                                    "docid,packagetype,subject,subdoctype," +
85
                                    "relationship, object, objdoctype) " + 
86
                                    "VALUES (?, ?, ?, ?, ?, ?, ?)");
87
    }
88
    while(hasmorerows) {
89
      subject = rs.getString(1);
90
      relationship = rs.getString(2);
91
      object = rs.getString(3);
92

    
93
      // cut out the revision number for subject and object
94
      subject = (new DocumentIdentifier(subject)).getIdentifier();
95
      object = (new DocumentIdentifier(object)).getIdentifier();
96

    
97
      //subDoctype and objDoctype are N/A
98
      subDoctype = null;
99
      objDoctype = null;
100

    
101
      //put the new relation into xml_relation
102
      tstmt.setString(1, docid);
103
      tstmt.setString(2, packagetype);
104
      tstmt.setString(3, subject);
105
      tstmt.setString(4, subDoctype);
106
      tstmt.setString(5, relationship);
107
      tstmt.setString(6, object);
108
      tstmt.setString(7, objDoctype);
109
      tstmt.execute(); 
110
        
111
      hasmorerows = rs.next();
112
    }
113
      
114
    if ( tstmt != null ) {
115
      tstmt.close();
116
    }
117
    pstmt.close();
118
    conn.commit();
119
  }
120
  
121
  /**
122
   * Deletes all of the relations with a docid of 'docid'.
123
   * @param docid the docid of the package which relations to delete.
124
   */
125
  public void deleteRelations(String docid) throws SQLException
126
  {
127
    try {
128
      PreparedStatement pstmt = conn.prepareStatement(
129
                                "DELETE FROM xml_relation " +
130
                                "WHERE docid = '" + docid + "'");
131
      pstmt.execute();
132
      pstmt.close();
133
    } catch(SQLException e) {
134
      MetaCatUtil.debugMessage("error in RelationHandler.deleteRelations(): " + 
135
                          e.getMessage());
136
      System.out.println("error in RelationHandler.deleteRelations(): " + 
137
                          e.getMessage());
138
      throw e;
139
    }
140
  }
141

    
142
  /**
143
   * Get the access file id for a package
144
   * @param docid the document identifier of the package
145
   * @return the document identifier of the access file for that package
146
   */
147
  public String getAccessFileID(String docid) throws SQLException
148
  {
149
    String aclid = null;
150
    PreparedStatement pstmt = 
151
      conn.prepareStatement("SELECT docid FROM xml_documents " +
152
                            "WHERE docid in " +
153
                            "(SELECT subject FROM xml_relation " +
154
                            "WHERE docid = ?) " +
155
                            "AND doctype = ?");
156
    pstmt.setString(1, docid);
157
    pstmt.setString(2, MetaCatUtil.getOption("accessdoctype"));
158
    pstmt.execute();
159
    ResultSet rs = pstmt.getResultSet();
160
    boolean hasRow = rs.next();
161
    if (hasRow) {
162
      aclid = rs.getString(1);
163
    }
164
    pstmt.close();
165
    
166
    return aclid;
167
  }
168

    
169
}
(37-37/40)