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

📄 shoppingcartaction.java

📁 《精通SOA:基于服务总线的Struts+EJB+Web Service整合应用开发》原书的实例代码
💻 JAVA
字号:
/*
 * $Id: TypeAction.java 54929 2004-10-16 16:38:42Z germuska $ 
 *
 * Copyright 2000-2004 The Apache Software Foundation.
 * 
 * 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.bkstore.controller.action;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;

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

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.validator.DynaValidatorForm;

import com.bkstore.ejb.sessionbean.ArrayOf_tns2_OrderItemDTO;
import com.bkstore.exception.ApplicationException;
import com.bkstore.model.service.dto.OrderDTO;
import com.bkstore.model.service.dto.OrderItemDTO;
import com.bkstore.model.service.dto.ShoppingCartDTO;
import com.bkstore.model.service.dto.UserAccountDTO;
import com.bkstore.model.service.imp.BookStoreMgrImp;

/**
 * Implementation of <strong>Action</strong> that validates a different 'types'.
 *
 */
public final class ShoppingCartAction extends Action {

    /**
     * Commons Logging instance.
     */
    private Log log = LogFactory.getFactory().getInstance(this.getClass().getName());


    public ActionForward execute(
        ActionMapping mapping,
        ActionForm form,
        HttpServletRequest request,
        HttpServletResponse response)
        throws Exception {
    	
    	System.out.println(" user list action ..........");
    	
    	
        HttpSession session = request.getSession();
        ArrayList arrlist = (ArrayList) session.getAttribute("shoppingCart");
        
        if(request.getParameter("delete") != null) {
        	
        	for (int i=arrlist.size()-1; i>=0; i--) {
        		ShoppingCartDTO shoppingCartDTO = (ShoppingCartDTO) arrlist.get(i);
        		String checkBoxName = "bookID["+i+"]";
        		if(request.getParameter(checkBoxName) != null) {
        			arrlist.remove(shoppingCartDTO);
        		}
        	}
        	
        }
        
        if(request.getParameter("modifyQuantity") != null) {
        	
        	for (int i=arrlist.size()-1; i>=0; i--) {
        		String quantityName = "quantity["+i+"]";
        		int quantity = (new Integer(request.getParameter(quantityName))).intValue();
        		ShoppingCartDTO shoppingCartDTO = (ShoppingCartDTO) arrlist.get(i);
        		shoppingCartDTO.setQuantity(quantity);
        	}
        	
        }
                
        if(request.getParameter("submit") != null) {
        	UserAccountDTO userAccountDTO = (UserAccountDTO) session.getAttribute("userSession");
        	Date date  = new Date();
        	
        	OrderDTO orderDTO = new OrderDTO();
        	orderDTO.setUserID(userAccountDTO.getUserID());
        	orderDTO.setOrderTime(new Long(date.getTime()));
        	orderDTO.setTax(getTax());
        	orderDTO.setTotalPrice(getTotalPrice(arrlist));
        	
        	BookStoreMgrImp bookStoreMgr = new BookStoreMgrImp();  
            ActionMessages messages = new ActionMessages();
        	
        	try {
	        	int orderNumber = bookStoreMgr.createOrder(orderDTO);
	        	
	        	OrderItemDTO[] orderItemDTOs = new OrderItemDTO[arrlist.size()];
	        	
	        	for (int i=0; i<arrlist.size(); i++) {
	        		ShoppingCartDTO shoppingCartDTO = (ShoppingCartDTO) arrlist.get(i);
	        		OrderItemDTO orderItemDTO = new OrderItemDTO();
	        		orderItemDTO.setBookID(new Integer(shoppingCartDTO.getBookID()));
	        		orderItemDTO.setDiscount(shoppingCartDTO.getBookDTO().getDiscount());
	        		orderItemDTO.setQuantity(new Integer(shoppingCartDTO.getQuantity()));
	        		orderItemDTO.setOrderID(new Integer(orderNumber));
	        		orderItemDTO.setOrderItemNum(new Integer(i));
	        		orderItemDTOs[i] = orderItemDTO;
	        	}
	        	
	        	
	        	ArrayOf_tns2_OrderItemDTO orderItemArr = new ArrayOf_tns2_OrderItemDTO();
	        	orderItemArr.setOrderItemDTO(orderItemDTOs);
	        	bookStoreMgr.createOrderItem(orderItemArr);
	        	

	            messages.add("shoppingCart",new ActionMessage("submitShopCart"));    
            
        	} catch (ApplicationException ae) {
        		messages.add("shoppingCart",new ActionMessage(ae.getMessage1()));   
        	}
            
            this.saveMessages(request,messages);
            
            return mapping.findForward("submitShopCart");	
        	
        }
    	
        
        
        if(arrlist != null && arrlist.size() > 0)
        {
        
	        DynaValidatorForm shoppingCartForm = (DynaValidatorForm)  form;
	        
	        shoppingCartForm.set("bookID",new String[arrlist.size()]);
	        shoppingCartForm.set("bookName",new String[arrlist.size()]);    
	        shoppingCartForm.set("unitPrice",new String[arrlist.size()]);
	        shoppingCartForm.set("discount",new String[arrlist.size()]);   
	        shoppingCartForm.set("quantity",new String[arrlist.size()]);  
	        
	        for (int i=0; i<arrlist.size(); i++) {
	        	ShoppingCartDTO shoppingCartDTO = (ShoppingCartDTO) arrlist.get(i);
	        	shoppingCartForm.set("bookID",i,(new Integer(shoppingCartDTO.getBookID()).toString()));
	        	shoppingCartForm.set("quantity",i,(new Integer(shoppingCartDTO.getQuantity()).toString()));
	        	shoppingCartForm.set("bookName",i,shoppingCartDTO.getBookDTO().getBookName());
	        	shoppingCartForm.set("unitPrice",i,shoppingCartDTO.getBookDTO().getUnitPrice().toString());
	        	shoppingCartForm.set("discount",i,shoppingCartDTO.getBookDTO().getDiscount().toString());
	        }
	        
	        shoppingCartForm.set("totalPrice",getTotalPrice(arrlist).toString());
	        shoppingCartForm.set("tax",getTax().toString());
	        
        }
               
        return mapping.findForward("success");
    }
    
    
    private BigDecimal getTax()
    {
    	return new BigDecimal(0);
    }
    
