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: daigle $'
9
 *     '$Date: 2008-08-29 10:20:19 -0700 (Fri, 29 Aug 2008) $'
10
 * '$Revision: 4335 $'
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.service.DatabaseService;
37
import edu.ucsb.nceas.metacat.service.PropertyService;
38
import edu.ucsb.nceas.metacat.util.MetaCatUtil;
39

    
40
import org.apache.log4j.Logger;
41

    
42
public class IndexingTimerTask extends TimerTask{
43

    
44
	 private Logger logMetacat = Logger.getLogger(IndexingTimerTask.class);
45

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

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

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

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

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