Project

General

Profile

1
/**
2
 *  '$RCSfile$'
3
 *  Copyright: 2004 Regents of the University of California and the
4
 *             National Center for Ecological Analysis and Synthesis
5
 *
6
 *   '$Author: daigle $'
7
 *     '$Date: 2009-08-24 14:42:25 -0700 (Mon, 24 Aug 2009) $'
8
 * '$Revision: 5035 $'
9
 *
10
 * This program is free software; you can redistribute it and/or modify
11
 * it under the terms of the GNU General Public License as published by
12
 * the Free Software Foundation; either version 2 of the License, or
13
 * (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 * GNU General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU General Public License
21
 * along with this program; if not, write to the Free Software
22
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23
 */
24
package edu.ucsb.nceas.metacattest;
25

    
26
import java.sql.PreparedStatement;
27
import java.sql.ResultSet;
28
import java.util.Enumeration;
29
import java.util.Hashtable;
30

    
31
import edu.ucsb.nceas.MCTestCase;
32
import edu.ucsb.nceas.metacat.database.DBConnection;
33
import edu.ucsb.nceas.metacat.database.DBConnectionPool;
34
import edu.ucsb.nceas.metacat.properties.PropertyService;
35

    
36
import junit.framework.Test;
37
import junit.framework.TestSuite;
38

    
39

    
40
/**
41
 * @author jones
42
 *
43
 * Test class for getting idea how long it will take - to build 
44
 * records xml_queryresult table for a given return fields
45
 */
