@Transform を使って JSON を作成し XAgent で出力として利用する方法

Put this piece of code into a view column; in _exclude list all items from the document, that should not be included in the output
All items in the document that are not excluded are used to build a JSON String 
 
=========================== @Formula START =========================================
 
_exclude:="$FILE":"$Fonts":"Form":"$UpdatedBy":"$Revisions":"ID":"ModifiedBy":"AddressInvoiceAppartment";
_fld:=@Trim(@ReplaceSubstring(@DocFields;_exclude;@Nothing));
 
"{\"@unid\":\""
+@Text(@DocumentUniqueID)+"\","
+ @Implode (
@Transform (
_fld; "_fn" ; "\"" + _fn + "\":\"" + @Text ( @GetField ( _fn) ) + "\"" ) ; "," ) +
"},"
 
=========================== @Formula  END=========================================
 
To retrieve the JSON string, create a new XAgent
 
=========================== XAgent START=========================================
 
<?xml version="1.0" encoding="UTF-8"?>
<xp:view xmlns:xp="http://www.ibm.com/xsp/core" rendered="false">
  <xp:this.afterRenderResponse><![CDATA[#{javascript:try{
  var externalContext = facesContext.getExternalContext();
  var writer = facesContext.getResponseWriter();
  var response = externalContext.getResponse();
     
  // Set content type 
  response.setContentType("application/json");   
  response.setHeader("Cache-Control", "no-cache");
  var json:java.lang.StringBuilder = new java.lang.StringBuilder();
  var v:NotesView = database.getView("($jsonContact)");
  //do not do AutoUpdates
  v.AutoUpdate = false;
  var nav:NotesViewNavigator = v.createViewNav();
  nav.setEntryOptions(
  NotesViewNavigator.VN_ENTRYOPT_NOCOUNTDATA);
  //enable cache for max buffering
  nav.BufferMaxEntries = 400
  var entry:NotesViewEntry = nav.getFirst();
 
  while (entry != null) {
    json.append( entry.getColumnValues().elementAt(0).toString());
    var tmpentry:NotesViewEntry = nav.getNext(entry);
    entry.recycle();
    entry = tmpentry;
  }
 
  writer.write('[' + @Left(json.toString(), @Length(json.toString()) - 1) + ']');  
  writer.endDocument();
} catch(e){
  _dump(e); 
}}]]>
  </xp:this.afterRenderResponse>
</xp:view>
 
=========================== XAgent END =========================================
 
The XAgent uses a ViewNavigator for fast retrieval of the column entries. The real magic is the use of java.lang.StringBuilder() to concat the view entries. A stringBuilder is the fastest way to concat a large number of strings. It is faster than just to use string1 + string2 
 
UPDATE:
 
Here is an enhancement, if you want to fetch the column values with Java
 
public class ViewColumn {
  private static final String MSG_STRING_ERROR = "ERROR: ";
  private static final String MSG_STRING_NOT_FOUND = " not found";
 
  public ViewColumn() {
  }
   
  public String getViewColumnValueJSON(String viewname, int pos) {
    ViewNavigator nav = null;
    StringBuilder json = new StringBuilder();
    json.append('[');
    String strValue = "";
    try {
      View view = getCurrentDatabase().getView(viewname);
      if (null != view) {
        view.setAutoUpdate(false);
        nav = view.createViewNav();
        nav.setEntryOptions(ViewNavigator.VN_ENTRYOPT_NOCOUNTDATA);
        nav.setBufferMaxEntries(400);
        ViewEntry entry = nav.getFirst();
 
        while (entry != null) {
          json.append( entry.getColumnValues().elementAt(pos).toString());
          ViewEntry tmpentry= nav.getNext(entry);
          entry.recycle();
          entry = tmpentry;
        }   
        strValue = json.toString();
        strValue = strValue.substring(0, strValue.lastIndexOf(",")) + "]";
        view.setAutoUpdate(true);
      } else {
        System.out.println(MSG_STRING_ERROR + viewname + MSG_STRING_NOT_FOUND);
      }
    } catch (NotesException e) {
      System.out.println(MSG_STRING_ERROR);
      strValue = "[{}]";
    }
     
    return strValue;
 
  }
}




JavaScript (Server)
katoman
August 19, 2015 at 3:26 PM
Rating
0





No comments yetLogin first to comment...