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: tao $'
7
 *     '$Date: 2016-01-13 13:05:11 -0800 (Wed, 13 Jan 2016) $'
8
 * '$Revision: 9489 $'
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.metacat;
25

    
26
import java.sql.PreparedStatement;
27
import java.sql.ResultSet;
28
import java.sql.SQLException;
29
import java.sql.Timestamp;
30
import java.util.ArrayList;
31
import java.util.Date;
32
import java.util.HashMap;
33
import java.util.List;
34
import java.util.Map;
35
import java.util.Vector;
36

    
37
import org.apache.log4j.Logger;
38
import org.dataone.service.types.v1.Identifier;
39
import org.dataone.service.types.v2.Log;
40
import org.dataone.service.types.v2.LogEntry;
41
import org.dataone.service.types.v1.Event;
42
import org.dataone.service.types.v1.NodeReference;
43
import org.dataone.service.types.v1.Subject;
44
import org.dataone.service.util.DateTimeMarshaller;
45

    
46
import edu.ucsb.nceas.metacat.database.DBConnection;
47
import edu.ucsb.nceas.metacat.database.DBConnectionPool;
48
import edu.ucsb.nceas.metacat.database.DatabaseService;
49
import edu.ucsb.nceas.metacat.index.MetacatSolrIndex;
50
import edu.ucsb.nceas.metacat.properties.PropertyService;
51
import edu.ucsb.nceas.metacat.util.DocumentUtil;
52
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
53

    
54
/**
55
 * EventLog is used to intialize and store a log of events that occur in an
56
 * application. The events are registered with the logger as they occur, but
57
 * EventLog writes them to permenant storage when it is most convenient or
58
 * efficient. EventLog is a Singleton as there should always be only one object
59
 * for these logging events.
60
 * 
61
 * TODO: Logging to the database needn't be synchronous with the event.  
62
 * Instead, a separate thread can be launched that periodically sleeps and only
63
 * wakes periodically to see if metacat is idle.  The log event can be cached
64
 * and inserted later when the thread wakes and finds metacat idle.
65
 * 
66
 * TODO: Write a function that archives a part of the log table to an 
67
 * external text file so that the log table doesn't get to big.  This 
68
 * function should be able to be called manually or on a schedule. 
69
 * 
70
 * TODO: Write an access function that returns an XML report for a
71
 * specific subset of events.  Users should be able to query on
72
 * principal, docid/rev, date, event, and possibly other fields.
73
 * 
74
 * @author jones
75
 */
