I have the following Java bean code that stores a list of tabs in an ArrayList<String> in a sessionScope, so I do not have to look it up for every request.
public class BeanMethods {
private static final long serialVersionUID = 1L;
public Database db;
public ArrayList<String> userTabs;
public BeanMethods() {
db = ExtLibUtil.getCurrentDatabase();
userTabs = new ArrayList<String>();
}
public ArrayList<String> getUserTabs(String uid) throws NotesException {
if (userTabs.isEmpty()) {
userTabs = getUserTabs2(uid);
}
return userTabs;
}
public ArrayList<String> getUserTabs2(String uid) throws NotesException {
db = ExtLibUtil.getCurrentDatabase();
String key = "TAB_UID_" + uid;
DocumentCollection dc = db.getView("LookupKey").getAllDocumentsByKey(key, true);
Document d = dc.getFirstDocument();
while (d != null) {
String tabTitle = d.getItemValueString("Title");
userTabs.add(tabTitle + "|" + d.getItemValueString("UniqueId"));
d = dc.getNextDocument(d);
}
return userTabs;
}
}
I have a few question about this
- How can I update user tabs property at runtime?
- I have noticed that in some or all cases the db variable that is set in the constructor is not maintained, looks like I need to set it in each method? am I missing something here?
- Is there a better way to write this code?
You can’t store any Notes object (those that have a
recycle()method in the session or application scope. No session, database, view, document or RichText object.Plain Java objects are ok. So you can store db.filePath or replicaId. For documents the unid.
So remove db from the constructor. You could add db to the method parameters.
You can write a setUserTabs method. It would need the user and a collection as parameters. Something like
The hassle then is to load all user documents and check if they are still in the collection or are there new ones.
Roughly along these lines. Be aware you can't add/remove to a collection from inside a forEach loop.
Finally in your loop you are missing doc.recycle();