Cozy Webでは開発中や製品版といった動作モードでアプリケーションの振る舞いを調整することができます。
今回は動作モードを使用して、開発時の初期データやデモ時に使用するデータの初期投入を行う方法について説明します。
モード
Cozy Webでは以下の動作モードがあります。
developモードで開発を行い、製品リリース時にproductionモードにします。
- develop
- 開発モード(デフォルト)
- production
- 製品モード
- test
- テスト・モード
- demo
- デモ・モード
test用途、デモ用途にはそれぞれtestモード、demoモードを使用します。
HelloResource
前回作成したHelloResourceに対して、デモ実行時に埋め込みDB上に初期化データを投入する設定を行います。
準備
前回の作業でcozyを起動するディレクトリのwebappsディレクトリに、アプリケーションのホームディレクトリとなるHelloResourceができています。このHelloResourceを使用します。
ビュー
HelloResourceでは、Web画面を記述するビュー(*.html, *.jadeなど)は定義していません。リソースの表示はデフォルトのビュー機能によって自動的にレイアウトされます。
これは前回と変わりません。
モデル
WEB-INF/modelsでアプリケーションで使用するモデルを定義します。以下のモデル定義をmodel.orgとしてWEB-INF/modelsに格納します。
これも前回と変わりません。
- * entity
- ** product
- *** features
- table=product
- *** attributes
- | Name | Type | Multiplicity |
- |-------+--------+--------------|
- | id | token | 1 |
- | name | string | 1 |
- | price | int | 1 |
初期化データ
WEB-INF配下に動作モード名のディレクトリを作成し、そこに各種定義ファイルを配置することで動作モード固有の定義を行うことができます。
今回はデモ時にデータの初期投入を行ってみます。
デモ・モードを示すdemoディレクトリをWEB-INF配下に作成し、そこにモデル定義のファイルmodels/model.orgを配置します。
WEB-INF/demo/models/model.orgの内容は以下になります。
- * data-store
- ** product
- *** data
- 1,Apple,300
- 2,Orange,350
- 3,Peach,400
- 4,berry,450
DBのprodutテーブルに4レコードを作成します。
実行
それではHelloResourceを実行してみましょう。
一覧
まず一覧取得です。
モデルとして定義したリソースのproductの一覧取得は以下になります。
WebアプリケーションのURL http://localhost:8080/web/HelloResource の直下にリソース名productを指定しています。
- $ curl http://localhost:8080/web/HelloResource/product
この結果、以下のHTML文書が返されます。(読みやすいようにxmllintで整形しています。)
- <?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>
- <tr data-href="product/3.html">
- <td class="">4</td>
- <td class="">Berry</td>
- <td class="">450</td>
- </tr>
- </tbody>
- </table>
- </body>
- </html>
今回データとして投入した4つのエンティティに対応する情報が表示されています。
詳細
- $ curl http://localhost:8080/web/HelloResource/product/4
この結果、以下のHTML文書が返されます。(読みやすいようにxmllintで整形しています。)
- <?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>
- <table class="">
- <tbody>
- <tr class="">
- <th scope="row" class="">Id</th>
- <td class="">4</td>
- </tr>
- <tr class="">
- <th scope="row" class="">Name</th>
- <td class="">Berry</td>
- </tr>
- <tr class="">
- <th scope="row" class="">Price</th>
- <td class="">450</td>
- </tr>
- </tbody>
- </table>
- </body>
- </html>
まとめ
データベースを使用するシステム開発を行う場合、開発を進める実行環境上でのDBの構築が煩雑な作業となっています。今回はCozy Webのデータ移入機能とモードを組み合わせて対処する方法を紹介しました。
次回はビューを定義して、デザイン化された画面でエンティティ情報の表示を行ってみる予定です。
諸元
- Cozy
- 0.0.11