46
public class QueryResultBuilderTest extends MCTestCase
47
{
48
	/** DBConnection pool*/
49
	private DBConnectionPool connPool = null;
50
	/** Return field value in xml_returnfield table*/
51
	private String[] returnFieldString = {"dataset/title", "entityName", "individualName/surName", "keyword"};
52
	/** Return field id value for above return field string in xml_returnfield*/
53
	private int returnFieldId =11;
54
	
55
	/**
56
     * Create a suite of tests to be run together
57
     */
58
    public static Test suite()
59
    {
60
        TestSuite suite = new TestSuite();
61
        suite.addTest(new QueryResultBuilderTest("initialize"));
62
        //suite.addTest(new QueryResultBuilderTest("testBuildRecords"));
63
        return suite;
64
    }
65
   
66
    /**
67
     * Constructor to build the test
68
     * 
69
     * @param name the name of the test method
70
     */
71
    public QueryResultBuilderTest(String name)
72
    {
73
        super(name);
74
    }
75
	
76
	/*
77
     * @see TestCase#setUp()
78
     */
79
    protected void setUp() throws Exception
80
    {
81
        super.setUp();
82
        try {
83
        	connPool = DBConnectionPool.getInstance();      
84
        } catch (Exception e) {
85
            fail(e.getMessage());
86
        }
87
    }
88
    
89
    /**
90
     * Run an initial test that always passes to check that the test
91
     * harness is working.
92
     */
93
    public void initialize()
94
    {
95
        assertTrue(1 == 1);
96
    }
97
    
98
    
99

    
100
    /**
101
     * Tests how long it will take to build records in xml_queryresult table
102
     */
103
    public void testBuildRecords() throws Exception
104
    {
105
        double start = System.currentTimeMillis()/1000;
106
        DBConnection dbconn = DBConnectionPool.getDBConnection("DBQuery.findDocuments");
107
        int serialNumber = dbconn.getCheckOutSerialNumber();
108
        String sql = "SELECT docid, nodedata, path FROM xml_path_index WHERE path IN (";
109
        PreparedStatement pstmt = null;
110
        ResultSet result = null;
111
        //Gets the document information such as doctype, docname and et al
112
        Hashtable docsInformation = getDocumentsInfo();
113
        //The key of the hash is docid, the element is the return field value (in xml_format)
114
        Hashtable table = new Hashtable();
115
        int size = returnFieldString.length;
116
        // Constructs the sql base on the return fields
117
        boolean first = true;
118
        for (int i=0; i< size; i++)
119
        {
120
        	if (!first)
121
        	{
122
        		sql=sql+",";
123
        	}
124
        	sql=sql+"'";
125
        	String path = returnFieldString[i];
126
        	 sql = sql+path;
127
        	 sql=sql+"'";
128
        	 first = false;
129
        }
130
        sql = sql+")";
131
        System.out.println("The final sql is "+sql);
132
    	pstmt = dbconn.prepareStatement(sql);
133
        pstmt.execute();
134
        result = pstmt.getResultSet();
135
        boolean hasNext = result.next();
136
        // Gets returning value for docids and puts them into hashtable
137
        while (hasNext)
138
        {
139
        	String docid = result.getString(1);
140
        	String value = result.getString(2);
141
        	String path  = result.getString(3);
142
        	// Gets document information from document information hash table.
143
        	String docInfo = (String)docsInformation.get(docid);
144
        	StringBuffer buffer  = new StringBuffer();
145
        	if (docInfo != null)
146
        	{
147
        		buffer.append(docInfo);
148
        	}
149
        	buffer.append("<param name=\"");
150
            buffer.append(path);
151
            buffer.append("\">");
152
            buffer.append(value);
153
            buffer.append("</param>");
154
            String xmlValue = buffer.toString();
155
            //If the hashtable already has key for this docid,
156
            //we should append obove value to the old value
157
        	if (table.containsKey(docid))
158
        	{
159
        		String oldValue = (String)table.get(docid);
160
        		String newValue = oldValue+xmlValue;
161
        		table.put(docid, newValue);
162
        	}
163
        	else
164
        	{
165
        		table.put(docid, xmlValue);
166
        	}
167
        	hasNext = result.next();
168
        }
169
        result.close();
170
        pstmt.close();
171
        // Insert the hashtable value into xml_queryresult table
172
        Enumeration docids = table.keys();
173
        while (docids.hasMoreElements())
174
        {
175
        	String docId = (String)docids.nextElement();
176
        	String returnString = (String)table.get(docId);
177
        	String query = "INSERT INTO xml_queryresult (returnfield_id, docid, "
178
                + "queryresult_string) VALUES (?, ?, ?)";
179
         
180
            pstmt = dbconn.prepareStatement(query);
181
            pstmt.setInt(1,returnFieldId );
182
            pstmt.setString(2,docId);
183
            pstmt.setString(3, returnString);
184
           	pstmt.execute();     
185
            pstmt.close();
186
           
187
        }
188
        
189
        double end = System.currentTimeMillis()/1000;
190
        System.out.println("The time to build xml_queryresult is "+(end-start));
191
        DBConnectionPool.returnDBConnection(dbconn, serialNumber);
192
        assertTrue(1 == 1);
193
    }
194
    
195
    /*
196
     * Gets a Hashtable which contains documents' information. 
197
     * The key of this Hashtable is docid. The element value is the information, like
198
     * docid, docname,doctype and et al. This method will be used in testBuildRecords 
199
     * method to combine a completed return information for query.
200
     */
201
    private Hashtable getDocumentsInfo() throws Exception
202
    {
203
    	DBConnection dbconn = DBConnectionPool.getDBConnection("DBQuery.findDocuments");
204
        int serialNumber = dbconn.getCheckOutSerialNumber();
205
        String sql = "SELECT docid, rev, docname, doctype, date_created,date_updated from xml_documents";
206
        PreparedStatement pstmt = null;
207
        ResultSet result = null;
208
        //The key of the hash is docid, the element is the document information (in xml_format)
209
        Hashtable table = new Hashtable();
210
        pstmt = dbconn.prepareStatement(sql);
211
        pstmt.execute();
212
        result = pstmt.getResultSet();
213
        boolean hasNext = result.next();
214
        // Get  values from the xml_document table
215
        while (hasNext)
216
        {
217
        	String             docid = result.getString(1);
218
        	int                      rev = result.getInt(2);
219
        	String        docname = result.getString(3);
220
        	String         doctype = result.getString(4);
221
        	String   createDate = result.getString(5);
222
        	String updateDate = result.getString(6);
223
        	String completeDocid = docid
224
                             + PropertyService.getProperty("document.accNumSeparator");
225
            completeDocid += rev;
226
            // Put everything into a string buffer
227
            StringBuffer document = new StringBuffer();
228
            document.append("<docid>").append(completeDocid).append("</docid>");
229
            if (docname != null)
230
            {
231
                document.append("<docname>" + docname + "</docname>");
232
            }
233
            if (doctype != null)
234
            {
235
               document.append("<doctype>" + doctype + "</doctype>");
236
            }
237
            if (createDate != null)
238
            {
239
                document.append("<createdate>" + createDate + "</createdate>");
240
            }
241
            if (updateDate != null)
242
            {
243
              document.append("<updatedate>" + updateDate + "</updatedate>");
244
            }
245
            String information = document.toString();
246
            // Put the docid and info into Hashtable
247
            table.put(docid, information);
248
            hasNext = result.next();
249
        }
250
        result.close();
251
        pstmt.close();
252
        return table;
253
    }
254

    
255
}
(21-21/30)