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 4080 daigle
import edu.ucsb.nceas.metacat.service.PropertyService;
24 4698 daigle
import edu.ucsb.nceas.metacat.util.MetacatUtil;
25 1577 tao
import edu.ucsb.nceas.morpho.datapackage.Triple;
26
import edu.ucsb.nceas.morpho.datapackage.TripleCollection;
27 4080 daigle
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
28 1577 tao
29
import java.io.StringReader;
30 523 berkley
import java.sql.*;
31
import java.util.*;
32 662 berkley
import java.net.*;
33 523 berkley
34 2663 sgarg
import org.apache.log4j.Logger;
35 1577 tao
36 2663 sgarg
37 797 bojilova
public class RelationHandler //implements Runnable
38 523 berkley
{
39 1217 tao
  private DBConnection connection = null;
40 797 bojilova
  private String docid = null;
41 1592 tao
  private String docType = null;
42 2663 sgarg
  private static Logger logMetacat = Logger.getLogger(RelationHandler.class);
43
44 1592 tao
  TripleCollection tripleForPackage = null;
45
46 523 berkley
  /**
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 797 bojilova
   * @param docid the ID of the XML document to index.
51 1592 tao
   * @param doctype the doctype of this document
52
   * @param conn the db connection
53
   * @param list the triple list
54 523 berkley
   */
55 1592 tao
  public RelationHandler(String docid, String doctype,
56
                        DBConnection conn, TripleCollection list)
57 819 bojilova
              throws McdbException, SQLException, AccessionNumberException
58 523 berkley
  {
59 1217 tao
    this.connection = conn;
60 797 bojilova
    this.docid = docid;
61 1592 tao
    this.docType = doctype;
62
    tripleForPackage = list;
63 1577 tao
    createRelations();
64 523 berkley
  }
65
66 1577 tao
   /**
67 797 bojilova
   * insert the relations specified in the triples into xml_relation table
68 683 berkley
   */
69 1577 tao
  private void createRelations()
70 819 bojilova
              throws McdbException, SQLException, AccessionNumberException
71
  {
72 1592 tao
    String packagetype = docType;
73 819 bojilova
    String subject = null;
74 1388 tao
    String subjectParentId = null;
75 819 bojilova
    String subDoctype = null;
76
    String relationship = null;
77 1388 tao
    String relationshipParentId = null;
78 819 bojilova
    String object = null;
79
    String objDoctype = null;
80
    PreparedStatement tstmt = null; // to insert each relation into xml_relation
81 1388 tao
82 2663 sgarg
    logMetacat.info("Running relation handler!");
83 1592 tao
84 819 bojilova
    // first delete the relations for this package document if any
85
    deleteRelations(docid);
86 1592 tao
87 1577 tao
    //get the vetor of triples
88 1592 tao
    Vector tripleList= new Vector();
89
    //get vector form tripleCollection
90
    if (tripleForPackage != null)
91
    {
92
      tripleList =tripleForPackage.getCollection();
93
    }
94 1577 tao
95
    if (tripleList != null && tripleList.size()>0)
96
    {
97 1388 tao
98 1577 tao
       tstmt = connection.prepareStatement("INSERT INTO xml_relation (" +
99 819 bojilova
                                    "docid,packagetype,subject,subdoctype," +
100
                                    "relationship, object, objdoctype) " +
101
                                    "VALUES (?, ?, ?, ?, ?, ?, ?)");
102 1592 tao
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 2663 sgarg
        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 1595 tao
        //subject = (new DocumentIdentifier(triple.getSubject())).getIdentifier();
115 4698 daigle
        subject = MetacatUtil.getDocIdFromString(triple.getSubject());
116 1592 tao
        relationship = triple.getRelationship();
117 1595 tao
        //object = (new DocumentIdentifier(triple.getObject())).getIdentifier();
118 4698 daigle
        object = MetacatUtil.getDocIdFromString(triple.getObject());
119 1595 tao
120 1619 tao
        if (subject != null && relationship != null && object != null)
121
        {
122
123
          //put the new relation into xml_relation
124 2663 sgarg
          logMetacat.info("Insert into xml_relation table");
125 1619 tao
          tstmt.setString(1, docid);
126 2663 sgarg
          logMetacat.info("Insert docid into xml_relation table" +
127
                                docid);
128 1619 tao
          tstmt.setString(2, packagetype);
129
          tstmt.setString(3, subject);
130 2663 sgarg
          logMetacat.info("Insert subject into xml_relation table" +
131
                               subject);
132 1619 tao
          tstmt.setString(4, subDoctype);
133
          tstmt.setString(5, relationship);
134 2663 sgarg
          logMetacat.info("Insert relationship into xml_relation table" +
135
                                relationship);
136 1619 tao
          tstmt.setString(6, object);
137 2663 sgarg
          logMetacat.info("Insert object into xml_relation table" +
138
                                object);
139 1619 tao
          tstmt.setString(7, objDoctype);
140
          tstmt.execute();
141
        }//if
142 1577 tao
143 1592 tao
      }//for
144
    }//if
145 1577 tao
146
    if ( tstmt != null )
147
    {
148 819 bojilova
      tstmt.close();
149 523 berkley
    }
150 1577 tao
151
152 523 berkley
  }
153 1577 tao
154 634 berkley
  /**
155
   * Deletes all of the relations with a docid of 'docid'.
156 819 bojilova
   * @param docid the docid of the package which relations to delete.
157 634 berkley
   */
158 797 bojilova
  public void deleteRelations(String docid) throws SQLException
159 634 berkley
  {
160 797 bojilova
    try {
161 1217 tao
      PreparedStatement pstmt = connection.prepareStatement(
162 819 bojilova
                                "DELETE FROM xml_relation " +
163
                                "WHERE docid = '" + docid + "'");
164 1217 tao
      //increase usage count
165
      connection.increaseUsageCount(1);
166 634 berkley
      pstmt.execute();
167 645 bojilova
      pstmt.close();
168 797 bojilova
    } catch(SQLException e) {
169 2663 sgarg
      logMetacat.error("error in RelationHandler.deleteRelations(): " +
170
                          e.getMessage());
171 1499 tao
172 797 bojilova
      throw e;
173 634 berkley
    }
174
  }
175 819 bojilova
176
  /**
177 4080 daigle
	 * 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 819 bojilova
188 4080 daigle
		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 4698 daigle
			accessdoctypes = MetacatUtil.getOptionList(PropertyService
196 4213 daigle
				.getProperty("xml.accessdoctype"));
197 4080 daigle
		} 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 4212 daigle
					sep = PropertyService.getProperty("document.accNumSeparator");
223 4080 daigle
				} 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 523 berkley
}