01: package com.odal.petstore.persistence.dao;
02:
03: import com.completex.objective.components.persistency.MappingPersistency;
04: import com.completex.objective.components.persistency.OdalPersistencyException;
05: import com.odal.petstore.OdalPetstoreException;
06: import com.odal.petstore.domain.Item;
07: import com.odal.petstore.domain.LineItem;
08: import com.odal.petstore.domain.Order;
09: import com.odal.petstore.persistence.gen.cmp.CmpItem;
10: import com.odal.petstore.persistence.gen.pos.InventoryPO;
11: import com.odal.petstore.persistence.iface.ItemDao;
12:
13: import java.util.List;
14:
15: /**
16: * @author Gennady Krizhevsky
17: */
18: public class ItemDaoImpl implements ItemDao {
19: private MappingPersistency persistency;
20:
21: public ItemDaoImpl(MappingPersistency persistency) {
22: this .persistency = persistency;
23: }
24:
25: public void updateAllQuantitiesFromOrder(Order order)
26: throws OdalPetstoreException {
27: try {
28: for (int i = 0; i < order.getLineItems().size(); i++) {
29: LineItem lineItem = (LineItem) order.getLineItems()
30: .get(i);
31: String itemId = lineItem.getItemId();
32: InventoryPO inventory = new InventoryPO(itemId);
33: inventory.setQuantity(lineItem.getQuantity());
34: persistency.update(inventory);
35: }
36: } catch (OdalPersistencyException e) {
37: throw new OdalPetstoreException(e);
38: }
39: }
40:
41: public boolean isItemInStock(String itemId)
42: throws OdalPetstoreException {
43: try {
44: return persistency.selectExists(new InventoryPO(itemId));
45: } catch (OdalPersistencyException e) {
46: throw new OdalPetstoreException(e);
47: }
48: }
49:
50: public List getItemListByProduct(String productId)
51: throws OdalPetstoreException {
52: try {
53: CmpItem itemQuery = new CmpItem();
54: itemQuery.getProduct().setProductId(productId);
55: return (List) persistency.select(itemQuery);
56: } catch (OdalPersistencyException e) {
57: throw new OdalPetstoreException(e);
58: }
59: }
60:
61: public Item getItem(String itemId) throws OdalPetstoreException {
62: try {
63: InventoryPO inventory = (InventoryPO) persistency
64: .load(new InventoryPO(itemId));
65:
66: Item item = (Item) persistency.load(new CmpItem(itemId));
67: item.setQuantity(inventory.getQuantity());
68: return item;
69: } catch (OdalPersistencyException e) {
70: throw new OdalPetstoreException(e);
71: }
72: }
73: }
|