76
public class EventLog
77
{
78
    public static final String DELETE = "delete";
79
    /**
80
     * The single instance of the event log that is always returned.
81
     */
82
    private static EventLog self = null;
83
    private Logger logMetacat = Logger.getLogger(EventLog.class);
84
    private static final int USERAGENTLENGTH = 512;
85

    
86

    
87
    /**
88
     * A private constructor that initializes the class when getInstance() is
89
     * called.
90
     */
91
    private EventLog()
92
    {
93
    }
94

    
95
    /**
96
     * Return the single instance of the event log after initializing it if it
97
     * wasn't previously initialized.
98
     * 
99
     * @return the single EventLog instance
100
     */
101
    public static EventLog getInstance()
102
    {
103
        if (self == null) {
104
            self = new EventLog();
105
        }
106
        return self;
107
    }
108

    
109
    /**
110
     * Log an event of interest to the application. The information logged can
111
     * include basic identification information about the principal or computer
112
     * that initiated the event.
113
     * 
114
     * @param ipAddress the internet protocol address for the event
115
     * @param userAgent the agent making the request
116
	 * @param principal the principal for the event (a username, etc)
117
	 * @param docid the identifier of the document to which the event applies
118
	 * @param event the string code for the event
119
     */
120
    public void log(String ipAddress, String userAgent, String principal, String docid, String event) {
121
        EventLogData logData = new EventLogData(ipAddress, userAgent, principal, docid, event);
122
        insertLogEntry(logData);
123
        
124
        // update the event information in the index
125
        try {
126
	        String localId = DocumentUtil.getSmartDocId(docid);
127
			int rev = DocumentUtil.getRevisionFromAccessionNumber(docid);
128
			
129
	        String guid = IdentifierManager.getInstance().getGUID(localId, rev);
130
	        Identifier pid = new Identifier();
131
	        pid.setValue(guid);
132
	        
133
	        // submit for indexing
134
	        MetacatSolrIndex.getInstance().submit(pid, null, this.getIndexFields(pid, event), false);
135
	        
136
        } catch (Exception e) {
137
        	logMetacat.error("Could not update event index information", e);
138
        }
139
    }
140
    
141
    public Map<String, List<Object>> getIndexFields(Identifier pid, String event) {
142
    	// update the search index for the event
143
        try {
144
        	
145
        	if (event != null) {
146
        		
147
	        	String fieldName = event + "_count_i";
148
	        	int eventCount = 0;
149
	        	
150
	        	String docid = IdentifierManager.getInstance().getLocalId(pid.getValue());
151
	        	Log eventLog = this.getD1Report(null, null, new String[] {docid}, event, null, null, false, 0, 0);
152
	        	eventCount = eventLog.getTotal();
153
	
154
		        List<Object> values = new ArrayList<Object>();
155
				values.add(eventCount);
156
		        Map<String, List<Object>> fields = new HashMap<String, List<Object>>();
157
		        fields.put(fieldName, values);
158
		        
159
		        return fields;
160
        	}
161
	        
162
        } catch (Exception e) {
163
        	logMetacat.error("Could not update event index information on pid: " + pid.getValue() + " for event: " + event, e);
164
        }
165
        // default if we can't find the event information
166
    	return null;
167

    
168
    }
169
    
170
    /**
171
     * Insert a single log event record to the database.
172
     * 
173
     * @param logData the data to be logged when an event occurs
174
     */
175
    private void insertLogEntry(EventLogData logData)
176
    {
177
        String insertString = "insert into access_log"
178
                + "(ip_address, user_agent, principal, docid, "
179
                + "event, date_logged) "
180
                + "values ( ?, ?, ?, ?, ?, ? )";
181
 
182
        DBConnection dbConn = null;
183
        int serialNumber = -1;
184
        try {
185
            // Get a database connection from the pool
186
            dbConn = DBConnectionPool.getDBConnection("EventLog.insertLogEntry");
187
            serialNumber = dbConn.getCheckOutSerialNumber();
188
            String userAgent = logData.getUserAgent();
189
            if(userAgent != null && userAgent.length() > USERAGENTLENGTH) {
190
                userAgent = userAgent.substring(0, USERAGENTLENGTH);
191
            }
192
            
193
            // Execute the insert statement
194
            PreparedStatement stmt = dbConn.prepareStatement(insertString);
195
            
196
            stmt.setString(1, logData.getIpAddress());
197
            stmt.setString(2, userAgent);
198
            stmt.setString(3, logData.getPrincipal());
199
            stmt.setString(4, logData.getDocid());
200
            stmt.setString(5, logData.getEvent());
201
            stmt.setTimestamp(6, new Timestamp(new Date().getTime()));
202
            stmt.executeUpdate();
203
            stmt.close();
204
        } catch (SQLException e) {
205
        	logMetacat.error("Error while logging event to database: " 
206
                    + e.getMessage());
207
        } finally {
208
            // Return database connection to the pool
209
            DBConnectionPool.returnDBConnection(dbConn, serialNumber);
210
        }
211
    }
212
    
213
    /**
214
     * Get a report of the log events that match a set of filters.  The
215
     * filter parameters can be null; log records are subset based on
216
     * non-null filter parameters.
217
     * 
218
     * @param ipAddress the internet protocol address for the event
219
	 * @param principal the principal for the event (a username, etc)
220
	 * @param docid the identifier of the document to which the event applies
221
	 * @param event the string code for the event
222
	 * @param startDate beginning of date range for query
223
	 * @param endDate end of date range for query
224
	 * @return an XML-formatted report of the access log entries
225
     */
226
    public String getReport(String[] ipAddress, String[] principal, String[] docid,
227
            String[] event, Timestamp startDate, Timestamp endDate, boolean anonymous)
228
    {
229
        StringBuffer resultDoc = new StringBuffer();
230
        StringBuffer query = new StringBuffer();
231
        query.append("select entryid, ip_address, user_agent, principal, docid, "
232
            + "event, date_logged from access_log");
233
//                        + ""
234
//                        + "event, date_logged) " + "values (" + "'"
235
//                        + logData.getIpAddress() + "', " + "'"
236
//                        + logData.getPrincipal() + "', " + "'"
237
//                        + logData.getDocid() + "', " + "'" + logData.getEvent()
238
//                        + "', " + " ? " + ")";
239
        if (ipAddress != null || principal != null || docid != null
240
                        || event != null || startDate != null || endDate != null) {
241
            query.append(" where ");
242
        }
243
        boolean clauseAdded = false;
244
        int startIndex = 0;
245
        int endIndex = 0;
246
        
247
        List<String> paramValues = new ArrayList<String>();
248
        if (ipAddress != null) {
249
        	query.append("ip_address in (");
250
        	for (int i = 0; i < ipAddress.length; i++) {
251
        		if (i > 0) {
252
            		query.append(", ");
253
        		}
254
        		query.append("?");
255
        		paramValues.add(ipAddress[i]);
256
        	}
257
        	query.append(") ");
258
            clauseAdded = true;
259
        }
260
        if (principal != null) {
261
        	if (clauseAdded) {
262
                query.append(" and ");
263
            }
264
        	query.append("principal in (");
265
        	for (int i = 0; i < principal.length; i++) {
266
        		if (i > 0) {
267
            		query.append(", ");
268
        		}
269
        		query.append("?");
270
        		paramValues.add(principal[i]);
271
        	}
272
        	query.append(") ");
273
            clauseAdded = true;
274
        }
275
        if (docid != null) {
276
        	if (clauseAdded) {
277
                query.append(" and ");
278
            }
279
        	query.append("docid in (");
280
        	for (int i = 0; i < docid.length; i++) {
281
        		if (i > 0) {
282
            		query.append(", ");
283
        		}
284
        		query.append("?");
285
        		String fullDocid = docid[i];
286
        		// allow docid without revision - look up latest version
287
        		try {
288
        			fullDocid = DocumentUtil.appendRev(fullDocid);
289
        		} catch (Exception e) {
290
					// just warn about this
291
        			logMetacat.warn("Could not check docid for revision: " + fullDocid, e);
292
				}
293
        		paramValues.add(fullDocid);
294
        	}
295
        	query.append(") ");
296
            clauseAdded = true;
297
        }
298
        if (event != null) {
299
        	if (clauseAdded) {
300
                query.append(" and ");
301
            }
302
        	query.append("event in (");
303
        	for (int i = 0; i < event.length; i++) {
304
        		if (i > 0) {
305
            		query.append(", ");
306
        		}
307
        		query.append("?");
308
        		paramValues.add(event[i]);
309
        	}
310
        	query.append(") ");
311
            clauseAdded = true;
312
        }
313
        if (startDate != null) {
314
            if (clauseAdded) {
315
                query.append(" and ");
316
            }
317
            query.append("date_logged >= ?");
318
            clauseAdded = true;
319
            startIndex++;
320
        }
321
        if (endDate != null) {
322
            if (clauseAdded) {
323
                query.append(" and ");
324
            }
325
            query.append("date_logged < ?");
326
            clauseAdded = true;
327
            endIndex = startIndex + 1;
328
        }
329
        DBConnection dbConn = null;
330
        int serialNumber = -1;
331
        try {
332
            // Get a database connection from the pool
333
            dbConn = DBConnectionPool.getDBConnection("EventLog.getReport");
334
            serialNumber = dbConn.getCheckOutSerialNumber();
335

    
336
            // Execute the query statement
337
            PreparedStatement stmt = dbConn.prepareStatement(query.toString());
338
            //set the param values
339
            int parameterIndex = 1;
340
            for (String val: paramValues) {
341
            	stmt.setString(parameterIndex++, val);
342
            }
343
            if (startDate != null) {
344
                stmt.setTimestamp(parameterIndex++, startDate); 
345
            }
346
            if (endDate != null) {
347
            	stmt.setTimestamp(parameterIndex++, endDate);
348
            }
349
            stmt.execute();
350
            ResultSet rs = stmt.getResultSet();
351
            //process the result and return it as an XML document
352
            resultDoc.append("<?xml version=\"1.0\"?>\n");
353
            resultDoc.append("<log>\n");
354
            while (rs.next()) {
355
                resultDoc.append(
356
                		generateXmlRecord(
357
                				rs.getString(1), //id
358
                				anonymous ? "" : rs.getString(2), //ip
359
                				rs.getString(3), //userAgent	
360
                				anonymous ? "" : rs.getString(4), //principal
361
                                rs.getString(5), 
362
                                rs.getString(6), 
363
                                rs.getTimestamp(7)));
364
            }
365
            resultDoc.append("</log>");
366
            stmt.close();
367
        } catch (SQLException e) {
368
        	logMetacat.info("Error while logging event to database: "
369
                            + e.getMessage());
370
        } finally {
371
            // Return database connection to the pool
372
            DBConnectionPool.returnDBConnection(dbConn, serialNumber);
373
        }
374
        return resultDoc.toString();
375
    }
376
    
377
    /**
378
     * A utility method to determine if the given docid was deleted. 
379
     * @param docid the specified docid
380
     * @return true if there is a delete event for the id; false otherwise.
381
     */
382
    public boolean isDeleted(String docid) {
383
        boolean deleted =false;
384
        if(docid != null || !docid.trim().equals("")) {
385
            String[] docids = new String[1];
386
            docids[0] = docid;
387
            String[] events = new String[1];
388
            events[0]= DELETE;
389
            String[] ipAddress = null;
390
            String[] principal = null;
391
            Timestamp startDate = null;
392
            Timestamp endDate = null;
393
            boolean anonymous = false;
394
            
395
            String report =getReport(ipAddress, principal, docids,
396
                     events, startDate, endDate, anonymous);
397
            //System.out.println("the report is "+report);
398
            if(report != null && report.contains("<event>"+DELETE+"</event>") ){
399
                deleted = true;
400
            }
401
        }
402
        return deleted;
403
    }
404
    
405
    public Log getD1Report(String[] ipAddress, String[] principal, String[] docid,
406
            String event, Timestamp startDate, Timestamp endDate, boolean anonymous, Integer start, Integer count)
407
    {
408
        boolean isCreateEvent = false;
409
        Log log = new Log();
410
    	
411
    	NodeReference memberNode = new NodeReference();
412
        String nodeId = "localhost";
413
        try {
414
            nodeId = PropertyService.getProperty("dataone.nodeId");
415
        } catch (PropertyNotFoundException e1) {
416
            // TODO Auto-generated catch block
417
            e1.printStackTrace();
418
        }
419
        memberNode.setValue(nodeId);
420
        
421
        // subquery does the heavy lifting
422
        StringBuffer subQueryFrom = new StringBuffer();
423
        subQueryFrom.append("from access_log ");
424
        
425
        boolean clauseAdded = false;
426
        
427
        List<String> paramValues = new ArrayList<String>();
428
        if (ipAddress != null) {
429
        	if (clauseAdded) {
430
        		subQueryFrom.append(" and ");
431
            } else {
432
            	subQueryFrom.append(" where ");
433
            }
434
        	subQueryFrom.append("ip_address in (");
435
        	for (int i = 0; i < ipAddress.length; i++) {
436
        		if (i > 0) {
437
        			subQueryFrom.append(", ");
438
        		}
439
        		subQueryFrom.append("?");
440
        		paramValues.add(ipAddress[i]);
441
        	}
442
        	subQueryFrom.append(") ");
443
            clauseAdded = true;
444
        }
445
        if (principal != null) {
446
        	if (clauseAdded) {
447
        		subQueryFrom.append(" and ");
448
            } else {
449
            	subQueryFrom.append(" where ");
450
            }
451
        	subQueryFrom.append("principal in (");
452
        	for (int i = 0; i < principal.length; i++) {
453
        		if (i > 0) {
454
        			subQueryFrom.append(", ");
455
        		}
456
        		subQueryFrom.append("?");
457
        		paramValues.add(principal[i]);
458
        	}
459
        	subQueryFrom.append(") ");
460
            clauseAdded = true;
461
        }
462
        if (docid != null) {
463
        	if (clauseAdded) {
464
        		subQueryFrom.append(" and ");
465
            } else {
466
            	subQueryFrom.append(" where ");
467
            }
468
        	subQueryFrom.append("docid in (");
469
        	for (int i = 0; i < docid.length; i++) {
470
        		if (i > 0) {
471
        			subQueryFrom.append(", ");
472
        		}
473
        		subQueryFrom.append("?");
474
        		paramValues.add(docid[i]);
475
        	}
476
        	subQueryFrom.append(") ");
477
            clauseAdded = true;
478
        }
479
        
480
       //please make sure the handling of event is just before the the startDate clause!!!
481
        if (event != null) {
482
            if (clauseAdded) {
483
                subQueryFrom.append(" and ");
484
            } else {
485
                subQueryFrom.append(" where ");
486
            }
487
            subQueryFrom.append("event in (");
488
            subQueryFrom.append("?");
489
            String eventString = event;
490
            if (eventString.equals(Event.CREATE.xmlValue())) {
491
                isCreateEvent = true; //since the create event maps create, insert and et al, we handle it in different way
492
                subQueryFrom.append(",?");// for INSERT, the insert is handled by line 508
493
                subQueryFrom.append(",?");// for upload
494
                subQueryFrom.append(",?");// for UPLOAD
495
                subQueryFrom.append(",?");// for create
496
                //eventString = "insert,INSERT,upload,UPLOAD,create";
497
            } else {
498
                paramValues.add(eventString);
499
            }
500
            subQueryFrom.append(") ");
501
            clauseAdded = true;
502
        }
503

    
504
        if (startDate != null) {
505
            if (clauseAdded) {
506
            	subQueryFrom.append(" and ");
507
            } else {
508
            	subQueryFrom.append(" where ");
509
            }
510
            subQueryFrom.append("date_logged >= ?");
511
            clauseAdded = true;
512
        }
513
        if (endDate != null) {
514
            if (clauseAdded) {
515
            	subQueryFrom.append(" and ");
516
            } else {
517
            	subQueryFrom.append(" where ");
518
            }
519
            subQueryFrom.append("date_logged < ?");
520
            clauseAdded = true;
521
        }
522
        
523
        
524

    
525
        // count query
526
        String countSelect = "select count(*) ";
527
        
528
        // subquery select
529
        String subquerySelect = "select entryid ";
530
        
531
        // for selecting fields we want in the join
532
        String fieldSelect = 
533
        		"select " +
534
        		"entryid, " +
535
        		"id.guid as identifier, " +
536
        		"ip_address, " +
537
        		"user_agent, " +
538
        		"principal, " +
539
        		"case " +
540
        		"	when event = 'insert' then 'create' " +
541
        		" when event = 'INSERT' then 'create' " +
542
        		" when event = 'upload' then 'create' " +
543
        		" when event = 'UPLOAD' then 'create' " +
544
        		"	else event " +
545
        		"end as event, " +
546
        		"date_logged " +	 
547
        		"from access_log al, identifier id " +
548
        		"where al.docid = id.docid||'.'||id.rev " +
549
        		"and al.entryid in ";
550
        
551
        // order by
552
        String orderByClause = " order by entryid ";
553
        
554
        // select the count
555
        String countQuery = countSelect + subQueryFrom.toString();
556
        logMetacat.debug("The count query is " + countQuery);
557
		// select the fields using paged subquery and fields join query
558
        String pagedSubquery = DatabaseService.getInstance().getDBAdapter().getPagedQuery(subquerySelect + subQueryFrom.toString() + orderByClause, start, count);
559
        String pagedQuery = fieldSelect + " ( " + pagedSubquery + " ) " + orderByClause; 
560
        logMetacat.debug("The selection query is " + pagedQuery);
561
        logMetacat.debug("The startDate in the query is " + startDate);
562
        logMetacat.debug("The endDate in the query is " + startDate);
563

    
564
        DBConnection dbConn = null;
565
        int serialNumber = -1;
566
        try {
567
            // Get a database connection from the pool
568
            dbConn = DBConnectionPool.getDBConnection("EventLog.getD1Report");
569
            serialNumber = dbConn.getCheckOutSerialNumber();
570

    
571
            // Execute the query statement
572
            PreparedStatement fieldsStmt = dbConn.prepareStatement(pagedQuery);
573
            PreparedStatement countStmt = dbConn.prepareStatement(countQuery);
574

    
575
            //set the param values
576
            int parameterIndex = 1;
577
            for (String val: paramValues) {
578
            	countStmt.setString(parameterIndex, val);
579
            	fieldsStmt.setString(parameterIndex, val);
580
            	parameterIndex++;
581
            }
582
            
583
            if(isCreateEvent) {
584
                //handle the event mapping. If we add another event mapping, we need add a "?" in line 496
585
                //those values are not in the paramValues.
586
                countStmt.setString(parameterIndex, "insert");
587
                fieldsStmt.setString(parameterIndex, "insert");
588
                parameterIndex++;
589
                countStmt.setString(parameterIndex, "INSERT");
590
                fieldsStmt.setString(parameterIndex, "INSERT");
591
                parameterIndex++;
592
                countStmt.setString(parameterIndex, "upload");
593
                fieldsStmt.setString(parameterIndex, "upload");
594
                parameterIndex++;
595
                countStmt.setString(parameterIndex, "UPLOAD");
596
                fieldsStmt.setString(parameterIndex, "UPLOAD");
597
                parameterIndex++;
598
                countStmt.setString(parameterIndex, "create");
599
                fieldsStmt.setString(parameterIndex, "create");
600
                parameterIndex++;
601
            }
602
            
603
            if (startDate != null) {
604
            	countStmt.setTimestamp(parameterIndex, startDate); 
605
                fieldsStmt.setTimestamp(parameterIndex, startDate); 
606
            	parameterIndex++;
607
            }
608
            if (endDate != null) {
609
            	countStmt.setTimestamp(parameterIndex, endDate);
610
            	fieldsStmt.setTimestamp(parameterIndex, endDate);
611
            	parameterIndex++;
612
            }
613
            
614

    
615
            // for the return Log list
616
            List<LogEntry> logs = new Vector<LogEntry>();
617

    
618
            // get the fields form the query
619
            if (count != 0) {
620
                long startTime = System.currentTimeMillis();
621
                logMetacat.debug("Time to start to execute the selection query "+startTime);
622
	            fieldsStmt.execute();
623
	            long endTime = System.currentTimeMillis();
624
	            logMetacat.debug("Time to run the selection query is "+(endTime-startTime)/1000+" seconds.");
625
	            ResultSet rs = fieldsStmt.getResultSet();
626
	            //process the result and return it            
627
	            while (rs.next()) {
628
	            	LogEntry logEntry = new LogEntry();
629
	            	logEntry.setEntryId(rs.getString(1));
630
	            	
631
	            	Identifier identifier = new Identifier();
632
	            	identifier.setValue(rs.getString(2));
633
					logEntry.setIdentifier(identifier);
634
	
635
	            	logEntry.setIpAddress(anonymous ? "N/A" : rs.getString(3));
636
	            	String userAgent = "N/A";
637
	            	if (rs.getString(4) != null) {
638
	            		userAgent = rs.getString(4);
639
	            	}
640
	            	logEntry.setUserAgent(userAgent);
641
	            	
642
	            	Subject subject = new Subject();
643
	            	subject.setValue(anonymous ? "N/A" : rs.getString(5));
644
					logEntry.setSubject(subject);
645
					
646
					String logEventString = rs.getString(6);
647
					if(logEventString == null) {
648
					    logEventString = "unknown";
649
					}
650
					logEntry.setEvent(logEventString);
651
					logEntry.setDateLogged(rs.getTimestamp(7));
652
					
653
					logEntry.setNodeIdentifier(memberNode);
654
					logs.add(logEntry);
655
	            }
656
	            fieldsStmt.close();
657
	            long endTime2 = System.currentTimeMillis();
658
	            logMetacat.debug("Time to put the query result to the log is "+(endTime2-endTime)/1000+" seconds.");
659
            }
660
            
661
            // set what we have
662
            log.setLogEntryList(logs);
663
		    log.setStart(start);
664
		    log.setCount(logs.size());
665
            			
666
			// get total for out query
667
		    int total = 0;
668
		    long startTime = System.currentTimeMillis();
669
            logMetacat.debug("Time to start to execute the counting query "+startTime);
670
            countStmt.execute();
671
            long endTime = System.currentTimeMillis();
672
            logMetacat.debug("Time to run the counting query is "+(endTime-startTime)/1000+" seconds.");
673
            ResultSet countRs = countStmt.getResultSet();
674
            if (countRs.next()) {
675
            	total = countRs.getInt(1);
676
            }
677
            countStmt.close();
678
		    log.setTotal(total);
679

    
680
        } catch (SQLException e) {
681
        	logMetacat.error("Error while getting log events: " + e.getMessage(), e);
682
        } finally {
683
            // Return database connection to the pool
684
            DBConnectionPool.returnDBConnection(dbConn, serialNumber);
685
        }
686
        return log;
687
    }
688
    
689
    /**
690
     * Format each returned log record as an XML structure.
691
     * 
692
     * @param entryId the identifier of the log entry
693
     * @param ipAddress the internet protocol address for the event
694
     * @param the agent making the request
695
	 * @param principal the principal for the event (a username, etc)
696
	 * @param docid the identifier of the document to which the event applies
697
	 * @param event the string code for the event
698
     * @param dateLogged the date on which the event occurred
699
     * @return String containing the formatted XML
700
     */
701
    private String generateXmlRecord(String entryId, String ipAddress, String userAgent,
702
            String principal, String docid, String event, Timestamp dateLogged)
703
    {
704
        StringBuffer rec = new StringBuffer();
705
        rec.append("<logEntry>");
706
        rec.append(generateXmlElement("entryid", entryId));
707
        rec.append(generateXmlElement("ipAddress", ipAddress));
708
        rec.append(generateXmlElement("userAgent", userAgent));
709
        rec.append(generateXmlElement("principal", principal));
710
        rec.append(generateXmlElement("docid", docid));
711
        rec.append(generateXmlElement("event", event));
712
        rec.append(generateXmlElement("dateLogged", DateTimeMarshaller.serializeDateToUTC(dateLogged)));
713
        rec.append("</logEntry>\n");
714

    
715
        return rec.toString();
716
    }
717
    
718
    /**
719
     * Return an XML formatted element for a given name/value pair.
720
     * 
721
     * @param name the name of the xml element
722
     * @param value the content of the xml element
723
     * @return the formatted XML element as a String
724
     */
725
    private String generateXmlElement(String name, String value)
726
    {
727
        return "<" + name + ">" + value + "</" + name + ">";
728
    }
729
}
(34-34/64)