THANKSGIVING QUICKIE: Notes Document Object in Freemarker

I’m doing some research for a client in using Freemarker to generate documents using templates and Notes data. It turns out that it’s deceptively simple to create a very basic Notes wrapper object to pass into Freemarker’s templating engine for merging document fields with a template. The closest abstract class to implement for a document is Freemarker’s TemplateHashModel. The idea is that for any given Notes field, you’d want the template to get the value by referencing the field’s name. Assuming that we call getText() on any retrieved item (I said it was a BASIC Notes wrapper) and convert the string results to Freemarker’s expected SimpleScalar, here’s the wrapper:

  1. import lotus.domino.*;
  2. import freemarker.template.*;
  3.  
  4. public class SimpleNotesDocumentWrapper implements TemplateHashModel {
  5. private Document doc;
  6.  
  7. public SimpleNotesDocumentWrapper(Document note) {
  8. doc = note;
  9. }
  10.  
  11. @Override
  12. public TemplateModel get(String key) throws TemplateModelException {
  13. Item item;
  14. try {
  15. if ((item = doc.getFirstItem(key)) != null) return new SimpleScalar(item.getText());
  16. } catch (Exception e) {
  17. e.printStackTrace();
  18. }
  19. return null;
  20. }
  21.  
  22. @Override
  23. public boolean isEmpty() throws TemplateModelException {
  24. // TODO Auto-generated method stub
  25. return (doc == null) ? true : false;
  26. }
  27. }

and you can then access document fields in your template like so: memo subject: ${doc.Subject}. (Assuming you’ve named the SimpleNotesDocumentWrapper object you placed in the root of the Freemarker object parse tree “doc.”)