Examples: encrypt method and EncryptionKeys property

This agent encrypts a document after making sure "Top Secret" is one of the encryption keys.

import lotus.domino.*;
import java.util.Vector;
public class JavaAgent extends AgentBase {
  public void NotesMain() {
    try {
      Session session = getSession();
      AgentContext agentContext = session.getAgentContext();
      // (Your code goes here) 
      Database db = agentContext.getCurrentDatabase();
      View view = db.getView("By Category");
      Document doc = view.getFirstDocument();
      while (doc != null) {
        Item item = doc.getFirstItem("Body");
        if (item != null) {
          // Make sure the item can be encrypted
          if (!item.isEncrypted()) item.setEncrypted(true);
          // Add "Top Secret" to the encryption keys
          // if not there
          boolean found = false;
          Vector v = doc.getEncryptionKeys();
          for (int i=0; i<v.size(); i++) {
            String k = (String)v.elementAt(i);
            if (k.compareTo("Top Secret") == 0) {
              found = true; break; } }
          if (!found) {
            v.addElement("Top Secret");
            doc.setEncryptionKeys(v); }
          // Encrypt the document
          try {
            doc.encrypt();
          } catch (NotesException e) {
            System.out.println(e.id + " " + e.text);
            break; } }
        doc.save();
        doc = view.getNextDocument(doc); }
    } catch(Exception e) {
      e.printStackTrace();
    }
  }
}