Examples: Opening a database (Java)

  1. This agent opens a local database and prints its title. The user supplies the database file name, which does not need the .NSF extension, as the agent comment.
    import lotus.domino.*;
    public class JavaAgent extends AgentBase {
      public void NotesMain() {
        try {
          Session session = getSession();
          AgentContext agentContext = 
            session.getAgentContext();
          // (Your code goes here) 
          Agent agent = agentContext.getCurrentAgent();
          DbDirectory dir = session.getDbDirectory(null);
          Database db = dir.openDatabase(agent.getComment());
          if (db == null) System.out.println(
             "Database open failed");
          else System.out.println("Title:\t\t" + 
              db.getTitle());
        } catch(NotesException e) {
          if (e.id + NotesError.NOTES_MIN_ERROR_CODE ==
          NotesError.NOTES_ERR_NOTAFILE)
            System.out.println("No such database");
          else {
            System.out.println(e.id + " " + e.text);
            e.printStackTrace(); }
        } catch(Exception e) {
          e.printStackTrace();
        }
      }
    }
  2. This agent opens the current database.
    import lotus.domino.*;
    public class JavaAgent extends AgentBase {
      public void NotesMain() {
        try {
          Session session = getSession();
          AgentContext agentContext = 
             session.getAgentContext();
          // (Your code goes here) 
          Database db = agentContext.getCurrentDatabase();
          System.out.println
          ("Title:\t\t" + db.getTitle());
          System.out.println
          ("File name:\t" + db.getFileName());
          String msg = "closed";
          if (db.isOpen()) msg = "open";
          System.out.println("Database is " + msg);
        } catch(Exception e) {
          e.printStackTrace();
        }
      }
    }
  3. This agent shows that the database returned by getFirstDatabase is closed until opened with the open method.
    import lotus.domino.*;
    public class JavaAgent extends AgentBase {
      public void NotesMain() {
        try {
          Session session = getSession();
          AgentContext agentContext = 
             session.getAgentContext();
          // (Your code goes here) 
          DbDirectory dir = session.getDbDirectory(null);
          Database db = dir.getFirstDatabase(
              DbDirectory.DATABASE);
          if (db.isOpen()) System.out.println("Is open");
          else System.out.println("Is closed");
          db.open();
          if (db.isOpen()) System.out.println("Is open");
          else System.out.println("Is closed");
        } catch(Exception e) {
          e.printStackTrace();
        }
      }
    }
  4. This agent traverses the local databases and opens the database whose title is specified as the agent comment.
    import lotus.domino.*;
    public class JavaAgent extends AgentBase {
      public void NotesMain() {
        try {
          Session session = getSession();
          AgentContext agentContext = 
              session.getAgentContext();
          // (Your code goes here) 
          Agent agent = agentContext.getCurrentAgent();
          DbDirectory dir = session.getDbDirectory(null);
          Database tmpdb;
          Database db = dir.getFirstDatabase(
              DbDirectory.DATABASE);
          while (db != null) {
            if (db.getTitle() != null)
            if (db.getTitle().equalsIgnoreCase(
                 agent.getComment())) {
              db.open();
              DocumentCollection dc = db.getAllDocuments();
              Document tmpdoc;
              Document doc = dc.getFirstDocument();
              while (doc != null) {
                System.out.println(doc.getItemValueString("Subject"));
                tmpdoc = dc.getNextDocument();
                doc.reycle;
                doc = tmpdoc; }
              break; }
            tmpdb = dir.getNextDatabase();
            db.recycle();
            db = tmpdb; }
        } catch(Exception e) {
          e.printStackTrace();
        }
      }
    }
  5. This agent opens the user's mail database.
    import lotus.domino.*;
    public class JavaAgent extends AgentBase {
      public void NotesMain() {
        try {
          Session session = getSession();
          AgentContext agentContext = 
              session.getAgentContext();
          // (Your code goes here) 
          DbDirectory dir = session.getDbDirectory(null);
          Database db = dir.openMailDatabase();
          System.out.println("File name:\t" + 
              db.getFileName());
          String msg = "closed";
          if (db.isOpen()) msg = "open";
          System.out.println("Database is " + msg);
        } catch(Exception e) {
          e.printStackTrace();
        }
      }
    }
  6. This agent opens the database on the server NotesUA1 whose replica ID is the same as the current database.
    import lotus.domino.*;
    public class JavaAgent extends AgentBase {
      public void NotesMain() {
        try {
          Session session = getSession();
          AgentContext agentContext = 
              session.getAgentContext();
          // (Your code goes here) 
          DbDirectory dir = session.getDbDirectory(
              "NotesUA1");
          Database dbLocal = 
               agentContext.getCurrentDatabase();
          Database dbRemote = 
               dir.openDatabaseByReplicaID
          (dbLocal.getReplicaID());
          System.out.println
          ("Title:\t\t" + dbRemote.getTitle());
          System.out.println
          ("Server:\t\t" + dbRemote.getServer());
          System.out.println
          ("File path:\t\t" + dbRemote.getFilePath());
          String msg = "closed";
          if (dbRemote.isOpen()) msg = "open";
          System.out.println("Database is " + msg);
        } catch(Exception e) {
          e.printStackTrace();
        }
      }
    }