Project

General

Profile

1
/**
2
 *   '$Author: daigle $'
3
 *     '$Date: 2009-08-24 14:34:17 -0700 (Mon, 24 Aug 2009) $'
4
 * '$Revision: 5030 $'
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.metacat.database.DBConnection;
24
import edu.ucsb.nceas.metacat.database.DBConnectionPool;
25
import edu.ucsb.nceas.metacat.properties.PropertyService;
26
import edu.ucsb.nceas.metacat.util.MetacatUtil;
27
import edu.ucsb.nceas.metacat.util.DocumentUtil;
28
import edu.ucsb.nceas.morpho.datapackage.Triple;
29
import edu.ucsb.nceas.morpho.datapackage.TripleCollection;
30
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
31

    
32
import java.io.StringReader;
33
import java.sql.*;
34
import java.util.*;
35
import java.net.*;
36

    
37
import org.apache.log4j.Logger;
38

    
39

    
40
public class RelationHandler //implements Runnable
41
{
42
  private DBConnection connection = null;
43
  private String docid = null;
44
  private String docType = null;
45
  private static Logger logMetacat = Logger.getLogger(RelationHandler.class);
46

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

    
179
  /**
180
	 * Get the access file id for a package
181
	 * @param docid the document identifier of the package
182
	 * @return the document identifier of the access file for that package
183
	 */
184
	public static String getAccessFileIDWithRevision(String docid) throws SQLException {
185
		String aclid = null;
186
		int rev;
187
		PreparedStatement pstmt = null;
188
		DBConnection dbConn = null;
189
		int serialNumber = -1;
190

    
191
		StringBuffer sql = new StringBuffer();
192
		sql
193
				.append("SELECT docid, rev FROM xml_documents WHERE docid in (SELECT subject ");
194
		sql.append("FROM xml_relation WHERE docid='").append(docid);
195
		sql.append("' AND (");
196
		Vector accessdoctypes;
197
		try {
198
			accessdoctypes = MetacatUtil.getOptionList(PropertyService
199
				.getProperty("xml.accessdoctype"));
200
		} catch (PropertyNotFoundException pnfe) {
201
			throw new SQLException("Could not find access doctype: " + pnfe.getMessage());
202
		}
203
		for (int i = 0; i < accessdoctypes.size(); i++) {
204
			String atype = (String) accessdoctypes.elementAt(i);
205
			sql.append("doctype='").append(atype).append("'");
206
			if (i < accessdoctypes.size() - 1) {
207
				sql.append(" OR ");
208
			}
209
		}
210
		sql.append("))");
211
		//System.out.println("new sql script: " + sql.toString());
212

    
213
		try {
214
			dbConn = DBConnectionPool.getDBConnection("RelationHandler.getAccessFileID");
215
			serialNumber = dbConn.getCheckOutSerialNumber();
216
			pstmt = dbConn.prepareStatement(sql.toString());
217
			pstmt.execute();
218
			ResultSet rs = pstmt.getResultSet();
219
			boolean hasRow = rs.next();
220
			if (hasRow) {
221
				aclid = rs.getString(1);
222
				rev = rs.getInt(2);
223
				String sep = ".";
224
				try {
225
					sep = PropertyService.getProperty("document.accNumSeparator");
226
				} catch (PropertyNotFoundException pnfe) {
227
					logMetacat.error("Could not find account separator.  Setting to '.': " + pnfe.getMessage());
228
				}
229
				aclid = sep + rev;
230
			}
231
			pstmt.close();
232
		}//try
233
		finally {
234
			try {
235
				pstmt.close();
236
			}//try
237
			finally {
238
				DBConnectionPool.returnDBConnection(dbConn, serialNumber);
239
			}//finally
240
		}//finally
241

    
242
		logMetacat.info("The access docid get from xml_relation is: " + aclid);
243
		return aclid;
244
	}
245

    
246
}
(57-57/65)