Project

General

Profile

1
/**
2
 *   '$Author: tao $'
3
 *     '$Date: 2003-04-19 19:09:05 -0700 (Sat, 19 Apr 2003) $'
4
 * '$Revision: 1592 $'
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 edu.ucsb.nceas.morpho.datapackage.Triple;
24
import edu.ucsb.nceas.morpho.datapackage.TripleCollection;
25

    
26
import java.io.StringReader;
27
import java.sql.*;
28
import java.util.*;
29
import java.net.*;
30

    
31

    
32
public class RelationHandler //implements Runnable
33
{
34
  private DBConnection connection = null;
35
  private String docid = null;
36
  private String docType = null;
37
  
38
  TripleCollection tripleForPackage = null;
39
   
40
  /** 
41
   * Constructor for this class.  finds all of the relations to a single xml
42
   * document and writes them to the database.  This includes transitive
43
   * relations.
44
   * @param docid the ID of the XML document to index.
45
   * @param doctype the doctype of this document
46
   * @param conn the db connection
47
   * @param list the triple list
48
   */
49
  public RelationHandler(String docid, String doctype, 
50
                        DBConnection conn, TripleCollection list)
51
              throws McdbException, SQLException, AccessionNumberException
52
  {
53
    this.connection = conn;
54
    this.docid = docid;
55
    this.docType = doctype;
56
    tripleForPackage = list;
57
    createRelations();
58
  }
59
  
60
   /**
61
   * insert the relations specified in the triples into xml_relation table
62
   */ 
63
  private void createRelations() 
64
              throws McdbException, SQLException, AccessionNumberException
65
  {
66
    String packagetype = docType;
67
    String subject = null;
68
    String subjectParentId = null;
69
    String subDoctype = null;
70
    String relationship = null;
71
    String relationshipParentId = null;
72
    String object = null;
73
    String objDoctype = null;
74
    PreparedStatement tstmt = null; // to insert each relation into xml_relation
75
   
76
    MetaCatUtil.debugMessage("Running relation handler!", 40);
77
   
78
    // first delete the relations for this package document if any
79
    deleteRelations(docid);
80
 
81
    //get the vetor of triples 
82
    Vector tripleList= new Vector();
83
    //get vector form tripleCollection
84
    if (tripleForPackage != null)
85
    {
86
      tripleList =tripleForPackage.getCollection();
87
    }
88
    
89
    if (tripleList != null && tripleList.size()>0) 
90
    {
91
      
92
       tstmt = connection.prepareStatement("INSERT INTO xml_relation (" +
93
                                    "docid,packagetype,subject,subdoctype," +
94
                                    "relationship, object, objdoctype) " + 
95
                                    "VALUES (?, ?, ?, ?, ?, ?, ?)");
96
      
97
      // go through tripe list 
98
      for (int i= 0; i<tripleList.size(); i++)
99
      {
100
        //increase usage count
101
        connection.increaseUsageCount(1);
102
        // Get the triple
103
        Triple triple = (Triple)tripleList.elementAt(i);
104
        MetaCatUtil.debugMessage("Info from triple: ", 30);
105
        MetaCatUtil.debugMessage("subject from triple:"+triple.getSubject(), 30);
106
        MetaCatUtil.debugMessage("relationship from triple:"+triple.getRelationship(),30);
107
        MetaCatUtil.debugMessage("object from triple: "+triple.getObject(),30);
108
        subject = (new DocumentIdentifier(triple.getSubject())).getIdentifier();
109
        relationship = triple.getRelationship();
110
        object = (new DocumentIdentifier(triple.getObject())).getIdentifier();
111
     
112
        //put the new relation into xml_relation
113
        MetaCatUtil.debugMessage("Insert into xml_relation table", 30);
114
        tstmt.setString(1, docid);
115
        MetaCatUtil.debugMessage("Insert docid into xml_relation table" + 
116
                                docid, 30);
117
        tstmt.setString(2, packagetype);
118
        tstmt.setString(3, subject);
119
        MetaCatUtil.debugMessage("Insert subject into xml_relation table" + 
120
                               subject, 30);
121
        tstmt.setString(4, subDoctype);
122
        tstmt.setString(5, relationship);
123
        MetaCatUtil.debugMessage("Insert relationship into xml_relation table" + 
124
                                relationship, 30);
125
        tstmt.setString(6, object);
126
        MetaCatUtil.debugMessage("Insert object into xml_relation table" + 
127
                                object, 30);
128
        tstmt.setString(7, objDoctype);
129
        tstmt.execute();  
130
     
131
      }//for
132
    }//if
133
    
134
    if ( tstmt != null ) 
135
    {
136
      tstmt.close();
137
    }
138
  
139
   
140
  }
141
 
142
  /**
143
   * Deletes all of the relations with a docid of 'docid'.
144
   * @param docid the docid of the package which relations to delete.
145
   */
146
  public void deleteRelations(String docid) throws SQLException
147
  {
148
    try {
149
      PreparedStatement pstmt = connection.prepareStatement(
150
                                "DELETE FROM xml_relation " +
151
                                "WHERE docid = '" + docid + "'");
152
      //increase usage count
153
      connection.increaseUsageCount(1);
154
      pstmt.execute();
155
      pstmt.close();
156
    } catch(SQLException e) {
157
      MetaCatUtil.debugMessage("error in RelationHandler.deleteRelations(): " + 
158
                          e.getMessage(), 30);
159
      
160
      throw e;
161
    }
162
  }
163

    
164
  /**
165
   * Get the access file id for a package
166
   * @param docid the document identifier of the package
167
   * @return the document identifier of the access file for that package
168
   */
169
  public static String getAccessFileID(String docid) throws SQLException
170
  {
171
    String aclid = null;
172
    PreparedStatement pstmt = null;
173
    DBConnection dbConn = null;
174
    int serialNumber = -1;
175
    
176
    StringBuffer sql = new StringBuffer();
177
    sql.append("SELECT docid FROM xml_documents WHERE docid in (SELECT subject ");
178
    sql.append("FROM xml_relation WHERE docid='").append(docid);
179
    sql.append("' AND (");
180
    Vector accessdoctypes = MetaCatUtil.getOptionList(
181
                              MetaCatUtil.getOption("accessdoctype"));
182
    for(int i=0; i<accessdoctypes.size(); i++)
183
    {
184
      String atype = (String)accessdoctypes.elementAt(i);
185
      sql.append("doctype='").append(atype).append("'");
186
      if(i < accessdoctypes.size()-1)
187
      {
188
        sql.append(" OR ");
189
      }
190
    }
191
    sql.append("))");
192
    //System.out.println("new sql script: " + sql.toString());
193
    
194
    try
195
    {
196
      dbConn=DBConnectionPool.
197
                  getDBConnection("RelationHandler.getAccessFileID");
198
      serialNumber=dbConn.getCheckOutSerialNumber();
199
      pstmt = dbConn.prepareStatement(sql.toString());
200
      pstmt.execute();
201
      ResultSet rs = pstmt.getResultSet();
202
      boolean hasRow = rs.next();
203
      if (hasRow) {
204
        aclid = rs.getString(1);
205
      }
206
      pstmt.close();
207
    }//try
208
    finally
209
    {
210
      try
211
      {
212
        pstmt.close();
213
      }//try
214
      finally
215
      {
216
        DBConnectionPool.returnDBConnection(dbConn, serialNumber);
217
      }//finally
218
    }//finally
219
     
220
    MetaCatUtil.debugMessage("The access docid get from xml_relation is: "+
221
                              aclid, 30);
222
    return aclid;
223
  }
224

    
225
}
(49-49/57)