/********** myNewCustomField **************/
/**
This class is an example of a customized
data field in PAE 4.0+. This data field manages data about
employees. The data field displays employee data in a table.
This class uses an object class called myDataObject to
manage the data for the field.
myDataObject simply has four variables, employeeId, employeeName,
phone and email
public class myDataObject {
public String employeeId;
public String employeeName;
public String phone;
public String email;
public myDataObject( )
{
super();
}
}
*/
/***************** myNewCustomField ************************/
// Put this class in a package
package customer.fields;
// Import basic Java classes
import java.io.*;
import java.util.*;
import java.util.Date;
// Import classes needed for database connections
// The following package is in kfcjdk11.jar file.
// which is part of iAS implementation of the JDBC spec
import com.netscape.server.jdbc.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
// Import the entity field classes
import com.netscape.pm.fields.*;
import com.netscape.pm.htmlFE.*;
import com.netscape.pm.model.*;
import com.netscape.pm.util.*;
// Import the data object class used by this entity field
import customer.fields.myDataObject;
// All custom data fields must extend BasicCustomField
public class myNewCustomField
extends BasicCustomField
{
// Define variables for presentation characteristics
// and database access
String bgColor;
String MY_DB_TABLE;
DataSourceImpl myDataSource;
public myNewCustomField( )
{
super();
}
/** Get the values that were set in the Builder */
protected void loadDataElementProperties( Hashtable entry )
throws Exception
{
super.loadDataElementProperties( entry );
// Get table background color. Default is white
bgColor = (String) entry.get( "bgColor" );
if( bgColor == null )
bgColor = "white";
// Get database table name
MY_DB_TABLE = (String) entry.get( "DBtableName" );
if( MY_DB_TABLE == null )
throw new Exception( "DB Table not specified" );
// Create a data source for accessing the database
myDataSource = new DataSourceImpl();
myDataSource.setDataSourceName ((String) entry.get("DSIdentifier" ));
// Database is Oracle
myDataSource.setDriverType("ORACLE_OCI");
// Set database name name
myDataSource.setDatabaseName((String) entry.get("DBname"));
// Set database user, and password
myDataSource.setUsername((String) entry.get("DBuser"));
myDataSource.setPassword((String) entry.get("DBpassword"));
}
/**************** DISPLAY ON ENTRYPOINT **************************/
/* The display() method determines the appearance of
* the datafield in a form in the Process Express.
* At the entrypoint, the process instance does not exist
* so display() cannot access it.
*
* In this case, just display a field in which
* the user enters their employee ID
*/
public void display( IHTMLPage htmlpage, int displayMode,
String displayFormat ) throws Exception
{
// Create a string buffer to hold the HTML code
StringBuffer buffer = new StringBuffer();
// Write the code to display the datafield data in a
// single row in a table
switch( displayMode)
{
case MODE_EDIT:
// Write HTML text to display the field in edit mode
buffer.append("
");
break;
case MODE_VIEW:
// In an entrypoint, this datafield should not
// be shown in view mode. Display information
// for debugging purposes.
buffer.append("Employee id not known.
");
buffer.append("This field should not be in view mode here.
");
break;
case MODE_HIDDEN:
default:
// Do nothing
}
// Write the contents to the HTML page
htmlpage.write( buffer.toString() );
}
/**************** DISPLAY ON WORKITEM **************************/
/* This display() method determines the appearance of the
* datafield in a workitem. The process instance now exists
* thus display() can access the process instance.
*
* The datafield displays its values in a 4 row 2 column table.
* The first column is the heading for the parameter (for example, "Your name").
* The second column shows the value.
* In Edit mode,the value is a textfield.
* In View mode, the value is plain text.
*
*/
public void display( IProcessInstance pi, IHTMLPage htmlpage,
int displayMode, String displayFormat ) throws Exception
{
StringBuffer buffer = new StringBuffer();
// Get the value of this datafield as an object.
// Use getName() to get the name of this datafield
myDataObject myObject = (myDataObject) pi.getData( getName() );
// Get the employee id, name, and phone number
String employeeID = myObject.employeeID;
String name = myObject.employeeName;
String phone = myObject.phone;
String email = myObject.email;
// Write the code to display the values of the datafield
switch( displayMode)
{
case MODE_EDIT:
// Write HTML text to display the field in edit mode
// Display a table that contains editable text fields
buffer.append("");
break;
case MODE_VIEW:
// Write HTML text to display the field in a table in view mode
buffer.append("");
buffer.append("| Your employee ID: | ");
buffer.append("" + employeeID + " | ");
buffer.append("
| Your name: | ");
buffer.append("" + name + " | ");
buffer.append("
| Your phone: | ");
buffer.append("" + phone + " | ");
buffer.append("
| Your email: | ");
buffer.append("" + email + " |
");
break;
case MODE_HIDDEN:
default:
// Do nothing
}
// Write the contents to the HTML page
htmlpage.write( buffer.toString() );
}
/************ LOAD **************/
/* This function loads the data from an external source.
* It is invoked by getData() when getData() tries to get
* the value of an entity field that does not yet have a value.
*/
/* In this case load() calls the user-defined method
* retrieveDataObject(), which returns an instance of
* myDataObject that has 4 variables,
* employeeID, name, phone and email.
*/
public void load( IProcessInstance pi )
throws Exception
{
// Load the data from wherever it is stored
// and put it in the PI
// Get the entity key
String myKey = (String) pi.getEntityKey(getName());
// Use the key to get the data from a database
myDataObject myObject = (myDataObject) retrieveMyData(myKey);
// Put the object in the data field
pi.setData( getName(), myObject );
}
protected myDataObject retrieveMyData (String employeeID)throws Exception
{
// Database-related variables
Connection c = null;
PreparedStatement myStatement = null;
ResultSet myResultSet = null;
// String MY_DB_TABLE; -- set in loadDataElementProperties()
// DataSource myDataSource; -- set in loadDataElementProperties()
// Create the data object
myDataObject myObject = new myDataObject();
myObject.employeeID = employeeID;
try {
// Connect to the database.
// Database parameters are specified by myDataSource
c = myDataSource.getConnection();
// Create a query string to get the name, and phone from
// the MY_DB_TABLE database table (for example EMPLOYEE_TABLE)
String MY_QUERY_DATA = "SELECT name, phone, email" +
" FROM MY_DB_TABLE WHERE employee_id = " + employeeID;
// Prepare and execute the query statement
myStatement = c.prepareStatement( MY_QUERY_DATA );
myResultSet = myStatement.executeQuery();
// Process the results
while( myResultSet.next() )
{
myObject.employeeName = myResultSet.getString("name");
myObject.phone = myResultSet.getString("phone");
myObject.email = myResultSet.getString("email");
}
}
catch( Exception e ) {
throw new Exception( "Cannot load " + getName() + " because: " + e );
}
return myObject;
}
/************* CREATE *******************/
/* This method sets a default value for the data field in the
* process instance.
*
* Any value entered for the datafield in an entrypoint form
* overrides the value set by this method.
*
* This method is intended to provide a default value when
* the datafield's value is not set in an entrypoint form.
*/
// In this case, we don't want a default value
public void create( IProcessInstance pi )
throws Exception
{
}
/************** UPDATE **************************/
/** This method parses the query string sent when the
* form is submitted. It extracts the appropriate parameters
* and updates the process instance.
*
* We used a form element called idEntryPointFE to solicit
* the user's ID in an entry point form. At the entrypoint,
* we need to set the ID as the database key, but
* we don't need to update any other data.
*
* In a workitem form, this datafield has 4 form elements
* called idFE, nameFE, phoneFE and emailFE.
* At a workitem, we need to set the name, address
* and phone on the process instance.
*
* To get the value of a parameter sent by the form submission
* call getParameter() on the IPMRequest that is passed
* automatically to update().
*/
// update is not called if the field is in view mode
public void update( IProcessInstance pi, IPMRequest rq )
throws Exception {
// If idEntryPointFE is a request parameter.
// we are at an entrypoint, in which case
// save the employeeID as the entity key then exit
if (rq.isParameterDefined("idEntryPointFE")
{
String entryPointID = rq.getParameter("idEntryPointFE");
pi.setEntityKey(getName(), entryPointID);
}
// If we are at a work item, create an instance of myDataObject,
// set its name, phone, and email instance variables
else
{
myDataObject myObject = new myDataObject();
if (rq.isParameterDefined("idFE");
myObject.employeeID = rq.getParameter("idFE");
if (rq.isParameterDefined("nameFE");
myObject.employeeName = rq.getParameter("nameFE");
if (rq.isParameterDefined("phoneFE");
myObject.phone = rq.getParameter("phoneFE");
if (rq.isParameterDefined("emailFE");
myObject.email = rq.getParameter("emailFE");
// Put myObject into the datafield on the process instance
pi.setData( getName(), myObject);
}
}
/*************** STORE **********************/
/** This method stores the values of the datafield.
*
* It is invoked if setData() has been called.
* setData() is typically called by update()
* when the user changes a value in the workitem form.
* This method gets the value out of the datafield
* on the process instance and stores it as desired.
* Since the datafield now has a value, a call to getData()
* simply fetches the value from the datafield -- it does
* not call load().
*/
public void store( IProcessInstance pi )throws Exception
{
myDataObject myObject = (myDataObject) pi.getData(getName());
String myKey = myObject.employeeID;
// Set the key so we can get it back when needed
pi.setEntityKey(getName(), myKey);
// Store the data
storeMyData(myKey, myObject);
}
// This method updates the database with the employee's name
// phone and email. This method will be called only
// if the name, phone or email was changed.
// We do not allow the employee to change their ID.
protected void storeMyData (String myKey, myDataObject myObject)
throws Exception
{
Connection c = null;
PreparedStatement myStatement = null;
// String MY_DB_TABLE; -- set in loadDataElementProperties()
// DataSource myDataSource; -- set in loadDataElementProperties()
try {
// Create the SQL statement for updating the database
String SQL_UPDATE_DATA = "UPDATE " +
MY_DB_TABLE +
" SET name = ? , phone = ? " +
" WHERE id = myKey";
// Connect to the database.
// Database parameters are specified by myDataSource
c = myDataSource.getConnection();
// Prepare and execute the SQL statement to update the database
// Do I need to do setString for each of name, phone, email?
// sqlCartUpdateStmt.setString( 1, name );
myStatement = c.prepareStatement( SQL_UPDATE_DATA );
myStatement.executeUpdate();
}
catch( Exception e ){
throw new Exception( "Cannot save data for datafield: "
+ getName() + " because: " + e );
};
c.commit();
}
// End class
}