View Javadoc

    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *      http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17  package com.ibatis.jpetstore.presentation;
    18
    19  import com.ibatis.jpetstore.domain.Account;
    20  import com.ibatis.jpetstore.domain.Order;
    21  import com.ibatis.jpetstore.service.AccountService;
    22  import com.ibatis.jpetstore.service.OrderService;
    23  import com.ibatis.struts.ActionContext;
    24  import com.ibatis.struts.BaseBean;
    25  import com.ibatis.common.util.PaginatedList;
    26
    27  import java.util.ArrayList;
    28  import java.util.List;
    29  import java.util.Map;
    30  import java.util.Collections;
    31
    32  public class OrderBean extends BaseBean {
    33
    34    /* Constants */
    35
    36    private static final AccountService accountService = AccountService.getInstance();
    37    private static final OrderService orderService = OrderService.getInstance();
    38
    39    private static final List CARD_TYPE_LIST;
    40
    41    /* Private Fields */
    42
    43    private Order order;
    44    private int orderId;
    45    private boolean shippingAddressRequired;
    46    private boolean confirmed;
    47    private PaginatedList orderList;
    48    private String pageDirection;
    49
    50    /* Static Initializer */
    51
    52    static {
    53      List cardList = new ArrayList();
    54      cardList.add("Visa");
    55      cardList.add("MasterCard");
    56      cardList.add("American Express");
    57      CARD_TYPE_LIST = Collections.unmodifiableList(cardList);
    58    }
    59
    60    /* Constructors */
    61
    62    public OrderBean() {
    63      this.order = new Order();
    64      this.shippingAddressRequired = false;
    65      this.confirmed = false;
    66    }
    67
    68    /* JavaBeans Properties */
    69
    70    public int getOrderId() {
    71      return orderId;
    72    }
    73
    74    public void setOrderId(int orderId) {
    75      this.orderId = orderId;
    76    }
    77
    78    public Order getOrder() {
    79      return order;
    80    }
    81
    82    public void setOrder(Order order) {
    83      this.order = order;
    84    }
    85
    86    public boolean isShippingAddressRequired() {
    87      return shippingAddressRequired;
    88    }
    89
    90    public void setShippingAddressRequired(boolean shippingAddressRequired) {
    91      this.shippingAddressRequired = shippingAddressRequired;
    92    }
    93
    94    public boolean isConfirmed() {
    95      return confirmed;
    96    }
    97
    98    public void setConfirmed(boolean confirmed) {
    99      this.confirmed = confirmed;
    100   }
    101
    102   public List getCreditCardTypes() {
    103     return CARD_TYPE_LIST;
    104   }
    105
    106   public List getOrderList() {
    107     return orderList;
    108   }
    109
    110   public String getPageDirection() {
    111     return pageDirection;
    112   }
    113
    114   public void setPageDirection(String pageDirection) {
    115     this.pageDirection = pageDirection;
    116   }
    117
    118   /* Public Methods */
    119
    120   public String newOrderForm() {
    121     Map sessionMap = ActionContext.getActionContext().getSessionMap();
    122     AccountBean accountBean = (AccountBean) sessionMap.get("accountBean");
    123     CartBean cartBean = (CartBean) sessionMap.get("cartBean");
    124
    125     clear();
    126     if (accountBean == null || !accountBean.isAuthenticated()){
    127       ActionContext.getActionContext().setSimpleMessage("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.getAccount().getUsername());
    132       order.initOrder(account, cartBean.getCart());
    133       return "success";
    134     } else {
    135       ActionContext.getActionContext().setSimpleMessage("An order could not be created because a cart could not be found.");
    136       return "failure";
    137     }
    138   }
    139
    140   public String newOrder() {
    141     Map sessionMap = ActionContext.getActionContext().getSessionMap();
    142
    143     if (shippingAddressRequired) {
    144       shippingAddressRequired = false;
    145       return "shipping";
    146     } else if (!isConfirmed()) {
    147       return "confirm";
    148     } else if (getOrder() != null) {
    149
    150       orderService.insertOrder(order);
    151
    152       CartBean cartBean = (CartBean)sessionMap.get("cartBean");
    153       cartBean.clear();
    154
    155       ActionContext.getActionContext().setSimpleMessage("Thank you, your order has been submitted.");
    156
    157       return "success";
    158     } else {
    159       ActionContext.getActionContext().setSimpleMessage("An error occurred processing your order (order was null).");
    160       return "failure";
    161     }
    162   }
    163
    164   public String listOrders() {
    165     Map sessionMap = ActionContext.getActionContext().getSessionMap();
    166     AccountBean accountBean = (AccountBean) sessionMap.get("accountBean");
    167     orderList = orderService.getOrdersByUsername(accountBean.getAccount().getUsername());
    168     return "success";
    169   }
    170
    171   public String switchOrderPage() {
    172     if ("next".equals(pageDirection)) {
    173       orderList.nextPage();
    174     } else if ("previous".equals(pageDirection)) {
    175       orderList.previousPage();
    176     }
    177     return "success";
    178   }
    179
    180
    181   public String viewOrder() {
    182     Map sessionMap = ActionContext.getActionContext().getSessionMap();
    183     AccountBean accountBean = (AccountBean) sessionMap.get("accountBean");
    184
    185     order = orderService.getOrder(orderId);
    186
    187     if (accountBean.getAccount().getUsername().equals(order.getUsername())) {
    188       return "success";
    189     } else {
    190       order = null;
    191       ActionContext.getActionContext().setSimpleMessage("You may only view your own orders.");
    192       return "failure";
    193     }
    194   }
    195
    196   public void reset() {
    197     shippingAddressRequired = false;
    198   }
    199
    200   public void clear() {
    201     order = new Order();
    202     orderId = 0;
    203     shippingAddressRequired = false;
    204     confirmed = false;
    205     orderList = null;
    206     pageDirection = null;
    207   }
    208
    209   public void validate() {
    210     ActionContext ctx = ActionContext.getActionContext();
    211
    212     if (!this.isShippingAddressRequired()) {
    213       validateRequiredField(order.getCreditCard(), "FAKE (!) credit card number required.");
    214       validateRequiredField(order.getExpiryDate(), "Expiry date is required.");
    215       validateRequiredField(order.getCardType(), "Card type is required.");
    216
    217       validateRequiredField(order.getShipToFirstName(), "Shipping Info: first name is required.");
    218       validateRequiredField(order.getShipToLastName(), "Shipping Info: last name is required.");
    219       validateRequiredField(order.getShipAddress1(), "Shipping Info: address is required.");
    220       validateRequiredField(order.getShipCity(), "Shipping Info: city is required.");
    221       validateRequiredField(order.getShipState(), "Shipping Info: state is required.");
    222       validateRequiredField(order.getShipZip(), "Shipping Info: zip/postal code is required.");
    223       validateRequiredField(order.getShipCountry(), "Shipping Info: country is required.");
    224
    225       validateRequiredField(order.getBillToFirstName(), "Billing Info: first name is required.");
    226       validateRequiredField(order.getBillToLastName(), "Billing Info: last name is required.");
    227       validateRequiredField(order.getBillAddress1(), "Billing Info: address is required.");
    228       validateRequiredField(order.getBillCity(), "Billing Info: city is required.");
    229       validateRequiredField(order.getBillState(), "Billing Info: state is required.");
    230       validateRequiredField(order.getBillZip(), "Billing Info: zip/postal code is required.");
    231       validateRequiredField(order.getBillCountry(), "Billing Info: country is required.");
    232     }
    233
    234     if (ctx.isSimpleErrorsExist()) {
    235       order.setBillAddress1(order.getShipAddress1());
    236       order.setBillAddress2(order.getShipAddress2());
    237       order.setBillToFirstName(order.getShipToFirstName());
    238       order.setBillToLastName(order.getShipToLastName());
    239       order.setBillCity(order.getShipCity());
    240       order.setBillCountry(order.getShipCountry());
    241       order.setBillState(order.getShipState());
    242       order.setBillZip(order.getShipZip());
    243     }
    244
    245   }
    246
    247 }