Examples: buildCollection method

This agent creates note collections based on form and view elements in the current database. The work occurs in a function, which creates the note collection depending on the input parameters, builds the collection, then clears the collection.

import lotus.domino.*;

public class JavaAgent extends AgentBase {
  
  Stream stream = null;
  NoteCollection nc = null;
  DxlExporter exporter = null;

  public void NotesMain() {

    try {
      Session session = getSession();
      AgentContext agentContext = session.getAgentContext();

      // (Your code goes here) 
      Database db = agentContext.getCurrentDatabase();
      
      // Create stream and note collection
      stream = session.createStream();
      nc = db.createNoteCollection(false);
      exporter = session.createDxlExporter();
      
      // Export forms
      exportFormView(true, false);
      
      // Export views
      exportFormView(false, true);
      
      // Export forms and views
      exportFormView(true, true);
      
      // Test bad call
      exportFormView(false, false);

    } catch(Exception e) {
      e.printStackTrace();
    }
  }
  
  void exportFormView(boolean form, boolean view) {

    try {
      // Check parameters and set up file name
      String filename = null;
      if (form & view)
        filename = "formview";
      else if (form)
        filename = "form";
      else if (view)
        filename = "view";
      else {
        System.out.println("Form and view both false - no action");
        return;
      }
      
      // Open DXL file named after type of action
      filename = "c:\\dxl\\" + filename + ".dxl";
      if (!stream.open(filename)) {
        System.out.println("Could not open " + filename);
        return;
      }
      stream.truncate();
        
      // Create note collection
      nc.selectAllNotes(false);
      if (form) {
        nc.setSelectForms(true);
        nc.setSelectSubforms(true);
        nc.setSelectSharedFields(true);
      }
      if (view) {
        nc.setSelectViews(true);
        nc.setSelectFolders(true);
      }
      nc.buildCollection();
        
      // Export note collection as DXL
      String output = exporter.exportDxl(nc);
      stream.writeText(output);
      System.out.println("DXL exported to " + filename);
      stream.close();
      nc.clearCollection();
      
      return;

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