商品データ(商品XML)更新処理
画面遷移
商品情報更新処理の画面遷移を以下の図に示します。
「商品情報更新」画面は、商品一覧上の該当商品の「編集」ボタンを押下することによって表示されます。商品情報を画面上で編集後「更新」ボタンを押下することによって、編集内容をXMLDBに反映します。
なお、商品データ更新画面は、HTMLから渡された商品コードを元に、商品を検索し、画面に表示しますが、商品名検索機能と説明が重複しますので、ここでは割愛させていただき、「更新」ボタンが押下された場合のXMLデータ更新処理のみについて説明します。
コントローラ(ItemCRUDServlet)
コントローラとなるサーブレットでは、HTMLから「action="update"」いうパラメータを受け取り、商品データ(商品XML)更新処理を実行します。
- 画面入力されたリクエストパラメータを元にItemクラスを生成し値を格納する
(createItemメソッド) - ItemDaoの
updateメソッドを呼び出す - 商品一覧画面(list.jsp)にForwardする
以下にItemCRUDServletのソースを示します。
}else if (action.equals("update")) { // 【商品更新】更新処理→一覧表示 Item item = createItem( req ); try { int cnt = itemDao.update( item ); } catch (Exception e) { e.printStackTrace(); forwardToErrorPage( req , res ); return; } forwardToList(req, res);
Daoクラス
ItemDaoImplクラス
次にサーブレットから呼ばれるItemDaoImplクラスの実装を説明します。ItemDaoImplクラスのupdateメソッドでは、以下の順でXML更新処理を実行します。
- XUGのテンプレートファイル上で変換するオブジェクトを作成し、Mapに格納
- LuxeonDaoクラスの
updateXMLメソッドを呼び出し、XQueryを実行 - 更新件数を戻す
以下にItemDaoImplクラスのupdateメソッドのソースを示します。
public class ItemDaoImpl implements ItemDao{ private static final String TEMPLATE_RESOURCE_PATH = "/jp/beproud/sample/ecitem/dao/template/"; // ~省略~ public int update(Item item) throws Exception{ Map<String, Object> templeteMap = new HashMap<String, Object>(); templeteMap.put( "item" , item ); return LuxeonDao.updateXML( "items/" + item.getItemCode() + ".xml" , TEMPLATE_RESOURCE_PATH + "Item_update.vm" , templeteMap ); }
LuxeonDaoクラス
ItemDaoImplのupdateメソッドから呼ばれるLuxeonDaoクラスのupdateXMLメソッドは以下の順でXML更新処理を実行します。なお、サンプルアプリケーションでは、XMLの更新に、xmlupdategram(Cyber Luxeonの独自XML更新言語。以下XUG)を用います。XUGの詳細については、Cyber Luxeonで学ぶXMLDB入門 第3回を参照してください。XUGの文字列の生成には検索処理と同様、Velocityを採用しています。
- セッション、XMLStore、検索対象XML(バインダドキュメント)を取得
- Velocityを使用してXQueryのテンプレートと実際のオブジェクト値をマージしてXUGの文字列を取得
- XUGの実行
- 更新結果件数を戻す
- セッションを閉じる
以下にLuxeonDaoクラスのupdateXMLメソッドのソースを示します。
public class LuxeonDao { // ~省略~ public static int updateXML (String xmlName, String templateName, Map<String, Object> templateObjMap ) throws Exception{ Session clientSession = null; VelocityWrapper vw = new VelocityWrapper(); vw.put( templateObjMap ); String xugStr = vw.merge( templateName ); try { clientSession = XlnClientSessionFactory.getSession(); Update xug = clientSession.createUpdate( xugStr ); return xug.execute( xmlName, clientSession.getXMLStore(XML_STORE_NAME) ); }finally{ if ( clientSession != null ) clientSession.release(); } }
以下にサンプルアプリケーションで使用するXUG(Velocityテンプレートファイル)を以下に示します。
<?xml version="1.0" encoding="UTF-8"?>
<xlnupdate version="1.0">
<update select="/item/item_name/text()">
<text>$item.itemName</textn
</updaten
<update select="/item/price/text()"n
<textn$item.price</text>
</update>
<update select="/item/description/caption/text()">
<text>$item.description.caption</text>
</update>
<update select="/item/description/header/text()">
<text>$item.description.header</text>
</update>
<update select="/item/description/footer/text()">
<text>$item.description.footer</text>
</update>
<update select="/item/option_name/text()">
<text>$item.optionName</text>
</update>
<remove select="/item/options"></remove>
<update select="/item/option_name">
<element location="after">
<options/>
</element>
</update>
#foreach ( $option in $item.options)
<update select="/item/options">
<element location="lastchild">
<option>$option</option>
</element>
</update>
#end
</xlnupdate>

