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"