Nov 29, 2011 9:31 AM
64 Posts

Web Services Code Stripped for Parts (Xpages and Java)

  • Category: Managed Beans in NSF
  • Platform: Windows
  • Release: 8.5.2
  • Role: Developer
  • Tags: JavaBean,Backing Bean,XPages Application
  • Replies: 0

Xpages

Web Services Code Stripped for Parts

 

Knowledge Sharing:

 

At times when building Xpages Applications, it is helpful to salvage Java code wherever you can find it, whether it be in a JavaAgent or a Web Services providing or consuming code; find code that is doing what you need,  use, and share it with your community or forum. 

 

I recently reported a conversion to JavaBean from a JavaAgent, and also mentioned looking to JSF samples to get Xpages-based code.  Well, my friends, set your eyes on below Web Services code, we’re salvaging Java code to reuse elsewhere for Xpages development.  Normally of course the Web Services code would have sufficed, but if you needed to get the data by any other means, the best efficient way is to write a Java program that retrieves a view, loads the data to Xpages.

 

 

Brief outline: 

1.     Yank Web Services Code, add to Java classes

2.     Remove Web Services-specific code/method

3.     Implement JSF to call the nsf database

4.     Grab data from a view directly

5.     Remove search feature, grab all rows

6.     Build an Xpages form

7.     Configure face-config xml file

8.     Clean up and run this bad boy!

 

Web Services Code Courtesy of: http://www.jpgtutorials.com/lotus-notes-blackberry-integration-using-mds-studio-and-web-services

 

 

[CODE] 

 

import lotus.domino.*;  
import lotus.domino.types.*;
 
public class EmployeeAction {
 
// This is a template implementation class for your web service. It
// becomes extraneous if you import a WSDL document. Consumers of this
// web service can call any public method in the implementation class.
//
// To obtain a Session object use this code:
 
Session s = WebServiceBase.getCurrentSession();
//write the method to get Employee collection
 
public Employee[] getEmployees(){
 
//declare Employee array
Employee[] employees=null;
 
try{
 
AgentContext agentContext=s.getAgentContext();
//get reference to current database
Database db=agentContext.getCurrentDatabase();
//get collection of Employee documents
DocumentCollection collection=db.search("Form=\"Employee\"");
//initialize employees array with size as employee documents count
employees=new Employee[collection.getCount()];
Document doc=collection.getFirstDocument();
//iterate through employee document collection
int count=0;
while(doc!=null){
//create Employee object
Employee employee=new Employee();
//populate the variables of employee object with the values from employee document
employee.setEmployeeID(doc.getItemValueInteger("EmployeeID"));
employee.setEmployeeName(doc.getItemValueString("EmployeeName"));
employee.setEmployeeAge(doc.getItemValueInteger("EmployeeAge"));
employee.setEmployeeDesignation(doc.getItemValueString("EmployeeDesignation"));
//put the employee object into employees array
employees[count]=employee;
count+=1;
doc=collection.getNextDocument(doc);
}
 
return employees;
 
}catch(Exception e){
e.printStackTrace();
}
 
return null;
 
}
 

} 

 

 

[/CODE] 

 

 

 TIP: Web Services /JavaAgent code above is to be converted to a Backing Bean

 

 

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>>

 

 

[CODE] 

 

 

public class Employee {
 
//declare variables
private int employeeID;
private String employeeName;
private int employeeAge;
private String employeeDesignation;
 
//getter methods
 
public int getEmployeeID(){
return employeeID;
}
 
public String getEmployeeName(){
return employeeName;
}
 
public int getEmployeeAge(){
return employeeAge;
}
 
public String getEmployeeDesignation(){
return employeeDesignation;
}
 
//setter methods
 
public void setEmployeeID(int employeeID){
this.employeeID=employeeID;
}
 
public void setEmployeeName(String employeeName){
this.employeeName=employeeName;
}
 
public void setEmployeeAge(int employeeAge){
this.employeeAge=employeeAge;
}
 
public void setEmployeeDesignation(String employeeDesignation){
this.employeeDesignation=employeeDesignation;
}
 
}

 

 

[/CODE] 

 

 

 TIP: Additional code will be added in the above to collect array of values from the Backing Bean

 

 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>>

 

 

 

Finished Product:

 

At this point, we assume you have already set up a Source folder, and necessary packages and class files have been created; we will skip redoing your effort, jump ahead copy and paste as needed.  Areas of interest will be highlighted below for your convenience... 

 

 

STEP 1

Copy and Paste below code into a new JavaBean called ‘Keyword’.  This JavaBean is slightly different than the Web Services sample.  We added an additional method, see bold items below.

 

[CODE] 

 

/**

 * Created: 2011.02.10.9.21.AM

 * JavaBean Class to load view data to Xpages

 * Modified: 2011.11.27.7.24.PM

 * Adding actual data from a view

 */

package com.osc.blackberry.test;

 

/**

 * @author kcherizard

 * @version: 2011.02.10.9.17.AM

 */

public class Keyword {

 

      //declare variables

      //TO DO: Bring in Creation Date as well

      private String CategoryName;

      private String TopicName;

       

      //getter/setter methods

       

      /**

       * @return the topicName

       */

      public String getTopicName() {

            return TopicName;

      }

 

      /**

       * @param topicName the topicName to set

       */

      public void setTopicName(String topicName) {

            this.TopicName = topicName;

      }

 

      /**

       * @param categoryName the categoryName to set

       */

      public void setCategoryName(String categoryName) {

            this.CategoryName = categoryName;

      }

 

      /**

       * @return the categoryName

       */

