1
votes

I need a value from my application scope managed bean in my session scope managed bean. Not sure how to get this done. Saw a poste here: https://guedebyte.wordpress.com/2012/05/19/accessing-beans-from-java-code-in-xpages-learned-by-reading-the-sourcecode-of-the-extensionlibrary/ But I get a bunch of errors... I also found this: JSF 2.0 Accessing Application Scope bean from another Bean so IM thinking maybe I need to redefine my application bean??? Totally clueless...

How can I make that happen?

Here is the application scope bean's code:

public class AppConfig implements Serializable { 

    private static final long serialVersionUID = 2768250939591274442L;

    public AppConfig() {
        initDefaults();
        initFromConfigDoc();
    }

    // Control the number of entries displayed in the widgets
    private int nbWidgetFavorites = 0;
    private int nbWidgetMostPopular = 0;
    private int nbWidgetToolbox = 0;
    // Control the number of entries to display in the What's new view
    private int nbWhatsNew = 0;
    private String showDetailsWhatsNew = "no";
    //controls various search options
    private int nbSearchResults = 0;
    private int nbMaxSearchResults = 0;
    //the home page to use for each language
    private String homePageUNID_FR = "";
    private String homePageUNID_EN = "";
    //application email address to use (webmaster)
    //  DEV ADDRESS
    private String appEmailAddress = "DEVTEAMTEST/DEV@DEVELOPMENTCORP";
    //  UAT ADDRESS
    //  PROD ADDRESS
    //path to the stats DB
    private String statsDB = "";
    //application message, if needed
    private String systemMessageFR = "";
    private String systemMessageEN = "";
    //default lang (defined here as session bean will read from the App bean first to
    //  see if there's a value stored there)
    private String defaultLang = "";
    //default prov
    private String defaultProv = "";
    // show Province drop down?
    private String showProv = "no";
    //various text for "share this link" emails
    private String senderEmail = "";
    private String senderName = "";
    private String appURL = "";
    private String emailText = "";
    private String clickLinkText = "";
    private String emailFooter = "";
    private String messageIntro = "";
    private String allowRatingModification = "";




    /*****************************************************************************/

    private void initDefaults() {
        // Control the number of entries displayed in the widgets
        nbWidgetFavorites = 10;
        nbWidgetMostPopular = 10;
        nbWidgetToolbox = 10;
        nbWhatsNew = 15;
        showDetailsWhatsNew = "no";
        nbSearchResults = 25;
        nbMaxSearchResults = 100;
        homePageUNID_FR = "";
        homePageUNID_EN = "";
        appEmailAddress = "DEVTEAMTEST/DEV@DEVELOPMENTCORP";
        statsDB = "belair\\xBiblioStats.nsf";
        systemMessageFR = "";
        systemMessageEN = "";
        defaultLang = "FR";
        defaultProv = "QC";
        showProv = "no";
        allowRatingModification = "1";
    }

    /*****************************************************************************/

