Project

General

Profile

1
/**
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
 *    Release: @release@
8
 *
9
 *   '$Author: sgarg $'
10
 *     '$Date: 2005-11-17 13:27:21 -0800 (Thu, 17 Nov 2005) $'
11
 * '$Revision: 2759 $'
12
 *
13
 * This program is free software; you can redistribute it and/or modify
14
 * it under the terms of the GNU General Public License as published by
15
 * the Free Software Foundation; either version 2 of the License, or
16
 * (at your option) any later version.
17
 *
18
 * This program is distributed in the hope that it will be useful,
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
 * GNU General Public License for more details.
22
 *
23
 * You should have received a copy of the GNU General Public License
24
 * along with this program; if not, write to the Free Software
25
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26
 */
27

    
28
package edu.ucsb.nceas.metacat;
29

    
30
import java.sql.PreparedStatement;
31
import java.sql.ResultSet;
32
import java.sql.SQLException;
33
import java.util.TimerTask;
34
import java.util.Vector;
35
import java.util.Iterator;
36

    
37
import edu.ucsb.nceas.metacat.MetaCatUtil;
38
import org.apache.log4j.Logger;
39

    
40
public class IndexingTimerTask extends TimerTask{
41

    
42
	 private Logger logMetacat = Logger.getLogger(IndexingTimerTask.class);
43

    
44
	 int count = 0;
45
	 
46
	  /*
47
	     * Run a separate thread to build the XML index for this document.  This
48
	     * thread is run asynchronously in order to more quickly return control to
49
	     * the submitting user.  The run method checks to see if the document has
50
	     * been fully inserted before trying to update the xml_index table.
51
	     */
52
	    public void run()
53
	    {
54
	    	DBConnection dbConn = null;
55
	    	int serialNumber = 0;
56
	    	try{
57
	    		logMetacat.warn("Running indexing timer task");
58
	    		
59
	    		dbConn = DBConnectionPool.getDBConnection("IndexingThread");
60
	    		serialNumber = dbConn.getCheckOutSerialNumber();
61
	            Vector indexNamespaces = MetaCatUtil.getOptionList(MetaCatUtil.getOption("indexNamespaces"));
62
	            String nonJoinCriteria = "b.docid is NULL";
63
	    		boolean first = true;
64

    
65
	    		if(indexNamespaces != null && !indexNamespaces.isEmpty()){
66
	    			Iterator it = indexNamespaces.iterator();
67
	    			while(it.hasNext()){
68
	    				if(first){
69
	    					nonJoinCriteria = nonJoinCriteria + " AND (";
70
	    					nonJoinCriteria = nonJoinCriteria 
71
	    						+ " a.doctype like '" 
72
	    						+ it.next()
73
	    						+ "'";
74

    
75
	    					first = false;
76
	    				} else {
77
	    					nonJoinCriteria = nonJoinCriteria + " OR ";
78
	    					nonJoinCriteria = nonJoinCriteria 
79
	    						+ " a.doctype like '" 
80
	    						+ it.next()
81
	    						+ "'";
82
	    				}
83
	    			}
84
	    			nonJoinCriteria = nonJoinCriteria + ")";
85
	    		} else {
86
	    			// if no namespaces are defined in metacat.properties then return
87
	    			return;
88
	    		}
89
	    		
90
	    		String xmlDocumentsCheck = 
91
	    			MetaCatUtil.dbAdapter.getLeftJoinQuery("a.docid, a.rev", "xml_documents", 
92
	    					"xml_path_index", "a.docid = b.docid", nonJoinCriteria);
93

    
94
	    		PreparedStatement xmlDocCheck = dbConn.prepareStatement(xmlDocumentsCheck);
95
	    		
96
	    		// Increase usage count
97
	    		dbConn.increaseUsageCount(1);
98
	    		xmlDocCheck.execute();
99
	    		ResultSet rs = xmlDocCheck.getResultSet();
100
	    		
101
	    		boolean tableHasRows = rs.next();
102
	    		while (tableHasRows) {
103
	    			String docid = rs.getString(1);
104
	    			String rev = rs.getString(2);
105
	    			
106
	    			IndexingQueue.getInstance().add(docid, rev);
107

    
108
	    			tableHasRows = rs.next();
109
	            }
110
	                	
111
	    		rs.close();
112
	    		xmlDocCheck.close();
113
	    		
114
	       	} catch (SQLException se){
115
	       				se.printStackTrace();
116
		        		
117
		    } catch (Exception e){
118
	        		e.printStackTrace();
119
	        		
120
	        }finally {
121
	                DBConnectionPool.returnDBConnection(dbConn, serialNumber);
122
	        }
123
		      	
124
			logMetacat.warn("Indexing timer task returning");		
125
			count++;
126
	    }
127
}
(41-41/65)