    private BigDecimal getTotalPrice(ArrayList arrlist) {
    	BigDecimal  totalPrice = new BigDecimal(0);
    	
    	for (int i=0; i<arrlist.size(); i++) {
    		ShoppingCartDTO shoppingCartDTO = (ShoppingCartDTO) arrlist.get(i);
    		BigDecimal actualUnitPrice = shoppingCartDTO.getBookDTO().getUnitPrice().subtract(shoppingCartDTO.getBookDTO().getDiscount());
    		BigDecimal actualPrice = actualUnitPrice.multiply(new BigDecimal(shoppingCartDTO.getQuantity()));
    		totalPrice = totalPrice.add(actualPrice);
    	}
    	
    	return totalPrice;
    }
    
    private void refreshShoppingCartForm(ArrayList arrlist,DynaValidatorForm form) {
        DynaValidatorForm shoppingCartForm = (DynaValidatorForm)  form;
        
        shoppingCartForm.set("bookID",new String[arrlist.size()]);
        shoppingCartForm.set("bookName",new String[arrlist.size()]);    
        shoppingCartForm.set("unitPrice",new String[arrlist.size()]);
        shoppingCartForm.set("discount",new String[arrlist.size()]);   
        shoppingCartForm.set("quantity",new String[arrlist.size()]);
 
    	
    }
}

⌨️ 快捷键说明

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