Application Configuration / Parameters in XPages
Any application of reasonable size has parameters that configure it. Be it the salutations, the names of departments or the task priorities. In classic Notes and Domino application you typically find @DBColumn, @DBLookup or @GetProfileField to retrieve these values. Short of @GetProfileField you could do the same in an XPages application. However I would advocate a different approach. These are the steps:
As usual YMMV.
Sample parameter library
<> : Updated the sample a little since writing back of the cache was missing and a java.util.Hashmap seems to be more useful than {}
<>
- Create a SSJS script library. Call it "ApplicationConfiguration[ForNameOfYourApp[Group ] ] "
- Create one JavaScript object (see source below), call it "[ NameOfApp ]Config" (e.g companyConfig)
- Create inside this object functions that start with get and the Parameter you want to retrieve (e.g. getDepartments)
- In your application you then include that library and simply use the object with its function (e.g.
companyConfig.getDepartments()
)
getLocations()
where one retrieves sales locations and the other manufacturing locations) that clash. The object name will help to distinguish them. Furthermore the call to the function is independent from the actual implementation. Doing a @DBLookup/@DBColumn for rather static values might not make a lot of sense, so some caching might make sense. Instead of building that into every page you can leave that to the function -- adding caching or reading config values differently will not change its signature (signature is the fancy way of saying: how it is called and what it returns). There is an additional reason why you want to stick to the " getParameterName()
" syntax. It will allow you, if you want to, to replace your SSJS object with a managed bean that allows you more flexible options how to retrieve and cache your parameters. With a managed bean you also could use parameters directly in XPages' expression language (EL). You gain the option instead of writing #{JavaScript:companyConfig.getDepartments()}
the more compact and understandable EL syntax #companyConfig.departments}
. As said: only if you want to. Sticking to that syntax i SSJS doesn't have, as far as I can see, any substantial drawback. Planning for that keeps your options open.
As usual YMMV.
Sample parameter library
var companyConfig = {
getDepartments : function ( ) {
var cache = this. getCacheObject ( ) ;
var result = cache. get ( "departments" ) ;
if (result == null ) {
// Here is where you would do @DBSomething or worse
result = @DbLookup ( "configDB.nsf" , "configView" , "departments" , 2 ) ;
cache. put ( "departments" ,result ) ; //Cache it back
sessionScope. put ( "companyConfig" ,cache ) ; //Put the variable back in the scope
}
return result ;
} ,
/*Traffic light colors don't need to be cached*/
getColors : function ( ) {
return [ "red" , "yellow" , "green" ] ;
} ,
/*Here would be much more of these functions*/
/*Utility functions for cache management*/
/*Retrieves the configuration object from a cache store.
There are many ways to do that*/
getCacheObject : function ( ) {
// Consider carefully where to cache. Typical places
// are sessions or applications
var curCache = sessionScope. get ( "companyConfig" ) ;
if (curCache == null ) {
curCache = new java. util. HashMap ( ) ;
sessionScope. put ( "companyConfig" ,curCache ) ;
}
return curCache ;
} ,
/*Resets the cache*/
reset : function ( ) {
var curCache = new java. util. HashMap ( ) ;
sessionScope. put ( "companyConfig" ,curCache ) ;
}
}
getDepartments : function ( ) {
var cache = this. getCacheObject ( ) ;
var result = cache. get ( "departments" ) ;
if (result == null ) {
// Here is where you would do @DBSomething or worse
result = @DbLookup ( "configDB.nsf" , "configView" , "departments" , 2 ) ;
cache. put ( "departments" ,result ) ; //Cache it back
sessionScope. put ( "companyConfig" ,cache ) ; //Put the variable back in the scope
}
return result ;
} ,
/*Traffic light colors don't need to be cached*/
getColors : function ( ) {
return [ "red" , "yellow" , "green" ] ;
} ,
/*Here would be much more of these functions*/
/*Utility functions for cache management*/
/*Retrieves the configuration object from a cache store.
There are many ways to do that*/
getCacheObject : function ( ) {
// Consider carefully where to cache. Typical places
// are sessions or applications
var curCache = sessionScope. get ( "companyConfig" ) ;
if (curCache == null ) {
curCache = new java. util. HashMap ( ) ;
sessionScope. put ( "companyConfig" ,curCache ) ;
}
return curCache ;
} ,
/*Resets the cache*/
reset : function ( ) {
var curCache = new java. util. HashMap ( ) ;
sessionScope. put ( "companyConfig" ,curCache ) ;
}
}
Posted by Stephan H Wissel on 20 September 2010 | Comments (6) | categories: XPages