001: /**
002: * Objective Database Abstraction Layer (ODAL)
003: * Copyright (c) 2004, The ODAL Development Group
004: * All rights reserved.
005: * For definition of the ODAL Development Group please refer to LICENCE.txt file
006: *
007: * Distributable under LGPL license.
008: * See terms of license at gnu.org.
009: */package com.odal.petstore.app;
010:
011: import com.completex.objective.components.persistency.core.adapter.DefaultMappingPersistencyAdapter;
012: import com.completex.objective.components.persistency.core.adapter.DefaultPersistencyAdapter;
013: import com.completex.objective.components.persistency.transact.TransactionManager;
014: import com.completex.objective.tools.SqlCmdTool;
015: import com.completex.objective.util.PropertyMap;
016: import com.odal.petstore.OdalPetstoreException;
017: import com.odal.petstore.domain.Account;
018: import com.odal.petstore.domain.Cart;
019: import com.odal.petstore.domain.CartItem;
020: import com.odal.petstore.domain.Category;
021: import com.odal.petstore.domain.Item;
022: import com.odal.petstore.domain.Order;
023: import com.odal.petstore.domain.Product;
024: import com.odal.petstore.persistence.dao.AccountDaoImpl;
025: import com.odal.petstore.persistence.dao.BeanToPoMapper;
026: import com.odal.petstore.persistence.dao.CategoryDaoImpl;
027: import com.odal.petstore.persistence.dao.ItemDaoImpl;
028: import com.odal.petstore.persistence.dao.OrderDaoImpl;
029: import com.odal.petstore.persistence.dao.ProductDaoImpl;
030: import com.odal.petstore.service.AccountService;
031: import com.odal.petstore.service.CatalogService;
032: import com.odal.petstore.service.OrderService;
033:
034: import java.io.IOException;
035: import java.io.File;
036: import java.sql.SQLException;
037: import java.util.List;
038:
039: /**
040: * @author Gennady Krizhevsky
041: */
042: public class BusinessApp {
043:
044: private AccountService accountService;
045: private CatalogService catalogService;
046: private OrderService orderService;
047: public static final String TEST = "test";
048:
049: public BusinessApp(String configPath) throws Exception,
050: SQLException {
051: init(configPath);
052: resetDatabase(configPath);
053: }
054:
055: private void resetDatabase(String configPath) throws Exception,
056: SQLException {
057: PropertyMap propertyMap = new PropertyMap();
058: propertyMap.loadSdl(configPath);
059: SqlCmdTool sqlCmdTool = new SqlCmdTool(propertyMap);
060: sqlCmdTool.processScript("@sql/petstore-hsqldb-schema.sql");
061: sqlCmdTool.processScript("@sql/petstore-hsqldb-dataload.sql");
062: }
063:
064: void init(String configPath) throws IOException {
065: DefaultMappingPersistencyAdapter mappingPersistency = new DefaultMappingPersistencyAdapter(
066: configPath);
067:
068: TransactionManager transactionManager = mappingPersistency
069: .getTransactionManager();
070: AccountDaoImpl accountDao = new AccountDaoImpl(
071: mappingPersistency);
072: CategoryDaoImpl categoryDao = new CategoryDaoImpl(
073: mappingPersistency);
074: ItemDaoImpl itemDao = new ItemDaoImpl(mappingPersistency);
075: OrderDaoImpl orderDao = new OrderDaoImpl(mappingPersistency);
076: ProductDaoImpl productDao = new ProductDaoImpl(
077: mappingPersistency);
078:
079: accountService = new AccountService(transactionManager,
080: accountDao);
081: catalogService = new CatalogService(transactionManager,
082: categoryDao, itemDao, productDao);
083: orderService = new OrderService(transactionManager, itemDao,
084: orderDao);
085: }
086:
087: //
088: // Account operations:
089: //
090: public void insertAccount() throws OdalPetstoreException {
091: Account account = newAccount();
092: accountService.insertAccount(account);
093: }
094:
095: public Account getAccount() throws OdalPetstoreException {
096: return accountService.getAccount(TEST);
097: }
098:
099: public void updateAccount(Account account)
100: throws OdalPetstoreException {
101: accountService.updateAccount(account);
102: }
103:
104: public void updateAccount2(Account account)
105: throws OdalPetstoreException {
106: accountService.updateAccount2(account);
107: }
108:
109: private Account newAccount() {
110: Account account = new Account(TEST);
111: account.setAddress1("Addr1");
112: account.setAddress2("Addr2");
113: account.setPassword("pass");
114: account.setBannerName("FISH");
115: account.setBannerOpt(new Long(1));
116: account.setCity("New York");
117: account.setCountry("US");
118: account.setEmail("us@email.com");
119: account.setFavCategory("FISH");
120: account.setFirstName("Joe");
121: account.setLastName("Moe");
122: account.setLangPref("en");
123: account.setMyListOpt(new Long(2));
124: account.setPhone("111111111");
125: account.setState("NV");
126: account.setStatus("new");
127: account.setZip("111111");
128: return account;
129: }
130:
131: //
132: // Category operations:
133: //
134: public Category getCategory(String categoryId)
135: throws OdalPetstoreException {
136: return catalogService.getCategory(categoryId);
137: }
138:
139: public List getCategoryList() throws OdalPetstoreException {
140: return catalogService.getCategoryList();
141: }
142:
143: public List getProductsByCategory(String category, int pageNumber)
144: throws OdalPetstoreException {
145: return catalogService.getProductListByCategory(category,
146: pageNumber);
147: }
148:
149: public List getItemListByProduct(String productId)
150: throws OdalPetstoreException {
151: return catalogService.getItemListByProduct(productId);
152: }
153:
154: public Product getProduct(String productId)
155: throws OdalPetstoreException {
156: return catalogService.getProduct(productId);
157: }
158:
159: public List searchProductsByKeywords(String keywords)
160: throws OdalPetstoreException {
161: return catalogService.searchProductList(keywords, 1);
162: }
163:
164: //
165: // Order operations:
166: //
167: public void insertOrder(Order order) throws OdalPetstoreException {
168: orderService.insertOrder(order);
169: }
170:
171: public Order createOrder(Account account, Cart cart) {
172: Order order = new Order();
173: order.initOrder(account, cart);
174: return order;
175: }
176:
177: private void processOrder(List itemListByProduct, BusinessApp app,
178: Account account) throws OdalPetstoreException {
179: Cart cart = new Cart();
180: for (int q = 0; q < itemListByProduct.size(); q++) {
181: Item item = (Item) itemListByProduct.get(q);
182: cart.addItem(item, true);
183: }
184:
185: List cartItemList = cart.getCartItemList();
186: for (int i = 0; i < cartItemList.size(); i++) {
187: CartItem cartItem = (CartItem) cartItemList.get(i);
188: cartItem.incrementQuantity();
189: }
190: int numberOfItems = cart.getNumberOfItems();
191: println("Number Of Items: ", new Integer(numberOfItems));
192:
193: Order order = app.createOrder(account, cart);
194: println("Order in memory: ", order);
195:
196: app.insertOrder(order);
197: }
198:
199: private List getOrdersByUserName(String userName)
200: throws OdalPetstoreException {
201: return orderService.getOrdersByUsername(userName, 1);
202: }
203:
204: private Order getOrder(long orderId) throws OdalPetstoreException {
205: return orderService.getOrder(orderId);
206: }
207:
208: public static void println(String method, Object value) {
209: String valueStr = value == null ? null : value.toString();
210: System.err.println("App: " + method + ": " + valueStr);
211: }
212:
213: public static void main(String[] args) throws Exception,
214: IOException, OdalPetstoreException {
215: if (args.length == 0) {
216: System.out.println("Usage: BusinessApp <persistency.sdl>");
217: System.exit(1);
218: }
219: BusinessApp app = new BusinessApp(args[0]);
220: app.insertAccount();
221: Account account = app.getAccount();
222: println("Account", account);
223:
224: account.setAddress1("Addr1-modified-1");
225: app.updateAccount(account);
226: account = app.getAccount();
227: println("Account", account);
228:
229: account.setAddress1("Addr1-modified-2");
230: app.updateAccount2(account);
231: account = app.getAccount();
232: println("Account", account);
233:
234: Category category = app.getCategory("FISH");
235: println("Category", category);
236:
237: List categoryList = app.getCategoryList();
238: println("Category List", categoryList);
239:
240: categoryList = app.getCategoryList();
241: // println("Category List : is from cache : " + ((Category) categoryList.get(0)).fromCache(), categoryList);
242:
243: List productsByCategory = app.getProductsByCategory("FISH", 1);
244: println("Products By Category", productsByCategory);
245:
246: String productId = ((Product) productsByCategory.get(0))
247: .getProductId();
248: List itemListByProduct = app.getItemListByProduct(productId);
249: println("Item List By Product", itemListByProduct);
250:
251: Product product = app.getProduct(productId);
252: println("Product", product);
253:
254: product = app.getProduct(productId);
255: println("Product : ", product);
256:
257: //
258: // Create cart:
259: //
260: app.processOrder(itemListByProduct, app, account);
261:
262: // Get orders:
263: List orders = app.getOrdersByUserName(account.getUserName());
264: println("Orders of user [" + account.getUserName() + "] : ",
265: orders);
266:
267: // Get the 1st order with details:
268: Order order = app
269: .getOrder(((Order) orders.get(0)).getOrderId());
270: println("Order with id " + order.getOrderId(), order);
271: println("Line items for order with id " + order.getOrderId(),
272: order.getLineItems());
273:
274: List list = app
275: .searchProductsByKeywords("fish cats dogs birds reptiles");
276: println("searchProductsByKeywords: " + productId, list);
277:
278: }
279:
280: }
|