EGL レポート・ハンドラーの作成

EGL レポート・ハンドラーは、JasperReports エンジンが実行時にアクセスできるコードのブロックを提供します。 事前定義関数名により、これらのコード・ブロックの一部が、 JasperReports によるレポートの埋め込みの際に発生するイベントに結合されます。 このようなイベントには、ページ、行項目、またはレポート自体の先頭または末尾が含まれることがあります。 その他のカスタム関数を直接 XML 設計ファイル・ソースから呼び出すことができます。

EGL レポート・ハンドラーを作成するには、次のようにします。
  1. ファイルを入れるプロジェクトまたはフォルダーを識別する。 プロジェクトまたはフォルダーがない場合は、 プロジェクトまたはフォルダーを作成する必要があります。
  2. ワークベンチで「ファイル」>「新規」>「その他」とクリックする。
  3. 「新規」ウィンドウで、EGL を展開する。
  4. レポート・ハンドラー」をクリックする。
  5. 次へ」をクリックする。
  6. EGL ソース・ファイルを入れるプロジェクトまたはフォルダーを選択し、パッケージを選択する。
  7. 「EGL ソース・ファイル名」フィールドに、レポート・ハンドラーのソース・ファイルの名前を入力する。 レポート・ハンドラー名はファイル名と同じになるため、 EGL パーツの命名規則に準拠したファイル名 (例えば myReportHandler) を選択してください。
  8. 完了」をクリックする。

「新規 EGL レポート・ハンドラー」ウィザードは、 レポート埋め込みイベントに対応する関数名のリストを提供します。 JasperReports は、例えばページを入力する前に「beforePageInit()」を呼び出します。 これらの関数のコードを作成するのはユーザーです。

代わりに、以下のステップを実行して、このテンプレート情報を既存のファイルに追加することもできます。
  1. 新規 EGL ソース・ファイルを作成する。
  2. handler と入力した後、Ctrl+space を押す。
このトピックのこれ以降の部分では、次のものを示すコード例を記載しています。

これらの例は、レポート・ハンドラーで考えられる複雑さに十分対応しているとは限りません。 詳しくは、JasperReports の資料を参照してください。

レポート・ハンドラー・テンプレート

以下は、「新規 EGL レポート・ハンドラー」ウィザードで作成されたテンプレート・コードです。
handler handlerName type jasperReport

	// 使用宣言
	use usePartReference;

	// 定数宣言
	const constantName constantType = literal;

	// Data Declarations
	identifierName declarationType;

	// 事前定義された Jasper コールバック関数
	function beforeReportInit()
	end

	 function afterReportInit()
	end

	function beforePageInit()
	end

	function afterPageInit()
	end

	function beforeColumnInit()
	end

	function afterColumnInit()
	end

	function beforeGroupInit(stringVariable string)
	end

	function afterGroupInit(stringVariable string)
	end

	function beforeDetailEval()
	end

	function afterDetailEval()
	end
end

レポート・パラメーターの取得

レポートには、以下の 3 種類の項目が含まれます。 パラメーター (XML ファイル内に設定され変更されない)、変数 (XML ファイルまたはレポート・ハンドラーで変更可能)、およびフィールド (データ・ソース内の名前に合わせる) です。 次のコードの断片は、レポート・ハンドラーでレポート・パラメーターを取得する方法を示しています。
handler my_report_handler type jasperReport

	// Data Declarations
	report_title	String;

	// Jasper callback function
	function beforeReportInit()
		report_title = getReportParameter("ReportTitle");
	end

end

レポート変数の取得および設定

次のコードの断片は、レポート・ハンドラーでレポート変数を取得し、設定する方法を示しています。
handler my_report_handler type jasperReport

	// Data Declarations
	item_count	int;

	// Jasper callback function
	function beforeDetailEval()
		item_count = getReportVariableValue("itemCount");
	end

	function afterDetailEval()
		setReportVariableValue("itemCount", (item_count + 1));
	end
end
レポート・ハンドラー内の変数型は、必ずご使用の XML ソース・ファイル内の変数型と一致させるようにしてください。

レポート・フィールド値の取得

次のコードの断片は、レポート・ハンドラーでレポート・フィールド値を取得する方法を示しています。
handler my_report_handler type jasperReport
	
	// Data Declarations
	employee_first_name	String;

	// Jasper callback function
	function beforeColumnInit()
		employee_first_name = getFieldValue("fName");
	end
end

レポート・ハンドラーでのレポート・データの保存

次のコード例は、後でアクセスするために、顧客レコードを「saveCustomer」という名前で保存する方法を示しています。
handler my_report_handler type jasperReport
	
	// Data Declarations
	customer_array	customerRecordType[];
	c             	customerRecordType;

	// Jasper callback function
	function beforeReportInit()
		customer ReportData;

		//create the ReportData object for the Customer subreport
		c.customer_num = getFieldValue("c_customer_num");
		c.fname        = getFieldValue("c_fname");
		c.lname        = getFieldValue("c_lname");
		c.company      = getFieldValue("c_company");
		c.address1     = getFieldValue("c_address1");
		c.address2     = getFieldValue("c_address2");
		c.city         = getFieldValue("c_city");
		c.state        = getFieldValue("c_state");
		c.zipcode      = getFieldValue("c_zipcode");
		c.phone        = getFieldValue("c_phone");	
		customer_array.appendElement(c);
		customer.data = customer_array;
		addReportData(customer, "saveCustomer");
	end	
end

XML ファイル内のレポート・データの検索

レポート・ハンドラーでレポート・データを保管しました。 次は、以下のようにして、そのデータを XML ソース・ファイルで検索し、サブレポートに渡すことができます。
<jasperReport name="MasterReport" ... scriptletClass="subreports.my_report_handler">
...

<subreport>
	<dataSourceExpression>
		<![CDATA[(JRDataSource)(((subreports.SubReportHandler)
			$P{REPORT_SCRIPTLET}).getReportData( new String("saveCustomer")))]]>;
	</dataSourceExpression>
	<subreportExpression class="java.lang.String">
		<![CDATA["C:/RAD/workspaces/customer_subreport.jasper"]]>;
	</subreportExpression>
</subreport>

...
</jasperReport>

XML 設計文書からの関数の呼び出し

以下は、ごく単純な関数を 1 つ持つレポート・ハンドラーです。
handler my_report_handler type jasperReport
	
function hello () returns (String)
		return("Hello, world!");
	end
end
次のコードを使用して、この関数を XML 設計文書から呼び出します。
<jasperReport name="MasterReport" ... scriptletClass="my_package.my_report_handler">
...

	<summary>
		<band height="40">
			<textField>
				<reportElement positionType="Float" x="0" y="20" width="500" height="15"/>
				<textElement textAlignment="Center">
					<font reportFont="Arial_Bold" size="10"/>
				</textElement>
				<textFieldExpression class="java.lang.String">
					<![CDATA[((my_package.my_report_handler)$P{REPORT_SCRIPTLET}).hello()]]>
				</textFieldExpression>
			</textField>
		</band>
	</summary>

...
</jasperReport>

「Hello, world!」という句がレポートの末尾に印刷されます。

関連概念
EGL レポート作成プロセスの概要
EGL レポートの概要

関連タスク
EGL ソース・ファイルの作成
レポート・テンプレートの使用

関連リファレンス
追加の EGL レポート・ハンドラー関数
EGL ライブラリー ReportLib
EGL レポート・ハンドラー
事前定義された EGL レポート・ハンドラー関数

フィードバック
(C) Copyright IBM Corporation 2000, 2005. All Rights Reserved.