Project

General

Profile

1
/**
2
 *   '$Author: daigle $'
3
 *     '$Date: 2008-08-05 17:50:14 -0700 (Tue, 05 Aug 2008) $'
4
 * '$Revision: 4213 $'
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.service.PropertyService;
24
import edu.ucsb.nceas.metacat.util.MetaCatUtil;
25
import edu.ucsb.nceas.morpho.datapackage.Triple;
26
import edu.ucsb.nceas.morpho.datapackage.TripleCollection;
27
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
28

    
29
import java.io.StringReader;
30
import java.sql.*;
31
import java.util.*;
32
import java.net.*;
33

    
34
import org.apache.log4j.Logger;
35

    
36

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

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

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

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

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

    
239
		logMetacat.info("The access docid get from xml_relation is: " + aclid);
240
		return aclid;
241
	}
242

    
243
}
(58-58/68)