Examples: remove method (NoteCollection - Java)

This agent builds a note collection of documents from the current database. It removes any document with a subject containing the text "example" then exports the revised collection as DXL.

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();
      
      // Open DXL file named after current database
      Stream stream = session.createStream();
      String filename = "c:\\dxl\\";
      filename = filename + db.getFileName();
      filename = filename.substring(0, filename.length() - 3) + "dxl";
      if (stream.open(filename)) {
        System.out.println("Opened " + filename);
        stream.truncate();
        
        // Create note collection of all agents
        NoteCollection nc = db.createNoteCollection(false);
        nc.setSelectDocuments(true);
        nc.buildCollection();
        
        if (nc.getCount() > 0) {
          // Remove documents whose Subject contains "example"
          String id = nc.getFirstNoteID();
          while (id.length() > 0) {
            String idZap = id;
            // Get next doc before zapping current
            id = nc.getNextNoteID(id);
            Document doc = db.getDocumentByID(idZap);
            String subject = doc.getItemValueString("Subject");
            if (subject.toLowerCase().indexOf("example") >= 0)
              nc.remove(idZap);
          }

          // Export note collection as DXL
          if (nc.getCount() > 0) {
            DxlExporter exporter = session.createDxlExporter();
            String output = exporter.exportDxl(nc);
            stream.writeText(output);
            stream.close();
            System.out.println(nc.getCount() + " notes exported");
          }
          else
            System.out.println("No notes exported");
        }
        else
          System.out.println("No agents in database");
      }
      else {
        System.out.println("Cannot open " + filename);
      }

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