/** * Created: 2014.03.08.9.06.AM * Load HTML content to Xpages via String * Grab view column values into tableData tag */ package com.dokoll.solutions.inc.Utils; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import javax.faces.context.FacesContext; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import lotus.domino.Document; import lotus.domino.View; import lotus.domino.local.Database; /** * @author Dököll Solutions, Inc. * @version 2014.03.08.9.06.AM * */ public class CreateHtmlJavaBean { // declare and initialise Back-end view variables // ... static String LINKS_VIEW = "URLSecurityAccessView"; // get the current database being used Database database = (Database) FacesContext.getCurrentInstance() .getApplication().getVariableResolver().resolveVariable( FacesContext.getCurrentInstance(), "database"); public String GenResourceHtml; public String ViewURLID; public String UserNameValue; public String RoleNameValue; /** * @return the genResourceHtml */ public String getGenResourceHtml() { return GenResourceHtml; } /** * @param genResourceHtml * the genResourceHtml to set */ public void setGenResourceHtml(String genResourceHtml) { GenResourceHtml = genResourceHtml; } /** * @return the viewURLID */ public String getViewURLID() { return ViewURLID; } /** * @param viewURLID the viewURLID to set */ public void setViewURLID(String viewURLID) { ViewURLID = viewURLID; } /** * @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; } public CreateHtmlJavaBean(){ // 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("WOAAAHHH! Found this UserNameValue Cookie..." + UserNameValue); } if(cookieName.equals("cookieKeyRole")){ RoleNameValue = cookie[i].getValue(); System.out.println("WOAAAHHH! 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 } } } //... // prepare String to compile Tags public String outputHtmlTags(java.util.Collection<String> allTags) throws IOException { //get an External context to promote content HttpServletResponse httpServResp = (HttpServletResponse) FacesContext .getCurrentInstance().getExternalContext().getResponse(); //render content as HTML in this case httpServResp.setContentType("text/html;charset=UTF-8"); // use outputHtml variable to plug tags to page PrintWriter outputHtml = httpServResp.getWriter(); //maintain proper/well-formed doctype outputHtml.append("<!doctype html>"); outputHtml.append("<html lang='en'>"); outputHtml.append("<head>"); outputHtml.append("<meta charset='utf-8'>"); outputHtml.append("<title>My HTML to Xpages Test</title>"); outputHtml.append("</head>"); outputHtml.append("<body>"); // outputHtml.append( "<h1>HTML Body Items</h1>" ); // Create an HTML table, add links // ... outputHtml.append("<table><tr>"); // run though tags 'allTags' plug into tableData tag for (String htmlTags : allTags) { outputHtml.append("<td>" + htmlTags + "</td>"); } //close tags outputHtml.append("</tr></table>"); outputHtml.append("</body>"); outputHtml.append("</html>"); return outputHtml.toString(); } //... //button code: use starts/endsWith to find specific URL items // refrain from writing additional database calls to get values into button //TODO: Grab cookie value and search based on specific user public void doLoadHtmlTags() throws IOException { try { // Continue only if the database was properly instantiated if (database != null) { // ... View uLogs = database.getView(LINKS_VIEW); if (database.isFTIndexed()) { System.out.println(database +" is ready to be searched..."); // Continue only if the View object has been properly // instantiated if (uLogs != null) { // Retrieve the first document in the view Document doc = uLogs.getFirstDocument(); // Declare String objects to be used for field values int c = 0; // Loop through all documents in the view while (doc != null) { // TODO: Add jQuery functionality, also load link index, if needed, using 'int c' c += 1; // Retrieve the contents spit out HTML //... //plug in pageName +strViewURLText+ to server as Page Links //plug in pageNameExtension +strAddURLText+ to arrive as current page //when clicked it should resolve to specific pageURL, based on link accessed String strViewURLText = doc.getItemValueString("ViewURL"); String strAddURLText = doc.getItemValueString("AddURL"); ViewURLID ="<td width='300'><a href="+strAddURLText + "?" + "documentId="+ doc.getUniversalID() + "&action="+"EditDocument" +">"+strViewURLText+"</a></td>"; // display //... ArrayList<String> postHtmlTagItems = new ArrayList<String>(); // Add default Tag items // TODO: Add live links here postHtmlTagItems.add(ViewURLID); // ... // make available all tags compiled into loadHtml... // output to as Design to HTML page CreateHtmlJavaBean loadTags = new CreateHtmlJavaBean(); // ... String loadHtml = loadTags .outputHtmlTags(postHtmlTagItems); // DEBUG ONLY: Remove this, meant for debugging // purposes System.out.println("HTML Design Created! " + new java.util.Date().toString()); System.out.println(loadHtml); doc = uLogs.getNextDocument(doc); } } else { System.out.println("Can't do it... database is not indexed..."); } // return memory used by the View object to the system uLogs.recycle(); } // Return memory used by the Database object to the system database.recycle(); } } catch (Exception e) { e.printStackTrace(); } } }