close

Authors


XPages tip: Keep your Notes Document in memory with Java and get easily access to the fields.

chatFrank van der Linden | e-office   14 January 2013 18:12:27
Last week I have finished our new Social Intranet Framework build with XPages

In this new version, one of my goals was to use a much Java as possible and reduce the Server Side Javascript to a minimum, because that was causing us a lot of trouble the previous version.
I went straight forward with Java Beans for the logic and XPages for the presentation.

The content was dynamically loaded with some Java methods, but mayor problem was to handle the RichText items, containing MIME or HTML.
Notes objects in Java are very unreliable and can not be kept in memory and should be recycled as soon as possible.

Via Skype I had a small chat Jesse Gallagher, the fanastic who ported Ruby on Rails and a lot of others languages inside XPages.
He pointed me to the Javadoc of the XPages Extensibility API and then especially to the DominoDocument object.

This DominoDocument can be kept in memory, so you have access to all your fields of the backend document, and is nicely serialized.

How do you do this

Step 1: a Content object, to hold the DominoDocument,as a private property

Content content = new Content();
private DominoDocument contentDocument;

And a Getter and Setter
public void setContentDocument(DominoDocument contentDocument) {
    this.contentDocument = contentDocument;
}

public DominoDocument getContentDocument() {
   return contentDocument;
}

Step 2: In the Java Bean you have access to your backend NotesDocument.
doc = vw.getFirstDocument();

Step 3: Put this backend document as a DominoDocument in the Content Object.
content.setContentDocument(DominoDocument.wrap(doc.getParentDatabase().getFilePath(),doc,null,null,false,null,null));

Step 4: Access the fields of the DominoDocument. I have created a method who will access the fields.
In my JSFUtil class I have isRecycled method who will check if my backend document is recycled.
When it is the case, the backend document is nicely restored.
So you have always access to yor backend document.
The getValue() will return an object by providing the fieldname.

public Object getFieldValue(String field){
  try{
       if(JSFUtil.isRecycled(contentDocument.getDocument())) {
           contentDocument.restoreWrappedDocument();
         }
         return contentDocument.getValue(field);
                       
     }catch(Exception e){
         Configuration.getOpenLogItem().logError(e);
     }
         return null;
}


Step 5: In my XPages I can call the getFieldValue() method. The return value is an Object, and can be displayed immediately, without conversion.
So you can also display MIME in a RichTextItem or a Number value.
"false" id="contextTypeText">
         return pageBean.getContent(compositeData.contentID).getFieldValue("body");
   }}]]>




Conclusion
Initial setup of the objects and the Java Bean took me more time, but in the end it saves me a lot of time. Especially also my co-workers can easily use the methods in the other ContentTypes, without the need of accessing the Content Object.


Comments Disabled