Search code examples
xpagesmanaged-beanxpages-ssjs

Trying to compute a method to call in a Bean


I have a managed bean that returns a number of different properties that describe an application. So it can be called to return the FilePath of a database by calling

appProps[sessionScope.ssApplication].helpFilePath

or

appProps[sessionScope.ssApplication].ruleFilePath

I'm trying to work out a generalized case where I need to call for the file path based on a value in a compositeData variable which can take on any one of 4 different values help/rule/app/main.

I wrote this SSJS and it works but I am wondering if there is a better way to make it work:

var target:String = compositeData.DBSource;
switch (target){
    case "app" :
        return appProps[sessionScope.ssApplication].appFilePath;
        break;
    case "help" :
        return appProps[sessionScope.ssApplication].helpFilePath;
        break;
    case "rule" :
        return appProps[sessionScope.ssApplication].ruleFilePath;
        break;
    case "main" :
        return appProps[sessionScope.ssApplication].mainFilePath;
        break;

}

I can't figure out if there is a way to compute the method by using compositeData.DBSource + "FilePath" . when I try this I get an error that the method does not exist. Using the SSJS code above is not really a problem but it just seems a bit redundant.


Solution

  • You can make a new method in your managed bean that takes target as an argument:

    public String getFilePath(String target) {
        String returnValue = "";
        if (target.equalsIgnoreCase("app")) {
            returnValue = this.appFilePath;
        } else if (target.equalsIgnoreCase("help")) {
            returnValue = this.helpFilePath;
        } else if (target.equalsIgnoreCase("rule")) {
            returnValue = this.ruleFilePath;
        } else if (target.equalsIgnoreCase("main")) {
            returnValue = this.mainFilePath;
        }
    
        return returnValue;
    }
    

    And then call it like this in your SSJS:

    appProps[sessionScope.ssApplication].getFilePath(compositeData.DBSource);