Greetings,
I’m writing an agent to read the body of the document and search for “U.S.” and when it finds it I want to replace it with “United States”. My search and replace is working but when I write the body back out and save it the formatting is lost and everything is strung together.
If anyone can shed light/direction on how to retain the RichText format it would be appreciated.
Here’s my agent code:
import lotus.domino.*;
import java.lang.String;
import java.util.*;
import java.io.*;
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("($Inbox)");
View view = db.getView("IDDE");
Document doc = view.getFirstDocument();
while (doc != null)
{
// System.out.println(doc.getItemValueString("Subject"));
Enumeration items = doc.getItems().elements();
while (items.hasMoreElements())
{
Item item = (Item)items.nextElement();
// if (item.getName().equals("Subject"))
// {
// System.out.println("Subject Text:\n" + item.getText());
// }
if (item.getName().equals( "Body" ))
{
// System.out.println("Body Text:\n" + item.getText());
String oldBody = doc.getItemValueString("Body");
String oldText = "UNITED STATES";
String newText = "UNITED STATES AND IRELAND";
StringBuffer result = new StringBuffer();
int startIdx = 0;
int idxOld = 0;
while ((idxOld = oldBody.indexOf(oldText, startIdx)) >= 0)
{
result.append(oldBody.substring(startIdx, idxOld));
result.append(newText);
startIdx = idxOld + oldText.length();
}
result.append(oldBody.substring(startIdx));
String newBody = result.toString();
// System.out.println("New Body Text:\n" + newBody);
oldBody = newBody;
oldText = "U.S.";
newText = "U.S. AND IRISH";
result = new StringBuffer();
startIdx = 0;
idxOld = 0;
while ((idxOld = oldBody.indexOf(oldText, startIdx)) >= 0)
{
result.append(oldBody.substring(startIdx, idxOld));
result.append(newText);
startIdx = idxOld + oldText.length();
}
result.append(oldBody.substring(startIdx));
newBody = result.toString();
doc.replaceItemValue("Body", newBody);
System.out.println("New Body Text:\n" + newBody);
System.out.println("rich text:\n" + getRichTextItem());
doc.save();
System.out.println("bodyText:\n" + item.getText());
}
}
doc = view.getNextDocument(doc);
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
If there is an easier way I’m all for it. I tried the @ReplaceSubString function but had problems getting it to work.
Cheers,
Andrew