Cozy Web上でエンティティのモデル定義をすることで、Web上でのリソースアクセスをノンプログラミングで行う方法についてみています。
ここまで、エンティティの作成についてみてきました。
今回はエンティティの更新についてみていきます。
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となります。
トップページ
<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>
</ul>
</body>
</html>
エンティティ操作シナリオ
エンティティのモデル定義を行うとエンティティ作成シナリオが自動的に作成されます。
このエンティティ作成シナリオによって、エンティティの作成オペレーションが可能になったことを前々回、前回に確認しました。
エンティティの更新オペレーションも同様にエンティティ作成シナリオによって可能になります。
エンティティの更新には以下の2種類があります。
- 更新するエンティティのIDを更新画面から入力
- 更新するエンティティのIDを更新開始時に指定
今回は前者の更新エンティティIDを更新画面から入力するシナリオを実行します。
実行
それではエンティティの更新を実行してみましょう。
エンティティ更新ページ
以下のようにリソース名の後ろに「_update_」をつけてアクセスします。
$ curl http://localhost:8080/web/HelloResource/product/_update_
次のようにエンティティ作成に必要なデータを登録するためのFORMの画面が表示されました。
まだビューの定義はされていないので、Cozy Webのデフォルト入力画面が表示されます。
<?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"/>
</head>
<body>
<form action="" method="POST">
<table class="">
<tbody>
<tr class="">
<th scope="row" class="">Id</th>
<td class="">
<input type="text" name="id" value=""/>
</td>
</tr>
<tr class="">
<th scope="row" class="">Name</th>
<td class="">
<input type="text" name="name" value=""/>
</td>
</tr>
<tr class="">
<th scope="row" class="">Price</th>
<td class="">
<input type="text" name="price" value=""/>
</td>
</tr>
</tbody>
</table>
<table>
<tr>
<td>
<input type="submit" name="$submit" value="Input"/>
</td>
<td>
<input type="submit" name="$submit" value="Cancel"/>
</td>
</tr>
</table>
<input type="hidden" name="$scenario" value="{"name":"update-entity","state":"input","entity":"product","schema":{"columns":[{"name":"id","datatype":"token","multiplicity":"1"}, {"name":"name","datatype":"string","multiplicity":"1"}, {"name":"price","datatype":"int","multiplicity":"1"}]},"data":{}}"/>
</form>
</body>
</html>
このページでは以下のボタンが表示されています。
- Input
- データ登録
- Cancel
- キャンセル Inputボタンを押下するとエンティティ作成のための次の画面に遷移します。
Cancelボタンを押下するとトップページに戻ります。
登録データ確認ページ
データ登録画面でInputボタンが押されたので、登録データ確認画面が表示されます。
<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"/>
</head>
<body>
<form action="" method="POST">
<table class="">
<tbody>
<tr class="">
<th scope="row" class="">Id</th>
<td class="">1</td>
</tr>
<tr class="">
<th scope="row" class="">Name</th>
<td class="">Apple</td>
</tr>
<tr class="">
<th scope="row" class="">Price</th>
<td class="">300</td>
</tr>
</tbody>
</table>
<input type="hidden" name="id" value="5"/>
<input type="hidden" name="name" value="Banana"/>
<input type="hidden" name="price" value="250"/>
<table>
<tr>
<td>
<input type="submit" name="$submit" value="Ok"/>
</td>
<td>
<input type="submit" name="$submit" value="Back"/>
</td>
<td>
<input type="submit" name="$submit" value="Cancel"/>
</td>
</tr>
</table>
<input type="hidden" name="$scenario" value="{"name":"update-entity","state":"confirm","entity":"product","schema":{"columns":[{"name":"id","datatype":"token","multiplicity":"1"}, {"name":"name","datatype":"string","multiplicity":"1"}, {"name":"price","datatype":"int","multiplicity":"1"}]},"data":{"name":"Banana","$scenario":"{\"name\":\"update-entity\",\"state\":\"input\",\"entity\":\"product\",\"schema\":{\"columns\":[{\"name\":\"id\",\"datatype\":\"token\",\"multiplicity\":\"1\"},{\"name\":\"name\",\"datatype\":\"string\",\"multiplicity\":\"1\"},{\"name\":\"price\",\"datatype\":\"int\",\"multiplicity\":\"1\"}]},\"data\":{}}","price":"250","id":"5","$submit":"Input"}}"/>
</form>
</body>
</html>
画面に入力したデータが表示されます。この結果を確認した上で、以下のいずれかのボタンを押下します。
- OK
- 確認OK
- Back
- 入力画面にモデル
- Cancel
- キャンセル
Okボタンを押下するとエンティティ作成のための次の画面に遷移します。
結果ページ
登録データ確認画面でOKボタンが押されたので、エンティティの作成が行われ、作成されたエンティティの情報表示ページに移ります。
<?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"/>
</head>
<body>
<form action="" method="POST">
<table class="">
<tbody>
<tr class="">
<th scope="row" class="">Id</th>
<td class="">1</td>
</tr>
<tr class="">
<th scope="row" class="">Name</th>
<td class="">Apple</td>
</tr>
<tr class="">
<th scope="row" class="">Price</th>
<td class="">100</td>
</tr>
</tbody>
</table>
<input type="hidden" name="id" value="1"/>
<input type="hidden" name="name" value="Apple"/>
<input type="hidden" name="price" value="100"/>
<table>
<tr>
<td>
<input type="submit" name="$submit" value="Ok"/>
</td>
</tr>
</table>
<input type="hidden" name="$scenario" value="{"name":"update-entity","state":"show","entity":"product","schema":{"columns":[{"name":"id","datatype":"token","multiplicity":"1"}, {"name":"name","datatype":"string","multiplicity":"1"}, {"name":"price","datatype":"int","multiplicity":"1"}]},"data":{"name":"Banana","$scenario":"{\"name\":\"update-entity\",\"state\":\"confirm\",\"entity\":\"product\",\"schema\":{\"columns\":[{\"name\":\"id\",\"datatype\":\"token\",\"multiplicity\":\"1\"},{\"name\":\"name\",\"datatype\":\"string\",\"multiplicity\":\"1\"},{\"name\":\"price\",\"datatype\":\"int\",\"multiplicity\":\"1\"}]},\"data\":{\"name\":\"a\",\"$scenario\":\"{\\\"name\\\":\\\"update-entity\\\",\\\"state\\\":\\\"input\\\",\\\"entity\\\":\\\"product\\\",\\\"schema\\\":{\\\"columns\\\":[{\\\"name\\\":\\\"id\\\",\\\"datatype\\\":\\\"token\\\",\\\"multiplicity\\\":\\\"1\\\"},{\\\"name\\\":\\\"name\\\",\\\"datatype\\\":\\\"string\\\",\\\"multiplicity\\\":\\\"1\\\"},{\\\"name\\\":\\\"price\\\",\\\"datatype\\\":\\\"int\\\",\\\"multiplicity\\\":\\\"1\\\"}]},\\\"data\\\":{}}\",\"price\":\"10\",\"id\":\"5\",\"$submit\":\"Input\"}}","price":"250","id":"5","$submit":"Ok"}}"/>
</form>
</body>
</html>
作成したエンティティのデータが表示されます。この結果を確認した後に、OKボタンを押下するとトップページに戻ります。
<?xml version="1.0"?>
<!DOCTYPE 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>
</ul>
</body>
</html>
確認
確認のためにリソースproductの一覧を表示してみましょう。
$ curl http://localhost:8080/web/HelloResource/product
以下のようにオブジェクトID 1のAppleの値段が100に変更されました。
<?xml version="1.0"?>
<!DOCTYPE html>
<html>
<head>
<title>Product List</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="">100</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>
<tr data-href="product/4.html">
<td class="">4</td>
<td class="">Banana</td>
<td class="">250</td>
</tr>
</tbody>
</table>
</body>
</html>
まとめ
Cozy Web上でエンティティのモデル定義をすることで、Web上でのリソースアクセスをノンプログラミングで行う方法についてみています。
リソースの作成に続いて、今回はリソースの更新の基本形について説明しました。
次回も引き続きリソースの更新について説明します。
諸元
- Cozy
- 0.0.13
