Search code examples
javaxpagesxpages-ssjs

Setting Document as global property in java class, not working


In my Xpages application I have a java class in which I want to set a Document as global property and re-use across my methods. The Document represents a Notes configuration document and I want to perform the lookup only once. Unfortunately it is not working as expected. Perhaps someone can guide me to the proper process?

First I have set up a managed bean:

<managed-bean>
    <managed-bean-name>emplDataMining</managed-bean-name>
    <managed-bean-class>se.bank.employeeApp.utils.EmployeeDataMining</managed-bean-class>
    <managed-bean-scope>view</managed-bean-scope>
</managed-bean>

My class contains several methods who will go to different systems. All the URL's to the system are stored in a Notes configuration document, which I want to load only once and re-use across these methods

public class EmployeeDataMining implements Serializable{

    private static final long serialVersionUID = 1L;
    private Document configuration;

    //constructor class. not so very special, so I wont post it

    public void getConfiguration(){
        //setting up database and view
        //only 1 document stored in the view so I can hard-code the reference
        configuration = vw.getDocumentByKey("ConfigDocument", true);
        //... rest of code e.g. setting up httpclient, JSONobj
    }

    public void collectDataFromSystemX(CloseableHttpClient httpclient, Employee employee, JSONObject JSONobj){
        //I wont post all of my code  
        HttpPost httpPost = new HttpPost(this.configuration.getItemValueString("urlSystemX"));
        //this.configuration is null :-?
        //..rest of code
    }

    public void collectDataFromSystemY(CloseableHttpClient httpclient, Employee employee, JSONObject JSONobj){
        //I wont post all of my code  
        HttpPost httpPost = new HttpPost(this.configuration.getItemValueString("urlSystemY"));
        //this.configuration is null :-?
        //..rest of code
    }

}

My code is initiated from SSJS:

emplDataMining.getConfiguration(); 
emplDataMining.collectDataFromSystemX(//passing in the variables which are setup in getConfiguration method)

So my main concern is that the profile Document is not properly set or exchanged between the methods.

Can someone tell me what I have overlooked?


Solution

  • There are 2 issues:

    • being view scoped the bean gets reloaded for every document you open. If it is a config document you want to use session scope
    • You can’t store Notes objects in managed beans (request scope might work). What you do instead: in your bean’s constructor, load the document and extract the field values into bean internal variables (Strings, Lists etc). That will give you what you want