1 package org.springframework.samples.jpetstore.dao.ibatis; 2 3 import java.util.HashMap ; 4 import java.util.List ; 5 import java.util.Map ; 6 7 import org.springframework.dao.DataAccessException; 8 import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport; 9 import org.springframework.samples.jpetstore.dao.ItemDao; 10 import org.springframework.samples.jpetstore.domain.Item; 11 import org.springframework.samples.jpetstore.domain.LineItem; 12 import org.springframework.samples.jpetstore.domain.Order; 13 14 public class SqlMapItemDao extends SqlMapClientDaoSupport implements ItemDao { 15 16 public void updateQuantity(Order order) throws DataAccessException { 17 for (int i = 0; i < order.getLineItems().size(); i++) { 18 LineItem lineItem = (LineItem) order.getLineItems().get(i); 19 String itemId = lineItem.getItemId(); 20 Integer increment = new Integer (lineItem.getQuantity()); 21 Map param = new HashMap (2); 22 param.put("itemId", itemId); 23 param.put("increment", increment); 24 getSqlMapClientTemplate().update("updateInventoryQuantity", param, 1); 25 } 26 } 27 28 public boolean isItemInStock(String itemId) throws DataAccessException { 29 Integer i = (Integer ) getSqlMapClientTemplate().queryForObject("getInventoryQuantity", itemId); 30 return (i != null && i.intValue() > 0); 31 } 32 33 public List getItemListByProduct(String productId) throws DataAccessException { 34 return getSqlMapClientTemplate().queryForList("getItemListByProduct", productId); 35 } 36 37 public Item getItem(String itemId) throws DataAccessException { 38 Item item = (Item) getSqlMapClientTemplate().queryForObject("getItem", itemId); 39 if (item != null) { 40 Integer qty = (Integer ) getSqlMapClientTemplate().queryForObject("getInventoryQuantity", itemId); 41 item.setQuantity(qty.intValue()); 42 } 43 return item; 44 } 45 46 } 47 | Popular Tags |