home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World 1998 October
/
PCWorld_1998-10_cd.bin
/
software
/
prehled
/
inprise
/
JSAMPLES.Z
/
ProviderBean.java
< prev
next >
Wrap
Text File
|
1998-05-08
|
8KB
|
224 lines
/*
* Copyright (c) 1997-1998 Borland International, Inc. All Rights Reserved.
*
* This SOURCE CODE FILE, which has been provided by Borland as part
* of a Borland product for use ONLY by licensed users of the product,
* includes CONFIDENTIAL and PROPRIETARY information of Borland.
*
* USE OF THIS SOFTWARE IS GOVERNED BY THE TERMS AND CONDITIONS
* OF THE LICENSE STATEMENT AND LIMITED WARRANTY FURNISHED WITH
* THE PRODUCT.
*
* IN PARTICULAR, YOU WILL INDEMNIFY AND HOLD BORLAND, ITS RELATED
* COMPANIES AND ITS SUPPLIERS, HARMLESS FROM AND AGAINST ANY CLAIMS
* OR LIABILITIES ARISING OUT OF THE USE, REPRODUCTION, OR DISTRIBUTION
* OF YOUR PROGRAMS, INCLUDING ANY CLAIMS OR LIABILITIES ARISING OUT OF
* OR RESULTING FROM THE USE, MODIFICATION, OR DISTRIBUTION OF PROGRAMS
* OR FILES CREATED FROM, BASED ON, AND/OR DERIVED FROM THIS SOURCE
* CODE FILE.
*/
//Title: Custom Providers and Resolvers
//Version: 2.0
//Copyright: Copyright (c) 1998
//Author: Jens Ole Lauridsen
//Company: Borland International
//Description: Tutorial, example of provider and resolver.
package borland.samples.tutorial.dataset.providers;
import borland.jbcl.dataset.*;
import borland.jbcl.util.Variant;
import java.io.*;
import java.math.BigDecimal;
// ProviderBean
// Provider is an abstract base class for all providers
// LoadCancel is an interface allowing interruptions in the load
// DataLayout is an interface with constants describing the data layout in "data.txt"
//
public class ProviderBean extends Provider implements LoadCancel, DataLayout {
// provideData = main provider method
// Parameters:
// dataSet : StorageDataSet to receive the data.
// toOpen : Flag, that is true if this call happened during dataset.open().
// Certain providers may optionally ignore such a request.
// See the ExecuteOnOpen property for a QueryDescriptor.
//
public void provideData(StorageDataSet dataSet, boolean toOpen) throws DataSetException {
// Definition of columns in the data:
// (See the data file in data.txt)
Column[] columns = new Column[COLUMN_COUNT];
for (int i=0; i<COLUMN_COUNT; i++)
columns[i] = new Column( COLUMN_NAMES[i],
COLUMN_CAPTIONS[i],
COLUMN_TYPES[i] );
// Now create the columns in the StorageDataSet.
// The flags to this method are:
// updateColumns: true; means merge columns into existing persistent columns.
// keepExistingColumns: false; means throw away any non persistent columns.
// emptyRows: true; means empty the rows of the dataSet now.
//
// Returned is the ordinal map from our array of columns to the ordinal
// in the dataset. Note, that the ordinal order will be the same as the
// order as it was specified in replaceColumns, unless a user added some
// persistent columns with preferredOrdinal set.
//
int[] columnMap = ProviderHelp.initData(dataSet, columns, true, false, true);
// Optionally the rowId could be set here.
// The ResolverBean is the only cunsumer of this information.
dataSet.setAllRowIds(false);
Column rowid = dataSet.getColumn(ROWID_NAME);
rowid.setRowId(true);
// Dont display the rowId column.
// The Column property "Hidden" is designed for Providers. It can
// be overridden by the "Visible" property in the designer.
rowid.setHidden(true);
// Empty the dataSet for existing data:
dataSet.empty();
// What is the maximum number of rows wanted ?
int maxRows = java.beans.Beans.isDesignTime() ? dataSet.getMaxDesignRows() : dataSet.getMaxRows();
// Load the data:
if (maxRows != 0)
loadData(dataSet,maxRows,columnMap);
}
// loadData = private method to load the actual data
// Parameters:
// dataSet : StorageDataSet to receive the data.
// maxRows : Maximal number of rows to load, (-1 = load all)
// columnMap: Map from the data columns to the physical ordinals.
//
private void loadData(StorageDataSet dataSet, int maxRows, int[] columnMap) throws DataSetException {
// Reset the cancel value:
cancel = false;
// StorageDataSet.startLoading gives access to an internal array of Variants.
// This is the fastest way to load data into a DataSet.
//
Variant[] variants = dataSet.startLoading(this, RowStatus.LOADED, false);
// Initialize a stream, such that we can close it before the method is exited.
//
FileInputStream fs = null;
try {
// Open the text file: "data.txt", see context from the project tab
//
fs = new FileInputStream("data.txt");
// Skip the first 2 lines. They contain an ansi ruler
//
skipLine(fs);
skipLine(fs);
// For each of the wanted rows:
//
for (int row = 0; row < maxRows || maxRows < 0; row++) {
// For each of the columns defined by this provider:
//
for (int columnNo = 0; columnNo < columnMap.length; columnNo++) {
// Get the ordinal of the column in the dataSet:
int ordinal = columnMap[columnNo];
// Read the String value of the field from the file:
String field = readField(fs,columnNo);
// If the field is empty, record this as a NULL:
if (field.length() == 0)
variants[ordinal].setAssignedNull();
else {
// Otherwise parse the text and store the value:
switch(variants[ordinal].getSetType()) {
case Variant.STRING:
variants[ordinal].setString(field);
break;
case Variant.INT:
variants[ordinal].setInt( Integer.parseInt(field.trim()) );
break;
case Variant.BIGDECIMAL:
variants[ordinal].setBigDecimal( new BigDecimal(field) );
break;
// There are no other types (see DataLayout.java)
}
}
}
// StorageDataSet.loadRow() will save the row values in the
// variant array to the store of the dataSet.
dataSet.loadRow();
// Skip the eol marker:
skipLine(fs);
// Stop if the loading was cancelled by another thread:
if (cancel)
break;
}
}
// These error handlers are pretty bad...
catch (FileNotFoundException fnf) {
System.out.println("File: data.txt was not found");
// No Data to load...
}
catch (IOException ex) {
// Probably end of file marker...
}
// Close the file:
if (fs != null) {
try {
fs.close();
}
catch (IOException ex) {
// Ignore...
}
}
// Close the load buffer in the StorageDataSet:
dataSet.endLoading();
}
// Skip up to the next line feed character:
private void skipLine(InputStream stream) throws IOException {
int ch = stream.read();
while (ch != '\n')
ch = stream.read();
}
// Read a field from file.
// The columns are setup in a fixed column format.
private String readField(InputStream stream, int columnNo) throws IOException {
int width = COLUMN_WIDTHS[columnNo];
byte[] buffer = new byte[width];
int len = stream.read(buffer);
if (len < width)
throw new IOException("End of data"); // Cheap way to mark the eof...
String field = new String(buffer,0,width);
return field.trim();
}
// The loading can be interrupted by calling this method.
// Another thread may call this method to stop a very slow load.
public void cancelLoad() {
cancel = true;
}
boolean cancel;
}