3 XPages hints #2

3 XPages hints #2

I wrote the first post for some XPages tipps several months ago (5 XPages hints #1). Actually i planned to do it as a series, but soon got very involved into customer projects so i did not have the time to describe 5 tipps in detail, so i downsized it to 3:-) Anyway, i hope they will help ..

1. Getting the session as signer

Sometimes you need to get document information (e.g. for a website) with higher access rights, than the logged-in user (or “Anonymous”). For this case, you can get a session as the signer of the design element(s). In the traditional Domino Web Development world, this would be the agent property “run as webuser” disabled. Here is the code for it (normally placed as a static method):

public static Session getSessionAsSigner() {
	Session sessionAsSigner = null;
	try {
		FacesContext context = JSFUtil.getFacesContext();
		sessionAsSigner = (Session) context.getApplication()
				.getVariableResolver().resolveVariable(context,
					"sessionAsSigner");
	} catch (Exception e) {
		Util.logError(e);
	}
	return sessionAsSigner;
}

Danger
You have to sign all the according design elements with the same ID, otherwise you may get some unexpected results.

 

2. Getting results of a called agent from an XPage

We had a pretty old Domino Application (R6.5), for which we had to renew the Web Browser UI, so we decided to do it with XPages. Since many of the business logic is already written in Lotusscript Agents and Script Libraries, we tried to use the existing (and tested) code as much as possible. Starting a Lotusscript agent from an XPage is easy, but to know what this agent was actually doing or getting the results of that agent is a little bit more tricky. For example you want to know how many documents the agent changed and display it in the XPages user interface. In our case we had to validate a reservation document with an existing lotusscript function, so when validation fails, the document is not saved.. For techniques like this, the following technique comes in handy:

–> We actually pass the “in memory” document to the agent. This can be an existing, unsaved document, or a completely new one..

//getting the agent
Agent agent = app.getDb().getAgent("(AGENTNAME)");
//start the agent with the document as parameter
agent.runWithDocumentContext(doc);

//the agent writes his results into a field names "tmpReturnValue"
//in this case writes "1", if the validation succeeded.. if the agent failed, or the values of the documents are not valid, then the validation turns out false..
String agentResult = doc.getItemValueString("tmpReturnValue");

if (agentResult.equals("1")) {
	result = true;
} else {
	result = false;
}
//we don't need the result of the agent to be saved in the doc, so we remove the item..
doc.removeItem("tmpReturnValue");
  • doc: This is the Document Object, resolved with the current datasources backend document. This could be also a newly created document from scratch within this method..
  • app.getDb(): app is my session bean, which i use to get DBs, Views, etc.

In the Lotusscript agent you can get the document and validate the entered values like this:

Dim s As New NotesSession
Dim doc as notesdocument
Set doc = s.Documentcontext

if validateSomething(doc) then
	Call doc.Replaceitemvalue("tmpReturnValue", "1")
end if

 

3. Creating an ICS Calendar feed

We had a request to create an ICS Calendar feed to download. We decided to use biweekly (http://sourceforge.net/projects/biweekly/) and are pretty happy with it. First we need a managed bean from which we can call a function like this:

public String getIcsFeed(){
String result = "";
try {

ICalendar ical = new ICalendar();

ical.setProductId("-//My Feed");
VEvent event = new VEvent();
event.setSummary("Hungergames");
event.setLocation("TheArena, 860345 Capitol");
event.setOrganizer("Capitol");
event.setDateStart(new Date());
event.setDateEnd(new Date());
ical.addEvent(event);

result = ical.write();

} catch (Exception e) {
Util.logError(e);
}
return result;
}

In a real life situation, you would loop through some events, and then add the events to the ical object..

You can then call this function in an XPage like this:

<?xml version="1.0" encoding="UTF-8"?>
<xp:view xmlns:xp="http://www.ibm.com/xsp/core" rendered="false">
<xp:this.afterRenderResponse><![CDATA[#{javascript:var externalContext = facesContext.getExternalContext();
var writer = facesContext.getResponseWriter();
var response = externalContext.getResponse();
response.setContentType("text/ics");
response.setHeader("Cache-Control", "no-cache");
writer.write(managedBean.getIcsFeed());
writer.endDocument();}]]></xp:this.afterRenderResponse>
</xp:view>

Hope this tipps can help you..

Leave a Reply

comment-avatar

*