Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get all the attachments from a .nsf(lotus notes) file using java

Steps followed :

Took a back of my lotus notes as sample.nsf

And then tried to read the attachments from the sample.nsf

Code snippet :

Database db = session.getDatabase("","C:\\Projects\\NotesToJava\\sample.nsf");
DocumentCollection dc = db.getAllDocuments();
Document doc = dc.getFirstDocument();

while (doc != null) {
    RichTextItem body = (RichTextItem) doc.getFirstItem("Body");

    if (body.getEmbeddedObject("Request.xlsx") != null)
         System.out.println("Found BPM_Dev_Access_Request.xlsx in " + doc.getItemValueString("Subject"));

    doc = dc.getNextDocument();
}
like image 393
user1007180 Avatar asked Jan 18 '23 11:01

user1007180


2 Answers

No need to use evaluate, look up the extractFile in the Lotus Designer Help

From the Lotus help:

import lotus.domino.*;
import java.util.Vector;
import java.util.Enumeration;
public class JavaAgent extends AgentBase {
  public void NotesMain() {
    try {
      Session session = getSession();
      AgentContext agentContext = session.getAgentContext();
      // (Your code goes here) 
      Database db = agentContext.getCurrentDatabase();
      DocumentCollection dc = db.getAllDocuments();
      Document doc = dc.getFirstDocument();
      boolean saveFlag = false;
      while (doc != null) {
        RichTextItem body = 
        (RichTextItem)doc.getFirstItem("Body");
        System.out.println(doc.getItemValueString("Subject"));
        Vector v = body.getEmbeddedObjects();
        Enumeration e = v.elements();
        while (e.hasMoreElements()) {
          EmbeddedObject eo = (EmbeddedObject)e.nextElement();
          if (eo.getType() == EmbeddedObject.EMBED_ATTACHMENT) {
            eo.extractFile("c:\\extracts\\" + eo.getSource());
            eo.remove();
            saveFlag = true;
            }
        }
        if (saveFlag) {
          doc.save(true, true);
          saveFlag = false;
          }
        doc = dc.getNextDocument();
      }
    } catch(NotesException e) {
      System.out.println(e.id + " " + e.text);
      e.printStackTrace();
    }
  }
}
like image 197
Jasper Duizendstra Avatar answered Feb 08 '23 16:02

Jasper Duizendstra


You need to get the attachments out of each document, as opposed to the EmbeddedObjects. Something like this:

import java.util.Iterator;

import lotus.domino.*;

public final class DocAttachmentParser implements Iterator {

private Session s;
private Document doc;
private Double count ;
private Iterator attIterator = null;
public  Double getCount() {
    return count;
}
public  DocAttachmentParser(Session s, Document doc) throws NotesException {
        this.s  = s;
        this.doc = doc;
        if (s!=null && doc !=null){
            this.count = (Double) s.evaluate("@Attachments", doc).elementAt(0);
            if (count.intValue() > 0){
                attIterator = s.evaluate("@AttachmentNames", doc).iterator();
                }
        }

}
    public boolean hasNext() {
        return count.intValue() > 0 ? attIterator.hasNext(): false;
    }

    public Object next() {
        return count.intValue() > 0 ? attIterator.next(): null;
    }
    private String nextAttName(){
        return count.intValue() > 0 ? attIterator.next().toString(): null;
    }

    public void remove() {
        if (count.intValue() > 0) attIterator.remove();
    }

    public String getAll(){

        StringBuilder sb = new StringBuilder();
        if (count.intValue()>0){

            while (hasNext()) {
                sb.append(nextAttName());
            }
        }

        return sb.toString();
    }

}
like image 25
Mike Avatar answered Feb 08 '23 14:02

Mike