JavaBean to Submit Values and Record Cookie (SendSubscriptionJavaBean.java)


//add getters and setters for these variables to submit values to back-end

        public String Doc_Date_Format = "yyyy.MM.dd.HH.mm.a";
	SimpleDateFormat docdateformat = new SimpleDateFormat(Doc_Date_Format);
	Calendar docalendar = Calendar.getInstance();
	// ...
	public String UserID;
	public String FirstName;
	public String LastName;
	public String Identifyer;
	public String EmailAddress;
	public String ConfirmEmail;
	public String WorkPhone;
	public String DocDate = docdateformat.format(docalendar.getTime());
	public String MobilePhone;
	public String AdsType;
	public String ShelfLife;
	public String UserRoleType;
	public String DeliveryType;
	public String Body;

//NOTE: PageID and UserIP must also have getter/setter methods, but are reflected/ declared in Servlet call

	HttpServletRequest httpServletRequest = (HttpServletRequest) FacesContext
			.getCurrentInstance().getExternalContext().getRequest();
	String UserIP = httpServletRequest.getRemoteAddr();

	HttpServletRequest reqURL = (HttpServletRequest) FacesContext
			.getCurrentInstance().getExternalContext().getRequest();
	String PageID = reqURL.getRequestURL().toString();




	// Add Subscription button code
	public void doAddSubscription() {

		try {

			// get the current database being used
			Database database = (Database) FacesContext.getCurrentInstance()
					.getApplication().getVariableResolver().resolveVariable(
							FacesContext.getCurrentInstance(), "database");

			// instantiate the document create call
			Document submitSubscription = (Document) database.createDocument();
			// submit using Subscription form (Alternatively, one can add the
			// Alias of that form)
			submitSubscription.appendItemValue("form", "Subscription");
			// using appendItemValue to insert in the fields needed
			// notice we are referencing the field on the Xpage, and the
			// JavaBean variables
			submitSubscription.appendItemValue("userID", UserID);
			submitSubscription.appendItemValue("firstName", FirstName);
			submitSubscription.appendItemValue("lastName", LastName);
			submitSubscription.appendItemValue("identifyer", Identifyer);
			submitSubscription.appendItemValue("mobilePhone", MobilePhone);
			submitSubscription.appendItemValue("workPhone", WorkPhone);
			submitSubscription.appendItemValue("emailAddress", EmailAddress);
			submitSubscription.appendItemValue("confirmEmail", ConfirmEmail);
			submitSubscription.appendItemValue("adsType", AdsType);
			submitSubscription.appendItemValue("deliveryType", DeliveryType);
			submitSubscription.appendItemValue("shelfLife", ShelfLife);
			// no need to show this 'docDate', 'userIP', and 'pageID' values on the Xpage form			
                        submitSubscription.appendItemValue("userIP", UserIP);
			submitSubscription.appendItemValue("docDate", DocDate);
			submitSubscription.appendItemValue("pageID", PageID);
			submitSubscription.appendItemValue("userRoleType", UserRoleType);
			submitSubscription.appendItemValue("body", Body);
			SetUserCookie();
			// saves the data, based on above fields to Company form
			submitSubscription.save();
			// doLoadSubscriptions();
			// cleans up the system
			submitSubscription.recycle();

			// throwable initialized if there is an error, either in the field
			// or the query
		} catch (NotesException e) {
			// print this error to the server
			e.printStackTrace();
		}

	}

	public void SetUserCookie() {
		// declare and instantiate variable, grab external context, create
		// cookie...
		FacesContext facesContext = FacesContext.getCurrentInstance();
		// create cookie, plug userName variable in to grab values added from
		// xploginform
		Cookie cookieKeyUser = new Cookie("cookieKeyUser", UserName);
		// Cookie cookieKeyRole = new Cookie("cookieKeyRole", SearchRole);
		// DEBUG ONLY: Send message to Log... make sure a cookie is indeed
		// registered
		System.out.println("COOOOKIIIIEEEE USER... " + cookieKeyUser);
		// System.out.println("COOOOKIIIIEEEE ROLE... " + cookieKeyRole);
		// setMaxAge of cookie items
		cookieKeyUser.setMaxAge(3600);
		// cookieKeyRole.setMaxAge(3600);
		// attach userName and searchRole variables to a cookie
		((HttpServletResponse) facesContext.getExternalContext().getResponse())
				.addCookie(cookieKeyUser);
		// ((HttpServletResponse) facesContext.getExternalContext()
		// .getResponse()).addCookie(cookieKeyRole);

		// TODO: Add this method to the JSFUtil class

	}
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...