如何从xpage按钮单击事件触发java代理?

时间:2013-03-08 03:30:36

标签: java xpages lotus-domino lotus

我想通过触发xpage上的按钮点击事件来测试java代理以清除视图中的所有文档。我在java代理中没有错误,但它无法正常工作。你能帮助我度过这个阶段吗?

按钮点击事件:

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代理:

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();
       }
     }


}

1 个答案:

答案 0 :(得分:5)

您有3种触发代码的可能性:

  1. 您采取的方法
  2. 对/yourdatabase.nsf/SnapshotUpdate?OpenAgent进行ajax调用
  3. 不要使用推荐的代理商(高度) - >它已经是Java,使用类并在SSJS中调用它
  4. 可能出现的问题:

    1. 代理人的目标不是“无”
    2. 您在该数据库中没有删除权限
    3. 您无权运行代理(不太可能,因为您可以运行XPage)
    4. 我会写这样的循环:

        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();
           }
       }
      

      让我们知道你的进步。