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: 2008-08-05 17:33:45 -0700 (Tue, 05 Aug 2008) $'
8
 * '$Revision: 4212 $'
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.metacat.DBConnection;
32
import edu.ucsb.nceas.metacat.DBConnectionPool;
33
import edu.ucsb.nceas.metacat.service.PropertyService;
34

    
35
import junit.framework.Test;
36
import junit.framework.TestCase;
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 TestCase
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
	static
56
	  {
57
		  try
58
		  {
59
			  PropertyService.getInstance("build/tests");
60
		  }
61
		  catch(Exception e)
62
		  {
63
			  System.err.println("Exception in initialize option in MetacatServletNetTest "+e.getMessage());
64
		  }
65
	  }
66
	
67
	/**
68
     * Create a suite of tests to be run together
69
     */
70
    public static Test suite()
71
    {
72
        TestSuite suite = new TestSuite();
73
        suite.addTest(new QueryResultBuilderTest("initialize"));
74
        //suite.addTest(new QueryResultBuilderTest("testBuildRecords"));
75
        return suite;
76
    }
77
   
78
    /**
79
     * Constructor to build the test
80
     * 
81
     * @param name the name of the test method
82
     */
83
    public QueryResultBuilderTest(String name)
84
    {
85
        super(name);
86
    }
87
	
88
	/*
89
     * @see TestCase#setUp()
90
     */
91
    protected void setUp() throws Exception
92
    {
93
        super.setUp();
94
        try {
95
        	connPool = DBConnectionPool.getInstance();      
96
        } catch (Exception e) {
97
            fail(e.getMessage());
98
        }
99
    }
100
    
101
    /**
102
     * Run an initial test that always passes to check that the test
103
     * harness is working.
104
     */
105
    public void initialize()
106
    {
107
        assertTrue(1 == 1);
108
    }
109
    
110
    
111

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

    
267
}
(13-13/19)