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
 *
8
 *   '$Author: jones $'
9
 *     '$Date: 2006-11-10 10:25:38 -0800 (Fri, 10 Nov 2006) $'
10
 * '$Revision: 3077 $'
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
import java.util.Vector;
34
import java.util.Iterator;
35

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

    
39
public class IndexingTimerTask extends TimerTask{
40

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

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

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

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

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

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