• 検索結果がありません。

– LCRUD – フィルター

ドキュメント内 Microsoft PowerPoint - sMashHandson_06_RESTDB.ppt (ページ 74-92)

Zero Resource Model API

ƒ WebSphere sMash アプリケーションから使用できるコレクショ ン・ライクな API

– SQL は不要

ƒ 主な機能

– LCRUD

ƒ 主要なクラス

– zero.resource.Type

• モデルのメタデータを保持する

• Collectionのpublicメソッドと同じメソッドを持ち、処理はCollectionに委譲する – zero.resource.TypeCollection

• Collectionを操作する(LCRUD)ためのメソッドを提供する

– zero.resource.Collection

• 複数メンバーから成るコレクションを表す

• メンバーを操作するLCRUDインターフェースを提供する

– Typeが提供するメソッドから、処理が移譲される

– zero.resource.Member

• コレクション内の1メンバーを表す

• ユニークなidフィールドと、最終更新日時を表すupdateフィールドが含まれる

Zero Resource Model API ( 続き )

ƒ コーディング・イメージ

import zero.resource.TypeCollection

//Type Collection

の取得

def collection = TypeCollection.retrieve('persons') //全メンバー(Member型)の取得

def allPersons = collection.list()

//フィルタによるメンバーのサブセットの取得

def somePersons = collection.list(firstname__startswith: 'Jo') //1メンバーの取り出し(プライマリー・キー=99)

def person = collection.retrieve(99) //新しいメンバーの作成

def person = collection.create(firstname:'Joe', birthdate:'1978-01-21', ischild: true) //

メンバーの更新

def result = collection.update(id: '1', firstname:'Bob', birth_date:'1961-12-05') //メンバーの削除

def result = collection.delete('1') //Paging APIによるページング

Equal to any field type equals first_name__equals=Alice

Greater than any field gt first_name__gt=A

Greater than or equal any field gte first_name__gte=A

Less than any field lt first_name__lt=L

Less than or equal any field lte first_name__lte=L

Ends with string endswith first_name__endswith=ice

Starts with string startswith first_name__startswith=Al

Contains string contains first_name__contains=Al

In any field in first_name__in=Alice,Bill

Year equal to date, date-time year birthdate__year=2005 Month equal to date, date-time month birthdate__month=12

Day equal to date date-time day birthdate__day=5

After date, time date-time after birthdate__after=2005-12-12 Before date, time date-time before birthdate__before=2005-12-12

birthdate__between=2005-12-12,2006-Zero Resource Model API : データ・バリデーション

ƒ Member クラスの validate メソッド

– エラー情報を List<Map<String,String>> で 返す

• キーはフィールド名

• 値はエラー・メッセージ

ƒ データ作成,更新時に暗黙的にバリ デーションが実施される

– エラー時には

zero.resource.exceptions.ValidationExce

ption がスローされる

//validate

メソッドで明示的にバリデーションを実行

def m = new Member('person', [firstname: 'Joe']) def messages = member.validate()

if(messages) {

// do something useful with messages } else {

TypeCollection.retrieve('person').create(member) }

//

データ作成時には暗黙的にバリデーションが実行される

try {

persons.create(firstname:'Joe’) } catch (ValidationException e) {

def messages = e.messages if (messages) {

// do something useful with messages }

}

ƒ 検索

– URI パターン

• http://localhost:8080/resources/persons

• http://localhost:8081/resources/persons?firstname__contains=Jo&birthdate__d ay=25

– 名前として’Jo’、生年月日の日が25のメンバーを返す

• http://localhost:8080/resources/persons?id__lt=100&order_by=-birthdate,firstname

– IDが100未満のメンバーを、生年月日は降順、名前は昇順で返す

– フォーマット

• 省略時はJSONで返す

• HTTPヘッダー”Accept”または、HTTPパラメータ”format_as”で指定する

• Acceptヘッダー – application/json

– application/atom+xml

• format_asパラメータ

– http://localhost:8080/resources/persons?format_as=atom

– http://localhost:8080/resources/persons?format_as=json

ƒ 検索(続き) – ページング

• startとcountを利用する

• 0ベース・インデックス

–最初の要素は0番目-• http://localhost/resources/persons?start=100&count=199

– 101番目から200番目のメンバーを返す

• http://localhost/resources/persons?firstname_contains=rand&start=5&count=5

– 6番目から10番目までの、名前として’rand’を持つメンバーを返す

Zero Resource Model HTTP REST API ( 続き )

ƒ メンバーの作成

POST /resources/persons Content-Type: application/json {

"firstname": "Bill",

"birthdate": "1976-09-25"

}

ƒ メンバーの更新

PUT /resources/persons/1 Content-Type: application/json {

"firstname": "Janie",

"birthdate": "1973-12-04"

}

ƒ メンバーの削除

Dojo から ZRM へのアクセス

ƒ IBM Dojo 拡張モジュールから, ZRM にアクセ ス可能

– 依存性解決

• <dependency org="dojo" name="zero.dojo"

rev="[1.0.0.0, 2.0.0.0["/>

ƒ zero.resource.DataStore

– Dojo Data API を実装

– サーバーとの間でデータの CRUD 処理が可能

– HTTP REST API を発行し, ZRM と協業可能

var dataStore = new zero.resource.DataStore({ contextRoot: "" });

// Define a callback that fires when all the items are returned.

var gotList = function(items, request) { var itemsList = "";

dojo.forEach(items, function(i) {

itemsList += dataStore.getValue(i, "name") + " ";

});

console.debug("All items are: " + itemsList);

}

var gotError = function(error, request){

alert("The request to the store failed. " + error);

}

//Invoke the search dataStore.fetch({

onComplete: gotList, onError: gotError });

