1 7 package org.jfox.petstore.action; 8 9 import java.util.ArrayList ; 10 import java.util.Date ; 11 import java.util.List ; 12 import javax.ejb.EJB ; 13 14 import org.jfox.entity.EntityFactory; 15 import org.jfox.entity.dao.PKGenerator; 16 import org.jfox.framework.annotation.Service; 17 import org.jfox.mvc.ActionSupport; 18 import org.jfox.mvc.Invocation; 19 import org.jfox.mvc.InvocationContext; 20 import org.jfox.mvc.PageContext; 21 import org.jfox.mvc.SessionContext; 22 import org.jfox.mvc.annotation.ActionMethod; 23 import org.jfox.mvc.validate.LongValidation; 24 import org.jfox.petstore.bo.OrderBO; 25 import org.jfox.petstore.domain.Cart; 26 import org.jfox.petstore.domain.CartItem; 27 import org.jfox.petstore.entity.Account; 28 import org.jfox.petstore.entity.LineItem; 29 import org.jfox.petstore.entity.Order; 30 31 34 @Service(id = "order") 35 public class OrderAction extends ActionSupport { 36 public static final String ORDER_SESSION_KEY = "__ORDER__"; 37 private static List <String > creditCardTypes = new ArrayList <String >(); 38 39 static { 40 creditCardTypes.add("Visa"); 41 creditCardTypes.add("American Express"); 42 creditCardTypes.add("mobile c"); 43 } 44 45 @EJB 46 OrderBO orderBO; 47 48 @ActionMethod(successView = "NewOrder.vhtml") 49 public void doGetNew(InvocationContext invocationContext) throws Exception { 50 SessionContext sessionContext = invocationContext.getSessionContext(); 51 Cart cart = (Cart)sessionContext.getAttribute(CartAction.CART_SESSION_KEY); 52 53 PageContext pageContext = invocationContext.getPageContext(); 54 55 if(!sessionContext.containsAttribute(AccountAction.ACCOUNT_SESSION_KEY)) { 56 pageContext.setTargetView("signon.vhtml"); 58 return; 59 } 60 61 Account account = (Account)sessionContext.getAttribute(AccountAction.ACCOUNT_SESSION_KEY); 62 63 Order order = EntityFactory.newEntityObject(Order.class); 64 order.setOrderId(PKGenerator.getInstance().nextPK()); 65 order.setUsername(account.getUsername()); 66 order.setOrderDate(new Date ()); 67 68 order.setShipToFirstName(account.getFirstName()); 69 order.setShipToLastName(account.getLastName()); 70 order.setShipAddress1(account.getAddress1()); 71 order.setShipAddress2(account.getAddress2()); 72 order.setShipCity(account.getCity()); 73 order.setShipState(account.getState()); 74 order.setShipZip(account.getZip()); 75 order.setShipCountry(account.getCountry()); 76 77 order.setBillToFirstName(account.getFirstName()); 78 order.setBillToLastName(account.getLastName()); 79 order.setBillAddress1(account.getAddress1()); 80 order.setBillAddress2(account.getAddress2()); 81 order.setBillCity(account.getCity()); 82 order.setBillState(account.getState()); 83 order.setBillZip(account.getZip()); 84 order.setBillCountry(account.getCountry()); 85 86 order.setTotalPrice(cart.getSubTotal()); 87 88 order.setCreditCard("999 9999 9999 9999"); 89 order.setExpiryDate("12/03"); 90 order.setCardType("Visa"); 91 order.setCourier("UPS"); 92 order.setLocale("CA"); 93 order.setStatus("P"); 94 95 List <LineItem> lineItems = new ArrayList <LineItem>(); 96 97 int i = 1; 98 for (CartItem cartItem : cart.getCartItemList()) { 99 LineItem lineItem = EntityFactory.newEntityObject(LineItem.class); 100 lineItem.setLineNumber(i); 101 lineItem.setOrderId(order.getOrderId()); 102 lineItem.setQuantity(cartItem.getQuantity()); 103 lineItem.setItemId(cartItem.getItem().getItemId()); 104 lineItem.setUnitPrice(cartItem.getItem().getListPrice()); 105 lineItem.setItem(cartItem.getItem()); 106 lineItems.add(lineItem); 107 i++; 108 } 109 order.setLineItems(lineItems); 110 111 sessionContext.setAttribute(ORDER_SESSION_KEY, order); 112 113 pageContext.setAttribute("creditCardTypes", creditCardTypes); 114 pageContext.setAttribute("order", order); 115 } 116 117 @ActionMethod(successView = "ConfirmOrder.vhtml", invocationClass = NewOrderInvocation.class) 118 public void doPostNew(InvocationContext invocationContext) throws Exception { 119 NewOrderInvocation invocation = (NewOrderInvocation)invocationContext.getInvocation(); 120 SessionContext sessionContext = invocationContext.getSessionContext(); 121 Order order = (Order)sessionContext.getAttribute(ORDER_SESSION_KEY); 123 124 order.setShipToFirstName(invocation.getBillToFirstName()); 125 order.setShipToLastName(invocation.getBillToLastName()); 126 order.setShipAddress1(invocation.getBillAddress1()); 127 order.setShipAddress2(invocation.getBillAddress2()); 128 order.setShipCity(invocation.getBillCity()); 129 order.setShipState(invocation.getBillState()); 130 order.setShipZip(invocation.getBillZip()); 131 order.setShipCountry(invocation.getBillCountry()); 132 133 order.setBillToFirstName(invocation.getBillToFirstName()); 134 order.setBillToLastName(invocation.getBillToLastName()); 135 order.setBillAddress1(invocation.getBillAddress1()); 136 order.setBillAddress2(invocation.getBillAddress2()); 137 order.setBillCity(invocation.getBillCity()); 138 order.setBillState(invocation.getBillState()); 139 order.setBillZip(invocation.getBillZip()); 140 order.setBillCountry(invocation.getBillCountry()); 141 142 order.setCardType(invocation.getCardType()); 143 order.setCreditCard(invocation.getCreditCard()); 144 145 if ("1".equals(invocation.getShippingAddressRequired())) { 146 invocationContext.getPageContext().setTargetView("Shipping.vhtml"); 148 } 149 150 PageContext pageContext = invocationContext.getPageContext(); 151 pageContext.setAttribute("order", order); 152 } 153 154 @ActionMethod(successView = "ConfirmOrder.vhtml", invocationClass = ShippingOrderInvocation.class) 155 public void doPostConfirmShipping(InvocationContext invocationContext) throws Exception { 156 ShippingOrderInvocation invocation = (ShippingOrderInvocation)invocationContext.getInvocation(); 157 SessionContext sessionContext = invocationContext.getSessionContext(); 158 Order order = (Order)sessionContext.getAttribute(ORDER_SESSION_KEY); 159 160 order.setShipToFirstName(invocation.getShipToFirstName()); 161 order.setShipToLastName(invocation.getShipToLastName()); 162 order.setShipAddress1(invocation.getShipAddress1()); 163 order.setShipAddress2(invocation.getShipAddress2()); 164 order.setShipCity(invocation.getShipCity()); 165 order.setShipState(invocation.getShipState()); 166 order.setShipZip(invocation.getShipZip()); 167 order.setShipCountry(invocation.getShipCountry()); 168 169 PageContext pageContext = invocationContext.getPageContext(); 170 pageContext.setAttribute("order", order); 171 } 172 173 @ActionMethod(successView = "ViewOrder.vhtml", errorView = "Cart.vhtml") 174 public void doGetConfirm(InvocationContext invocationContext) throws Exception { 175 SessionContext sessionContext = invocationContext.getSessionContext(); 176 PageContext pageContext = invocationContext.getPageContext(); 177 178 Order order = (Order)sessionContext.getAttribute(ORDER_SESSION_KEY); 179 if (order != null) { 180 try { 182 orderBO.insertOrder(order); 183 184 sessionContext.removeAttribute(ORDER_SESSION_KEY); 185 sessionContext.removeAttribute(CartAction.CART_SESSION_KEY); 186 pageContext.setAttribute("order", order); 187 pageContext.setAttribute("orderUtil",OrderUtil.getInstance()); 188 } 189 catch (Exception e) { 190 Cart cart = (Cart)sessionContext.getAttribute(CartAction.CART_SESSION_KEY); 191 pageContext.setAttribute("cart", cart); 192 throw e; 193 } 194 } 195 } 196 197 @ActionMethod(successView = "ListOrders.vhtml") 198 public void doGetList(InvocationContext invocationContext) throws Exception { 199 SessionContext sessionContext = invocationContext.getSessionContext(); 200 Account account = (Account)sessionContext.getAttribute(AccountAction.ACCOUNT_SESSION_KEY); 201 List <Order> orders = orderBO.getOrdersByUsername(account.getUsername()); 202 203 PageContext pageContext = invocationContext.getPageContext(); 204 pageContext.setAttribute("orders",orders); 205 } 206 207 @ActionMethod(successView = "ViewOrder.vhtml", invocationClass = ViewOrderInvocation.class) 208 public void doGetView(InvocationContext invocationContext) throws Exception { 209 ViewOrderInvocation invocation = (ViewOrderInvocation)invocationContext.getInvocation(); 210 long orderId = invocation.getOrderId(); 211 Order order = orderBO.getOrder(orderId); 212 PageContext pageContext = invocationContext.getPageContext(); 213 pageContext.setAttribute("order",order); 214 pageContext.setAttribute("orderUtil",OrderUtil.getInstance()); 215 } 216 217 public static class OrderUtil{ 218 private static OrderUtil instance = new OrderUtil(); 219 private OrderUtil(){ 220 221 } 222 public static OrderUtil getInstance() { 223 return instance; 224 } 225 226 public double getTotalPrice(LineItem lineItem){ 227 return lineItem.getUnitPrice() * lineItem.getQuantity(); 228 } 229 } 230 231 public static class ViewOrderInvocation extends Invocation { 232 233 @LongValidation 234 private long orderId; 235 236 public long getOrderId() { 237 return orderId; 238 } 239 240 public void setOrderId(long orderId) { 241 this.orderId = orderId; 242 } 243 } 244 245 public static class NewOrderInvocation extends Invocation { 246 private String cardType; 247 private String creditCard; 248 private String expiryDate; 249 private String billToFirstName; 250 private String billToLastName; 251 private String billAddress1; 252 private String billAddress2; 253 private String billCity; 254 private String billState; 255 private String billZip; 256 private String billCountry; 257 private String shippingAddressRequired; 258 259 public String getBillAddress1() { 260 return billAddress1; 261 } 262 263 public void setBillAddress1(String billAddress1) { 264 this.billAddress1 = billAddress1; 265 } 266 267 public String getBillAddress2() { 268 return billAddress2; 269 } 270 271 public void setBillAddress2(String billAddress2) { 272 this.billAddress2 = billAddress2; 273 } 274 275 public String getBillCity() { 276 return billCity; 277 } 278 279 public void setBillCity(String billCity) { 280 this.billCity = billCity; 281 } 282 283 public String getBillCountry() { 284 return billCountry; 285 } 286 287 public void setBillCountry(String billCountry) { 288 this.billCountry = billCountry; 289 } 290 291 public String getBillState() { 292 return billState; 293 } 294 295 public void setBillState(String billState) { 296 this.billState = billState; 297 } 298 299 public String getBillToFirstName() { 300 return billToFirstName; 301 } 302 303 public void setBillToFirstName(String billToFirstName) { 304 this.billToFirstName = billToFirstName; 305 } 306 307 public String getBillToLastName() { 308 return billToLastName; 309 } 310 311 public void setBillToLastName(String billToLastName) { 312 this.billToLastName = billToLastName; 313 } 314 315 public String getBillZip() { 316 return billZip; 317 } 318 319 public void setBillZip(String billZip) { 320 this.billZip = billZip; 321 } 322 323 public String getCardType() { 324 return cardType; 325 } 326 327 public void setCardType(String cardType) { 328 this.cardType = cardType; 329 } 330 331 public String getCreditCard() { 332 return creditCard; 333 } 334 335 public void setCreditCard(String creditCard) { 336 this.creditCard = creditCard; 337 } 338 339 public String getExpiryDate() { 340 return expiryDate; 341 } 342 343 public void setExpiryDate(String expiryDate) { 344 this.expiryDate = expiryDate; 345 } 346 347 public String getShippingAddressRequired() { 348 return shippingAddressRequired; 349 } 350 351 public void setShippingAddressRequired(String shippingAddressRequired) { 352 this.shippingAddressRequired = shippingAddressRequired; 353 } 354 } 355 356 public static class ShippingOrderInvocation extends Invocation { 357 private String shipToFirstName; 358 private String shipToLastName; 359 private String shipAddress1; 360 private String shipAddress2; 361 private String shipCity; 362 private String shipState; 363 private String shipZip; 364 private String shipCountry; 365 366 public String getShipAddress1() { 367 return shipAddress1; 368 } 369 370 public void setShipAddress1(String shipAddress1) { 371 this.shipAddress1 = shipAddress1; 372 } 373 374 public String getShipAddress2() { 375 return shipAddress2; 376 } 377 378 public void setShipAddress2(String shipAddress2) { 379 this.shipAddress2 = shipAddress2; 380 } 381 382 public String getShipCity() { 383 return shipCity; 384 } 385 386 public void setShipCity(String shipCity) { 387 this.shipCity = shipCity; 388 } 389 390 public String getShipCountry() { 391 return shipCountry; 392 } 393 394 public void setShipCountry(String shipCountry) { 395 this.shipCountry = shipCountry; 396 } 397 398 public String getShipState() { 399 return shipState; 400 } 401 402 public void setShipState(String shipState) { 403 this.shipState = shipState; 404 } 405 406 public String getShipToFirstName() { 407 return shipToFirstName; 408 } 409 410 public void setShipToFirstName(String shipToFirstName) { 411 this.shipToFirstName = shipToFirstName; 412 } 413 414 public String getShipToLastName() { 415 return shipToLastName; 416 } 417 418 public void setShipToLastName(String shipToLastName) { 419 this.shipToLastName = shipToLastName; 420 } 421 422 public String getShipZip() { 423 return shipZip; 424 } 425 426 public void setShipZip(String shipZip) { 427 this.shipZip = shipZip; 428 } 429 430 } 431 432 public static void main(String [] args) { 433 434 } 435 } 436 | Popular Tags |