Project

General

Profile

1 523 berkley
/**
2
 *   '$Author$'
3
 *     '$Date$'
4
 * '$Revision$'
5 669 jones
 *
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 523 berkley
 */
20
21
package edu.ucsb.nceas.metacat;
22
23 5015 daigle
import edu.ucsb.nceas.metacat.database.DBConnection;
24
import edu.ucsb.nceas.metacat.database.DBConnectionPool;
25 5030 daigle
import edu.ucsb.nceas.metacat.properties.PropertyService;
26 4698 daigle
import edu.ucsb.nceas.metacat.util.MetacatUtil;
27 5025 daigle
import edu.ucsb.nceas.metacat.util.DocumentUtil;
28 1577 tao
import edu.ucsb.nceas.morpho.datapackage.Triple;
29
import edu.ucsb.nceas.morpho.datapackage.TripleCollection;
30 4080 daigle
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
31 1577 tao
32
import java.io.StringReader;
33 523 berkley
import java.sql.*;
34
import java.util.*;
35 662 berkley
import java.net.*;
36 523 berkley
37 2663 sgarg
import org.apache.log4j.Logger;
38 1577 tao
39 2663 sgarg
40 797 bojilova
public class RelationHandler //implements Runnable
41 523 berkley
{
42 1217 tao
  private DBConnection connection = null;
43 797 bojilova
  private String docid = null;
44 1592 tao
  private String docType = null;
45 2663 sgarg
  private static Logger logMetacat = Logger.getLogger(RelationHandler.class);
46
47 1592 tao
  TripleCollection tripleForPackage = null;
48
49 523 berkley
  /**
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 797 bojilova
   * @param docid the ID of the XML document to index.
54 1592 tao
   * @param doctype the doctype of this document
55
   * @param conn the db connection
56
   * @param list the triple list
57 523 berkley
   */
58 1592 tao
  public RelationHandler(String docid, String doctype,
59
                        DBConnection conn, TripleCollection list)
60 819 bojilova
              throws McdbException, SQLException, AccessionNumberException
61 523 berkley
  {
62 1217 tao
    this.connection = conn;
63 797 bojilova
    this.docid = docid;
64 1592 tao
    this.docType = doctype;
65
    tripleForPackage = list;
66 1577 tao
    createRelations();
67 523 berkley
  }
68
69 1577 tao
   /**
70 797 bojilova
   * insert the relations specified in the triples into xml_relation table
71 683 berkley
   */
72 1577 tao
  private void createRelations()
73 819 bojilova
              throws McdbException, SQLException, AccessionNumberException
74
  {
75 1592 tao
    String packagetype = docType;
76 819 bojilova
    String subject = null;
77 1388 tao
    String subjectParentId = null;
78 819 bojilova
    String subDoctype = null;
79
    String relationship = null;
80 1388 tao
    String relationshipParentId = null;
81 819 bojilova
    String object = null;
82
    String objDoctype = null;
83
    PreparedStatement tstmt = null; // to insert each relation into xml_relation
84 1388 tao
85 2663 sgarg
    logMetacat.info("Running relation handler!");
86 1592 tao
87 819 bojilova
    // first delete the relations for this package document if any
88
    deleteRelations(docid);
89 1592 tao
90 1577 tao
    //get the vetor of triples
91 1592 tao
    Vector tripleList= new Vector();
92
    //get vector form tripleCollection
93
    if (tripleForPackage != null)
94
    {
95
      tripleList =tripleForPackage.getCollection();
96
    }
97 1577 tao
98
    if (tripleList != null && tripleList.size()>0)
99
    {
100 1388 tao
101 1577 tao
       tstmt = connection.prepareStatement("INSERT INTO xml_relation (" +
102 819 bojilova
                                    "docid,packagetype,subject,subdoctype," +
103
                                    "relationship, object, objdoctype) " +
104
                                    "VALUES (?, ?, ?, ?, ?, ?, ?)");
105 1592 tao
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 2663 sgarg
        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 1595 tao
        //subject = (new DocumentIdentifier(triple.getSubject())).getIdentifier();
118 5025 daigle
        subject = DocumentUtil.getDocIdFromString(triple.getSubject());
119 1592 tao
        relationship = triple.getRelationship();
120 1595 tao
        //object = (new DocumentIdentifier(triple.getObject())).getIdentifier();
121 5025 daigle
        object = DocumentUtil.getDocIdFromString(triple.getObject());
122 1595 tao
123 1619 tao
        if (subject != null && relationship != null && object != null)
124
        {
125
126
          //put the new relation into xml_relation
127 2663 sgarg
          logMetacat.info("Insert into xml_relation table");
128 1619 tao
          tstmt.setString(1, docid);
129 2663 sgarg
          logMetacat.info("Insert docid into xml_relation table" +
130
                                docid);
131 1619 tao
          tstmt.setString(2, packagetype);
132
          tstmt.setString(3, subject);
133 2663 sgarg
          logMetacat.info("Insert subject into xml_relation table" +
134
                               subject);
135 1619 tao
          tstmt.setString(4, subDoctype);
136
          tstmt.setString(5, relationship);
137 2663 sgarg
          logMetacat.info("Insert relationship into xml_relation table" +
138
                                relationship);
139 1619 tao
          tstmt.setString(6, object);
140 2663 sgarg
          logMetacat.info("Insert object into xml_relation table" +
141
                                object);
142 1619 tao
          tstmt.setString(7, objDoctype);
143
          tstmt.execute();
144
        }//if
145 1577 tao
146 1592 tao
      }//for
147
    }//if
148 1577 tao
149
    if ( tstmt != null )
150
    {
151 819 bojilova
      tstmt.close();
152 523 berkley
    }
153 1577 tao
154
155 523 berkley
  }