ƒ zero.grid.DataGrid

– Dojo の dojox.grid.Grid を拡張

– グリッド上のデータを作成,編集,

削除,ソート可能

– HTTP REST API を発行し, ZRM と協業可能

<script type="text/javascript"

src="/dojo/dojo.js"

djConfig="parseOnLoad: true"></script>

<script type="text/javascript">

dojo.require("dojo.parser");

dojo.require("zero.resource.DataStore");

dojo.require("zero.grid.DataGrid");

</script>

<style type="text/css">

@import "/dijit/themes/soria/soria.css";

@import "/dojo/resources/dojo.css";

@import "/zero/grid/DataGrid/DataGrid.css";

@import "/dojox/grid/_grid/Grid.css";

@import "/dojox/grid/_grid/soriaGrid.css";

</style>

</head>

……….

<div dojoType="zero.grid.DataGrid" id="thegrid"

resourceCollection="employees"

コマンド・ライン・インターフェース (CLI)

ƒ model sync

– リソース・モデル定義を元に DB テーブルを作成

• 既にテーブルが存在する場合には何もしない

– initial_data.json がある場合は,それを元に初期データを DB テーブルにロード

– 使用方法

• zero model sync

ƒ model reset

– リソース・モデル定義を元に DB テーブルを ( 再 ) 作成

• DBテーブルのドロップ後にmodel syncを実行することと同義

– 使用方法

• zero model reset

ƒ model loaddata

– /app/models/fixtures 下の指定された JSON 形式のファイルからデータを DB テーブルにロード

– 使用方法

• zero model loaddata ファイル名 [ファイル名]*

– 使用例

• zero model loaddata exporteddata.json

ƒ model dumpdata

– DB テーブルのデータを JSON 形式のファイルに書き出す

– 使用方法

• zero model dumpdata [--model={モデル名}] ファイル名

• zero model dumpdata --split [--prefix={モデル名接頭字}]

– 使用例

• zero model dumpdata exporteddata.json

– データを<appRoot>/app/models/fixtures/exporteddata.jsonにJSON形式で書き出す

• zero model dumpdata --model=persons personsdata.json

– personsテーブルのデータを<appRoot>/app/models/fixtures/personsdata.jsonにJSON形 式で書き出す

• zero model dumpdata --split app/models/fixtures

– 各テーブルのデータを<appRoot>/app/models/fixtures下のdump_テーブル名.jsonという名 前のファイルにJSON形式で書き出す

• zero model dumpdata --split --prefix=export app/models/fixtures

– 各テーブルのデータを<appRoot>/app/models/fixtures下のexport_テーブル名.jsonという

名前のファイルにJSON形式で書き出す

コマンド・ライン・インターフェース (CLI) ( 続き )

ƒ model sql

– DB テーブルを操作するための SQL ファイルを生成する使用方法

• zero model sql {sync|drop|reset} ファイル名

– 使用例

• zero model sql sync create_only.sql

– model sync と同等の処理を行う SQL ステートメントを create_only.sql ファイ ルに出力する

• zero model sql drop drop_only.sql

– モデルをドロップする SQL ステートメントを create_only.sql ファイルに出力す る

• zero model sql reset create_and_drop.sql

– model reset と同等の処理を行う SQL ステートメントを create_and_drop.sql

ファイルに出力する

ハンズオン :

RESTful Web アプリケーション開発

ZRM API 編

演習資料 6. REST & DB アクセスの

2.RESTful Web アプリケーション開発 ZRM API 編

をご覧ください。

ハンズオン :

RESTful Web アプリケーション開発

ZRM HTTP API 編

演習資料 6. REST & DB アクセスの

3.RESTful Web アプリケーション開発 ZRM HTTP API 編

をご覧ください。

御清聴ありがとうございました

ドキュメント内 Microsoft PowerPoint - sMashHandson_06_RESTDB.ppt (ページ 74-92)

関連したドキュメント