예제 #1
0
  /**
   * Invoked after addSubscriptionItem
   *
   * @throws Exception
   */
  public void addItem() throws Exception {

    boolean quantityUpdated = false;

    // get store country
    Map lcountries =
        RefCache.getAllcountriesmap(LanguageUtil.getLanguageNumberCode(value.getLang()));
    if (lcountries != null) {
      Country country = (Country) lcountries.get(store.getCountry());
      getServletRequest().getSession().setAttribute("COUNTRY", country);
    }

    // check if language is supported by the store
    if (lcountries != null) {
      Country country = (Country) lcountries.get(store.getCountry());
      getServletRequest().getSession().setAttribute("COUNTRY", country);
    }

    // store can not be null, if it is the case, generic error page
    if (store == null) {
      throw new Exception("Invalid Store!");
    }

    // check if order product already exist. If that orderproduct already
    // exist
    // and has no ptoperties, so just update the quantity
    if (value.getAttributeId() == null
        || (value.getAttributeId() != null && value.getAttributeId().size() == 0)) {
      Map savedProducts = SessionUtil.getOrderProducts(getServletRequest());
      if (savedProducts != null) {
        Iterator it = savedProducts.keySet().iterator();
        while (it.hasNext()) {
          String line = (String) it.next();
          OrderProduct op = (OrderProduct) savedProducts.get(line);
          if (op.getProductId() == value.getProductId()) {
            Set attrs = op.getOrderattributes();
            if (attrs.size() == 0) {
              int qty = op.getProductQuantity();
              qty = qty + value.getQty();
              op.setProductQuantity(qty);
              quantityUpdated = true;
              break;
            }
          }
        }
      }
    }

    // create an order with merchantId and all dates
    // will need to create a new order id when submited
    Order order = SessionUtil.getOrder(getServletRequest());
    if (order == null) {
      order = new Order();
    }

    order.setMerchantId(store.getMerchantId());
    order.setDatePurchased(new Date());
    SessionUtil.setOrder(order, getServletRequest());

    if (!StringUtils.isBlank(value.getReturnUrl())) {
      // Return to merchant site Url is set from store.
      value.setReturnUrl(store.getContinueshoppingurl());
    }
    SessionUtil.setMerchantStore(store, getServletRequest());

    if (!quantityUpdated) { // new submission

      // Prepare order
      OrderProduct orderProduct =
          com.salesmanager.core.util.CheckoutUtil.createOrderProduct(
              value.getProductId(), getLocale(), store.getCurrency());
      orderProduct.setProductQuantity(value.getQty());
      orderProduct.setProductId(value.getProductId());

      List<OrderProductAttribute> attributes = new ArrayList<OrderProductAttribute>();
      if (value.getAttributeId() != null && value.getAttributeId().size() > 0) {
        for (Long attrId : value.getAttributeId()) {
          if (attrId != null && attrId != 0) {
            ProductAttribute pAttr =
                cservice.getProductAttributeByOptionValueAndProduct(value.getProductId(), attrId);
            if (pAttr != null && pAttr.getProductId() == value.getProductId()) {
              OrderProductAttribute orderAttr = new OrderProductAttribute();
              orderAttr.setProductOptionValueId(pAttr.getOptionValueId());

              attributes.add(orderAttr);
            } else {
              LogMerchantUtil.log(
                  value.getMerchantId(),
                  getText(
                      "error.validation.product.attributes.ids",
                      new String[] {String.valueOf(attrId), String.valueOf(value.getProductId())}));
            }
          }
        }
      }

      if (!attributes.isEmpty()) {
        // ShoppingCartUtil.addAttributesFromRawObjects(attributes,
        // orderProduct, store.getCurrency(), getServletRequest());
        com.salesmanager.core.util.CheckoutUtil.addAttributesToProduct(
            attributes, orderProduct, store.getCurrency(), getLocale());
      }

      Set attributesSet = new HashSet(attributes);
      orderProduct.setOrderattributes(attributesSet);

      SessionUtil.addOrderProduct(orderProduct, getServletRequest());
    }

    // because this is a submission, cannot continue browsing, so that's it
    // for the OrderProduct
    Map orderProducts = SessionUtil.getOrderProducts(super.getServletRequest());
    // transform to a list
    List products = new ArrayList();

    if (orderProducts != null) {
      Iterator i = orderProducts.keySet().iterator();
      while (i.hasNext()) {
        String line = (String) i.next();
        OrderProduct op = (OrderProduct) orderProducts.get(line);
        products.add(op);
      }
      super.getServletRequest().getSession().setAttribute("ORDER_PRODUCT_LIST", products);
    }

    // for displaying the order summary, need to create an OrderSummary
    // entity
    OrderTotalSummary summary =
        oservice.calculateTotal(order, products, store.getCurrency(), super.getLocale());

    Map totals =
        OrderUtil.getOrderTotals(
            order.getOrderId(), summary, store.getCurrency(), super.getLocale());

    HttpSession session = getServletRequest().getSession();

    // transform totals to a list
    List totalsList = new ArrayList();
    if (totals != null) {
      Iterator totalsIterator = totals.keySet().iterator();
      while (totalsIterator.hasNext()) {
        String key = (String) totalsIterator.next();
        OrderTotal total = (OrderTotal) totals.get(key);
        totalsList.add(total);
      }
    }

    SessionUtil.setOrderTotals(totalsList, getServletRequest());

    value.setLangId(LanguageUtil.getLanguageNumberCode(value.getLang()));
    prepareZones();

    // set locale according to the language passed in parameters and store
    // information
    Locale locale = LocaleUtil.getLocaleFromStoreEntity(store, value.getLang());
    setLocale(locale);
  }