protected void initActionableDynamicQuery(ActionableDynamicQuery actionableDynamicQuery) {
    actionableDynamicQuery.setBaseLocalService(
        com.liferay.shopping.service.ShoppingOrderLocalServiceUtil.getService());
    actionableDynamicQuery.setClassLoader(getClassLoader());
    actionableDynamicQuery.setModelClass(ShoppingOrder.class);

    actionableDynamicQuery.setPrimaryKeyPropertyName("orderId");
  }
  protected void checkout(ActionRequest actionRequest) throws Exception {
    if (!hasLatestOrder(actionRequest)) {
      ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest.getAttribute(WebKeys.THEME_DISPLAY);

      ShoppingOrderLocalServiceUtil.addLatestOrder(
          themeDisplay.getUserId(), themeDisplay.getScopeGroupId());
    }
  }
  protected void saveLatestOrder(ActionRequest actionRequest, ActionResponse actionResponse)
      throws Exception {

    ShoppingCart cart = ShoppingUtil.getCart(actionRequest);

    ShoppingOrder order = ShoppingOrderLocalServiceUtil.saveLatestOrder(cart);

    forwardCheckout(actionRequest, actionResponse, order);
  }
  @Override
  public ActionableDynamicQuery getActionableDynamicQuery() {
    ActionableDynamicQuery actionableDynamicQuery = new DefaultActionableDynamicQuery();

    actionableDynamicQuery.setBaseLocalService(
        com.liferay.shopping.service.ShoppingOrderLocalServiceUtil.getService());
    actionableDynamicQuery.setClassLoader(getClassLoader());
    actionableDynamicQuery.setModelClass(ShoppingOrder.class);

    actionableDynamicQuery.setPrimaryKeyPropertyName("orderId");

    return actionableDynamicQuery;
  }
  protected boolean hasLatestOrder(ActionRequest actionRequest) throws Exception {

    try {
      ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest.getAttribute(WebKeys.THEME_DISPLAY);

      ShoppingOrderLocalServiceUtil.getLatestOrder(
          themeDisplay.getUserId(), themeDisplay.getScopeGroupId());

      return true;
    } catch (NoSuchOrderException nsoe) {
      return false;
    }
  }
  protected void forwardCheckout(
      ActionRequest actionRequest, ActionResponse actionResponse, ShoppingOrder order)
      throws Exception {

    ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest.getAttribute(WebKeys.THEME_DISPLAY);

    ShoppingCart cart = ShoppingUtil.getCart(actionRequest);

    ShoppingWebComponentProvider shoppingWebComponentProvider =
        ShoppingWebComponentProvider.getShoppingWebComponentProvider();

    SettingsFactory settingsFactory = shoppingWebComponentProvider.getSettingsFactory();

    ShoppingGroupServiceSettings shoppingGroupServiceSettings =
        settingsFactory.getSettings(
            ShoppingGroupServiceSettings.class,
            new GroupServiceSettingsLocator(
                themeDisplay.getScopeGroupId(), ShoppingConstants.SERVICE_NAME));

    String returnURL =
        ShoppingUtil.getPayPalReturnURL(
            ((ActionResponseImpl) actionResponse).createActionURL(), order);
    String notifyURL = ShoppingUtil.getPayPalNotifyURL(themeDisplay);

    if (shoppingGroupServiceSettings.usePayPal()) {
      double total =
          ShoppingUtil.calculateTotal(
              cart.getItems(),
              order.getBillingState(),
              cart.getCoupon(),
              cart.getAltShipping(),
              cart.isInsure());

      String redirectURL =
          ShoppingUtil.getPayPalRedirectURL(
              shoppingGroupServiceSettings, order, total, returnURL, notifyURL);

      actionResponse.sendRedirect(redirectURL);
    } else {
      ServiceContext serviceContext = ServiceContextFactory.getInstance(actionRequest);

      ShoppingOrderLocalServiceUtil.sendEmail(order, "confirmation", serviceContext);

      actionResponse.sendRedirect(returnURL);
    }
  }
  @Test
  public void testActionableDynamicQuery() throws Exception {
    final IntegerWrapper count = new IntegerWrapper();

    ActionableDynamicQuery actionableDynamicQuery =
        ShoppingOrderLocalServiceUtil.getActionableDynamicQuery();

    actionableDynamicQuery.setPerformActionMethod(
        new ActionableDynamicQuery.PerformActionMethod<ShoppingOrder>() {
          @Override
          public void performAction(ShoppingOrder shoppingOrder) {
            Assert.assertNotNull(shoppingOrder);

            count.increment();
          }
        });

    actionableDynamicQuery.performActions();

    Assert.assertEquals(count.getValue(), _persistence.countAll());
  }
  protected void updateLatestOrder(ActionRequest actionRequest) throws Exception {

    ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest.getAttribute(WebKeys.THEME_DISPLAY);

    String billingFirstName = ParamUtil.getString(actionRequest, "billingFirstName");
    String billingLastName = ParamUtil.getString(actionRequest, "billingLastName");
    String billingEmailAddress = ParamUtil.getString(actionRequest, "billingEmailAddress");
    String billingCompany = ParamUtil.getString(actionRequest, "billingCompany");
    String billingStreet = ParamUtil.getString(actionRequest, "billingStreet");
    String billingCity = ParamUtil.getString(actionRequest, "billingCity");

    String billingStateSel = ParamUtil.getString(actionRequest, "billingStateSel");
    String billingState = billingStateSel;

    if (Validator.isNull(billingStateSel)) {
      billingState = ParamUtil.getString(actionRequest, "billingState");
    }

    String billingZip = ParamUtil.getString(actionRequest, "billingZip");
    String billingCountry = ParamUtil.getString(actionRequest, "billingCountry");
    String billingPhone = ParamUtil.getString(actionRequest, "billingPhone");

    boolean shipToBilling = ParamUtil.getBoolean(actionRequest, "shipToBilling");
    String shippingFirstName = ParamUtil.getString(actionRequest, "shippingFirstName");
    String shippingLastName = ParamUtil.getString(actionRequest, "shippingLastName");
    String shippingEmailAddress = ParamUtil.getString(actionRequest, "shippingEmailAddress");
    String shippingCompany = ParamUtil.getString(actionRequest, "shippingCompany");
    String shippingStreet = ParamUtil.getString(actionRequest, "shippingStreet");
    String shippingCity = ParamUtil.getString(actionRequest, "shippingCity");

    String shippingStateSel = ParamUtil.getString(actionRequest, "shippingStateSel");
    String shippingState = shippingStateSel;

    if (Validator.isNull(shippingStateSel)) {
      shippingState = ParamUtil.getString(actionRequest, "shippingState");
    }

    String shippingZip = ParamUtil.getString(actionRequest, "shippingZip");
    String shippingCountry = ParamUtil.getString(actionRequest, "shippingCountry");
    String shippingPhone = ParamUtil.getString(actionRequest, "shippingPhone");

    String ccName = ParamUtil.getString(actionRequest, "ccName");
    String ccType = ParamUtil.getString(actionRequest, "ccType");
    String ccNumber = ParamUtil.getString(actionRequest, "ccNumber");
    int ccExpMonth = ParamUtil.getInteger(actionRequest, "ccExpMonth");
    int ccExpYear = ParamUtil.getInteger(actionRequest, "ccExpYear");
    String ccVerNumber = ParamUtil.getString(actionRequest, "ccVerNumber");

    String comments = ParamUtil.getString(actionRequest, "comments");

    ShoppingOrderLocalServiceUtil.updateLatestOrder(
        themeDisplay.getUserId(),
        themeDisplay.getScopeGroupId(),
        billingFirstName,
        billingLastName,
        billingEmailAddress,
        billingCompany,
        billingStreet,
        billingCity,
        billingState,
        billingZip,
        billingCountry,
        billingPhone,
        shipToBilling,
        shippingFirstName,
        shippingLastName,
        shippingEmailAddress,
        shippingCompany,
        shippingStreet,
        shippingCity,
        shippingState,
        shippingZip,
        shippingCountry,
        shippingPhone,
        ccName,
        ccType,
        ccNumber,
        ccExpMonth,
        ccExpYear,
        ccVerNumber,
        comments);
  }