156 1577 tao
157 634 berkley
  /**
158
   * Deletes all of the relations with a docid of 'docid'.
159 819 bojilova
   * @param docid the docid of the package which relations to delete.
160 634 berkley
   */
161 797 bojilova
  public void deleteRelations(String docid) throws SQLException
162 634 berkley
  {
163 797 bojilova
    try {
164 1217 tao
      PreparedStatement pstmt = connection.prepareStatement(
165 819 bojilova
                                "DELETE FROM xml_relation " +
166 6595 leinfelder
                                "WHERE docid = ?");
167
      pstmt.setString(1, docid);
168 1217 tao
      //increase usage count
169
      connection.increaseUsageCount(1);
170 634 berkley
      pstmt.execute();
171 645 bojilova
      pstmt.close();
172 797 bojilova
    } catch(SQLException e) {
173 2663 sgarg
      logMetacat.error("error in RelationHandler.deleteRelations(): " +
174
                          e.getMessage());
175 1499 tao
176 797 bojilova
      throw e;
177 634 berkley
    }
178
  }
179 819 bojilova
180
  /**
181 4080 daigle
	 * Get the access file id for a package
182
	 * @param docid the document identifier of the package
183
	 * @return the document identifier of the access file for that package
184
	 */
185
	public static String getAccessFileIDWithRevision(String docid) throws SQLException {
186
		String aclid = null;
187
		int rev;
188
		PreparedStatement pstmt = null;
189
		DBConnection dbConn = null;
190
		int serialNumber = -1;
191 819 bojilova
192 4080 daigle
		StringBuffer sql = new StringBuffer();
193
		sql
194
				.append("SELECT docid, rev FROM xml_documents WHERE docid in (SELECT subject ");
195 6595 leinfelder
		sql.append("FROM xml_relation WHERE docid = ? ");
196
		sql.append(" AND (");
197 4080 daigle
		Vector accessdoctypes;
198
		try {
199 4698 daigle
			accessdoctypes = MetacatUtil.getOptionList(PropertyService
200 4213 daigle
				.getProperty("xml.accessdoctype"));
201 4080 daigle
		} catch (PropertyNotFoundException pnfe) {
202
			throw new SQLException("Could not find access doctype: " + pnfe.getMessage());
203
		}
204
		for (int i = 0; i < accessdoctypes.size(); i++) {
205
			String atype = (String) accessdoctypes.elementAt(i);
206
			sql.append("doctype='").append(atype).append("'");
207
			if (i < accessdoctypes.size() - 1) {
208
				sql.append(" OR ");
209
			}
210
		}
211
		sql.append("))");
212
		//System.out.println("new sql script: " + sql.toString());
213
214
		try {
215
			dbConn = DBConnectionPool.getDBConnection("RelationHandler.getAccessFileID");
216
			serialNumber = dbConn.getCheckOutSerialNumber();
217
			pstmt = dbConn.prepareStatement(sql.toString());
218 6595 leinfelder
			pstmt.setString(1, docid);
219 4080 daigle
			pstmt.execute();
220
			ResultSet rs = pstmt.getResultSet();
221
			boolean hasRow = rs.next();
222
			if (hasRow) {
223
				aclid = rs.getString(1);
224
				rev = rs.getInt(2);
225
				String sep = ".";
226
				try {
227 4212 daigle
					sep = PropertyService.getProperty("document.accNumSeparator");
228 4080 daigle
				} catch (PropertyNotFoundException pnfe) {
229
					logMetacat.error("Could not find account separator.  Setting to '.': " + pnfe.getMessage());
230
				}
231
				aclid = sep + rev;
232
			}
233
			pstmt.close();
234
		}//try
235
		finally {
236
			try {
237
				pstmt.close();
238
			}//try
239
			finally {
240
				DBConnectionPool.returnDBConnection(dbConn, serialNumber);
241
			}//finally
242
		}//finally
243
244
		logMetacat.info("The access docid get from xml_relation is: " + aclid);
245
		return aclid;
246
	}
247
248 523 berkley
}