2023年9月30日土曜日

Cozy Web/一覧表示

Cozy Web上でエンティティのモデル定義によって設定されたリソースの一覧アクセスをノンプログラミングで行う方法についてみていきます。

今回はリソースの一覧表示の機能機能について説明します。

HelloResource

リソースの作成・更新処理の題材としてHelloResourceを定義しました。

今回からこのHelloResourceを使用して、リソースの一覧表示の方法についてみていきます。

準備

cozyを起動するディレクトリのwebappsディレクトリに、アプリケーションのホームディレクトリとなるHelloResourceが作成されています。これをベースに開発を進めます。

モデル

WEB-INF/models/model.orgにアプリケーションで使用するモデルが定義されています。

* entity  
** product
*** features  
table=product
*** attributes  
| Name  | Type   | Multiplicity |
|-------+--------+--------------|
| id    | token  |            1 |
| name  | string |            1 |
| price | int    |            1 |

エンティティproductを定義しています。

これがWebのリソースproductとなります。

トップページ

トップページとしてホームディレクトリに以下のindex.htmlを配備しています。

<html>
    <head>
	<title>HelloResource</title>
    </head>
    <body>
	<ul>
	    <li><a href="product">List</a></li>
	    <li><a href="product/_create_">Create</a></li>
	    <li><a href="product/_update_">Update</a></li>
	    <li><a href="product/_delete_">Delete</a></li>
	</ul>
    </body>
</html>

リソース一覧のシナリオ

リソースの作成・更新では入力フォームの入力から始まるシナリオによって、複数の画面によって作成・更新処理を実行しました。

一方、リソース一覧では一覧表示したいリソースの名前と表示方法を指定するパラメタを渡すことで、すぐに表示結果を得ることができます。

実行

それではリソース一覧の表示を行ってみましょう。

一覧表示

まず、パラメタなしの場合です。

以下のようにリソース名を指定してアクセスします。

$ curl http://localhost:8080/web/HelloResource/product

その結果、以下のページが表示されました。(この結果はxmllintで整形しています。他のHTML出力も同様です。)

<?xml version="1.0"?>
<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <meta name="keywords" content="TBD"/>
    <meta name="description" content="TBD"/>
    <meta name="robots" content="noindex,nofollow"/>
    <meta name="author" content="TBD"/>
    <title>product</title>
  </head>
  <body>
    <table class="">
      <caption class="">product</caption>
      <thead class="">
        <tr class="">
          <th scope="col" class="">Id</th>
          <th scope="col" class="">Name</th>
          <th scope="col" class="">Price</th>
        </tr>
      </thead>
      <tbody class="">
        <tr data-href="product/1.html">
          <td class="">1</td>
          <td class="">Apple</td>
          <td class="">300</td>
        </tr>
        <tr data-href="product/2.html">
          <td class="">2</td>
          <td class="">Orange</td>
          <td class="">350</td>
        </tr>
        <tr data-href="product/3.html">
          <td class="">3</td>
          <td class="">Peach</td>
          <td class="">400</td>
        </tr>
      </tbody>
    </table>
  </body>
</html>

範囲指定

query.offsetパラメタで開始オフセットを、query.limitパラメタで表示の範囲を指定することができます。

以下の例ではquery.offsetに1、query.limitに1を指定しているので、2番目のレコードから1レコードを表示します。

$ curl "http://localhost:8080/web/HelloResource/product?query.offset=1&query.limit=1"

実行の結果、以下のようにレコード数1の表が表示されました。

<?xml version="1.0"?>
<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <meta name="keywords" content="TBD"/>
    <meta name="description" content="TBD"/>
    <meta name="robots" content="noindex,nofollow"/>
    <meta name="author" content="TBD"/>
    <title>product</title>
  </head>
  <body>
    <table class="">
      <caption class="">product</caption>
      <thead class="">
        <tr class="">
          <th scope="col" class="">Id</th>
          <th scope="col" class="">Name</th>
          <th scope="col" class="">Price</th>
        </tr>
      </thead>
      <tbody class="">
        <tr data-href="product/2.html">
          <td class="">2</td>
          <td class="">Orange</td>
          <td class="">350</td>
        </tr>
      </tbody>
    </table>
  </body>
</html>

カラム指定

query.columnsパラメタで表示するカラムを指定することができます。

以下の例ではquery.columnsに「name,price」を指定しているのでnameとpriceの2カラムの表を表示します。

$ curl "http://localhost:8080/web/HelloResource/product?query.columns=name,price"

実行の結果、以下のように2カラムの表が表示されました。

<?xml version="1.0"?>
<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <meta name="keywords" content="TBD"/>
    <meta name="description" content="TBD"/>
    <meta name="robots" content="noindex,nofollow"/>
    <meta name="author" content="TBD"/>
    <title>product</title>
  </head>
  <body>
    <table class="">
      <caption class="">product</caption>
      <thead class="">
        <tr class="">
          <th scope="col" class="">Name</th>
          <th scope="col" class="">Price</th>
        </tr>
      </thead>
      <tbody class="">
        <tr data-href="product/1.html">
          <td class="">Apple</td>
          <td class="">300</td>
        </tr>
        <tr data-href="product/2.html">
          <td class="">Orange</td>
          <td class="">350</td>
        </tr>
        <tr data-href="product/3.html">
          <td class="">Peach</td>
          <td class="">400</td>
        </tr>
      </tbody>
    </table>
  </body>
</html>

まとめ

今回はリソースの一覧表示の基本機能について説明しました。

次回はページングやカスタム画面などの機能について説明する予定です。

諸元

Cozy
0.0.14