0

我想通过触发我的 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();
       }
     }


}
4

1 回答 1

5

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

  1. 你采取的方法
  2. 对 /yourdatabase.nsf/SnapshotUpdate?OpenAgent 进行 ajax 调用
  3. 不要使用代理(强烈推荐)-> 它已经是 Java,使用一个类并在你的 SSJS 中调用它

可能出什么问题:

  1. 代理的目标不是“无”
  2. 您在该数据库中没有删除权限
  3. 您无权运行代理(不太可能,因为您可以运行 XPage)

我会这样写循环:

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

让我们知道你的进步。

于 2013-03-08T06:14:29.900 回答