2

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?

Per Henrik Lausten
  • 21,331
  • 3
  • 29
  • 76
Malin
  • 697
  • 5
  • 21

1 Answers1

2

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
stwissel
  • 20,110
  • 6
  • 54
  • 101
  • Thank you Stephan, this morning on my walk to work I was thinking about storing the internal variables as string in the bean. Thank you for confirming my thoughts! – Malin Oct 22 '18 at 06:36
  • 2
    If it's a global config document then I would recommend to use application scope instead of session scope – Per Henrik Lausten Oct 22 '18 at 06:46
  • Do not use applicationScope. You will need to restart HTTP task (or similar method) to refresh values if your config document changes. viewScope is the best option, IMO. – Frantisek Kossuth Oct 22 '18 at 13:04
  • 2
    Or have a refresh() method you expose that your admin page calls after saving the config document – stwissel Oct 22 '18 at 13:06
  • 1
    Agree with Stephan Wissel. With application scope you of course need to maintain caching and refreshing of the cache but it improves performance instead of using view scope. – Per Henrik Lausten Oct 22 '18 at 13:23