⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 orderhandler.java

📁 简单的Jdon实现
💻 JAVA
字号:
/*
 * Copyright 2003-2005 the original author or authors.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package com.jdon.framework.samples.jpetstore.presentation.action;

import java.util.Date;
import java.util.Iterator;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.jdon.controller.WebAppUtil;
import com.jdon.controller.events.Event;
import com.jdon.controller.events.EventModel;
import com.jdon.framework.samples.jpetstore.domain.CartItem;
import com.jdon.framework.samples.jpetstore.domain.Order;
import com.jdon.framework.samples.jpetstore.presentation.form.AccountForm;
import com.jdon.framework.samples.jpetstore.presentation.form.OrderForm;
import com.jdon.framework.samples.jpetstore.service.CartService;
import com.jdon.framework.samples.jpetstore.service.OrderService;
import com.jdon.model.ModelForm;
import com.jdon.model.handler.XmlModelHandler;
import com.jdon.util.Debug;

/**
 * @author <a href="mailto:banqiao@jdon.com">banq</a>
 *
 */
public class OrderHandler extends XmlModelHandler {
    private final static String module = OrderHandler.class.getName();

    public ModelForm initForm(HttpServletRequest request) throws Exception {
        Debug.logVerbose(" enter initForm...", module);
        HttpSession session = request.getSession();
        AccountForm accountForm = (AccountForm) session.getAttribute("accountForm");
        if ((accountForm == null) || (!accountForm.isAuthenticated())) {
            Debug.logError("this user not sign on , accountForm is null", module);
            return new OrderForm();
        }

        CartService cartService = (CartService) WebAppUtil.getService("cartService", request);
        if (cartService.getAllCartItems().isEmpty()) {
            Debug.logError("cart is null, please add cart..", module);
            return new OrderForm();
        }

        OrderForm orderForm = createOrderForm(accountForm);

        orderForm.setTotalPrice(cartService.getSubTotal());

        //below can read from the user's creditCard service; 
        orderForm.setCreditCard("999 9999 9999 9999");
        orderForm.setExpiryDate("12/03");
        orderForm.setCardType("Visa");
        orderForm.setCourier("UPS");
        orderForm.setLocale("CA");
        orderForm.setStatus("P");

        Iterator i = cartService.getAllCartItems().iterator();
        while (i.hasNext()) {
            CartItem cartItem = (CartItem) i.next();
            orderForm.addLineItem(cartItem);
        }
        return orderForm;
    }

    private OrderForm createOrderForm(AccountForm account) {
        OrderForm orderForm = new OrderForm();
        orderForm.setUsername(account.getUsername());
        orderForm.setOrderDate(new Date());

        orderForm.setShipToFirstName(account.getFirstName());
        orderForm.setShipToLastName(account.getLastName());
        orderForm.setShipAddress1(account.getAddress1());
        orderForm.setShipAddress2(account.getAddress2());
        orderForm.setShipCity(account.getCity());
        orderForm.setShipState(account.getState());
        orderForm.setShipZip(account.getZip());
        orderForm.setShipCountry(account.getCountry());

        orderForm.setBillToFirstName(account.getFirstName());
        orderForm.setBillToLastName(account.getLastName());
        orderForm.setBillAddress1(account.getAddress1());
        orderForm.setBillAddress2(account.getAddress2());
        orderForm.setBillCity(account.getCity());
        orderForm.setBillState(account.getState());
        orderForm.setBillZip(account.getZip());
        orderForm.setBillCountry(account.getCountry());

        return orderForm;
    }

    public void serviceAction(EventModel em, HttpServletRequest request) throws java.lang.Exception {
        try {
            CartService cartService = (CartService) WebAppUtil.getService("cartService", request);
            cartService.clear();
            OrderService orderService = (OrderService) WebAppUtil.getService("orderService", request);
            switch (em.getActionType()) {
            case Event.CREATE:
                orderService.insertOrder(em); 
                cartService.clear();
                break;
            case Event.EDIT:

                break;
            case Event.DELETE:

                break;
            }
        } catch (Exception ex) {
            throw new Exception(" serviceAction Error:" + ex);
        }
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -