Project

General

Profile

1
/**
2
 *   '$Author: tao $'
3
 *     '$Date: 2003-04-26 15:19:22 -0700 (Sat, 26 Apr 2003) $'
4
 * '$Revision: 1619 $'
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
        subject = MetaCatUtil.getDocIdFromString(triple.getSubject());
110
        relationship = triple.getRelationship();
111
        //object = (new DocumentIdentifier(triple.getObject())).getIdentifier();
112
        object = MetaCatUtil.getDocIdFromString(triple.getObject());
113
        
114
        if (subject != null && relationship != null && object != null)
115
        {
116
        
117
          //put the new relation into xml_relation
118
          MetaCatUtil.debugMessage("Insert into xml_relation table", 30);
119
          tstmt.setString(1, docid);
120
          MetaCatUtil.debugMessage("Insert docid into xml_relation table" + 
121
                                docid, 30);
122
          tstmt.setString(2, packagetype);
123
          tstmt.setString(3, subject);
124
          MetaCatUtil.debugMessage("Insert subject into xml_relation table" + 
125
                               subject, 30);
126
          tstmt.setString(4, subDoctype);
127
          tstmt.setString(5, relationship);
128
          MetaCatUtil.debugMessage("Insert relationship into xml_relation table" + 
129
                                relationship, 30);
130
          tstmt.setString(6, object);
131
          MetaCatUtil.debugMessage("Insert object into xml_relation table" + 
132
                                object, 30);
133
          tstmt.setString(7, objDoctype);
134
          tstmt.execute();  
135
        }//if
136
     
137
      }//for
138
    }//if
139
    
140
    if ( tstmt != null ) 
141
    {
142
      tstmt.close();
143
    }
144
  
145
   
146
  }
147
 
148
  /**
149
   * Deletes all of the relations with a docid of 'docid'.
150
   * @param docid the docid of the package which relations to delete.
151
   */
152
  public void deleteRelations(String docid) throws SQLException
153
  {
154
    try {
155
      PreparedStatement pstmt = connection.prepareStatement(
156
                                "DELETE FROM xml_relation " +
157
                                "WHERE docid = '" + docid + "'");
158
      //increase usage count
159
      connection.increaseUsageCount(1);
160
      pstmt.execute();
161
      pstmt.close();
162
    } catch(SQLException e) {
163
      MetaCatUtil.debugMessage("error in RelationHandler.deleteRelations(): " + 
164
                          e.getMessage(), 30);
165
      
166
      throw e;
167
    }
168
  }
169

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

    
231
}
(52-52/61)