/** * Created from Original: 2014.05.19.5.25.AM * New Google Spreadsheet data Posting via Xpages form and JavaBean */ package com.dokoll.solutions.inc.google.dev; //... //Java imports... import java.io.Serializable; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Random; //... //faces imports... import javax.faces.context.FacesContext; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; //domino imports... import lotus.domino.NotesException; import lotus.domino.local.Database; import lotus.domino.local.Document; /** * @author Dököll Solutions, Inc. * @version 2014.05.19.5.25.AM */ public class SendGoogleRecordJavaBean implements Serializable { private static final long serialVersionUID = 1L; private String Date_Format = "MM/dd/yy"; // declare variables, and add static values as test SimpleDateFormat simpledateformat = new SimpleDateFormat(Date_Format); Calendar calendar = Calendar.getInstance(); // declare variables private String UserNameValue; private String RoleNameValue; private String UserID; private String RoleID; // @version 2011.08.16.3.14.PM private String ItemDate = simpledateformat.format(calendar.getTime()); // Reformat the date to our needs, we needs this format // (2010.12.30.10.13.AM) // @version: 2013.11.03.10.14.PM private String Doc_Date_Format = "yyyy.MM.dd.HH.mm a"; SimpleDateFormat docdateformat = new SimpleDateFormat(Doc_Date_Format); Calendar docalendar = Calendar.getInstance(); private String ItemNumber = docdateformat.format(docalendar.getTime()); // 2011.09.20.11.20.AM // Randomizing to get a ticket number // TO DO: add the date or the user's ID to the random number. // Alternatively, once can reformat the a date variable as such // 2012.11.10.8.20 + session ID, or random Number // arrive at a fancy ticket number, for this exercise the random will do Random Rand = new Random(); // private String ItemNumber; private String SubjectFirst; private String SubjectLast; private String SubjectAge; private String SubjectHeight; private String Categories; /** * @return the categories */ public String getCategories() { return Categories; } /** * @param categories * the categories to set */ public void setCategories(String categories) { Categories = categories; } /** * @return the userID */ public String getUserID() { return UserID; } /** * @param userID * the userID to set */ public void setUserID(String userID) { UserID = userID; } /** * @return the roleID */ public String getRoleID() { return RoleID; } /** * @param roleID * the roleID to set */ public void setRoleID(String roleID) { RoleID = roleID; } /** * @return the userNameValue */ public String getUserNameValue() { return UserNameValue; } /** * @param userNameValue * the userNameValue to set */ public void setUserNameValue(String userNameValue) { UserNameValue = userNameValue; } /** * @return the roleNameValue */ public String getRoleNameValue() { return RoleNameValue; } /** * @param roleNameValue * the roleNameValue to set */ public void setRoleNameValue(String roleNameValue) { RoleNameValue = roleNameValue; } /** * @return the subjectFirst */ public String getSubjectFirst() { return SubjectFirst; } /** * @param subjectFirst * the subjectFirst to set */ public void setSubjectFirst(String subjectFirst) { SubjectFirst = subjectFirst; } /** * @return the subjectLast */ public String getSubjectLast() { return SubjectLast; } /** * @param subjectLast * the subjectLast to set */ public void setSubjectLast(String subjectLast) { SubjectLast = subjectLast; } /** * @return the subjectAge */ public String getSubjectAge() { return SubjectAge; } /** * @param subjectAge * the subjectAge to set */ public void setSubjectAge(String subjectAge) { SubjectAge = subjectAge; } /** * @return the subjectHeight */ public String getSubjectHeight() { return SubjectHeight; } /** * @param subjectHeight * the subjectHeight to set */ public void setSubjectHeight(String subjectHeight) { SubjectHeight = subjectHeight; } /** * @return the itemNumber */ public String getItemNumber() { return ItemNumber + "-" + Rand.nextInt(); } /** * @param itemNumber * the itemNumber to set */ public void setItemNumber(String itemNumber) { ItemNumber = itemNumber; } /** * @return the itemDate */ public String getItemDate() { return ItemDate; } /** * @return the pageID */ public String getPageID() { return PageID; } /** * @param pageID * the pageID to set */ public void setPageID(String pageID) { PageID = pageID; } /** * @param itemDate * the itemDate to set */ public void setItemDate(String itemDate) { ItemDate = itemDate; } // 2011.09.20.12.23.PM // Grabbing User IPs // TO DO: Get User's real IP, this item gets the external IP HttpServletRequest httpServletRequest = (HttpServletRequest) FacesContext .getCurrentInstance().getExternalContext().getRequest(); String UserIP = httpServletRequest.getRemoteAddr(); HttpServletRequest reqURL = (HttpServletRequest) FacesContext .getCurrentInstance().getExternalContext().getRequest(); String PageID = reqURL.getRequestURL().toString(); /** * @return the userIP */ public String getUserIP() { return UserIP; } /** * @param userIP * the userIP to set */ public void setUserIP(String userIP) { UserIP = userIP; } public SendGoogleRecordJavaBean() { // get userCookies FacesContext facesContext = FacesContext.getCurrentInstance(); String cookieName = null; Cookie cookie[] = ((HttpServletRequest) facesContext .getExternalContext().getRequest()).getCookies(); if (cookie != null && cookie.length > 0) { for (int i = 0; i < cookie.length; i++) { cookieName = cookie[i].getName(); if (cookieName.equals("cookieKeyUser")) { UserNameValue = cookie[i].getValue(); System.out .println("SendGoogleRecordJavaBean! Found this UserNameValue Cookie..." + UserNameValue); } if (cookieName.equals("cookieKeyRole")) { RoleNameValue = cookie[i].getValue(); System.out .println("SendGoogleRecordJavaBean! Found this RoleNameValue Cookie..." + RoleNameValue); } else System.out.println("Cookies not found..."); // TODO: Add this method to JSFUtil class, // also delete the cookies when logging out } } } // button code public void submitEntry() { try { // get the current database being used Database database = (Database) FacesContext.getCurrentInstance() .getApplication().getVariableResolver().resolveVariable( FacesContext.getCurrentInstance(), "database"); // instantiate the document create call Document submitDocument = (Document) database.createDocument(); // submit using GoogleSpreadsData form (Alternatively, one can add // the Alias of that form) submitDocument.appendItemValue("form", "GoogleWorksheetsData"); // using appendItemValue to insert in the fields needed // notice we are referencing the field on the Xpage, and the // JavaBean variables submitDocument.appendItemValue("userID", UserID); submitDocument.appendItemValue("roleID", RoleID); submitDocument.appendItemValue("itemNumber", ItemNumber); submitDocument.appendItemValue("subjectFirst", SubjectFirst); submitDocument.appendItemValue("subjectLast", SubjectLast); submitDocument.appendItemValue("subjectAge", SubjectAge); submitDocument.appendItemValue("subjectHeight", SubjectHeight); submitDocument.appendItemValue("categories", Categories); submitDocument.appendItemValue("userIP", UserIP); // no need to show this on the Xpage submitDocument.appendItemValue("itemDate", ItemDate); submitDocument.appendItemValue("pageID", PageID); // saves the data, based on above fields to GoogleSpreadsData form submitDocument.save(); System.out .println("Connecting to Notes Agent, inserting rows to Worksheets..."); // ... // Run the agent... database.getAgent("GoogleAddRowForXpagesJavaAgentAlias") .runOnServer(); // cleans up the system submitDocument.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(); } } }