| < Day Day Up > |
|
The purpose of this example is to build a portlet which:
Accesses the Domino server
Creates a list of all databases on that server so the user can choose which database ACL to browse
Creates a listing of the ACL for the selected database
You will use a variety of techniques to create this portlet: JSPs, JavaBeans, data handling mechanisms with Java (arrays and collections) and iterators to output data. You will also add images and functionality from a JavaScript file to a portlet. Figure 6-14 shows the portlet that is the end result of this example.
Figure 6-14: ACLBrowser portlet creates list of databases in Domino and lists ACL entries.
Work through this example to extend your skills at Java programming using a mix of Java, JavaBeans, JSP, JavaScript, and image and data handling in a portlet context.
Start your development work by creating a new portlet; name it ACLBrowser. (Use the same steps presented in 6.4, "HelloWorldFromDominoServer portlet" on page 313 to do this.) Create and open the ACLBrowser.java file for editing.
Insert/replace the code in the ACLBrowser.java file with that in Example 6-6. (You can copy/paste it.) Modifications you make to the Java code will provide the following functionalities:
Initialize the portlet and create a session to Domino.
Create an own entry list for holding values for ACLEntry and ACLLevel, put it in ArrayList, and save it according to the "value" request. If "value" is empty by default, load the ACL list for the names.nsf, otherwise, load the list of the ACL entries for the selected database.
Create an own entry list holding values for database names in server and database path, put it in ArrayList, and save it to request.
Create a JavaBean for sets and gets of value.
Example 6-6: ACLBrowser.java code
package portletjavacode; //Domino Objects for JAVA API import lotus.domino.*; import lotus.domino.Database; //Portlet API import org.apache.jetspeed.portlet.*; import org.apache.jetspeed.portlets.*; //Java stuff import java.io.*; import java.io.PrintWriter; import java.util.*; import java.lang.String; public class ACLBrowser extends AbstractPortlet { public void init(PortletConfig portletConfig) throws UnavailableException { // Initializing super.init(portletConfig); } public void doView(PortletRequest request, PortletResponse response) throws PortletException, IOException { // Calling method getSessios in viewmode getSession(request, response); } private Session getSession( PortletRequest request, PortletResponse response) throws PortletException, IOException { // Check first if session already exist Session diiopsession = (Session) request.getSession().getAttribute("diiopsession"); // Creating writer for output PrintWriter writer = response.getWriter(); // Check if diiopsession alredy exists,if it does not, create one if (diiopsession == null) try { // Creating diiop session for Domino diiopsession = NotesFactory.createSession("<servername>","<username>","<password>"); // Set session to attribute for reuse request.getSession().setAttribute("diiopsession", diiopsession); //getACL(request,response,diiopsession); } catch (NotesException e) { writer.println("ACL browser portlet ran, but with exception"+ e.id+ " msg:"+ e.text); } //creating acllist getACL(request, response, diiopsession); return diiopsession; } //Method for getting ACL entries private String getACL(PortletRequest request,PortletResponse response,Session diiopsession) throws PortletException, IOException { try { //Make a bean PortletDominoBean bean = new PortletDominoBean(); //Read from request if database was selested from list request.getSession().setAttribute("value", request.getParameter("value")); String dbtoview = request.getParameter("value"); if(dbtoview == null){ dbtoview = "names.nsf"; } Database database = diiopsession.getDatabase("", dbtoview); ACL acl = database.getACL(); ACLEntry entry = acl.getFirstEntry(); entry.getLevel(); //Save name in bean bean.setACL((String) entry.getName()); //// ArrayList acllist = new ArrayList(); do { String ACLEntry = entry.getName(); int ACLLevel = entry.getLevel(); String lev = null; //transforming the ACL levels in to text switch (entry.getLevel()) { case ACL.LEVEL_NOACCESS : lev = "no"; break; case ACL.LEVEL_DEPOSITOR : lev = "depositor"; break; case ACL.LEVEL_READER : lev = "reader"; break; case ACL.LEVEL_AUTHOR : lev = "author"; break; case ACL.LEVEL_EDITOR : lev = "editor"; break; case ACL.LEVEL_DESIGNER : lev = "designer"; break; case ACL.LEVEL_MANAGER : lev = "manager"; break; } //build the list AclTestEntry NameAndLevel = new AclTestEntry(ACLEntry, lev); acllist.add(NameAndLevel); } while ((entry = acl.getNextEntry(entry)) != null); //Save bean in request request.setAttribute("DATA_ACL", acllist); getDBList(request, response, diiopsession); } catch (NotesException e) { PrintWriter writer2 = response.getWriter(); writer2.println("Error making ACL: " + e.id + " msg:" + e.text); } return null; } private class AclEntry implements ACLEntry { private String ACLEntry = null, ACLLevel = null; public AclEntry(String ACLEntry, String ACLLevel) { this.ACLEntry = ACLEntry; this.ACLLevel = ACLLevel; } public String getACLEntry() { return ACLEntry; } public String getACLLevel() { return ACLLevel; } } //method getting databaselist private String getDBList( PortletRequest request, PortletResponse response, Session diiopsession) throws PortletException, IOException { //lets check if databaselist has already created Session prevdblist = (Session) request.getSession().getAttribute("dblist"); if (prevdblist == null){ try { //Make a bean PortletDominoBean bean = new PortletDominoBean(); // ArrayList dblist = new ArrayList(); DbDirectory dir = diiopsession.getDbDirectory(null); String server = dir.getName(); if (server == "") server = "Local"; Database db = dir.getFirstDatabase(DbDirectory.DATABASE); //building the list of database while (db != null) { String dbfp = db.getFilePath(); String dbtitle = db.getTitle(); DBListTestEntry values = new DBListTestEntry(dbfp, dbtitle); dblist.add(values); db = dir.getNextDatabase(); } dir.recycle(); request.setAttribute("DATA_DBLIST", dblist); getPortletConfig().getContext().include("/jsp/View.jsp",request, response); } catch (NotesException e) { PrintWriter writer2 = response.getWriter(); writer2.println("Error making DBList: " + e.id + " msg:" + e.text); } } return null; } private class DBListEntry implements DBListEntry { private String DBFilePath = null, DBTitle = null; public DBListEntry(String DBFilePath, String DBTitle) { this.DBFilePath = DBFilePath; this.DBTitle = DBTitle; } public String getDBFilePath() { return DBFilePath; } public String getDBTitle() { return DBTitle; } } }
Remember to replace <servername>, <username> and <password> with the correct values for your environment.
Save the file.
Create the three Java files in Examples 6-7, 6-8, and 6-9 to take care of needed entries.
Example 6-7: ACLEntry.java
package portletjavacode; public interface ACLEntry { public abstract String getACLEntry(); public abstract String getACLLevel(); }
Example 6-8: DBListEntry.java file
package portletjavacode; public interface DBListEntry { public abstract String getDBFilePath(); public abstract String getDBTitle(); }
Example 6-9: PC.java file
package portletjavacode; public interface PC { public static final String DATA_ACL = "DATA_ACL"; public static final String DATA_DBLIST = "DATA_DBLIST"; public static final String PARAM_VALUE = "value"; }
Create the following JavaBean file.
Example 6-10: PortletDominoBean.java file
package portletjavacode; public class PortletDominoBean { private String ACL = ""; public void setACL(String s) { ACL = s; } public String getACL() { return (ACL); } }
Start to build the view.jsp for gathering output by first creating the view.jsp file.
Create a folder called images and place the file named logo.gif (the image you want to add to the portlet) into the folder. Add the following lines to view.jsp to retrieve the image.
%@ taglib uri="/WEB-INF/tld/portlet.tld" prefix="resources" %> % String imagePath = portletResponse.encodeURL("/images/"); %> img src="/books/3/366/1/html/2/<%= imagePath %>logo.gif" border="0" align="top">
Create a folder for JavaScript files called js. Create a JavaScript file, give it the name Redbook.js, and save it to the js folder. Insert the following code in the JavaScript file.
function SelectBoxActionSet(objForm, objSelect) { objForm.action = eval('objForm.' + objSelect + '.value') }
Add the following lines to view.jsp to add js functionalities:
<script language="JavaScript" src="/books/3/366/1/html/2/<portletAPI:encodeURI path="/js"/>/Redbook.js"></script> <form name="mobileview" onSubmit="SelectBoxActionSet(this,'gpResult');" method="post">
Add the following lines to view.jsp to retrieve a list of databases and ACLs, as well as to get JavaScript SelectBoxActionSet method.
Example 6-11: Retrieving list of database
/// <%@ page import="java.util.Collection, java.util.Iterator, portletjavacode.ACLEntry, portletjavacode.DBListEntry, portletjavacode.PC"%> <%@ taglib uri="/WEB-INF/tld/portlet.tld" prefix="portletAPI" %> <portletAPI:init /> <jsp:useBean scope="request"/> /// <select name="gpResult" onChange="SelectBoxActionSet(document.mobileview,'gpResult'); submit();"> <% Collection dblist2 = (Collection) request.getAttribute(PC.DATA_DBLIST); for (Iterator iterator = dblist2.iterator() ; iterator.hasNext() ;) { DBListEntry entry = (DBListEntry) iterator.next(); %> <option value="<portletAPI:createURI > <portletAPI:URIParameter name="<%= PC.PARAM_VALUE %>" value="<%=entry.getDBFilePath()%>"/> </portletAPI:createURI>"><%= entry.getDBTitle() %></option> <% } %> </select> // ACL List generated here: <table border="0"> <tr><td><b>ACL Entry</b></td><td><b>Access Level</b></td></tr> <% Collection ACLs = (Collection) request.getAttribute(PC.DATA_ACL); for (Iterator iterator = ACLs.iterator() ; iterator.hasNext() ;) { ACLEntry entry = (ACLEntry) iterator.next(); %> <tr><td> <%= entry.getACLEntry() %></td><td><%= entry.getACLLevel() %></td></tr> <% } %>
Save all the open files, deploy the portlet, and add it to a page.
| < Day Day Up > |
|