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

<sqlMap namespace="Item">

  <cacheModel id="oneDayItem" type="MEMORY">
    <flushInterval hours="24"/>
    <property name="reference-type" value="WEAK"/>
  </cacheModel>

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

  <resultMap id="result" class="org.springframework.samples.jpetstore.domain.Item">
    <result property="itemId" column="itemid" columnIndex="1"/>
    <result property="listPrice" column="listprice" columnIndex="2"/>
    <result property="unitCost" column="unitcost" columnIndex="3"/>
    <result property="supplierId" column="supplier" columnIndex="4"/>
    <result property="productId" column="productid" columnIndex="5"/>
    <result property="product.productId" column="productid" columnIndex="5"/>
    <result property="product.name" column="name" columnIndex="6"/>
    <result property="product.description" column="descn" columnIndex="7"/>
    <result property="product.categoryId" column="category" columnIndex="8"/>
    <result property="status" column="status" columnIndex="9"/>
    <result property="attribute1" column="attr1" columnIndex="10"/>
    <result property="attribute2" column="attr2" columnIndex="11"/>
    <result property="attribute3" column="attr3" columnIndex="12"/>
    <result property="attribute4" column="attr4" columnIndex="13"/>
    <result property="attribute5" column="attr5" columnIndex="14"/>
  </resultMap>

  <resultMap id="result-with-quantity" class="org.springframework.samples.jpetstore.domain.Item">
    <result property="itemId" column="itemid" columnIndex="1"/>
    <result property="listPrice" column="listprice" columnIndex="2"/>
    <result property="unitCost" column="unitcost" columnIndex="3"/>
    <result property="supplierId" column="supplier" columnIndex="4"/>
    <result property="productId" column="productid" columnIndex="5"/>
    <result property="product.productId" column="productid" columnIndex="5"/>
    <result property="product.name" column="name" columnIndex="6"/>
    <result property="product.description" column="descn" columnIndex="7"/>
    <result property="product.categoryId" column="category" columnIndex="8"/>
    <result property="status" column="status" columnIndex="9"/>
    <result property="attribute1" column="attr1" columnIndex="10"/>
    <result property="attribute2" column="attr2" columnIndex="11"/>
    <result property="attribute3" column="attr3" columnIndex="12"/>
    <result property="attribute4" column="attr4" columnIndex="13"/>
    <result property="attribute5" column="attr5" columnIndex="14"/>
    <result property="quantity" column="qty" columnIndex="15"/>
  </resultMap>

  <select id="getItemListByProduct" resultMap="result">
    select
      itemid, listprice, unitcost, supplier, i.productid, name,
      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="result-with-quantity">
    select
      i.itemid, listprice, unitcost, supplier, i.productid, name,
      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="java.lang.Integer">
    select qty as value from inventory where itemid = #value#
  </select>

  <update id="updateInventoryQuantity">
    update inventory set qty = qty - #increment# where itemid = #itemId#
  </update>

</sqlMap>
