I’m getting an error when a try to extract an attachment from a rich text field to disk. I made a static method (extractAttachment) in a class called Tools that receives 3 parameters: a notes Document, the name of the field that contains the attachment and the path to extract the file.Here is the code of the method:
static String extractAttachment (Document docu, String fieldName, String path)
{
String attachmentName = null;
boolean extracted = false;
try
{
System.out.println ("Method: extractAttachmnt. Field Name " + fieldName + " " + ". Path: " + path);
RichTextItem rti = (RichTextItem)docu.getFirstItem(fieldName);
Vector vec = rti.getEmbeddedObjects();
Enumeration en = vec.elements();
while (en.hasMoreElements()) {
EmbeddedObject emo = (EmbeddedObject)en.nextElement();
if (emo.getType() == EmbeddedObject.EMBED_ATTACHMENT) {
emo.extractFile(path + emo.getSource());
attachmentName = emo.getName();
System.out.println (attachmentName + " se extrajo a: " + path);
extracted = true;
}
}
if (extracted == false) {
System.out.println ("No se ha podido extraer el attachment");
}
} catch (Exception e) {
e.printStackTrace();
}
return attachmentName;
}
Here I use the method in two different documents, one is a config document that I use to setup the path and I save a logo that I’ll use later, and the other is the the current document (the one I have opened in the UI).
The method getConfigDoc(session, cdDb) is a method that search in a view the configuration document and returns it.
Session session = getSession();
agentContext = session.getAgentContext();
cdDb = agentContext.getCurrentDatabase();
configDoc = Tools.getConfigDoc(session, cdDb);
currentDoc = agentContext.getDocumentContext();
path = configDoc.getItemValueString(“extractionPath”);
System.out.println(Tools.extractAttachment (configDoc, “LogoOAA”, path)); //LogoOAA is a richtext field containing the logo
System.out.println(Tools.extractAttachment (currentDoc, “effiAtta”, path)); //effiAtta is a richtext field containing a JPG file
And this is the output in the Java Console, when I use the extractAttachment method in the current document I get this error:
Method: extractAttachmnt. Field Name LogoOAA . Path: C:\TEMP\PDF\
LogoOAA.jpg se extrajo a: C:\TEMP\PDF\ //success extracting attachment
LogoOAA.jpg
Method: extractAttachmnt. Field Name effiAtta . Path: C:\TEMP\PDF\
NotesException: Notes error: A database handle to a remote database cannot be used by more than one thread. (C:\TEMP\PDF\barbas.jpg) //no attachment extracted
What’s wrong with my code?.
Thanks in advance.
Darío.