      public String getCategoryName() {

            return CategoryName;

      }

     

     

      //return arrays of values for Keyword

//2011.11.28.9.30.AM

          public Keyword(String CategoryName, String TopicName) {

 

                 this.CategoryName=CategoryName;

                  this.TopicName=TopicName;

           

          }

 

     

       

}

 

 

 

[/CODE] 

 

 

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>>

 

 

 

 

STEP 2

 

Let’s feed the JavaBean the values we need loaded onto the Xpages form.  We will go ahead and write another Java class (Backing Bean), we’ll call this one KeyworkdsHelperBean.  Note: I recently added a post where I ‘married an Xpages form with a (JSF sample) Backing Bean’, that sample used a list to fill the Xpage with data, below uses an array.  Let’s get right into it.

 

 

[CODE] 

 

 

/**

 * Created: 2011.02.10.9.21.AM

 * Backing Bean class to load view data to Xpages

 * Modified: 2011.11.27.7.24.PM

 * Adding actual data from a view

 */

package com.osc.blackberry.test;

 

import javax.faces.context.FacesContext;

import com.osc.blackberry.test.Keyword;

import lotus.domino.Database;

import lotus.domino.Document;

import lotus.domino.View;

 

/**

 * @author kcherizard

 * @version: 2011.02.10.9.21.AM

 */

public class KeywordsHelperBean {

 

      // Get Keyword collection

      public Keyword[] getKeywords() {

 

            // Declare Keywords Array

            Keyword[] keywords = null;

 

            try {

                  // 2011.02.10.9.37.AM

                  // TO DO: Get the database directly and not through AgentContext

                  // Look into the benefit, if we need to return more

                  // than the 64K limit set for JavaAgents

                 

                  //get the current database being used

                  Database database= (Database) FacesContext.getCurrentInstance()

                        .getApplication().getVariableResolver()

                        .resolveVariable(FacesContext.getCurrentInstance(), "database");

                 

                  // Grab a collection of all Keyword documents

                  // TO DO: Use own search mechanism, connecting directly to view(s)

                  // Grab specific Keywords

 

                 

                  //Find the view in question

                  View view = database.getView("SiteListings");

                  System.out.println("View Obtained..." + view);

                 

                  Document sDoc;

                  Document ndoc;

                 

                 

                  sDoc = (Document) view.getFirstDocument();

                  System.out.println("Document Obtained..." + sDoc);

                 

                 

                  // Begin initialization of the Keywords Array

                  // load documents count

                  keywords = new Keyword[view.getEntryCount()];

                 

                  // Run through Keywords document collection

                  int docount = 0;

                  while (sDoc != null) {

                        // Create Keywords object

                        Keyword keyword = new Keyword(null, null);

                        // render document values to variables of Keywords object

                        keyword.setCategoryName(sDoc.getItemValueString("SiteName"));

                        keyword.setTopicName(sDoc.getItemValueString("SiteNumber"));

 

                        // Send Keywords object into keywords Array

                        keywords[docount] = keyword;

                        // incerement counts

                        docount += 1;

                        // load next doc

                        ndoc = view.getNextDocument(sDoc);

                        sDoc = ndoc;

                  }

 

                  // return the Array

                  return keywords;

 

            } catch (Exception e) {

                  e.printStackTrace();

            }

 

            return null;

 

      }

 

}

 

 

[/CODE] 

 

 

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>>

 

 

 

STEP 3

 

 

 

 

Build an Xpages form, add KeyworkdsHelperBean as page datasource.  You will also need to configure the generated faces-config.xml file to make the connection between the Backing Bean and the XSP (Xpages) code.

 

 

[CODE] 

 

 

<?xml version="1.0" encoding="UTF-8"?>

 

<xp:view xmlns:xp="http://www.ibm.com/xsp/core">

 

 

      <xp:dataTable rows="5" id="siteTable" var="siteInfo"

            value="#{javascript:KeywordsHelperBean.getKeywords()}" style="width:50%">

 

            <xp:this.facets>

 

                  <xp:pager layout="Previous Group Next" xp:key="header" id="pager1"

                        for="siteTable">

 

                  </xp:pager>

 

                  <xp:pager layout="Previous Group Next" xp:key="footer" id="pager2"

                        for="siteTable" partialRefresh="true">

 

                  </xp:pager>

 

            </xp:this.facets>

 

            <xp:column id="siteNameColumn">

 

                  <xp:text escape="true" id="siteNameField" value="#{siteInfo.categoryName}">

 

                  </xp:text>

 

            </xp:column>

 

            <xp:column id="siteNumberColumn">

 

                  <xp:text escape="true" id="siteNumberField" value="#{siteInfo.topicName}">

 

                  </xp:text>

 

            </xp:column>

 

           

 

      </xp:dataTable>

 

</xp:view>

 

 

[/CODE] 

 

 

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>>

 

 

 

STEP 4

 

 

Configure Faces XML file; grab the file from your WebContent folder, modify it as shown below

 

  <managed-bean>

    <managed-bean-name>KeywordsHelperBean

            </managed-bean-name>

    <managed-bean-class>com.osc.blackberry.test.KeywordsHelperBean

            </managed-bean-class>

    <managed-bean-scope>request</managed-bean-scope>

  </managed-bean>

 

Conclusion: 

Save and fire the application... You may want to try to get the data into a repeat control, just to see what behaviour, if any exists, might give us further functionality on top of what we get with dataTables.  Coming soon, adding a search feature to compliment our results dataTable, grab unique values from the nsf back-end into another richText-enabled/Readonly Xpage.
 
Happy Coding! 
 
Version: 2011.12.10.10.32.AM