Database access from another class - Java

Hello!Sorry for my English :slight_smile:

I am beginer and I don’t know how to get access to local database.

I wrote two classes:

  1. JavaAgent extend AgentBase with NotesMain function. From this level I have acces to my database, except this I can’t read all records from my ViewEntryCollection vec = view.getAllEntries(); (only 9000 - my database have 13000).

  2. MyWindow which is interface to my Agent.

From this level I have’t acces to database - please help!!!

This is my code:

import lotus.domino.*;

import java.util.*;

import java.util.Properties;

import javax.mail.*;

import javax.mail.internet.*;

import java.awt.*;

import java.awt.event.*;

public class JavaAgent extends AgentBase {

String myString=“”;

MyWindow myWindow;

public void NotesMain() {



	try {

		lotus.domino.Session session = getSession();

		AgentContext agentContext = session.getAgentContext();

		lotus.domino.Database db = agentContext.getCurrentDatabase();

		Document doc = agentContext.getDocumentContext(); 

		

			if(doc!=null)//sprawdzenie czy pobrano dokument

			{

			myString = doc.getFirstItem("e_mail").getText();	



			}

			else System.out.println("doc is null");





		} catch(Exception e) {e.printStackTrace();}



myWindow = new MyWindow(this,myString);

myWindow.setSize(500,500);

myWindow.setVisible(true);

}

}//end class JavaAgent

class MyWindow extends Frame implements WindowListener,ActionListener

{

Button sendToPerson;

Button sendToAll;

JavaAgent agent;

MyWindow(JavaAgent agent,String meil)

{

	

	super("Mailing");

	this.agent=agent;

	addWindowListener(this);

	setLayout(new BorderLayout());

	sendToPerson = new Button("Send to person");

	sendToPerson.addActionListener(this);

	add(BorderLayout.WEST, sendToPerson);

	

	sendToAll = new Button("Send to all");

	sendToAll.addActionListener(this);

	add(BorderLayout.EAST, sendToAll);

}

public void SendtoPerson()

{

}

public void SendToAll()

{

try {

	///// here I have problem

		lotus.domino.Session session = agent.getSession();

		AgentContext agentContext = session.getAgentContext();

		lotus.domino.Database db = agentContext.getCurrentDatabase();

		View view = db.getView("MyView");

		ViewEntryCollection vec = view.getAllEntries();

	    System.out.println("\"MyView\" view has " +vec.getCount() + " entries");

	

	ViewEntry entry;

 	for (int i=1; i< vec.getCount(); i++)

 	{

   		try {

   			entry = vec.getNthEntry(i);

        	System.out.println(i+"  "+entry.getDocument().getItemValueString("e_mail")+"\n");



    		//here I will send a message, but I have't access to database and field e_mail

    		//please Help ;)

			

			} catch(Exception en) {}

  

	}

	

	}

	 catch(Exception e) {e.printStackTrace();}

}

public void actionPerformed(ActionEvent e)

{

	if (e.getSource() instanceof Button)

	{

		if (e.getSource() == sendToPerson)

		{

			SendtoPerson();	

		}

		if (e.getSource() == sendToAll)

		{

			SendToAll();

		}

	}

}

public void windowActivated(WindowEvent e){}

public void windowClosed(WindowEvent e){}

public void windowClosing(WindowEvent e) {

setVisible(false);

dispose();

}

public void windowDeactivated(WindowEvent e){}

public void windowDeiconified(WindowEvent e){}

public void windowIconified(WindowEvent e){}

public void windowOpened(WindowEvent e){}

}

I have this error:

lotus.domino.NotesException

at lotus.domino.local.NotesBase.CheckObject(NotesBase.java:979)



at lotus.domino.local.Session.getAgentContext(Session.java:1221)



at MyWindow.SendToAll(JavaAgent.java:72)



at MyWindow.actionPerformed(JavaAgent.java:108)



at java.awt.Button.processActionEvent(Button.java:256)



at java.awt.Button.processEvent(Button.java:229)



at java.awt.Component.dispatchEventImpl(Component.java:1812)



at java.awt.Component.dispatchEvent(Component.java:1744)



at java.awt.EventDispatchThread.run(EventDispatchThread.java:79)

Subject: Database access from another class - Java

I’ve just had a quick look at your code, and I don’t have much experience of coding Java within Notes. However, you may find you get more joy if you explicitly pass the reference to your Database to the “SendToAll” method, rather than trying to re-instantiate the AgentContext and what have you.

Subject: RE: Database access from another class - Java

It doesn’t help. Aside from “NotesMain()” I can’t get database item even when variable “db” is global. I still loking for solution. :frowning:

Subject: Why can’t you … (WAS: Database access from another class - Java)

… pass the db object to the MyWindow constructor?

Subject: RE: Why can’t you … (WAS: Database access from another class - Java)

Sorry, being an idiot. You might want to look at Domino Designer help with regards additional classes accessing Domino objects. Specifically, look at the NotesThread class (which AgentBase extends, and therefore is also the superclass of your Java agent):

“Each thread of an application making local calls must initialize a NotesThread object. This includes AWT threads that access the Domino Objects. Listener threads must use the static methods because they cannot inherit from NotesThread.”

