home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
BUG 4
/
BUGCD1997_05.BIN
/
aplic
/
visualj
/
vjtrial.exe
/
RCDATA
/
CABINET
/
vjdbwiz.awx
/
TEMPLATE
/
DAO.JAV
< prev
next >
Wrap
Text File
|
1997-01-28
|
39KB
|
1,447 lines
$$IF(comments)
////////////////////////////////////////////////////////////////////////////////
//
// Java Source: $$AppName$$.java
$$IF(use_DAO)
// Description: Applet with DAO support for accessing the
// "$$fname$$" database.
$$ELSE
// Description: Applet with RDO support for accessing the
// "$$DSN$$" data source.
$$ENDIF
//
$$ENDIF
import java.applet.*;
import java.awt.*;
import java.util.*;
$$IF(comments)
//--------------------------------------------------------------------------
$$IF(use_DAO)
// DAO SUPPORT: Import the DA0 3.5 object library and the COM classes
$$ELSE
// RDO SUPPORT: Import the RDO 2.0 object library and the COM classes
$$ENDIF
//--------------------------------------------------------------------------
$$ENDIF
$$IF(todo)
$$IF(use_DAO)
// TODO: Run the "Type Library Wizard" on the DAO 3.5 object library!
$$ELSE
// TODO: Run the "Type Library Wizard" on the RDO 2.0 object library!
$$ENDIF
$$ENDIF
$$IF(use_DAO)
import dao350.*;
$$ELSE
import msrdo20.*;
$$ENDIF
import com.ms.com.*;
import Alert;
$$IF(comments)
//--------------------------------------------------------------------------
$$IF(use_DAO)
// DAO SUPPORT: Class that defines a database field
$$ELSE
// RDO SUPPORT: Class that defines a database field
$$ENDIF
//--------------------------------------------------------------------------
$$ENDIF
$$IF(comments)
/**
* Field class that defines the field's name and type as well as it's
* attached Label and TextField components.
*/
$$ENDIF
class DBField
{
$$IF(comments)
/**
* Name of field
*/
$$ENDIF
String strName;
$$IF(comments)
/**
* Type of field as a string
*/
$$ENDIF
String strType;
$$IF(comments)
$$IF(use_DAO)
/**
* Type of field as a Variant type (vt)
*/
$$ENDIF
$$ENDIF
$$IF(use_DAO)
short vt;
$$ENDIF
$$IF(comments)
/**
* Label attached to field (if any)
*/
$$ENDIF
Label label;
$$IF(comments)
/**
* TextField attached to field (if any)
*/
$$ENDIF
TextField field;
$$IF(comments)
/**
* Attributes of field
*/
$$ENDIF
int attributes;
$$IF(comments)
$$IF(use_DAO)
/**
* Validation text for field
*/
$$ENDIF
$$ENDIF
$$IF(use_DAO)
String strValidation;
$$ENDIF
$$IF(comments)
/**
* Constructor
* @param strName Name of field in database
* @param strType Description of field type
$$IF(use_DAO)
* @param vt Type of Variant (see Variant.Variant*)
$$ENDIF
*/
$$ENDIF
$$IF(use_DAO)
public DBField(String strName, String strType, short vt)
$$ELSE
public DBField(String strName, String strType)
$$ENDIF
{
this.strName = strName;
this.strType = strType;
$$IF(use_DAO)
this.vt = vt;
$$ENDIF
}
}
$$IF(comments)
/**
* Frame class for the $$AppName$$ applet.
*/
$$ENDIF
class DBFrame extends Frame
{
$$IF(comments)
/**
* The applet that brought up this frame window.
*/
$$ENDIF
$$AppName$$ applet;
$$IF(comments)
/**
* Constructor
* @param applet Applet to notify (via applet.stop) when the frame window closes.
* @param strTitle The title of this frame window.
*/
$$ENDIF
public DBFrame($$AppName$$ applet$$AppName$$, String strTitle)
{
$$IF(comments)
// Send title to superclass and set applet member
$$ENDIF
super(strTitle);
this.applet = applet$$AppName$$;
}
$$IF(comments)
/**
* This method is used to intercept the WINDOW_DESTROY event so the
* recordset can be closed. It is accomplished by calling the
* applet's exit() method.
* @param evt Event description class.
* @return True if the event was handled, false otherwise
*/
$$ENDIF
public synchronized boolean handleEvent(Event evt)
{
switch (evt.id)
{
case Event.WINDOW_DESTROY:
$$IF(comments)
//-------------------------------------------------------------
// DAO SUPPORT: Close database and window on WINDOW_DESTROY
//-------------------------------------------------------------
$$ENDIF
applet.exit();
return true;
default:
return super.handleEvent(evt);
}
}
}
$$IF(comments)
/**
* Main $$AppName$$ applet/application class.
*/
$$ENDIF
public class $$AppName$$ extends Applet
{
$$IF(comments)
/**
* The $$AppName$$ applet (if we created it in main).
*/
$$ENDIF
static protected $$AppName$$ applet = null;
$$IF(comments)
/**
* The frame window if it's currently active.
*/
$$ENDIF
protected Frame frame;
$$IF(comments)
//--------------------------------------------------------------------------
$$IF(use_DAO)
// DAO SUPPORT: Variables used for DAO support.
$$ELSE
// RDO SUPPORT: Variables used for RDO support.
$$ENDIF
//--------------------------------------------------------------------------
$$ENDIF
$$IF(readonly)
protected boolean readOnly = true;
$$ELSE
protected boolean readOnly = false;
$$ENDIF
$$IF(use_DAO)
protected String strDatabase = "$$dbesc$$";
$$IF(recordset)
protected String strRecordset = "$$recordset$$";
$$ENDIF
protected int recordCount;
protected static Variant varEmpty;
$$IF(comments)
// Valid table not specified via wizard,
// must specify table name here.
// protected String strRecordset = "<name>";
// DAO Objects
$$ENDIF
protected _DBEngine m_IEngine;
protected Database database;
protected Recordset recordset;
$$ELSE
$$IF(comments)
// RDO Data
$$ENDIF
protected String m_strConnect = "$$ODBC_CONNECT_ESC$$";
protected String m_strDummyQueryName = "Dummy_Query_$$AppName$$";
$$IF(resultset)
protected String m_strTable = "$$resultset$$";
$$ENDIF
protected int recordCount;
$$IF(comments)
// RDO Interfaces
$$ENDIF
protected static _rdoEngine m_IEngine;
protected static _rdoEnvironment m_IEnvironment;
protected static _rdoConnection m_IConnection;
protected static rdoPreparedStatement m_IPreparedStatement;
protected static _rdoResultset m_IResultset;
protected static Variant varEmpty;
$$ENDIF
$$IF(comments)
/**
* Fields to include in the display and their types.
* Any fields you didn't select will still appear here, but they will
* be commented out. To include a field, just uncomment it.
*/
$$ENDIF
static DBField[] field =
{
$$BEGINLOOP(fieldcount)
$$IF(include)
$$fieldentry$$
$$ELSE
// $$fieldentry$$
$$ENDIF
$$ENDLOOP
};
$$IF(comments)
/**
* An associative array that maps the string name of a field
* to the DBField class associated with it.
*/
$$ENDIF
static Hashtable hashField = new Hashtable();
static
{
for (int i = 0; i < field.length; i++)
{
hashField.put(field[i].strName, field[i]);
}
}
$$IF(comments)
/**
* The number of columns to use when laying out fields.
* If you want to specify anthing other than a fixed number of
* columns, you can change the initial value of this variable.
*
* Here is an example that will ensure that no column has more
* than 10 rows in it.
*
* protected int columns = (field.length + 9) / 10;
*
*/
$$ENDIF
protected int columns = 1;
$$IF(comments)
/**
* Width of TextFields (in characters)
*/
$$ENDIF
protected int textFieldWidth = 25;
$$IF(comments)
/**
* Global vName variant for COM thread safety
*/
$$ENDIF
Variant vName;
$$IF(comments)
/**
* Global vValue variant for COM thread safety
*/
$$ENDIF
Variant vValue;
$$IF(comments)
/**
* Global vOriginalValue variant for COM thread safety
*/
$$ENDIF
Variant vOriginalValue;
$$IF(comments)
// Containers of database components
$$ENDIF
protected Panel db = new Panel();
protected Panel dbcolumn[] = new Panel[columns * 2];
$$IF(comments)
// Toolbar with "Next", "Prev", "First" and "Last" buttons
$$ENDIF
protected Panel tools = new Panel();
protected Panel toolbar = new Panel();
protected Button buttonFirst = new Button("<< First");
protected Button buttonPrev = new Button("< Prev");
protected Button buttonNext = new Button("Next >");
protected Button buttonLast = new Button("Last >>");
$$IF(comments)
// Toolbar with "Save!" and "Re-read" buttons
$$ENDIF
protected Panel updatetools = new Panel();
protected Button buttonUpdate = new Button("Save!");
protected Button buttonAdd = new Button("Add!");
protected Button buttonDelete = new Button("Delete!");
protected Button buttonReread = new Button("Re-read");
protected Button buttonExit = new Button("Exit");
$$IF(comments)
// User feedback labels for showing current position in database.
// Need a big empty status label to allow for status string display
$$ENDIF
protected Panel feedback = new Panel();
protected Label labelDatabase = new Label("", Label.CENTER);
protected Label labelRecordset = new Label("", Label.CENTER);
protected Label labelPosition = new Label("", Label.CENTER);
protected Label labelStatus = new Label(" ");
$$IF(comments)
/**
* Run the frame window
*/
$$ENDIF
protected void makeFrame()
{
frame = new DBFrame(this, "$$AppName$$");
}
$$IF(comments)
/**
* Entry point for applications
* @param args Arguments to the application.
*/
$$ENDIF
public static void main(String[] args)
{
applet = new $$AppName$$();
applet.makeFrame();
applet.frame.add("Center", applet);
applet.init();
applet.start();
applet.frame.pack();
applet.frame.show();
applet.frame.setResizable(false);
}
$$IF(comments)
/**
* The getAppletInfo() method returns a string describing the applet's
* author, copyright date, or miscellaneous information.
*/
$$ENDIF
public String getAppletInfo()
{
return "Name: $$AppName$$\n" +
"Author: Unknown\n" +
"Created with Microsoft Visual J++ Version 1.1";
}
public void init()
{
$$IF(comments)
//--------------------------------------------------------------------------
$$IF(use_DAO)
// DAO SUPPORT: Open the database and set up field info
// Initialize database engine through license manager
$$ELSE
// RDO SUPPORT: Open the data source and set up field info
// Initialize database engine through license manager
$$ENDIF
//--------------------------------------------------------------------------
$$ENDIF
$$IF(comments)
// Use a global empty variant for optional variant args
$$ENDIF
varEmpty = new Variant();
varEmpty.putEmpty();
$$IF(use_DAO)
ILicenseMgr mgr = new LicenseMgr();
m_IEngine = (_DBEngine)mgr.createWithLic
(
"mbmabptebkjcdlgtjmskjwtsdhjbmkmwtrak",
"{00000010-0000-0010-8000-00AA006D2EA4}",
null,
ComContext.INPROC_SERVER
);
$$IF(comments)
// Open database
$$ENDIF
Variant vExclusive = new Variant();
Variant vReadOnly = new Variant();
Variant vConnect = new Variant();
vExclusive.putBoolean(false);
vReadOnly.putBoolean(readOnly);
vConnect.putString("");
database = m_IEngine.OpenDatabase(strDatabase, vExclusive, vReadOnly, vConnect);
$$IF(comments)
// Create a new recordset
$$ENDIF
Variant vOpenType = new Variant();
Variant vOptions = new Variant();
Variant vLockType = new Variant();
$$// REVIEW: These casts to short probably shouldn't be req'd.
$$// Maybe Variant needs a putShort that takes an int or something.
vOpenType.putShort((short)RecordsetTypeEnum.dbOpenDynaset);
vOptions.putShort((short)0);
$$IF(comments)
// NOTE: The RecordsetOptionEnum.dbReadOnly value is shared by LockTypeEnum
// There is no LockTypeEnum value denoting read only concurrency
$$ENDIF
vLockType.putInt(readOnly ? RecordsetOptionEnum.dbReadOnly : LockTypeEnum.dbOptimistic);
recordset = database.OpenRecordset(strRecordset, vOpenType, vOptions, vLockType);
$$IF(comments)
// Count records in record set
$$ENDIF
if (recordset.getEOF())
recordCount = 0;
else
{
int nOptions = 0;
recordset.MoveLast(nOptions);
recordCount = recordset.getRecordCount();
recordset.MoveFirst();
}
$$ELSE
$$IF(comments)
// Use the runtime key to get the engine interface
$$ENDIF
ILicenseMgr mgr = new LicenseMgr();
m_IEngine = (_rdoEngine)mgr.createWithLic(
"B1692F60-23B0-11D0-8E95-00A0C90F26F8",
"{5E71F04C-551F-11CF-8152-00AA00A40C25}",
null,
ComContext.INPROC_SERVER);
m_IEnvironment = m_IEngine.rdoCreateEnvironment("", "", "");
Variant varPromptOption = new Variant();
Variant varReadOnly = new Variant();
Variant varConnectString = new Variant();
Variant varOptions = varEmpty;
$$IF(comments)
// Set the OpenConnection parameters
$$ENDIF
varPromptOption.putInt(PromptConstants.rdDriverComplete);
varReadOnly.putBoolean(readOnly);
varConnectString.putString(m_strConnect);
$$IF(comments)
// Open a connection
$$ENDIF
m_IConnection = m_IEnvironment.OpenConnection("", varPromptOption,
varReadOnly, varConnectString, varOptions);
$$IF(comments)
// Set the SQL
$$ENDIF
String strSQL = "SELECT ";
for (int nField = 0; nField < field.length; nField++)
{
strSQL += field[nField].strName;
if (nField + 1 < field.length)
strSQL += ", ";
}
strSQL += " FROM " + m_strTable;
Variant varSQL = new Variant();
varSQL.putString(strSQL);
$$IF(comments)
// Set the prepared statement
$$ENDIF
m_IPreparedStatement = m_IConnection.CreatePreparedStatement(m_strDummyQueryName, varSQL);
$$IF(comments)
// Get a resultset, setting cursor type and concurrency
$$ENDIF
Variant varType = new Variant();
Variant varConcurrency = new Variant();
varOptions = varEmpty;
varType.putInt(ResultsetTypeConstants.rdOpenKeyset);
varConcurrency.putInt(LockTypeConstants.rdConcurRowVer);
m_IResultset = m_IConnection.OpenResultset(m_strDummyQueryName,
varType, varConcurrency, varOptions);
$$IF(comments)
// Don't count records, just set nonzero if not empty
$$ENDIF
if (!m_IResultset.getEOF())
recordCount = 1;
$$ENDIF
$$IF(comments)
// Create COM variants
$$ENDIF
vName = new Variant();
vValue = new Variant();
vOriginalValue = new Variant();
$$IF(comments)
//--------------------------------------------------------------------------
$$IF(use_DAO)
// DAO SUPPORT: Create, arrange and initialize components for each field.
$$ELSE
// RDO SUPPORT: Create, arrange and initialize components for each field.
$$ENDIF
//--------------------------------------------------------------------------
$$ENDIF
$$IF(comments)
// Create column panels
$$ENDIF
columns *= 2;
db.setLayout(new GridLayout(0, columns));
for (int col = 0; col < columns; col++)
{
db.add(dbcolumn[col] = new Panel());
dbcolumn[col].setLayout(new GridLayout(0, 1));
}
$$IF(comments)
// Add new components for each field
$$ENDIF
int col = 0;
$$IF(use_DAO)
Fields fields = recordset.getFields();
$$ELSE
rdoColumns rdoCols = m_IResultset.getrdoColumns();
$$ENDIF
for (int i = 0; i < field.length; i++)
{
DBField df = (DBField)hashField.get(field[i].strName);
vName.putString(field[i].strName);
$$IF(use_DAO)
_Field thisfield = fields.getItem(vName);
df.strValidation = thisfield.getValidationText();
df.attributes = thisfield.getAttributes();
$$ELSE
_rdoColumn rdoCol = rdoCols.getItem(vName);
df.attributes = rdoCol.getAttributes();
$$ENDIF
String attribs = new String();
df.field = new TextField(textFieldWidth);
$$IF(use_DAO)
if (!readOnly && ((df.attributes & FieldAttributeEnum.dbUpdatableField) == 0))
$$ELSE
if (!readOnly && ((df.attributes & AttributeConstants.rdUpdatableColumn) == 0))
$$ENDIF
{
attribs += " (ReadOnly)";
df.field.disable();
}
$$IF(use_DAO)
if ((df.attributes & FieldAttributeEnum.dbAutoIncrField) != 0)
$$ELSE
if ((df.attributes & AttributeConstants.rdAutoIncrColumn) != 0)
$$ENDIF
{
attribs += " (Auto)";
df.field.disable();
}
df.label = new Label
(
df.strName + " (" + df.strType + ")" + attribs + " :",
Label.RIGHT
);
if (readOnly)
{
df.field.disable();
}
dbcolumn[col++].add(df.label);
dbcolumn[col++].add(df.field);
col %= columns;
}
$$IF(comments)
// Set the layout of the toolbar panel
$$ENDIF
toolbar.setLayout(new GridLayout(1, 0));
toolbar.add(buttonFirst);
toolbar.add(buttonPrev);
toolbar.add(buttonNext);
toolbar.add(buttonLast);
$$IF(comments)
// Set the layout of the update tools panel
$$ENDIF
updatetools.setLayout(new GridLayout(1, 0));
updatetools.add(buttonUpdate);
updatetools.add(buttonAdd);
updatetools.add(buttonDelete);
updatetools.add(buttonReread);
$$IF(comments)
// Combine the tools together
$$ENDIF
tools.setLayout(new FlowLayout());
tools.add(toolbar);
if (!readOnly)
{
tools.add(updatetools);
}
if (frame != null)
tools.add(buttonExit);
$$IF(comments)
// Set layout for feedback panel
$$ENDIF
feedback.setLayout(new GridLayout(0, 1));
feedback.add(labelDatabase);
feedback.add(labelRecordset);
feedback.add(labelPosition);
feedback.add(labelStatus);
$$IF(comments)
// Lay out the component groups vertically
$$ENDIF
GridBagLayout gb = new GridBagLayout();
setLayout(gb);
GridBagConstraints c = new GridBagConstraints();
c.insets = new Insets(20, 30, 20, 30);
c.gridx = 0;
add(feedback);
gb.setConstraints(feedback, c);
add(db);
gb.setConstraints(db, c);
add(tools);
gb.setConstraints(tools, c);
$$IF(comments)
// Update fields based on current record
$$ENDIF
updateUI();
$$IF(todo)
// TODO: Place additional initialization code here
$$ENDIF
}
$$IF(comments)
/**
* Destroy() is called when your applet is terminating and being unloaded.
*/
$$ENDIF
public void destroy()
{
$$IF(todo)
// TODO: Place applet cleanup code here
$$ENDIF
}
$$IF(comments)
/**
* The start() method is called when the page containing the applet
* first appears on the screen. The AppletWizard's initial implementation
* of this method starts execution of the applet's thread.
*/
$$ENDIF
public void start()
{
$$IF(todo)
// TODO: Place applet startup code here
$$ENDIF
}
$$IF(comments)
/**
* The stop() method is called when the page containing the applet is
* no longer on the screen. We get rid of our frame window here (if one
* exists) and either exit the system (if we're an application) or
* enable the button again (if we're an applet).
*/
$$ENDIF
public synchronized void stop()
{
$$IF(comments)
// If started as application, exit
$$ENDIF
if (frame != null)
System.exit(0);
$$IF(todo)
// TODO: Place additional applet stop code here
$$ENDIF
}
$$IF(comments)
/**
* This method is called when an action occurs inside this component.
* @param evt Event class with information about the action.
* @param o Argument object.
* @return True if the event was handled, false otherwise
*/
$$ENDIF
public synchronized boolean action(Event evt, Object o)
{
$$IF(comments)
//--------------------------------------------------------------------------
$$IF(use_DAO)
// DAO SUPPORT: Perform actions when the user presses a toolbar button
$$ELSE
// RDO SUPPORT: Perform actions when the user presses a toolbar button
$$ENDIF
//--------------------------------------------------------------------------
$$ENDIF
labelStatus.setText("");
if (evt.target == buttonFirst)
{
$$IF(use_DAO)
recordset.MoveFirst();
updateUI();
$$ELSE
m_IResultset.MoveFirst();
updateUI();
buttonFirst.enable(false);
buttonPrev.enable(false);
$$ENDIF
return true;
}
else if (evt.target == buttonPrev)
{
$$IF(use_DAO)
recordset.MovePrevious();
updateUI();
$$ELSE
m_IResultset.MovePrevious();
if (m_IResultset.getBOF())
{
buttonPrev.enable(false);
labelStatus.setText("Beginning of resultset reached, can't scroll to prior record.");
}
else
updateUI();
$$ENDIF
return true;
}
else if (evt.target == buttonNext)
{
$$IF(use_DAO)
recordset.MoveNext();
updateUI();
$$ELSE
m_IResultset.MoveNext();
if (m_IResultset.getEOF())
{
buttonNext.enable(false);
labelStatus.setText("End of resultset reached, can't scroll to next record.");
}
else
updateUI();
$$ENDIF
return true;
}
else if (evt.target == buttonLast)
{
$$IF(use_DAO)
int nOptions = 0;
recordset.MoveLast(nOptions);
updateUI();
$$ELSE
Variant varOptions = varEmpty;
m_IResultset.MoveLast(varOptions);
updateUI();
buttonNext.enable(false);
buttonLast.enable(false);
$$ENDIF
return true;
}
else if (evt.target == buttonUpdate)
{
buttonUpdate.disable();
buttonAdd.disable();
buttonUpdate.disable();
$$IF(use_DAO)
updateDatabase(EditModeEnum.dbEditInProgress);
$$ELSE
updateDatabase(EditModeConstants.rdEditInProgress);
$$ENDIF
buttonUpdate.enable();
buttonAdd.enable();
buttonDelete.enable();
return true;
}
else if (evt.target == buttonAdd)
{
buttonUpdate.disable();
buttonAdd.disable();
buttonUpdate.disable();
$$IF(use_DAO)
updateDatabase(EditModeEnum.dbEditAdd);
$$ELSE
updateDatabase(EditModeConstants.rdEditAdd);
$$ENDIF
buttonUpdate.enable();
buttonAdd.enable();
buttonDelete.enable();
return true;
}
else if (evt.target == buttonDelete)
{
buttonUpdate.disable();
buttonAdd.disable();
buttonUpdate.disable();
$$IF(use_DAO)
updateDatabase(EditModeEnum.dbEditNone);
$$ELSE
updateDatabase(EditModeConstants.rdEditNone);
$$ENDIF
buttonUpdate.enable();
buttonAdd.enable();
buttonDelete.enable();
return true;
}
else if (evt.target == buttonReread)
{
updateUI();
return true;
}
else if (evt.target == buttonExit)
{
exit();
}
$$IF(todo)
// TODO: Place additional action handlers here
$$ENDIF
return false;
}
$$IF(comments)
/**
* Close any open results and notify the applet/application to
* close this frame window.
*/
$$ENDIF
protected void exit()
{
$$IF(comments)
$$IF(use_DAO)
// Close any open recordset
$$ELSE
// Close any open resultset
$$ENDIF
$$ENDIF
$$IF(use_DAO)
if (recordset != null)
{
recordset.Close();
recordset = null;
}
$$ELSE
if (m_IResultset != null)
{
m_IResultset.Close();
m_IResultset = null;
}
$$ENDIF
$$IF(comments)
// Notify the application to exit by calling the
// stop() method of the applet.
$$ENDIF
stop();
}
$$IF(comments)
//--------------------------------------------------------------------------
$$IF(use_DAO)
// DAO SUPPORT: Methods to update the database and the UI
$$ELSE
// RDO SUPPORT: Methods to update the database and the UI
$$ENDIF
//--------------------------------------------------------------------------
$$ENDIF
$$IF(comments)
/**
* Updates the contents of the TextField associated with a given field
* using the current record.
* @param f Field to update
*/
$$ENDIF
void updateTextField(DBField f)
{
$$IF(use_DAO)
if (recordset != null)
$$ELSE
if (m_IResultset != null)
$$ENDIF
{
String strValue;
try
{
$$IF(comments)
// Get the field's value at the current record
$$ENDIF
vName.putString(f.strName);
$$IF(comments)
// Attempt to get the value
$$ENDIF
Variant vValue;
$$IF(use_DAO)
vValue = recordset.getCollect(vName);
$$ELSE
vValue = m_IResultset.getCollect(vName);
$$ENDIF
$$IF(comments)
// We want booleans to be 'true' and 'false'
$$ENDIF
$$IF(use_DAO)
if (f.vt == Variant.VariantBoolean)
$$ELSE
if (f.strType.toUpperCase() == "BIT")
$$ENDIF
{
Boolean b = new Boolean(vValue.getBoolean());
strValue = b.toString();
}
else
{
strValue = vValue.toString();
}
}
$$IF(comments)
// Converting a variant of type VT_NULL to a String can cause
// this exception to be thrown.
$$ENDIF
catch (ClassCastException c)
{
$$IF(comments)
// Choose "" as the String representation for null.
$$ENDIF
strValue = "";
}
$$IF(comments)
// Set the text of the TextField associated with the field
$$ENDIF
f.field.setText(strValue);
}
}
$$IF(comments)
/**
* Updates the field in the database based on the associated TextField.
* @param df Field to update.
*/
$$ENDIF
void updateDatabaseField(DBField df) throws Exception
{
$$IF(use_DAO)
if (recordset != null)
$$ELSE
if (m_IResultset != null)
$$ENDIF
{
try
{
$$IF(comments)
// Convert field value to the right variant type
$$ENDIF
String strValue = df.field.getText();
vValue.putString(strValue);
vName.putString(df.strName);
$$IF(comments)
// We want booleans to be 'true' and 'false'
$$ENDIF
$$IF(use_DAO)
if (df.vt == Variant.VariantBoolean)
$$ELSE
if (df.strType.toUpperCase() == "BIT")
$$ENDIF
{
if (strValue.equals("true"))
{
vValue.putBoolean(true);
}
else
if (strValue.equals("false"))
{
vValue.putBoolean(false);
}
else
{
throw new Exception(df.strName + " must be either 'true' or 'false' in lowercase.");
}
}
$$IF(use_DAO)
else
{
$$IF(comments)
// Convert to variant type
$$ENDIF
vValue.changeType(df.vt);
}
$$ENDIF
$$IF(comments)
// Write to database unless the original value was null
// and the text field is empty.
$$ENDIF
$$IF(use_DAO)
vOriginalValue = recordset.getCollect(vName);
$$ELSE
vOriginalValue = m_IResultset.getCollect(vName);
$$ENDIF
if (!((vOriginalValue.getvt() == Variant.VariantNull) &&
(strValue.equals(""))))
{
$$IF(use_DAO)
recordset.putCollect(vName, vValue);
$$ELSE
m_IResultset.putCollect(vName, vValue);
$$ENDIF
}
}
$$IF(comments)
// Converting a variant of type VT_NULL can cause this exception
$$ENDIF
catch (ClassCastException c)
{
}
}
}
$$IF(comments)
/**
* Converts an exception into a human readable string possibly involving the
* DBField structure and the HRESULT value if it's a COM exception.
*/
$$ENDIF
protected String getExceptionMessage(Exception e, DBField f)
{
if (e instanceof ComFailException)
{
$$IF(use_DAO)
int h = ((ComFailException)e).getHResult();
switch (h)
{
case 0x800a0c5b:
return "Field value is too long";
case 0x800a0cf4:
if ((f == null) || f.strValidation.equals(""))
{
return "Validation rule failed";
}
else
{
return f.strValidation;
}
default:
return "DAO COM Exception " + e.getMessage();
}
$$ELSE
return "RDO COM Exception " + e.getMessage();
$$ENDIF
}
return e.getMessage();
}
$$IF(comments)
/**
* Updates the database from the information in the TextFields.
*/
$$ENDIF
protected void updateDatabase(int nMode)
{
$$IF(comments)
// Update each field
$$ENDIF
try
{
$$IF(comments)
// If no mode, must be a delete
$$ENDIF
$$IF(use_DAO)
if (nMode == EditModeEnum.dbEditNone)
{
recordset.Delete();
$$IF(comments)
// If deleting last record, scroll back
$$ENDIF
recordset.MoveNext();
if (recordset.getEOF())
recordset.MovePrevious();
$$IF(comments)
// Decrement the record count
$$ENDIF
recordCount--;
}
$$IF(comments)
// Go into edit/addnew mode to change the recordset
$$ENDIF
else
{
if (nMode == EditModeEnum.dbEditAdd)
recordset.AddNew();
else
recordset.Edit();
$$ELSE
if (nMode == EditModeConstants.rdEditNone)
{
m_IResultset.Delete();
m_IResultset.MoveNext();
$$IF(comments)
// If deleting last record, scroll back
$$ENDIF
if (m_IResultset.getEOF())
{
m_IResultset.MovePrevious();
$$IF(comments)
// If all records deleted set the record count to 0
$$ENDIF
if (m_IResultset.getBOF())
recordCount = 0;
}
}
$$IF(comments)
// Go into edit/addnew mode to change the recordset
$$ENDIF
else
{
if (nMode == EditModeConstants.rdEditAdd)
m_IResultset.AddNew();
else
m_IResultset.Edit();
$$ENDIF
int i;
for (i = 0; i < field.length; i++)
{
$$IF(comments)
// If the field is updatable and it is not autoincrement
$$ENDIF
$$IF(use_DAO)
if (((field[i].attributes & FieldAttributeEnum.dbUpdatableField) != 0) &&
((field[i].attributes & FieldAttributeEnum.dbAutoIncrField) == 0))
$$ELSE
if (((field[i].attributes & AttributeConstants.rdUpdatableColumn) != 0) &&
((field[i].attributes & AttributeConstants.rdAutoIncrColumn) == 0))
$$ENDIF
{
try
{
$$IF(comments)
// Try to update the field.
$$ENDIF
updateDatabaseField(field[i]);
}
catch (Exception e)
{
$$IF(comments)
// If the update failed, alert the user and cancel the update
$$ENDIF
String strError;
strError = "Error updating " + field[i].strName + " : " + getExceptionMessage(e, field[i]);
if (frame != null)
new Alert(frame, "Modification Failed!", strError);
else
labelStatus.setText(strError);
$$IF(use_DAO)
recordset.CancelUpdate(UpdateTypeEnum.dbUpdateRegular);
$$ELSE
m_IResultset.CancelUpdate();
$$ENDIF
break;
}
}
}
if (i == field.length)
{
$$IF(comments)
// Commit the update
$$ENDIF
$$IF(use_DAO)
recordset.Update(UpdateTypeEnum.dbUpdateRegular, false);
if (nMode == EditModeEnum.dbEditAdd)
{
$$IF(comments)
// Increment the record count
$$ENDIF
recordCount++;
$$IF(comments)
// If adding to empty recordset make sure to set currency
$$ENDIF
if (recordset.getEOF())
recordset.MoveFirst();
}
$$ELSE
m_IResultset.Update();
if (nMode == EditModeConstants.rdEditAdd)
{
$$IF(comments)
// Make sure recordCount not zero now
$$ENDIF
recordCount = 1;
$$IF(comments)
// If adding to empty recordset make sure to set currency
$$ENDIF
if (m_IResultset.getEOF())
m_IResultset.MoveFirst();
}
$$ENDIF
}
}
}
$$IF(use_DAO)
catch (ComException eCom)
{
String strError = new String();
Errors errs = m_IEngine.getErrors();
Variant var = new Variant();
for (int n = 0; n < errs.getCount(); n++)
{
var.putInt(n);
dao350.Error err = errs.getItem(var);
if (nMode == EditModeEnum.dbEditAdd)
strError += "Error adding record: ";
else if (nMode == EditModeEnum.dbEditInProgress)
strError += "Error updating record: ";
else
strError += "Error deleting record: ";
strError += err.getDescription();
}
if (frame != null)
new Alert(frame, "Modification Failed!", strError);
else
labelStatus.setText(strError);
}
$$ELSE
catch (ComException eCom)
{
String strError = new String();
rdoErrors errs = m_IEngine.getrdoErrors();
Variant var = new Variant();
for (int n = 0; n < errs.getCount(); n++)
{
var.putInt(n);
rdoError err = errs.getItem(var);
if (nMode == EditModeConstants.rdEditAdd)
strError += "Error adding record: ";
else if (nMode == EditModeConstants.rdEditInProgress)
strError += "Error updating record: ";
else
strError += "Error deleting record: ";
strError += err.getDescription();
}
if (frame != null)
new Alert(frame, "Modification Failed!", strError);
else
labelStatus.setText(strError);
}
$$ENDIF
catch (Exception e)
{
$$IF(comments)
// Failed to update the table in the database
$$ENDIF
String strError;
strError = "Error updating record: " + getExceptionMessage(e, null);
if (frame != null)
new Alert(frame, "Modification Failed!", strError);
else
labelStatus.setText(strError);
}
updateUI();
}
$$IF(comments)
/**
* Updates all TextFields using the field array. Then updates all feedback
* labels based on the current record.
*/
$$ENDIF
protected void updateUI()
{
$$IF(use_DAO)
int pos = -1;
$$ENDIF
$$IF(comments)
// Update each field if there are records
$$ENDIF
if (recordCount > 0)
{
for (int i = 0; i < field.length; i++)
{
try
{
updateTextField(field[i]);
}
catch (Exception e)
{
$$IF(comments)
// Getting the field value failed for some reason
$$ENDIF
String strError;
strError = "Error retrieving field value for " + field[i].strName + " : " + e.getMessage();
if (frame != null)
new Alert(frame, "Data Fetch Failed!", strError);
else
labelStatus.setText(strError);
break;
}
}
$$IF(use_DAO)
pos = recordset.getAbsolutePosition();
$$ENDIF
}
$$IF(comments)
// Set the states of the buttons and the feedback labels
$$ENDIF
$$IF(use_DAO)
buttonFirst.enable(pos != 0 && recordCount > 0);
buttonPrev.enable(pos != 0 && recordCount > 0);
buttonNext.enable(pos != (recordCount - 1));
buttonLast.enable(pos != (recordCount - 1));
labelDatabase.setText("Database: " + strDatabase + (readOnly ? " (Read Only)" : ""));
labelRecordset.setText("Table: " + strRecordset);
labelPosition.setText("Record: " + (pos + 1) + " of " + recordCount);
$$ELSE
buttonFirst.enable(recordCount > 0);
buttonPrev.enable(recordCount > 0);
buttonNext.enable(recordCount > 0);
buttonLast.enable(recordCount > 0);
labelDatabase.setText("Datasource: $$DSN$$" + (readOnly ? " (Read Only)" : ""));
labelRecordset.setText("Table: " + m_strTable);
$$ENDIF
if (recordCount == 0)
labelStatus.setText("There are no records in the table!");
}
$$IF(todo)
// TODO: Place additional applet code here
$$ENDIF
}
$$IF(comments)
///////////////////////////////// End of File /////////////////////////////////
$$ENDIF