<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE sqlMap PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN" "http://www.ibatis.com/dtd/sql-map-2.dtd">
<!--
 Copyright 2000-2004 Apache Software Foundation

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
-->

<!-- The SQL in these maps files has purposely been typed with more verbosity than is actually required.
 For example, many of the selects could have just used SELECT * FROM... instead of listing all of the columns.
 This is to demonstrate that even complex SQL can be handled and coded and formatted the way SQL was meant
 to be (i.e. not a bunch of concatenated strings in a Java class). -->

<sqlMap namespace="Item">

  <typeAlias alias="item" type="com.ibatis.jpetstore.domain.Item"/>

  <cacheModel id="oneDayItem" type="LRU">
    <flushInterval hours="24"/>
  </cacheModel>

  <cacheModel id="oneDayQuantity" type="MEMORY">
    <property value="WEAK" name="reference-type"/>
    <flushInterval hours="24"/>
    <flushOnExecute statement="updateInventoryQuantity"/>
  </cacheModel>

  <resultMap id="itemResult" class="item">
    <result property="itemId" column="ITEMID"/>
    <result property="listPrice" column="LISTPRICE"/>
    <result property="unitCost" column="UNITCOST"/>
    <result property="supplierId" column="SUPPLIER"/>
    <result property="productId" column="PRODUCTID"/>
    <result property="product.productId" column="PRODUCTID"/>
    <result property="product.name" column="NAME"/>
    <result property="product.imagePath" column="IMAGEPATH"/>
    <result property="product.description" column="DESCN"/>
    <result property="product.categoryId" column="CATEGORY"/>
    <result property="status" column="STATUS"/>
    <result property="attribute1" column="ATTR1"/>
    <result property="attribute2" column="ATTR2"/>
    <result property="attribute3" column="ATTR3"/>
    <result property="attribute4" column="ATTR4"/>
    <result property="attribute5" column="ATTR5"/>
  </resultMap>

  <resultMap id="resultWithQuantity" class="item" extends="itemResult">
    <result property="quantity" column="QTY"/>
  </resultMap>

  <select id="getItemListByProduct" resultMap="itemResult" parameterClass="string">
    select
      ITEMID, LISTPRICE, UNITCOST, SUPPLIER, I.PRODUCTID, NAME,
      IMAGEPATH, DESCN, CATEGORY, STATUS, ATTR1, ATTR2, ATTR3, ATTR4, ATTR5
    from ITEM I, PRODUCT P where P.PRODUCTID = I.PRODUCTID and I.PRODUCTID = #value#
  </select>

  <select id="getItem" resultMap="resultWithQuantity" parameterClass="string">
    select
      I.ITEMID, LISTPRICE, UNITCOST, SUPPLIER, I.PRODUCTID, NAME,
      IMAGEPATH, DESCN, CATEGORY, STATUS, ATTR1, ATTR2, ATTR3, ATTR4, ATTR5, QTY
    from ITEM I, INVENTORY V, PRODUCT P where P.PRODUCTID = I.PRODUCTID and I.ITEMID = V.ITEMID and I.ITEMID = #value#
  </select>

  <select id="getInventoryQuantity" resultClass="int" parameterClass="string">
    select QTY as value from INVENTORY where ITEMID = #value#
  </select>

  <update id="updateInventoryQuantity" parameterClass="map">
    update INVENTORY set QTY = QTY - #increment# where ITEMID = #itemId#
  </update>

</sqlMap>
