Extensionsを使用して、独自のAPIエンドポイントを作成できます。このチュートリアルでは、パブリックレポートをPDFにエクスポートするエンドポイントを作成する方法について説明します。

ここからエンドポイントを含む完全な拡張機能をダウンロードして、BellaDati拡張機能にインポートできます。拡張機能の「拡張機能のインポート」セクションを参照してください。

前提条件

  • BellaDati 2.8.6.1以降であること。
  • マルチドメイン環境ではSuper Admin、シングルドメイン環境ではドメイン管理者の役割を持っていること。
  • Java / Groovyに精通していること。
  • パブリックレポートであること。レポートの共有の「パブリックレポート」のセクションを参照してください。

チュートリアル

Step 1. 拡張機能の作成

  1. [管理]メニューの[拡張機能]セクションにある[拡張機能を管理する]ページに移動します。
  2. [拡張機能を作成する]をクリックします。
  3. [新しいページを作成する]タイプの拡張機能を作成します。


Step 2. 拡張機能でAPIエンドポイントを作成する

拡張機能を作成した後、拡張機能の詳細にリダイレクトする必要があります。

  1. 左側のパネルで、[APIエンドポイント]をクリックします。
  2. [追加する]ボタンをクリックします。
  3. エンドポイントに名前を付けて、[追加する]をクリックします。

Step 3. レポートをPDFにエクスポートしてクライアントに送信するコードを記述します

エンドポイントを作成した後、コードを記述/貼り付けることができるコードエディタを使用して、エンドポイントの詳細にリダイレクトする必要があります。

Endpointクラスには、 doGet()とdoPost()の2つのメソッドがあります。それぞれがエンドポイント呼び出しメソッド(GET/POST)に基づいて実行されます。

このエンドポイントをGETで呼び出すために、doGet()メソッドでコードを記述します。以下のコードを参照してください:

package com.example;

import com.belladati.extension.BaseExtensionEndpoint;
import org.apache.tapestry5.StreamResponse;

public class MyEndpoint extends BaseExtensionEndpoint {
  
	@Override
	public StreamResponse doGet() {
      	
      try {
        // get parameter value from URL
      	def reportIdParameter = getParameter("reportId");
        int reportId = reportIdParameter.toInteger();
        // export Report to PDF
        File pdfFile = exportToPdf(reportId);
        InputStream is = new FileInputStream(pdfFile);
        
        /**
         * @param is data which are sent to client
         * @param mimeType MIME type of data
         * @param responseStatusCode response status
         */
        // createStreamResponse( InputStream is, String mimeType, int responseStatusCode)
        
       	// send Report exported as PDF to client  
        return createStreamResponse(is, "application/pdf", 200);
      } catch (Exception e) {
            return createStreamResponse(e);
      } 
	}
	@Override
	public StreamResponse doPost() {
		// TODO Auto-generated method stub. Please insert here your code for POST operation.
		return null;
	}
}

Step 4. エンドポイントの使用

エンドポイントを使用する前に、拡張機能をアクティブ化する必要があります。また、パブリックユーザーがエンドポイントを使用できるように、パブリックアクセスをアクティブ化する必要があります。

これでエンドポイントを使用する準備ができたので、試してみることができます。

コードエディタの上にあるリンクをクリックすると、エンドポイントを含むURLが開きます。エクスポートするレポートのIDを、"reportId"パラメーターに追加するだけです(上記の実装を参照)。

URLは次のようになります:

https://service.belladati.com/extension/detail:endpoint/104/exportReportToPDF?reportId=12345

HTMLでの使用例:

<!--Use relative URL in BellaDati Extension-->
<!--Clicking on the button will open the PDF in browser built-in PDF reader if supported-->
<a href="/extension/detail:endpoint/104/exportReportToPDF?reportId=12345">
	<div class="my-button">
	Export to PDF
	</div>
</a>
 
<!--Using download attribute-->
<!--Clicking on the button will save the file without opening it-->
<a 
	href="/extension/detail:endpoint/104/exportReportToPDF?reportId=12345"
	download
>
	<div class="my-button">
	Export to PDF
	</div>
</a>
  • No labels