Project

General

Profile

1
/**
2
 *   '$Author: sgarg $'
3
 *     '$Date: 2005-10-10 11:06:55 -0700 (Mon, 10 Oct 2005) $'
4
 * '$Revision: 2663 $'
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
import org.apache.log4j.Logger;
32

    
33

    
34
public class RelationHandler //implements Runnable
35
{
36
  private DBConnection connection = null;
37
  private String docid = null;
38
  private String docType = null;
39
  private static Logger logMetacat = Logger.getLogger(RelationHandler.class);
40

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

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

    
237
}
(56-56/66)