Problems detaching file attachments Java Agent

I’m getting some strange behaviour when I try to detach items from a Notes Document using a Java agent. Here’s the method that demonstrates the problem:

-----Code starts

private Vector detachAllFiles(Document doc) throws NotesException {

	Vector<String> lofn0 = new Vector<String>(); // List of file names.

	Vector<String> lofn1 = new Vector<String>(); // Another list of file

	// names.



	try {

		Vector<Item> v = doc.getItems();

		for (Item item : v) {



			if (item.getType() == Item.ATTACHMENT) {



				String fn = item.getValueString();

				EmbeddedObject eo = doc.getAttachment(fn);

				eo.extractFile(tempDirPath + "/" + fn);



				lofn0.add(fn); // Add to the list of file names.



				System.out.println("Added text string " + fn

						+ " to list of file names in lofn0.\n");

				System.out.println("Analysis of Vector lofn0 so far : - ");

				System.out.println("There are " + lofn0.size()

						+ " elements in the Vector lofn0");



				for (String s : lofn0) {

					System.out.println(s + ",\n"); // Show the element in

					// the Java debug

					// console

				}



				// This segment of code does essentially the same thing, but

				// the data added to the Vector is a String literal.

				lofn1.add("Backstock-Shopfloor-297-09022009.txt");

				System.out.println("Added text string "

						+ "Backstock-Shopfloor-297-09022009.txt"

						+ " to list of file names in lofn1.\n");

				System.out.println("Analysis of Vector lofn1 so far : - ");

				System.out.println("There are " + lofn1.size()

						+ " elements in the Vector lofn1");



				for (String s : lofn1) {

					System.out.println(s + ",\n");

				}

			}



		}

	} catch (Exception e) {

		System.out.println("Error!");

		System.out.println(e.getMessage());

	}



	return lofn0;

}

-----Code ends

… and the output is:

-----Output starts

Documents to process.1

Processing document

Temp dir path: C:\Program Files\IBM\Lotus\Notes\Data/Temp-SBON-7YLHYW

Added text string Backstock-Shopfloor-297-09022009.txtAnalysis of Vector lofn0 so far : -

There are 1 elements in the Vector lofn0

Backstock-Shopfloor-297-09022009.txtAdded text string NewProduct-297-09022009.txtThere are 2 elements in the Vector lofn0

Backstock-Shopfloor-297-09022009.txtAdded text string ResultFile-297-09022009.txtThere are 3 elements in the Vector lofn0

Backstock-Shopfloor-297-09022009.txt

-----Output ends

If I comment out the statement

-----Code starts

for (String s : lofn0) {

System.out.println(s + ",\n"); // Show the element in

					// the Java  debug 						// console



			}

-----Code ends

then the output becomes:

-----Output starts

Documents to process.1

Processing document

Temp dir path: C:\Program Files\IBM\Lotus\Notes\Data/Temp-SBON-7YLJ4L

Added text string Backstock-Shopfloor-297-09022009.txtAnalysis of Vector lofn0 so far : -

There are 1 elements in the Vector lofn0

Added text string Backstock-Shopfloor-297-09022009.txt to list of file names in lofn1.

Analysis of Vector lofn1 so far : -

There are 1 elements in the Vector lofn1

Backstock-Shopfloor-297-09022009.txt,

Added text string NewProduct-297-09022009.txt

There are 2 elements in the Vector lofn0

Added text string Backstock-Shopfloor-297-09022009.txt to list of file names in lofn1.

Analysis of Vector lofn1 so far : -

There are 2 elements in the Vector lofn1

Backstock-Shopfloor-297-09022009.txt,

Backstock-Shopfloor-297-09022009.txt,

Added text string ResultFile-297-09022009.txt

There are 3 elements in the Vector lofn0

Added text string Backstock-Shopfloor-297-09022009.txt to list of file names in lofn1.

Analysis of Vector lofn1 so far : -

There are 3 elements in the Vector lofn1

Backstock-Shopfloor-297-09022009.txt,

Backstock-Shopfloor-297-09022009.txt,

Backstock-Shopfloor-297-09022009.txt,

File name: Backstock-Shopfloor-297-09022009.txt

-----Output ends

The point is, even if I’ve screwed up the code (a distinct possibility) the code should not just stop without feedback. This code is in a script library and does not return to the calling agent when it breaks. At least I should get some output from the catch block. But nothing seems to happen. Has anyone else got thoughts on this?

Subject: Simpler code showing the

More digging around. Here’s a simpler agent that shows the problem that I’m getting.

import java.util.Vector;

import lotus.domino.*;

public class JavaAgent extends AgentBase {

public void NotesMain() {



	try {

		Session session = getSession();

		AgentContext agentContext = session.getAgentContext();



			DocumentCollection dc = agentContext.getUnprocessedDocuments();

			System.out.println("Documents to process." + dc.getCount());

			Document doc = dc.getFirstDocument();



			while (doc != null) {

				System.out.println("Processing document");

				Vector<Item> v = doc.getItems();

				

				for (Item item : v) {

					if (item.getType() == Item.ATTACHMENT) {

						System.out.println("Found attachment");

						System.out.println(item.getValueString());

					}

				}

				

				doc = dc.getNextDocument();

			}

			

			

			System.out.println("Completed");





	} catch (Exception e) {

		e.printStackTrace();

	}

}

}

The output looks like this:


Documents to process.1

Processing document

Found attachment

ResultFile-416-07102008.txt


Note that the “Completed” message is never sent, and the try/catch block is never executed, even though the document has three attachments.

If I remove the line

System.out.println(item.getValueString());

then the output becomes:


Documents to process.1

Processing document

Found attachment

Found attachment

Found attachment

Completed


This is a real problem for me, as I am looking to process emails with attachments in Java, as I want to use 3rd party tools to process the attachments. The attachments must conform to a naming convention.

Subject: It’s getItemValueString(), not getValueString()…

  • Or at least that’s the one I use (grin)…

Subject: Hmmm -

Hi David, thanks for your reply!

but getItemValueString is a method of Document. I’m using getValueString of Item. I do have a workaround, which is to use session.evaluate(“@AttachmentNames”, doc). And this seems to work fine. But I don’t really like using evaluate unless I have to.

I’m putting a call through to IBM support to explore this one further; there’s definitely something fishy here.

Cheers

Subject: D’oh!

Subject: It’s a bug

IBM have confirmed that it’s a bug. In fact it has been reported once before, and my test code looks almost exactly like the test case prepared by IBM for the bug fix team. It’s possible that this fix may come as a Hot Fix or as part of 8.5.2. Thanks for replying, cheers.

Subject: Having dug around more . . .

It seems that the Java method getValueString() of Item does not, in certain circumstances, return or retain the correct value.