1   /*
2    * Copyright 2007 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.goetz.domino.log4j;
18  
19  import java.util.Vector;
20  
21  import junit.framework.TestCase;
22  import lotus.domino.Database;
23  import lotus.domino.DateTime;
24  import lotus.domino.Document;
25  import lotus.domino.DocumentCollection;
26  import lotus.domino.NotesFactory;
27  import lotus.domino.NotesThread;
28  import lotus.domino.Session;
29  
30  import org.apache.log4j.helpers.LogLog;
31  
32  /***
33   * Test class to find the last document in a view.
34   * 
35   * @author Bernd Götz
36   */
37  public class SearchLastLogDocumentTest extends TestCase {
38  	
39  	public void testGetLastDocument() throws Exception {
40  
41  		NotesThread.sinitThread();
42  		
43  		Session session = NotesFactory.createSession();
44  
45  		String databaseName = "DOMAPP/applog00.nsf";
46  		String serverName = "dominotest";
47  		Database db = session.getDatabase(serverName, databaseName);
48  		if (!db.isOpen()) {
49  			LogLog.debug("Log databaseName is closed. Trying to open it.");
50  			db.open();
51  		}
52  		
53  		// now search:
54  		String f = "Form = \"frmEvents\" & " +
55  			"AppName = \"RemoteTestServlet1\" & " +
56  			"Server = \"CN=dominotest/O=homenet\"";
57  		
58          DateTime t = session.createDateTime("Yesterday");
59          System.out.println("today: " + t.toString());
60  	
61  		DocumentCollection dc = db.search(f, t);
62  		
63  		Document d = dc.getFirstDocument();
64  		DateTime last = null;
65  		Document lastDoc = null;
66  		while (d != null) {
67  			Vector ve = d.getItemValue("Events");
68  			System.out.println("lines: " + ve.size());
69  			int len = 0;
70  			for (int i = 0; i < ve.size(); i++) {
71  				String l = (String)ve.elementAt(i);
72  				len += l.length();
73  			}
74  			System.out.println("bytes: " + len);
75  			Vector v = d.getItemValueDateTimeArray("StartTime");
76  			DateTime dt = (DateTime) v.elementAt(0);
77  			if (last != null) {
78  				int diff = dt.timeDifference(last);
79  				System.out.println("diff: " + diff);
80  			}
81  			if ((last == null) || (dt.timeDifference(last) >= 0)) {
82  				// if (dt - last) secs is minus, tag dt's document as the last one
83  				last = dt;
84  				lastDoc = d;
85  			}
86  			System.out.println("StartTime: " + dt.getLocalTime());
87  			d = dc.getNextDocument();
88  		}
89  		System.out.println("Last: " + last);
90  		if (last != null) {
91  			System.out.println("Doc Id: " + lastDoc.getNoteID());
92  		}
93  		
94  		db.recycle();
95  		session.recycle();
96  		NotesThread.stermThread();
97  		
98  	}
99  
100 }