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

📄 orderservicespringimpl.java

📁 struts+spring+hibernate的例子
💻 JAVA
字号:
/*
 * Created on Mar 8, 2004
 *
 * (c) 2004, Mark Eagle, meagle@meagle.com
 * relased under terms of the GNU public license 
 * http://www.gnu.org/licenses/licenses.html#TOCGPL
 */
package com.meagle.service.spring;

import java.util.Iterator;
import java.util.List;
import java.util.Set;

import com.meagle.bo.Order;
import com.meagle.bo.OrderLineItem;
import com.meagle.exception.OrderException;
import com.meagle.exception.OrderMinimumAmountException;
import com.meagle.service.IOrderService;
import com.meagle.service.dao.IOrderDAO;

/**
 * This class provides business logic and services to other layers
 * 
 * @author meagle
 *  
 */
public class OrderServiceSpringImpl implements IOrderService {

	private static final double ORDER_MINIMUM = 100.0;

	private IOrderDAO orderDAO;

	/**
	 * Default constructor
	 */
	public OrderServiceSpringImpl() {
		super();
	}

	public Order saveNewOrder(Order order)
		throws OrderException, OrderMinimumAmountException {

		// do some business logic
		if (order != null && order.getTotal() == 0) {

			double total = 0.0;

			Set items = order.getOrderLineItems();
			Iterator iter = items.iterator();
			while (iter.hasNext()) {
				OrderLineItem item = (OrderLineItem) iter.next();
				total += item.getLineItemPrice();
			}

			if (total < OrderServiceSpringImpl.ORDER_MINIMUM) {
				throw new OrderMinimumAmountException("Order did not exceed the order minimum");
			} else {
				order.setTotal(total);
			}
		}

		Order savedOrder = null;
		try {
			savedOrder = getOrderDAO().saveOrder(order);
		} catch (RuntimeException e) {			
			
			throw new OrderException("Could not save order " + e.toString());
		}

		return savedOrder;
	}

	/**
	 * Find an order by the user that placed the order
	 * 
	 * @param user Person that placed the order
	 * @return Order
	 */
	public List findOrderByUser(String user) throws OrderException {

		List orders = null;
		try {
			orders = getOrderDAO().findOrdersPlaceByUser(user);
		} catch (RuntimeException e) {
			// should really use a logger instead of System.out
			System.out.println(
				"Could not locate order by user " + e.getMessage());
			throw new OrderException(
				"Could not locate order by user " + e.getMessage());
		}
		return orders;
	}

	/**
	 * Find an order by ID
	 * 
	 * @param id
	 * @return Order
	 */
	public Order findOrderById(int id) throws OrderException {

		Order order = null;
		try {
			order = getOrderDAO().findOrderById(id);
		} catch (RuntimeException e) {
			// should really use a logger instead of System.out
			System.out.println(
				"Could not locate order by ID " + e.getMessage());
			throw new OrderException(
				"Could not locate order by ID " + e.getMessage());
		}
		return order;
	}

	/**
	 * @return
	 */
	public IOrderDAO getOrderDAO() {
		
		return orderDAO;
	}

	/**
	 * @param orderDAO
	 */
	public void setOrderDAO(IOrderDAO orderDAO) {
		this.orderDAO = orderDAO;
	}

}

⌨️ 快捷键说明

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