    public boolean persistToConfigDoc() {
        //write content of sessionScope vars to config doc

        try {
            Database db = ExtLibUtil.getCurrentSession().getCurrentDatabase();
            View view = db.getView("AppConfig");
            Document doc = view.getFirstDocument();

            if(doc == null) {
                doc = db.createDocument();
                doc.replaceItemValue("form", "AppConfig");
            }

            doc.replaceItemValue("nbWidgetFavorites", this.nbWidgetFavorites);
            doc.replaceItemValue("nbWidgetMostPopular", this.nbWidgetMostPopular);
            doc.replaceItemValue("nbWidgetToolbox", this.nbWidgetToolbox);
            doc.replaceItemValue("nbWidgetToolbox", this.nbWidgetToolbox);
            doc.replaceItemValue("nbWhatsNew", this.nbWhatsNew);
            doc.replaceItemValue("showDetailsWhatsNew", this.showDetailsWhatsNew);
            doc.replaceItemValue("nbSearchResults", this.nbSearchResults);
            doc.replaceItemValue("nbMaxSearchResults", this.nbMaxSearchResults);
            doc.replaceItemValue("homePageUNID_FR", this.homePageUNID_FR);
            doc.replaceItemValue("homePageUNID_EN", this.homePageUNID_EN);
            doc.replaceItemValue("appEmailAddress", this.appEmailAddress);
            doc.replaceItemValue("statsDB", this.statsDB);
            doc.replaceItemValue("systemMessageFR", this.systemMessageFR);
            doc.replaceItemValue("systemMessageEN", this.systemMessageEN);
            doc.replaceItemValue("defaultLang", this.defaultLang);
            doc.replaceItemValue("defaultProv", this.defaultProv);
            doc.replaceItemValue("showProv", this.showProv);
            doc.replaceItemValue("senderEmail", this.senderEmail);
            doc.replaceItemValue("senderName", this.senderName);
            doc.replaceItemValue("appURL", this.appURL);
            doc.replaceItemValue("emailText", this.emailText);
            doc.replaceItemValue("clickLinkText", this.clickLinkText);
            doc.replaceItemValue("emailFooter", this.emailFooter);
            doc.replaceItemValue("messageIntro", this.messageIntro);
            doc.replaceItemValue("allowRatingModification", this.allowRatingModification);

            doc.save();
            return true;

        } catch (NotesException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return false;
        }
    }

    /*****************************************************************************/

