Jamie Cole
Jamie Cole

Reputation: 11

How to trigger the java agent from xpage button click event?

I'd like to test java agent to clear all the documents in a view by triggering the button click event on my xpage. I have no errors in java agent, but it is not working. Could you help me out to get through this stage?

Button click event:

var serverName=session.getCurrentDatabase().getServer();
//@WarningMessage("current one");
//@WarningMessage("server=" + serverName);
//var db:NotesDatabase = session.getDatabase(session.getCurrentDatabase().getServer(), "\ProTexII.nsf");
var db:NotesDatabase=session.getCurrentDatabase();
@WarningMessage("db=" + db);

var agent:NotesAgent = db.getAgent("SnapShotUpdate");
@WarningMessage("agent" + agent);

if (agent!=null){

 agent.run();
 @WarningMessage("view is fired!");

}

Java agent:

package javaPkg;
import java.io.PrintWriter;

import lotus.domino.*;

public class SnapShotUpdate extends AgentBase{

 public void NotesMain() {
       try {

         //String p = session.getPlatform();
           //PrintWriter out=getAgentOutput();

          System.out.println("Hello i never give it up!!");


          Session session = getSession();
          AgentContext agentContext =session.getAgentContext();
          Database db=session.getCurrentDatabase();


          //**clear view "vActualSalesFromSD" before copying documents into it

          DocumentCollection dc= db.createDocumentCollection();
          View view= db.getView("vActualSalesFromSD");
          Document docToBeCleared= view.getFirstDocument();

          while (docToBeCleared != null) {

             {
                dc.addDocument(docToBeCleared);
             }

                docToBeCleared = view.getNextDocument(docToBeCleared);
          }

          dc.removeAll(true);
       } catch(Exception e) {
         e.printStackTrace();
       }
     }


}

Upvotes: 0

Views: 1658

Answers (1)

stwissel
stwissel

Reputation: 20384

You have 3 possibilities to trigger your code:

  1. The approach you have taken
  2. Make an ajax call to /yourdatabase.nsf/SnapshotUpdate?OpenAgent
  3. Don't use an agent (highly) recommended -> it is Java already, use a class and call it in your SSJS

What can go wrong:

  1. The agent has a target other than "none"
  2. You don't have delete rights in that database
  3. You don't have the right to run the agent (not very likely, since you can run a XPage)

I would write the loop like this:

  public function clearView(Database db, String vName ) {
     try {
         View view= db.getView("vActualSalesFromSD");
         Document docToBeCleared= view.getFirstDocument();
         Document nextDoc = null;

         while (docToBeCleared != null) {
            nextDoc = view.getNextDocument(docToBeCleared);
            try {
                 docToBeCleared.remove(true);
                } catch (Exception didntWork) {
                // Do some logging here
                }
            // Recycle your objects
            doc.recycle();
            docToBeCleared = nextDoc;             
         }

        // cleanup, we recycle what we opened, but not the parameters
        // nextDoc and docToBeCleared are null already;
        v.recycle();

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

Let us know you progress.

Upvotes: 5

Related Questions