Examples: Database class

  1. This agent creates a Database object called db and assigns a database to it. The database is names.nsf, located at the top level of the data directory on server doc. If the database exists, getDatabase automatically opens it.
    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 = 
             session.getDatabase("doc", "names");
          System.out.println(db.getTitle());
        } catch(Exception e) {
          e.printStackTrace();
        }
      }
    }
  2. This agent uses isOpen to test if the database quack.nsf exists locally. If not, the agent uses the create method to create a new database on disk.
    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, template;
          db = session.getDatabase(null, "quack");
          if (db.isOpen())
            System.out.println(db.getTitle());
          else {
            System.out.println("Database does not exist");
            System.out.println("Creating new database ...");
            template = session.getDatabase(null, 
            "discsw50.ntf");
            if (template.isOpen()) {
              db = template.createFromTemplate
              (null, "quack", true);
              db.setTitle("Ducks of North America");
              System.out.println(db.getTitle()); }
            else
              System.out.println
              ("Template discsw50.ntf does not exist"); }
        } catch(Exception e) {
          e.printStackTrace();
        }
      }
    }
  3. This agent is the same as the last except that the database is in a subdirectory of the data directory. Notice that two backslashes must be used because the backslash is an escape character in Java.
    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, template;
          db = session.getDatabase(null, "quack");
          if (db.isOpen())
            System.out.println(db.getTitle());
          else {
            System.out.println(
               "Database does not exist");
            System.out.println(
               "Creating new database ...");
            template = session.getDatabase(null, 
            "discsw50.ntf");
            if (template.isOpen()) {
              db = template.createFromTemplate
              (null, "birds\\quack", true);
              db.setTitle("Ducks of North America");
              System.out.println(db.getTitle()); }
            else
              System.out.println
              ("Template discsw50.ntf does not exist"); }
        } catch(Exception e) {
          e.printStackTrace();
        }
      }
    }
  4. This agent gives Brian Flokka Editor access to the current database. Using the CurrentDatabase property avoids having to use file names in agents and makes agents easily portable from one database to another.
    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();
          db.grantAccess("Brian Flokka", ACL.LEVEL_EDITOR);
        } catch(Exception e) {
          e.printStackTrace();
        }
      }
    }
  5. This agent shows how you can use the openIfModified method to open a database only if it's been modified after a certain date. The agent checks if quack.nsf on the current server was modified since yesterday; if so, the agent opens the database and compacts it.
    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);
          DateTime dt = session.createDateTime("Today");
          dt.setNow();
          dt.adjustDay(-1);
          Database db = dir.openDatabaseIfModified
          ("quack", dt);
          if (db != null) {
            System.out.println("Compacting database");
            db.compact(); }
          else
            System.out.println(
              "Database not modified in past 
               day");
        } catch(Exception e) {
          e.printStackTrace();
        }
      }