HTH

Subject: RE: Why can’t you … (WAS: Database access from another class - Java)

HI!I wrote this class:

class platform1 extends NotesThread

{

public static void main()

{

    platform1 t = new platform1();

    t.start();

}

public void runNotes()

{

try

  {

    lotus.domino.Session s = NotesFactory.createSession();

    String p = s.getPlatform();

    System.out.println("Platform = " + p);

    lotus.domino.Database db =s.getAgentContext().getCurrentDatabase();

    System.out.println(db.getTitle());

  }

catch (Exception e)

  {

    e.printStackTrace();

  }

}

}


Now my "SendToAll

public void SendToAll()

{

platform1 plat= new platform1();

plat.start();

}

It still doesn’t work and i have this massage:

Platform = Windows/32 - this is OK

java.lang.NullPointerException:

at platform1.runNotes(JavaAgent.java:145)



at lotus.domino.NotesThread.run(NotesThread.java:203)

;(

Subject: RE: Why can’t you … (WAS: Database access from another class - Java)

OK, see how you go with this… Taking your original example, I worked up some skeleton classes thus. One, “JavaAgent”, is the agent code. The other, “DomGUI” is the equivalent of your MyWindow class… some of the original code has been taken out for brevity. Notice the NotesThread calls in the sendToAll() method: they are key to the Domino interfaces / classes being accessible in sendToAll().

JAVAAGENT:

import lotus.domino.*;

public class JavaAgent extends AgentBase {

private Session session;

private AgentContext ac;

private Database db;

private String myString;

private DomGUI myWin;



public void NotesMain() {

	try {

		session = getSession();

		ac = session.getAgentContext();

		db = ac.getCurrentDatabase();

		Document doc = ac.getDocumentContext();



		if(doc!=null) {

			myString = doc.getFirstItem("e_mail").getText();

		}	// end if

		myWin = new DomGUI(session, db);

		while (!myWin.isClosed) {

			Thread.sleep(250);

		}	// end while

	} catch(Exception e) {

		e.printStackTrace();

	}	// end try catch

}	// end NotesMain method

} // end JavaAgent class

DOMGUI:

import lotus.domino.*;

import java.awt.*;

import java.awt.event.*;

class DomGUI extends Frame implements ActionListener {

public boolean isClosed = false;

private Button btnPerson;

private Button btnSend;

private Session session;

private Database db;

private View view;



DomGUI(Session parentSession, Database currentDb) {

	super("Mailing");

	try {

		this.session = parentSession;

		this.db = currentDb;;

		view = db.getView("MyView");

		// enable window events

		enableEvents(AWTEvent.WINDOW_EVENT_MASK);

	} catch(Exception e) {

		e.printStackTrace();

	}	// end try catch

			

	setLayout(new BorderLayout());

	btnSend = new Button("Send to all");

	btnSend.addActionListener(this);

	add(BorderLayout.EAST, btnSend);

	btnPerson = new Button("Send to a person");

	btnPerson.addActionListener(this);

	add(BorderLayout.WEST, btnPerson);

	setSize(500,500);

	setVisible(true);

}	// end constructor



public void sendToAll() {

	NotesThread.sinitThread();

	// do view stuff here

	NotesThread.stermThread();

}	// end sendToAll method



// necessary to implement ActionListener

public void actionPerformed (ActionEvent e) {

	if (e.getSource() == btnSend) {

		sendToAll();

		dispose();

		isClosed = true;

	}	// end if

}	// end method



protected void processWindowEvent (WindowEvent e) {

	if (e.getID() == WindowEvent.WINDOW_CLOSING) {

		dispose();

		isClosed = true;

	} else {

		super.processWindowEvent(e);

	}	// end if else

}	// end method

} // end DomGUI class

Hope this is of use. Like I said before, thi is fairly new to me – as is Java itself – so if my code contains glaring horrors, my apologies. The thread sleeping is a bit of a cludge, but it works, and makes sense in the context of a Notes agent running in the foreground in any case.

Subject: RE: Why can’t you … (WAS: Database access from another class - Java)

Not bad, Ben. That has all the elements that make for a decent GUI based Java Agent. The only think I would change is remove the NotesThread.stermThread() call. In essence, the JVM starts the AWT thread as needed and it lives for the life of the JVM. If you do an init/term each time, you are going to see a perfornace problem. So just do the init each time, it only really does something the first time through, everytime there after, it’s pretty much a no-op with some call overhead. If you leave the stermThread() there, each time init is called it has to reinitialize Notes on that thread. If you want to you can do an stermThread() on the window closing event.

–Joe

Joseph Millar

Principal Software Engineer

Brightline Technology

Subject: Thanks Joe, that’s useful to know!

Subject: It works :wink:

thanks awfully!it works !!!

I’m grateful.

Subject: Additional comment re loop

With regards this piece of the original code:

    for (int i=1; i< vec.getCount(); i++)

… you might want to change that to this:

    for(int i = 1; i <= vec.getCount(); i++)

… in order to get a true reflection of the ViewEntryCollection object.