Error when extracting an attachment using Java

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.

Subject: RE: Java attachment problem

I would try following:

  1. replace “static String extractAttachment(…” to “public String extractAttachment(…”

  2. make sure that calls to Tools.extractAttachment are not running concurrently, but after each other.

  3. recycle all objects in the Tool’s functions when you are done with them.

  4. Instead of sending Document as parameter, try sending Database and document’s UNID. Then use getDocumentByUNID method to get the document.

  5. In Tools class declare a fresh database object instead of using the object passed from the calling class. Use document’s UNID for locating the document.

I have experienced another Java-related problem which might be a variation of your problem. It is about some weird behaviour of the objects’ scope:

Subject: Done

Hi Andrei, thanks for your advice.I tried accesing the document using its ID (with getDocumentByUNID as you told me) and now I have no errors.

Bye bye.

Darío.