001: package com.ibatis.jpetstore.presentation;
002:
003: import java.util.ArrayList;
004: import java.util.Collections;
005: import java.util.List;
006: import java.util.Map;
007:
008: import org.huihoo.jfox.soaf.container.ServiceFactory;
009:
010: import com.ibatis.common.util.PaginatedList;
011: import com.ibatis.jpetstore.domain.Account;
012: import com.ibatis.jpetstore.domain.Order;
013: import com.ibatis.jpetstore.service.AccountService;
014: import com.ibatis.jpetstore.service.OrderService;
015: import com.ibatis.struts.ActionContext;
016: import com.ibatis.struts.BaseBean;
017:
018: public class OrderBean extends BaseBean {
019:
020: /* Constants */
021:
022: private AccountService accountService = (AccountService) ServiceFactory
023: .getInstance().getService(AccountService.class);
024:
025: private OrderService orderService = (OrderService) ServiceFactory
026: .getInstance().getService(OrderService.class);
027:
028: private static final List CARD_TYPE_LIST;
029:
030: /* Private Fields */
031:
032: private Order order;
033:
034: private int orderId;
035:
036: private boolean shippingAddressRequired;
037:
038: private boolean confirmed;
039:
040: private PaginatedList orderList;
041:
042: private String pageDirection;
043:
044: /* Static Initializer */
045:
046: static {
047: List cardList = new ArrayList();
048: cardList.add("Visa");
049: cardList.add("MasterCard");
050: cardList.add("American Express");
051: CARD_TYPE_LIST = Collections.unmodifiableList(cardList);
052: }
053:
054: /* Constructors */
055:
056: public OrderBean() {
057: this .order = new Order();
058: this .shippingAddressRequired = false;
059: this .confirmed = false;
060: }
061:
062: /* JavaBeans Properties */
063:
064: public int getOrderId() {
065: return orderId;
066: }
067:
068: public void setOrderId(int orderId) {
069: this .orderId = orderId;
070: }
071:
072: public Order getOrder() {
073: return order;
074: }
075:
076: public void setOrder(Order order) {
077: this .order = order;
078: }
079:
080: public boolean isShippingAddressRequired() {
081: return shippingAddressRequired;
082: }
083:
084: public void setShippingAddressRequired(
085: boolean shippingAddressRequired) {
086: this .shippingAddressRequired = shippingAddressRequired;
087: }
088:
089: public boolean isConfirmed() {
090: return confirmed;
091: }
092:
093: public void setConfirmed(boolean confirmed) {
094: this .confirmed = confirmed;
095: }
096:
097: public List getCreditCardTypes() {
098: return CARD_TYPE_LIST;
099: }
100:
101: public List getOrderList() {
102: return orderList;
103: }
104:
105: public String getPageDirection() {
106: return pageDirection;
107: }
108:
109: public void setPageDirection(String pageDirection) {
110: this .pageDirection = pageDirection;
111: }
112:
113: /* Public Methods */
114:
115: public String newOrderForm() {
116: Map sessionMap = ActionContext.getActionContext()
117: .getSessionMap();
118: AccountBean accountBean = (AccountBean) sessionMap
119: .get("accountBean");
120: CartBean cartBean = (CartBean) sessionMap.get("cartBean");
121:
122: clear();
123: if (accountBean == null || !accountBean.isAuthenticated()) {
124: ActionContext
125: .getActionContext()
126: .setSimpleMessage(
127: "You must sign on before attempting to check out. Please sign on and try checking out again.");
128: return "signon";
129: } else if (cartBean != null) {
130: // Re-read account from DB at team's request.
131: Account account = accountService.getAccount(accountBean
132: .getAccount().getUsername());
133: order.initOrder(account, cartBean.getCart());
134: return "success";
135: } else {
136: ActionContext
137: .getActionContext()
138: .setSimpleMessage(
139: "An order could not be created because a cart could not be found.");
140: return "failure";
141: }
142: }
143:
144: public String newOrder() {
145: Map sessionMap = ActionContext.getActionContext()
146: .getSessionMap();
147:
148: if (shippingAddressRequired) {
149: shippingAddressRequired = false;
150: return "shipping";
151: } else if (!isConfirmed()) {
152: return "confirm";
153: } else if (getOrder() != null) {
154:
155: orderService.insertOrder(order);
156:
157: CartBean cartBean = (CartBean) sessionMap.get("cartBean");
158: cartBean.clear();
159:
160: ActionContext.getActionContext().setSimpleMessage(
161: "Thank you, your order has been submitted.");
162:
163: return "success";
164: } else {
165: ActionContext
166: .getActionContext()
167: .setSimpleMessage(
168: "An error occurred processing your order (order was null).");
169: return "failure";
170: }
171: }
172:
173: public String listOrders() {
174: Map sessionMap = ActionContext.getActionContext()
175: .getSessionMap();
176: AccountBean accountBean = (AccountBean) sessionMap
177: .get("accountBean");
178: orderList = orderService.getOrdersByUsername(accountBean
179: .getAccount().getUsername());
180: return "success";
181: }
182:
183: public String switchOrderPage() {
184: if ("next".equals(pageDirection)) {
185: orderList.nextPage();
186: } else if ("previous".equals(pageDirection)) {
187: orderList.previousPage();
188: }
189: return "success";
190: }
191:
192: public String viewOrder() {
193: Map sessionMap = ActionContext.getActionContext()
194: .getSessionMap();
195: AccountBean accountBean = (AccountBean) sessionMap
196: .get("accountBean");
197:
198: order = orderService.getOrder(orderId);
199:
200: if (accountBean.getAccount().getUsername().equals(
201: order.getUsername())) {
202: return "success";
203: } else {
204: order = null;
205: ActionContext.getActionContext().setSimpleMessage(
206: "You may only view your own orders.");
207: return "failure";
208: }
209: }
210:
211: public void reset() {
212: shippingAddressRequired = false;
213: }
214:
215: public void clear() {
216: order = new Order();
217: orderId = 0;
218: shippingAddressRequired = false;
219: confirmed = false;
220: orderList = null;
221: pageDirection = null;
222: }
223:
224: public void validate() {
225: ActionContext ctx = ActionContext.getActionContext();
226:
227: if (!this .isShippingAddressRequired()) {
228: validateRequiredField(order.getCreditCard(),
229: "FAKE (!) credit card number required.");
230: validateRequiredField(order.getExpiryDate(),
231: "Expiry date is required.");
232: validateRequiredField(order.getCardType(),
233: "Card type is required.");
234:
235: validateRequiredField(order.getShipToFirstName(),
236: "Shipping Info: first name is required.");
237: validateRequiredField(order.getShipToLastName(),
238: "Shipping Info: last name is required.");
239: validateRequiredField(order.getShipAddress1(),
240: "Shipping Info: address is required.");
241: validateRequiredField(order.getShipCity(),
242: "Shipping Info: city is required.");
243: validateRequiredField(order.getShipState(),
244: "Shipping Info: state is required.");
245: validateRequiredField(order.getShipZip(),
246: "Shipping Info: zip/postal code is required.");
247: validateRequiredField(order.getShipCountry(),
248: "Shipping Info: country is required.");
249:
250: validateRequiredField(order.getBillToFirstName(),
251: "Billing Info: first name is required.");
252: validateRequiredField(order.getBillToLastName(),
253: "Billing Info: last name is required.");
254: validateRequiredField(order.getBillAddress1(),
255: "Billing Info: address is required.");
256: validateRequiredField(order.getBillCity(),
257: "Billing Info: city is required.");
258: validateRequiredField(order.getBillState(),
259: "Billing Info: state is required.");
260: validateRequiredField(order.getBillZip(),
261: "Billing Info: zip/postal code is required.");
262: validateRequiredField(order.getBillCountry(),
263: "Billing Info: country is required.");
264: }
265:
266: if (ctx.isSimpleErrorsExist()) {
267: order.setBillAddress1(order.getShipAddress1());
268: order.setBillAddress2(order.getShipAddress2());
269: order.setBillToFirstName(order.getShipToFirstName());
270: order.setBillToLastName(order.getShipToLastName());
271: order.setBillCity(order.getShipCity());
272: order.setBillCountry(order.getShipCountry());
273: order.setBillState(order.getShipState());
274: order.setBillZip(order.getShipZip());
275: }
276:
277: }
278:
279: }
|