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

📄 placeorderaction.java

📁 基于Struts的网络商店源码。使用Hibernate技术
💻 JAVA
字号:
package com.sush.webstore.store.web.struts;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.sush.webstore.store.domain.IAddress;
import com.sush.webstore.store.domain.ICart;
import com.sush.webstore.store.domain.ICreditCard;
import com.sush.webstore.store.domain.IOrder;
import com.sush.webstore.store.domain.IUserAccount;
import com.sush.webstore.store.domain.IWebStoreFacade;
import com.sush.webstore.store.domain.constants.IAddressType;
import com.sush.webstore.store.domain.constants.IOrderStatus;
import com.sush.webstore.store.domain.facade.WebStorePOJO;

public class PlaceOrderAction {

	private String address;

	private String phoneNumber;

	private String city;

	private String state;

	private String zip;

	private String cardType;

	private String cardNumber;

	private String expiryDate;

	private String country;

	private IWebStoreFacade webStore = new WebStorePOJO();

	private ICart cart;

	private IUserAccount userAccount;

	private IOrder order;

	public String execute() {

		if (!getCartFromSession())
			return "error";

		if (!getUserFromSession()) {
			removeCartFromSession();
			return "error";
		}

		if (!getOrderFromSession()) {
			removeCartFromSession();
			removeUserFromSession();
			return "error";
		}

		if (!validate())
			return "showBillingDetails";

		IAddress shippingAddress = webStore.createAddress();
		shippingAddress.setAddress(address);
		shippingAddress.setCity(city);
		shippingAddress.setCountry(country);
		shippingAddress.setState(state);
		shippingAddress.setType(IAddressType.BILLING);
		shippingAddress.setZip(zip);
		shippingAddress.setPhoneNumber(phoneNumber);

		ICreditCard creditCard = webStore.createCreditCard();
		creditCard.setCardType(cardType);
		creditCard.setCreditCardNumber(cardNumber);
		SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy");
		Date expDate = null;
		try {
			expDate = df.parse(expiryDate);
		} catch (Exception e) {
			e.printStackTrace();
			return "showBillingDetails";
		}
		creditCard.setExpiryDate(expDate);

		order.setDate(new Date());
		order.setICart(cart);
		order.setIShippingAddress(shippingAddress);
		order.setIUserAccount(userAccount);
		order.setOrderStatus(IOrderStatus.PENDING);
		order.setICreditCard(creditCard);

		return (webStore.addOrder(order)) ? "success" : "error";
	}

	/**
	 * @return the address
	 */
	public String getAddress() {
		return address;
	}

	/**
	 * @param address
	 *            the address to set
	 */
	public void setAddress(String address) {
		this.address = address;
	}

	/**
	 * @return the cardNumber
	 */
	public String getCardNumber() {
		return cardNumber;
	}

	/**
	 * @param cardNumber
	 *            the cardNumber to set
	 */
	public void setCardNumber(String cardNumber) {
		this.cardNumber = cardNumber;
	}

	/**
	 * @return the cardType
	 */
	public String getCardType() {
		return cardType;
	}

	/**
	 * @param cardType
	 *            the cardType to set
	 */
	public void setCardType(String cardType) {
		this.cardType = cardType;
	}

	/**
	 * @return the city
	 */
	public String getCity() {
		return city;
	}

	/**
	 * @param city
	 *            the city to set
	 */
	public void setCity(String city) {
		this.city = city;
	}

	/**
	 * @return the expiryDate
	 */
	public String getExpiryDate() {
		return expiryDate;
	}

	/**
	 * @param expiryDate
	 *            the expiryDate to set
	 */
	public void setExpiryDate(String expiryDate) {
		this.expiryDate = expiryDate;
	}

	/**
	 * @return the phoneNumber
	 */
	public String getPhoneNumber() {
		return phoneNumber;
	}

	/**
	 * @param phoneNumber
	 *            the phoneNumber to set
	 */
	public void setPhoneNumber(String phoneNumber) {
		this.phoneNumber = phoneNumber;
	}

	/**
	 * @return the state
	 */
	public String getState() {
		return state;
	}

	/**
	 * @param state
	 *            the state to set
	 */
	public void setState(String state) {
		this.state = state;
	}

	/**
	 * @return the zip
	 */
	public String getZip() {
		return zip;
	}

	/**
	 * @param zip
	 *            the zip to set
	 */
	public void setZip(String zip) {
		this.zip = zip;
	}

	/**
	 * @return the webStore
	 */
	public IWebStoreFacade getWebStore() {
		return webStore;
	}

	/**
	 * @param webStore
	 *            the webStore to set
	 */
	public void setWebStore(IWebStoreFacade webStore) {
		this.webStore = webStore;
	}

	/**
	 * @return the cart
	 */
	public ICart getCart() {
		return cart;
	}

	/**
	 * @param cart
	 *            the cart to set
	 */
	public void setCart(ICart cart) {
		this.cart = cart;
	}

	/**
	 * @return the cart
	 */
	public boolean getCartFromSession() {

		Map session = (Map) ActionContext.getContext().get("session");

		if (session == null) {
			return false;
		}

		cart = (ICart) session.get("cart");

		return (cart == null) ? false : true;
	}

	/**
	 * @return the cart
	 */
	public boolean removeCartFromSession() {

		Map session = (Map) ActionContext.getContext().get("session");

		if (session == null) {
			return false;
		}

		session.put("cart", null);
		return true;
	}

	/**
	 * @return the cart
	 */
	public boolean getUserFromSession() {

		Map session = (Map) ActionContext.getContext().get("session");

		if (session == null) {
			return false;
		}

		userAccount = (IUserAccount) session.get("userAccount");

		return (userAccount == null) ? false : true;
	}

	/**
	 * @return the cart
	 */
	public boolean removeUserFromSession() {

		Map session = (Map) ActionContext.getContext().get("session");

		if (session == null) {
			return false;
		}

		session.put("userAccount", null);
		return true;
	}

	/**
	 * @return the userAccount
	 */
	public IUserAccount getUserAccount() {
		return userAccount;
	}

	/**
	 * @param userAccount
	 *            the userAccount to set
	 */
	public void setUserAccount(IUserAccount userAccount) {
		this.userAccount = userAccount;
	}

	/**
	 * @return the country
	 */
	public String getCountry() {
		return country;
	}

	/**
	 * @param country
	 *            the country to set
	 */
	public void setCountry(String country) {
		this.country = country;
	}

	private boolean getOrderFromSession() {

		Map session = (Map) ActionContext.getContext().get("session");

		if (session == null) {
			return false;
		}

		order = (IOrder) session.get("order");
		return (order == null) ? false : true;
	}

	/**
	 * @return the order
	 */
	public IOrder getOrder() {
		return order;
	}

	/**
	 * @param order
	 *            the order to set
	 */
	public void setOrder(IOrder order) {
		this.order = order;
	}

	private boolean validate() {
		if (this.address == null || this.cardNumber == null
				|| this.cardType == null || this.city == null
				|| this.country == null /* || (this.expiryDate == null) */
				|| this.phoneNumber == null || this.state == null
				|| this.zip == null)
			return false;

		if (this.address.isEmpty() || this.cardNumber.isEmpty()
				|| this.cardType.isEmpty() || this.city.isEmpty()
				|| this.country.isEmpty() || this.phoneNumber.isEmpty()
				|| this.state.isEmpty() || this.zip.isEmpty())
			return false;
		return true;
	}
}

⌨️ 快捷键说明

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