ビジネス・コンポーネント・ランタイム・フレームワークは、ビュー・オブジェクトごとにデフォルトのイテレータを作成します。別のイテレータを作成して2つ目のイテレータとして使用することも、デフォルトのイテレータのかわりに使用することもできます。インタフェースoracle.jbo.RowIterator
では、スクロール、行の配置、および関連機能をサポートしています。結果セットのスクロールでは、setRangeSize
およびscrollRange
が重要な役割を果たします。
たとえば、次の図に示すように、([Page Down]キーを押して)行セットをスクロールするか、1行ずつ表示するかをクライアントで選択できるようにするとします。
次のコード例では、ビュー・オブジェクトを1つ作成し、デフォルトのイテレータを使用して1行ずつ表示し、2つ目のイテレータをさらに作成して5行ずつ表示します。この例では、アプリケーション・モジュールを初期化するヘルパー・ルーチンが別の場所ですでに実装されていることを前提にしています。例として、「コード内でのアプリケーション・モジュール・インスタンスの検索」を参照してください。
次のコードでは、setCurrentRowAtRangeIndex()により、行索引が指定された行に移動します。索引がゼロ(0)の場合、索引はRangeの先頭に設定されます。他の数値を指定した場合は、Rangeの指定した数値に対応する行に索引が移動します。
public static void pageDown() {
// Helper routine connects to the generic application module.
ApplicationModule appMod =
QueryDemo.getGenericAppMod(JboContext.PLATFORM_LOCAL);
// Specify the Java file that defines the view object.
// Format: <package>.<filename>
String voDefFile = "d2e.EmpView";
// Identify the view object. Must be a valid Java identifier.
String voName = "demoEmpVO";
// Create the view object within the context defined
// by the application module.
// The view object provides a default iterator.
ViewObject vo = appMod.createViewObject(voName, voDefFile);
// Create another iterator.
RowSetIterator secondIter = vo.createRowSetIterator("Two");
secondIter.setRangeSize(5);
// Array simulates user keystrokes/commands.
int[] keys = {1, 1, 5, 5, 1};
try {
vo.executeQuery();
for (int i = 0; i < keys.length; i++) {
switch (keys[i]) {
// Use the default iterator to print one row.
case 1 :
if (vo.hasNext()) {
Row[] rows = new Row[1];
rows[0] = vo.next();
printRows(rows);
}
break;
case 5 :
// Use the second iterator to print a range of rows.
secondIter.scrollRange(secondIter.getRangeSize());
printRows(secondIter.getAllRowsInRange());
break;
default :
System.exit(0);
break;
}
}
}
catch (Exception e) {
e.printStackTrace();
}
}
// This is a helper method that prints data to the screen.
public static void printRows(Row[] rows) {
for (int r = 0; r < rows.length; r++) {
String rowAttrs = "";
for (int a = 0; a < rows[r].getAttributeCount(); a++) {
rowAttrs += (rows[r].getAttribute(a) + "\t");
}
System.out.println(rowAttrs);
}
System.out.println("-------------");
}
次のコード例では、表内の移動に2つのイテレータを使用します。最初(デフォルト)のイテレータは偶数行を移動し、2番目のイテレータは奇数行を移動します。
public static void oddEven() {
// Helper routine connects to the generic application module.
ApplicationModule appMod =
//amdemo.AppModDemo.getGenericAppMod(platform, -1);
QueryDemo.getGenericAppMod(JboContext.PLATFORM_LOCAL);
// Specify the Java file that defines the view object.
// Format: <package>.<filename>
String voDefFile = "d2e.DeptView";
// Identify the view object. Must be a valid Java identifier.
String voName = "demoDeptVO";
// Create the view object within the context defined
// by the application module.
// The view object provides a default iterator.
ViewObject vo = appMod.createViewObject(voName, voDefFile);
vo.setRangeSize(-1); // -1 = all rows
// Create another iterator.
RowSetIterator secondIter = vo.createRowSetIterator("Two");
secondIter.setRangeSize(-1); // -1 = all rows.
if (!vo.hasNext()) return; // Execute the query.
vo.executeQuery();
// Default iterator gets even-numbered rows.
// Second iterator gets odd-numbered rows.
long nRows = vo.getRowCount();
String msg = "";
for (int i = 0; i < nRows; i +=2) {
// Get and set row index values relative to a range.
// Index of first row = 0.
vo.setCurrentRowAtRangeIndex(i);
Row currRow = vo.getCurrentRow();
msg = " Default iterator (even): " + vo.getRangeIndexOf(currRow);
printRow(currRow, msg);
secondIter.setCurrentRowAtRangeIndex(i + 1);
currRow = secondIter.getCurrentRow();
msg = " Second iterator (odd): " + vo.getRangeIndexOf(currRow);
printRow(secondIter.getCurrentRow(), msg);
}
}
// This is a helper method that prints data to the screen.
public static void printRow(Row row, String msg) {
String rowAttrs = "";
for (int i = 0; i < row.getAttributeCount(); i++) {
rowAttrs += (row.getAttribute(i) + "\t");
}
System.out.println(msg + "\t" + rowAttrs);
}