001: /*
002: * JFox - The most lightweight Java EE Application Server!
003: * more details please visit http://www.huihoo.org/jfox or http://www.jfox.org.cn.
004: *
005: * JFox is licenced and re-distributable under GNU LGPL.
006: */
007: package org.jfox.petstore.action;
008:
009: import java.util.ArrayList;
010: import java.util.Date;
011: import java.util.List;
012: import javax.ejb.EJB;
013:
014: import org.jfox.entity.EntityFactory;
015: import org.jfox.entity.dao.PKGenerator;
016: import org.jfox.framework.annotation.Service;
017: import org.jfox.mvc.ActionContext;
018: import org.jfox.mvc.ActionSupport;
019: import org.jfox.mvc.Invocation;
020: import org.jfox.mvc.PageContext;
021: import org.jfox.mvc.SessionContext;
022: import org.jfox.mvc.annotation.ActionMethod;
023: import org.jfox.mvc.validate.LongValidation;
024: import org.jfox.petstore.bo.OrderBO;
025: import org.jfox.petstore.domain.Cart;
026: import org.jfox.petstore.domain.CartItem;
027: import org.jfox.petstore.entity.Account;
028: import org.jfox.petstore.entity.LineItem;
029: import org.jfox.petstore.entity.Order;
030:
031: /**
032: * @author <a href="mailto:jfox.young@gmail.com">Young Yang</a>
033: */
034: @Service(id="order")
035: public class OrderAction extends ActionSupport {
036: public static final String ORDER_SESSION_KEY = "__ORDER__";
037: private static List<String> creditCardTypes = new ArrayList<String>();
038:
039: static {
040: creditCardTypes.add("Visa");
041: creditCardTypes.add("American Express");
042: creditCardTypes.add("mobile c");
043: }
044:
045: @EJB
046: OrderBO orderBO;
047:
048: @ActionMethod(name="new",successView="NewOrder.vhtml",httpMethod=ActionMethod.HttpMethod.GET)
049: public void doGetNew(ActionContext actionContext) throws Exception {
050: SessionContext sessionContext = actionContext
051: .getSessionContext();
052: Cart cart = (Cart) sessionContext
053: .getAttribute(CartAction.CART_SESSION_KEY);
054:
055: PageContext pageContext = actionContext.getPageContext();
056:
057: if (!sessionContext
058: .containsAttribute(AccountAction.ACCOUNT_SESSION_KEY)) {
059: // not login
060: pageContext.setTargetView("signon.vhtml");
061: return;
062: }
063:
064: Account account = (Account) sessionContext
065: .getAttribute(AccountAction.ACCOUNT_SESSION_KEY);
066:
067: Order order = EntityFactory.newEntityObject(Order.class);
068: order.setOrderId(PKGenerator.getInstance().nextPK());
069: order.setUsername(account.getUsername());
070: order.setOrderDate(new Date());
071:
072: order.setShipToFirstName(account.getFirstName());
073: order.setShipToLastName(account.getLastName());
074: order.setShipAddress1(account.getAddress1());
075: order.setShipAddress2(account.getAddress2());
076: order.setShipCity(account.getCity());
077: order.setShipState(account.getState());
078: order.setShipZip(account.getZip());
079: order.setShipCountry(account.getCountry());
080:
081: order.setBillToFirstName(account.getFirstName());
082: order.setBillToLastName(account.getLastName());
083: order.setBillAddress1(account.getAddress1());
084: order.setBillAddress2(account.getAddress2());
085: order.setBillCity(account.getCity());
086: order.setBillState(account.getState());
087: order.setBillZip(account.getZip());
088: order.setBillCountry(account.getCountry());
089:
090: order.setTotalPrice(cart.getSubTotal());
091:
092: order.setCreditCard("999 9999 9999 9999");
093: order.setExpiryDate("12/03");
094: order.setCardType("Visa");
095: order.setCourier("UPS");
096: order.setLocale("CA");
097: order.setStatus("P");
098:
099: List<LineItem> lineItems = new ArrayList<LineItem>();
100:
101: int i = 1;
102: for (CartItem cartItem : cart.getCartItemList()) {
103: LineItem lineItem = EntityFactory
104: .newEntityObject(LineItem.class);
105: lineItem.setLineNumber(i);
106: lineItem.setOrderId(order.getOrderId());
107: lineItem.setQuantity(cartItem.getQuantity());
108: lineItem.setItemId(cartItem.getItem().getItemId());
109: lineItem.setUnitPrice(cartItem.getItem().getListPrice());
110: lineItem.setItem(cartItem.getItem());
111: lineItems.add(lineItem);
112: i++;
113: }
114: order.setLineItems(lineItems);
115:
116: sessionContext.setAttribute(ORDER_SESSION_KEY, order);
117:
118: pageContext.setAttribute("creditCardTypes", creditCardTypes);
119: pageContext.setAttribute("order", order);
120: }
121:
122: @ActionMethod(name="new",successView="ConfirmOrder.vhtml",invocationClass=NewOrderInvocation.class,httpMethod=ActionMethod.HttpMethod.POST)
123: public void doPostNew(ActionContext actionContext) throws Exception {
124: NewOrderInvocation invocation = (NewOrderInvocation) actionContext
125: .getInvocation();
126: SessionContext sessionContext = actionContext
127: .getSessionContext();
128: // Account account = (Account)sessionContext.getAttribute(AccountAction.ACCOUNT_SESSION_KEY);
129: Order order = (Order) sessionContext
130: .getAttribute(ORDER_SESSION_KEY);
131:
132: order.setShipToFirstName(invocation.getBillToFirstName());
133: order.setShipToLastName(invocation.getBillToLastName());
134: order.setShipAddress1(invocation.getBillAddress1());
135: order.setShipAddress2(invocation.getBillAddress2());
136: order.setShipCity(invocation.getBillCity());
137: order.setShipState(invocation.getBillState());
138: order.setShipZip(invocation.getBillZip());
139: order.setShipCountry(invocation.getBillCountry());
140:
141: order.setBillToFirstName(invocation.getBillToFirstName());
142: order.setBillToLastName(invocation.getBillToLastName());
143: order.setBillAddress1(invocation.getBillAddress1());
144: order.setBillAddress2(invocation.getBillAddress2());
145: order.setBillCity(invocation.getBillCity());
146: order.setBillState(invocation.getBillState());
147: order.setBillZip(invocation.getBillZip());
148: order.setBillCountry(invocation.getBillCountry());
149:
150: order.setCardType(invocation.getCardType());
151: order.setCreditCard(invocation.getCreditCard());
152:
153: if ("1".equals(invocation.getShippingAddressRequired())) {
154: // don't need input shipping address
155: actionContext.getPageContext().setTargetView(
156: "Shipping.vhtml");
157: }
158:
159: PageContext pageContext = actionContext.getPageContext();
160: pageContext.setAttribute("order", order);
161: }
162:
163: @ActionMethod(name="confirmshipping",successView="ConfirmOrder.vhtml",invocationClass=ShippingOrderInvocation.class,httpMethod=ActionMethod.HttpMethod.POST)
164: public void doPostConfirmShipping(ActionContext actionContext)
165: throws Exception {
166: ShippingOrderInvocation invocation = (ShippingOrderInvocation) actionContext
167: .getInvocation();
168: SessionContext sessionContext = actionContext
169: .getSessionContext();
170: Order order = (Order) sessionContext
171: .getAttribute(ORDER_SESSION_KEY);
172:
173: order.setShipToFirstName(invocation.getShipToFirstName());
174: order.setShipToLastName(invocation.getShipToLastName());
175: order.setShipAddress1(invocation.getShipAddress1());
176: order.setShipAddress2(invocation.getShipAddress2());
177: order.setShipCity(invocation.getShipCity());
178: order.setShipState(invocation.getShipState());
179: order.setShipZip(invocation.getShipZip());
180: order.setShipCountry(invocation.getShipCountry());
181:
182: PageContext pageContext = actionContext.getPageContext();
183: pageContext.setAttribute("order", order);
184: }
185:
186: @ActionMethod(name="confirm",successView="ViewOrder.vhtml",errorView="Cart.vhtml",httpMethod=ActionMethod.HttpMethod.GET)
187: public void doGetConfirm(ActionContext actionContext)
188: throws Exception {
189: SessionContext sessionContext = actionContext
190: .getSessionContext();
191: PageContext pageContext = actionContext.getPageContext();
192:
193: Order order = (Order) sessionContext
194: .getAttribute(ORDER_SESSION_KEY);
195: if (order != null) {
196: //insert order
197: try {
198: orderBO.insertOrder(order);
199:
200: sessionContext.removeAttribute(ORDER_SESSION_KEY);
201: sessionContext
202: .removeAttribute(CartAction.CART_SESSION_KEY);
203: pageContext.setAttribute("order", order);
204: pageContext.setAttribute("orderUtil", OrderUtil
205: .getInstance());
206: } catch (Exception e) {
207: Cart cart = (Cart) sessionContext
208: .getAttribute(CartAction.CART_SESSION_KEY);
209: pageContext.setAttribute("cart", cart);
210: throw e;
211: }
212: }
213: }
214:
215: @ActionMethod(name="list",successView="ListOrders.vhtml",httpMethod=ActionMethod.HttpMethod.GET)
216: public void doGetList(ActionContext actionContext) throws Exception {
217: SessionContext sessionContext = actionContext
218: .getSessionContext();
219: Account account = (Account) sessionContext
220: .getAttribute(AccountAction.ACCOUNT_SESSION_KEY);
221: List<Order> orders = orderBO.getOrdersByUsername(account
222: .getUsername());
223:
224: PageContext pageContext = actionContext.getPageContext();
225: pageContext.setAttribute("orders", orders);
226: }
227:
228: @ActionMethod(name="view",successView="ViewOrder.vhtml",invocationClass=ViewOrderInvocation.class,httpMethod=ActionMethod.HttpMethod.GET)
229: public void doGetView(ActionContext actionContext) throws Exception {
230: ViewOrderInvocation invocation = (ViewOrderInvocation) actionContext
231: .getInvocation();
232: long orderId = invocation.getOrderId();
233: Order order = orderBO.getOrder(orderId);
234: PageContext pageContext = actionContext.getPageContext();
235: pageContext.setAttribute("order", order);
236: pageContext.setAttribute("orderUtil", OrderUtil.getInstance());
237: }
238:
239: public static class OrderUtil {
240: private static OrderUtil instance = new OrderUtil();
241:
242: private OrderUtil() {
243:
244: }
245:
246: public static OrderUtil getInstance() {
247: return instance;
248: }
249:
250: public double getTotalPrice(LineItem lineItem) {
251: return lineItem.getUnitPrice() * lineItem.getQuantity();
252: }
253: }
254:
255: public static class ViewOrderInvocation extends Invocation {
256:
257: @LongValidation
258: private long orderId;
259:
260: public long getOrderId() {
261: return orderId;
262: }
263:
264: public void setOrderId(long orderId) {
265: this .orderId = orderId;
266: }
267: }
268:
269: public static class NewOrderInvocation extends Invocation {
270: private String cardType;
271: private String creditCard;
272: private String expiryDate;
273: private String billToFirstName;
274: private String billToLastName;
275: private String billAddress1;
276: private String billAddress2;
277: private String billCity;
278: private String billState;
279: private String billZip;
280: private String billCountry;
281: private String shippingAddressRequired;
282:
283: public String getBillAddress1() {
284: return billAddress1;
285: }
286:
287: public void setBillAddress1(String billAddress1) {
288: this .billAddress1 = billAddress1;
289: }
290:
291: public String getBillAddress2() {
292: return billAddress2;
293: }
294:
295: public void setBillAddress2(String billAddress2) {
296: this .billAddress2 = billAddress2;
297: }
298:
299: public String getBillCity() {
300: return billCity;
301: }
302:
303: public void setBillCity(String billCity) {
304: this .billCity = billCity;
305: }
306:
307: public String getBillCountry() {
308: return billCountry;
309: }
310:
311: public void setBillCountry(String billCountry) {
312: this .billCountry = billCountry;
313: }
314:
315: public String getBillState() {
316: return billState;
317: }
318:
319: public void setBillState(String billState) {
320: this .billState = billState;
321: }
322:
323: public String getBillToFirstName() {
324: return billToFirstName;
325: }
326:
327: public void setBillToFirstName(String billToFirstName) {
328: this .billToFirstName = billToFirstName;
329: }
330:
331: public String getBillToLastName() {
332: return billToLastName;
333: }
334:
335: public void setBillToLastName(String billToLastName) {
336: this .billToLastName = billToLastName;
337: }
338:
339: public String getBillZip() {
340: return billZip;
341: }
342:
343: public void setBillZip(String billZip) {
344: this .billZip = billZip;
345: }
346:
347: public String getCardType() {
348: return cardType;
349: }
350:
351: public void setCardType(String cardType) {
352: this .cardType = cardType;
353: }
354:
355: public String getCreditCard() {
356: return creditCard;
357: }
358:
359: public void setCreditCard(String creditCard) {
360: this .creditCard = creditCard;
361: }
362:
363: public String getExpiryDate() {
364: return expiryDate;
365: }
366:
367: public void setExpiryDate(String expiryDate) {
368: this .expiryDate = expiryDate;
369: }
370:
371: public String getShippingAddressRequired() {
372: return shippingAddressRequired;
373: }
374:
375: public void setShippingAddressRequired(
376: String shippingAddressRequired) {
377: this .shippingAddressRequired = shippingAddressRequired;
378: }
379: }
380:
381: public static class ShippingOrderInvocation extends Invocation {
382: private String shipToFirstName;
383: private String shipToLastName;
384: private String shipAddress1;
385: private String shipAddress2;
386: private String shipCity;
387: private String shipState;
388: private String shipZip;
389: private String shipCountry;
390:
391: public String getShipAddress1() {
392: return shipAddress1;
393: }
394:
395: public void setShipAddress1(String shipAddress1) {
396: this .shipAddress1 = shipAddress1;
397: }
398:
399: public String getShipAddress2() {
400: return shipAddress2;
401: }
402:
403: public void setShipAddress2(String shipAddress2) {
404: this .shipAddress2 = shipAddress2;
405: }
406:
407: public String getShipCity() {
408: return shipCity;
409: }
410:
411: public void setShipCity(String shipCity) {
412: this .shipCity = shipCity;
413: }
414:
415: public String getShipCountry() {
416: return shipCountry;
417: }
418:
419: public void setShipCountry(String shipCountry) {
420: this .shipCountry = shipCountry;
421: }
422:
423: public String getShipState() {
424: return shipState;
425: }
426:
427: public void setShipState(String shipState) {
428: this .shipState = shipState;
429: }
430:
431: public String getShipToFirstName() {
432: return shipToFirstName;
433: }
434:
435: public void setShipToFirstName(String shipToFirstName) {
436: this .shipToFirstName = shipToFirstName;
437: }
438:
439: public String getShipToLastName() {
440: return shipToLastName;
441: }
442:
443: public void setShipToLastName(String shipToLastName) {
444: this .shipToLastName = shipToLastName;
445: }
446:
447: public String getShipZip() {
448: return shipZip;
449: }
450:
451: public void setShipZip(String shipZip) {
452: this .shipZip = shipZip;
453: }
454:
455: }
456:
457: public static void main(String[] args) {
458:
459: }
460: }
|