Add variable to Session in Java (Updated)

    package com.dokoll.solutions.inc.Utils;

/**
 * Created: 2014.02.08.4.02.PM
 * Maintain variable in session, based on NSF record returned
 * load custom error Message(s)
 */

import javax.faces.context.FacesContext;
import lotus.domino.Database;
import lotus.domino.NotesException;
import lotus.domino.Session;
import lotus.domino.View;
import lotus.domino.ViewEntry;
import lotus.domino.ViewEntryCollection;

import com.ibm.xsp.model.domino.DominoUtils;

/**
 * @author Dököll Solutions, Inc.
 * @version 2014.02.08.4.02.PM
 * 
 */
public class TestLoginJavaBean {
	//declare search/err Message variables
	private String UserName;
	private String MyEntries;
	private String message;
	//...
	static String URLACCESS_VIEWNAME = "URLAccessView";

	/**
	 * @return the userName
	 */
	public String getUserName() {
		return UserName;
	}

	/**
	 * @param userName
	 *            the userName to set
	 */
	public void setUserName(String userName) {
		UserName = userName;
	}
	
	/**
	 * @param myEntries
	 *            the myEntries to set
	 */
	public void setMyEntries(String myEntries) {
		MyEntries = myEntries;
	}

	/**
	 * @return the message
	 */
	public String getMessage() {
		return message;
	}

	/**
	 * @param message
	 *            the message to set
	 */
	public void setMessage(String message) {
		this.message = message;
	}

	/**
	 * @return the myEntries
	 */
	public String getMyEntries() {

		return MyEntries;
	}


	public TestLoginJavaBean() {
		// ...
	}

	//button code
	@SuppressWarnings("unchecked")
	public String doLoginUser() {

		// let's add a try catch here, to grab errors near the end
		try {

			// BEGIN DEBUG
			System.out
					.println("BEGIN DEBUG | TestLoginJavaBean.java - doLoginUser started... ");
			Session session = DominoUtils.getCurrentSession(FacesContext
					.getCurrentInstance());

			System.out
					.println("docucontent.nsf | doLoginUser() in TestLoginJavaBean.java Database Session Obtained..."
							+ session);
			// find database in question from current session
			System.out
					.println("docucontent.nsf | doLoginUser() in TestLoginJavaBean.java based on Database Session...");
			Database database = session.getDatabase(session.getServerName(),
					"docucontent.nsf");
			System.out
					.println("doLoginUser() in TestLoginJavaBean.java Fetched Database..."
							+ database);
			System.out.println("Getting a database connection... ");

			System.out
					.println("TestLoginJavaBean.java | doLoginUser Connected to "
							+ database);
			// Find the view in question
			View view = database.getView(URLACCESS_VIEWNAME);
			System.out
					.println("TestLoginJavaBean.java | doLoginUser Connected to "
							+ view);
			// determine database state for searching
			if (database.isFTIndexed())
				database.updateFTIndex(false);
			else
				database.updateFTIndex(true);

			//perform search via ViewEntryCollection
			ViewEntryCollection vec = view.getAllEntriesByKey(UserName, true);
			// ...
			// put the userName variable in session to be picked up through App
			 FacesContext.getCurrentInstance().getExternalContext()
			 .getSessionMap().put("userName", UserName);

			 //at this point we assume, the userID column in back-end (URLACCESS_VIEWNAME) has the value searched
			 //load count for debugging purposes
			 //Note, count can be 0 here, thus err Message(s) should load...
			System.out
					.println("TestLoginJavaBean.java | ViewEntryCollection Counts Obtained...");
			System.out.println("TestLoginJavaBean.java | ViewEntries "
					+ vec.getCount() + " Account(s)");
			System.out
					.println("TestLoginJavaBean.java | ViewEntryCollection completed successfully...");

			ViewEntry tmpentry = null;
			ViewEntry entry = vec.getFirstEntry();

			// ensure UserID field/column returned is not null (record not found) from NSF back-end,
			// if so, load text/err Message(s) to user
			if (entry == null) {

				// add Message value...
				// TODO: Do something else with the current component
				// example, combine with built-in items
				//...
				MyEntries = "Invalid UserID";
				//load to custom errMessage label control
				message = MyEntries.toString();
				
				// load friendlier debug message to log
				// in which case, inputText or Label should load 'Invalid UserID to
				// form
				// TODO: Create set of custom messages to load to Xpages form
				System.out
						.println("TestLoginJavaBean.java | ViewEntryCollection completed with error..."
								+ MyEntries);

				// otherwise load the value needed to let user continue
			} else {

				// add value needed for pairing with session, and so on...
				MyEntries = entry.getDocument().getItemValueString(
						"UserID");
				// DEBUG ONLY:
				// load friendly debug message to log
				// System.out
				// .println("TestLoginJavaBean.java | ViewEntryCollection completed with valid ID..."
				// + MyEntries);
				if (MyEntries.equalsIgnoreCase(UserName)) {
					//grab the session and make available in other pages
					//this should only happen if in fact NSF userID column has value searched via userName component
					UserName = (String) FacesContext.getCurrentInstance()
					 .getExternalContext().getSessionMap().get("userName");
					 System.out.println("Session obtained from Bean... "
					 + UserName);
					tmpentry = vec.getNextEntry();
					entry.recycle();
					entry = tmpentry;
					
					System.out
							.println("END DEBUG | TestLoginJavaBean.java - doLoginUser releasing access to controls... ");
					return "xsp-success";
					// END DEBUG
				}

			}

		} catch (NotesException e) {
			System.out.println(e.id + " " + e.text);
			e.printStackTrace();

		} catch (Exception e) {

			e.printStackTrace();
		}
		// return UserName;
		return UserName;
	}
}
// ...
// end of program





Use this XSnippet to add a variable from your JavaBean in session, info that can be used to load inputText value to variable in question and reference it in App pages, see Xpages file(s) that compliment this bean in a few...

Java
Köll S Cherizard
February 4, 2014 5:03 AM
Rating
24

All code submitted to OpenNTF XSnippets, whether submitted as a "Snippet" or in the body of a Comment, is provided under the Apache License Version 2.0. See Terms of Use for full details.



No comments yetLogin first to comment...