Reuse web agents that use PRINT - Bean edition
Using Java in XPages does have performance advantages especially when you can use EL instead of SSJS. So I created the Java version of the reuse web agents post. You need to copy the Java either into your NSF or include it in your own extension library. Calling an agent is limited to the same database as the XPage resides in, I leave it to an exercise for my readers to improve on that.
You define the Java class as a managed bean:
You define the Java class as a managed bean:
<managed-bean>
<managed-bean-name>agentHelper </managed-bean-name>
<managed-bean-class>com.notessensei.xsp.utilities.LsAgentHelper </managed-bean-class>
<managed-bean-scope>request </managed-bean-scope>
</managed-bean>
and then simply include (e.g. in the value of a computed field) <managed-bean-name>agentHelper </managed-bean-name>
<managed-bean-class>com.notessensei.xsp.utilities.LsAgentHelper </managed-bean-class>
<managed-bean-scope>request </managed-bean-scope>
</managed-bean>
${agentHelper.agentResult("SampleAgent")}
as EL entry. You have to be careful to ensure your agent doesn't run twice. The agent side adjustments (the LotusScript stuff) is the same as for the SSJS version. The Java version is slightly more flexible since it allows String or Stream results and acting on a new or existing document.
package com.notessensei.xsp.utilities ;
import java.io.IOException ;
import java.io.OutputStream ;
import java.io.OutputStreamWriter ;
import java.io.Writer ;
import javax.faces.context.FacesContext ;
import lotus.domino.Agent ;
import lotus.domino.Database ;
import lotus.domino.Document ;
import lotus.domino.MIMEEntity ;
import lotus.domino.NotesException ;
/**
* @author Stephan H. Wissel
*
* The agentHelper is a managed bean that allows to
* run a LotusScript agent from an XPage and get the result back. It has
* the ability to run with a given document or with a temporary in
* memory document as parameter
*/
public class LsAgentHelper {
private String resultFieldName ;
private Database nsf = null ;
public LsAgentHelper ( ) {
resultFieldName = "AgentPrintOutputResult" ;
}
public String agentResult ( String agentName ) throws NotesException {
Document doc = this. getEmptyDocument ( ) ;
String result = this. agentResult (agentName, doc ) ;
doc. recycle ( ) ;
return result ;
}
public String agentResult ( String agentName, Document doc )
throws NotesException {
String finalResult ;
Agent theAgent = this. getDatabase ( ). getAgent (agentName ) ;
if (theAgent == null ) {
finalResult = "Agent " + agentName + " not found in "
+ this. getDatabase ( ). getFilePath ( ) ;
} else {
theAgent. runWithDocumentContext (doc ) ;
MIMEEntity result = doc. getMIMEEntity ( this. getResultFieldName ( ) ) ;
if (result != null ) {
finalResult = result. getContentAsText ( ) ;
result. recycle ( ) ;
} else {
finalResult = "No result returned in field " + this. getResultFieldName ( ) + " for agent " +agentName ;
}
theAgent. recycle ( ) ;
}
return finalResult ;
}
public void agentResultToStream ( String agentName, OutputStream out )
throws NotesException, IOException {
Document doc = this. getEmptyDocument ( ) ;
this. agentResultToStream (agentName, doc, out ) ;
doc. recycle ( ) ;
}
public String getResultFieldName ( ) {
return resultFieldName ;
}
public void setResultFieldName ( String resultFieldName ) {
this. resultFieldName = resultFieldName ;
}
public void agentResultToStream ( String agentName, Document doc,
OutputStream out ) throws NotesException, IOException {
Writer w = new OutputStreamWriter (out ) ;
w. write ( this. agentResult (agentName, doc ) ) ;
w. close ( ) ;
}
private Document getEmptyDocument ( ) throws NotesException {
return this. getDatabase ( ). createDocument ( ) ;
}
private Database getDatabase ( ) {
if ( this. nsf == null ) {
FacesContext fc = FacesContext. getCurrentInstance ( ) ;
this. nsf = (Database ) fc. getApplication ( ). getVariableResolver ( ). resolveVariable (fc, "database" ) ;
}
return this. nsf ;
}
}
As usual: YMMVimport java.io.IOException ;
import java.io.OutputStream ;
import java.io.OutputStreamWriter ;
import java.io.Writer ;
import javax.faces.context.FacesContext ;
import lotus.domino.Agent ;
import lotus.domino.Database ;
import lotus.domino.Document ;
import lotus.domino.MIMEEntity ;
import lotus.domino.NotesException ;
/**
* @author Stephan H. Wissel
*
* The agentHelper is a managed bean that allows to
* run a LotusScript agent from an XPage and get the result back. It has
* the ability to run with a given document or with a temporary in
* memory document as parameter
*/
public class LsAgentHelper {
private String resultFieldName ;
private Database nsf = null ;
public LsAgentHelper ( ) {
resultFieldName = "AgentPrintOutputResult" ;
}
public String agentResult ( String agentName ) throws NotesException {
Document doc = this. getEmptyDocument ( ) ;
String result = this. agentResult (agentName, doc ) ;
doc. recycle ( ) ;
return result ;
}
public String agentResult ( String agentName, Document doc )
throws NotesException {
String finalResult ;
Agent theAgent = this. getDatabase ( ). getAgent (agentName ) ;
if (theAgent == null ) {
finalResult = "Agent " + agentName + " not found in "
+ this. getDatabase ( ). getFilePath ( ) ;
} else {
theAgent. runWithDocumentContext (doc ) ;
MIMEEntity result = doc. getMIMEEntity ( this. getResultFieldName ( ) ) ;
if (result != null ) {
finalResult = result. getContentAsText ( ) ;
result. recycle ( ) ;
} else {
finalResult = "No result returned in field " + this. getResultFieldName ( ) + " for agent " +agentName ;
}
theAgent. recycle ( ) ;
}
return finalResult ;
}
public void agentResultToStream ( String agentName, OutputStream out )
throws NotesException, IOException {
Document doc = this. getEmptyDocument ( ) ;
this. agentResultToStream (agentName, doc, out ) ;
doc. recycle ( ) ;
}
public String getResultFieldName ( ) {
return resultFieldName ;
}
public void setResultFieldName ( String resultFieldName ) {
this. resultFieldName = resultFieldName ;
}
public void agentResultToStream ( String agentName, Document doc,
OutputStream out ) throws NotesException, IOException {
Writer w = new OutputStreamWriter (out ) ;
w. write ( this. agentResult (agentName, doc ) ) ;
w. close ( ) ;
}
private Document getEmptyDocument ( ) throws NotesException {
return this. getDatabase ( ). createDocument ( ) ;
}
private Database getDatabase ( ) {
if ( this. nsf == null ) {
FacesContext fc = FacesContext. getCurrentInstance ( ) ;
this. nsf = (Database ) fc. getApplication ( ). getVariableResolver ( ). resolveVariable (fc, "database" ) ;
}
return this. nsf ;
}
}
Posted by Stephan H Wissel on 03 April 2012 | Comments (0) | categories: XPages