Project

General

Profile

1 2732 sgarg
/**
2
 *  '$RCSfile$'
3
 *    Purpose: A Class that tracks sessions for MetaCatServlet users.
4
 *  Copyright: 2000 Regents of the University of California and the
5
 *             National Center for Ecological Analysis and Synthesis
6
 *    Authors: Matt Jones
7
 *
8
 *   '$Author$'
9
 *     '$Date$'
10
 * '$Revision$'
11
 *
12
 * This program is free software; you can redistribute it and/or modify
13
 * it under the terms of the GNU General Public License as published by
14
 * the Free Software Foundation; either version 2 of the License, or
15
 * (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 * GNU General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU General Public License
23
 * along with this program; if not, write to the Free Software
24
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
25
 */
26
27
package edu.ucsb.nceas.metacat;
28
29
import java.sql.PreparedStatement;
30
import java.sql.ResultSet;
31
import java.sql.SQLException;
32
import java.util.TimerTask;
33 2758 sgarg
import java.util.Vector;
34
import java.util.Iterator;
35 2732 sgarg
36 4080 daigle
import edu.ucsb.nceas.metacat.service.PropertyService;
37
import edu.ucsb.nceas.metacat.util.MetaCatUtil;
38
39 2732 sgarg
import org.apache.log4j.Logger;
40
41
public class IndexingTimerTask extends TimerTask{
42
43
	 private Logger logMetacat = Logger.getLogger(IndexingTimerTask.class);
44
45
	 int count = 0;
46
47
	  /*
48
	     * Run a separate thread to build the XML index for this document.  This
49
	     * thread is run asynchronously in order to more quickly return control to
50
	     * the submitting user.  The run method checks to see if the document has
51
	     * been fully inserted before trying to update the xml_index table.
52
	     */
53
	    public void run()
54
	    {
55
	    	DBConnection dbConn = null;
56
	    	int serialNumber = 0;
57
	    	try{
58
	    		logMetacat.warn("Running indexing timer task");
59
60 2759 sgarg
	    		dbConn = DBConnectionPool.getDBConnection("IndexingThread");
61 2732 sgarg
	    		serialNumber = dbConn.getCheckOutSerialNumber();
62 4080 daigle
	            Vector indexNamespaces = MetaCatUtil.getOptionList(PropertyService.getProperty("indexNamespaces"));
63 2758 sgarg
	            String nonJoinCriteria = "b.docid is NULL";
64
	    		boolean first = true;
65
66
	    		if(indexNamespaces != null && !indexNamespaces.isEmpty()){
67
	    			Iterator it = indexNamespaces.iterator();
68
	    			while(it.hasNext()){
69
	    				if(first){
70
	    					nonJoinCriteria = nonJoinCriteria + " AND (";
71
	    					nonJoinCriteria = nonJoinCriteria
72
	    						+ " a.doctype like '"
73
	    						+ it.next()
74
	    						+ "'";
75
76
	    					first = false;
77
	    				} else {
78
	    					nonJoinCriteria = nonJoinCriteria + " OR ";
79
	    					nonJoinCriteria = nonJoinCriteria
80
	    						+ " a.doctype like '"
81
	    						+ it.next()
82
	    						+ "'";
83
	    				}
84
	    			}
85
	    			nonJoinCriteria = nonJoinCriteria + ")";
86
	    		} else {
87
	    			// if no namespaces are defined in metacat.properties then return
88
	    			return;
89
	    		}
90
91 2732 sgarg
	    		String xmlDocumentsCheck =
92
	    			MetaCatUtil.dbAdapter.getLeftJoinQuery("a.docid, a.rev", "xml_documents",
93 2780 sgarg
	    					"xml_index", "a.docid = b.docid", nonJoinCriteria);
94 2758 sgarg
95
	    		PreparedStatement xmlDocCheck = dbConn.prepareStatement(xmlDocumentsCheck);
96 2732 sgarg
97
	    		// Increase usage count
98
	    		dbConn.increaseUsageCount(1);
99
	    		xmlDocCheck.execute();
100
	    		ResultSet rs = xmlDocCheck.getResultSet();
101
102
	    		boolean tableHasRows = rs.next();
103
	    		while (tableHasRows) {
104
	    			String docid = rs.getString(1);
105
	    			String rev = rs.getString(2);
106
107 2734 sgarg
	    			IndexingQueue.getInstance().add(docid, rev);
108 2732 sgarg
109
	    			tableHasRows = rs.next();
110
	            }
111
112
	    		rs.close();
113
	    		xmlDocCheck.close();
114
115
	       	} catch (SQLException se){
116
	       				se.printStackTrace();
117
118
		    } catch (Exception e){
119
	        		e.printStackTrace();
120
121
	        }finally {
122
	                DBConnectionPool.returnDBConnection(dbConn, serialNumber);
123
	        }
124
125
			logMetacat.warn("Indexing timer task returning");
126
			count++;
127
	    }
128
}