    public void initFromConfigDoc() {
        try {
            Database db = ExtLibUtil.getCurrentSession().getCurrentDatabase();
            View view = db.getView("AppConfig");
            Document doc = view.getFirstDocument();

            if(doc != null) {
                //load default values
                if(doc.hasItem("nbWidgetFavorites")) {
                    int tmp = doc.getItemValueInteger("nbWidgetFavorites"); 
                    if(tmp > 0) {
                        this.nbWidgetFavorites = tmp;
                    }
                }
                if(doc.hasItem("nbWidgetMostPopular")) {
                    int tmp = doc.getItemValueInteger("nbWidgetMostPopular"); 
                    if(tmp > 0) {
                        this.nbWidgetMostPopular = tmp;
                    }
                }
                if(doc.hasItem("nbWidgetToolbox")) {
                    int tmp = doc.getItemValueInteger("nbWidgetToolbox"); 
                    if(tmp > 0) {
                        this.nbWidgetToolbox = tmp;
                    }
                }
                if(doc.hasItem("nbWhatsNew")) {
                    int tmp = doc.getItemValueInteger("nbWhatsNew"); 
                    if(tmp > 0) {
                        this.nbWhatsNew = tmp;
                    }
                }
                if(doc.hasItem("showDetailsWhatsNew")) {
                    String tmp = doc.getItemValueString("showDetailsWhatsNew"); 
                    this.showDetailsWhatsNew = tmp;
                }
                if(doc.hasItem("nbSearchResults")) {
                    int tmp = doc.getItemValueInteger("nbSearchResults"); 
                    if(tmp > 0) {
                        this.nbSearchResults = tmp;
                    }
                }
                if(doc.hasItem("nbMaxSearchResults")) {
                    int tmp = doc.getItemValueInteger("nbMaxSearchResults"); 
                    if(tmp > 0) {
                        this.nbMaxSearchResults = tmp;
                    }
                }
                if(doc.hasItem("homePageUNID_FR")) {
                    String tmp = doc.getItemValueString("homePageUNID_FR"); 
                    if(!"".equals(tmp)) {
                        this.homePageUNID_FR = tmp;
                    }
                }
                if(doc.hasItem("homePageUNID_EN")) {
                    String tmp = doc.getItemValueString("homePageUNID_EN"); 
                    if(!"".equals(tmp)) {
                        this.homePageUNID_EN = tmp;
                    }
                }
                if(doc.hasItem("appEmailAddress")) {
                    String tmp = doc.getItemValueString("appEmailAddress"); 
                    if(!"".equals(tmp)) {
                        this.appEmailAddress = tmp;
                    }
                }
                if(doc.hasItem("statsDB")) {
                    String tmp = doc.getItemValueString("statsDB"); 
                    if(!"".equals(tmp)) {
                        this.statsDB = tmp;
                    }
                }
                if(doc.hasItem("systemMessageFR")) {
                    String tmp = doc.getItemValueString("systemMessageFR"); 
                    if(!"".equals(tmp)) {
                        this.systemMessageFR = tmp;
                    }
                }
                if(doc.hasItem("systemMessageEN")) {
                    String tmp = doc.getItemValueString("systemMessageEN"); 
                    if(!"".equals(tmp)) {
                        this.systemMessageEN = tmp;
                    }
                }
                if(doc.hasItem("defaultLang")) {
                    String tmp = doc.getItemValueString("defaultLang"); 
                    if(!"".equals(tmp)) {
                        this.defaultLang = tmp;
                    }
                }
                if(doc.hasItem("defaultProv")) {
                    String tmp = doc.getItemValueString("defaultProv"); 
                    if(!"".equals(tmp)) {
                        this.defaultProv = tmp;
                    }
                }
                if(doc.hasItem("showProv")) {
                    String tmp = doc.getItemValueString("showProv"); 
                    if(!"".equals(tmp)) {
                        this.showProv = tmp;
                    }
                }
                if(doc.hasItem("senderEmail")) {
                    String tmp = doc.getItemValueString("senderEmail"); 
                    if(!"".equals(tmp)) {
                        this.senderEmail = tmp;
                    }
                }
                if(doc.hasItem("senderName")) {
                    String tmp = doc.getItemValueString("senderName"); 
                    if(!"".equals(tmp)) {
                        this.senderName = tmp;
                    }
                }
                if(doc.hasItem("appURL")) {
                    String tmp = doc.getItemValueString("appURL"); 
                    if(!"".equals(tmp)) {
                        this.appURL = tmp;
                    }
                }
                if(doc.hasItem("emailText")) {
                    String tmp = doc.getItemValueString("emailText"); 
                    if(!"".equals(tmp)) {
                        this.emailText = tmp;
                    }
                }
                if(doc.hasItem("clickLinkText")) {
                    String tmp = doc.getItemValueString("clickLinkText"); 
                    if(!"".equals(tmp)) {
                        this.clickLinkText = tmp;
                    }
                }
                if(doc.hasItem("emailFooter")) {
                    String tmp = doc.getItemValueString("emailFooter"); 
                    if(!"".equals(tmp)) {
                        this.emailFooter = tmp;
                    }
                }
                if(doc.hasItem("messageIntro")) {
                    String tmp = doc.getItemValueString("messageIntro"); 
                    if(!"".equals(tmp)) {
                        this.messageIntro = tmp;
                    }
                }
                //allowRatingModification
                if(doc.hasItem("allowRatingModification")) {
                    String tmp = doc.getItemValueString("allowRatingModification"); 
                    if(!"".equals(tmp)) {
                        this.allowRatingModification = tmp;
                    }
                }

            }

        } catch (NotesException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    /*****************************************************************************/

    public int getNbWidgetFavorites() {
        return nbWidgetFavorites;
    }
    public void setNbWidgetFavorites(int nbWidgetFavorites) {
        this.nbWidgetFavorites = nbWidgetFavorites;
    }
    public int getNbWidgetMostPopular() {
        return nbWidgetMostPopular;
    }
    public void setNbWidgetMostPopular(int nbWidgetMostPopular) {
        this.nbWidgetMostPopular = nbWidgetMostPopular;
    }
    public int getNbWidgetToolbox() {
        return nbWidgetToolbox;
    }
    public void setNbWidgetToolbox(int nbWidgetToolbox) {
        this.nbWidgetToolbox = nbWidgetToolbox;
    }
    public void setNbWhatsNew(int nbWhatsNew) {
        this.nbWhatsNew = nbWhatsNew;
    }
    public int getNbWhatsNew() {
        return nbWhatsNew;
    }
    public void setShowDetailsWhatsNew(String showDetailsWhatsNew) {
        this.showDetailsWhatsNew = showDetailsWhatsNew;
    }

    public String getShowDetailsWhatsNew() {
        return showDetailsWhatsNew;
    }

    public int getNbSearchResults() {
        return nbSearchResults;
    }
    public void setNbSearchResults(int nbSearchResults) {
        this.nbSearchResults = nbSearchResults;
    }
    public int getNbMaxSearchResults() {
        return nbMaxSearchResults;
    }
    public void setNbMaxSearchResults(int nbMaxSearchResults) {
        this.nbMaxSearchResults = nbMaxSearchResults;
    }
    public String getHomePageUNID_FR() {
        return homePageUNID_FR;
    }
    public void setHomePageUNID_FR(String homePageUNID_FR) {
        this.homePageUNID_FR = homePageUNID_FR;
    }
    public String getHomePageUNID_EN() {
        return homePageUNID_EN;
    }
    public void setHomePageUNID_EN(String homePageUNID_EN) {
        this.homePageUNID_EN = homePageUNID_EN;
    }
    public String getAppEmailAddress() {
        return appEmailAddress;
    }
    public void setAppEmailAddress(String appEmailAddress) {
        this.appEmailAddress = appEmailAddress;
    }
    public String getSystemMessageFR() {
        return systemMessageFR;
    }
    public void setSystemMessageFR(String systemMessageFR) {
        this.systemMessageFR = systemMessageFR;
    }
    public String getSystemMessageEN() {
        return systemMessageEN;
    }
    public void setSystemMessageEN(String systemMessageEN) {
        this.systemMessageEN = systemMessageEN;
    }
    public void setStatsDB(String statsDB) {
        this.statsDB = statsDB;
    }
    public String getStatsDB() {
        return statsDB;
    }
    public void setDefaultLang(String defaultLang) {
        this.defaultLang = defaultLang;
    }
    public String getDefaultProv() {
        return defaultProv;
    }

    public void setDefaultProv(String defaultPRov) {
        this.defaultProv = defaultPRov;
    }

    public void setShowProv(String showProv) {
        this.showProv = showProv;
    }

    public String getShowProv() {
        return showProv;
    }

    public String getDefaultLang() {
        return defaultLang;
    }

    public String getMessageIntro() {
        return messageIntro;
    }

    public void setMessageIntro(String messageIntro) {
        this.messageIntro = messageIntro;
    }

    public String getSenderEmail() {
        return senderEmail;
    }

    public void setSenderEmail(String senderEmail) {
        this.senderEmail = senderEmail;
    }

    public String getSenderName() {
        return senderName;
    }

    public void setSenderName(String senderName) {
        this.senderName = senderName;
    }

    public String getAppURL() {
        return appURL;
    }

    public void setAppURL(String appURL) {
        this.appURL = appURL;
    }

    public String getEmailText() {
        return emailText;
    }

    public void setEmailText(String emailText) {
        this.emailText = emailText;
    }

    public String getClickLinkText() {
        return clickLinkText;
    }

    public void setClickLinkText(String clickLinkText) {
        this.clickLinkText = clickLinkText;
    }

    public String getEmailFooter() {
        return emailFooter;
    }

    public void setEmailFooter(String emailFooter) {
        this.emailFooter = emailFooter;
    }

    //allowRatingModification
    public String getAllowRatingModification() {
        return allowRatingModification;
    }

    public void setAllowRatingModification(String allowRatingModification) {
        this.allowRatingModification = allowRatingModification;
    }
}
2

2 Answers

7
votes

The VariableResolver goes through all implicit variables (e.g. session, database) as well as scoped variables (e.g. applicationScope.myVar). Your bean is also accessed from SSJS via the VariableResolver.

So you can use:

ExtLibUtil.resolveVariable(FacesContext.getCurrentInstance(), "myAppScopeBean");
2
votes

This is not a direct answer to your question...

An alternative would be to set the value you want in applicationScope and then access it this way from your bean. To access the entire bean directly is a different answer.

You use this code get a handle to your applicationScope.

FacesContext facesContext = FacesContext.getCurrentInstance();
ExternalContext externalContext = facesContext.getExternalContext();
Map<String, Object> applicationScope = externalContext.getApplicationMap();

Then to use this you have code like this:

String agentLogDB = (String) applicationScope.get("LOGDB");