中规模应用的开发(一)

BookStore

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>bookstore</groupId>
  <artifactId>BookStore</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>BookStore Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
    	<groupId>org.hibernate</groupId>
    	<artifactId>hibernate-core</artifactId>
    	<version>3.3.1.GA</version>
    </dependency>
    <dependency>
    	<groupId>org.hibernate</groupId>
    	<artifactId>hibernate-annotations</artifactId>
    	<version>3.4.0.GA</version>
    </dependency>
        <dependency>
      <groupId>postgresql</groupId>
      <artifactId>postgresql</artifactId>
      <version>8.3-603.jdbc3</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-orm</artifactId>
      <version>2.5.6</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aop</artifactId>
      <version>2.5.6</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>2.5.6</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc-struts</artifactId>
      <version>2.5.6</version>
    </dependency>
	<dependency>
      <groupId>org.apache.struts</groupId>
      <artifactId>struts-core</artifactId>
      <version>1.3.9</version>
    </dependency>
	<dependency>
		<groupId>org.apache.velocity</groupId>
		<artifactId>velocity</artifactId>
		<version>1.6</version>
	</dependency>
	<dependency>
		<groupId>org.apache.velocity</groupId>
		<artifactId>velocity-tools</artifactId>
		<version>1.3</version>
	</dependency>
	<dependency>
		<groupId>commons-dbcp</groupId>
		<artifactId>commons-dbcp</artifactId>
		<version>1.2.2</version>
	</dependency>
	<dependency>
		<groupId>commons-codec</groupId>
		<artifactId>commons-codec</artifactId>
		<version>1.3</version>
	</dependency>
	    <dependency>
    	<groupId>org.slf4j</groupId>
    	<artifactId>slf4j-jdk14</artifactId>
    	<version>1.5.2</version>
    </dependency>
    <dependency>
    	<groupId>javassist</groupId>
    	<artifactId>javassist</artifactId>
    	<version>3.8.0.GA</version>
    </dependency>
  </dependencies>
  <build>
    <finalName>BookStore</finalName>
  </build>
</project>

BookStore\init

message.txt

errors.header=<font color="red">
errors.footer=</font>

error.login.pwmismatch=ログイン名とパスワードが一致しません。

error.createuser.pass2inmatch=確認用パスワードが一致しません。
error.createuser.useralreadyexist=このアカウントはすでに利用されています。
error.createuser.cannotcreate=ユーザが作成できませんでした。
error.createuser.hasempty=入力欄に空欄がありました。

error.addtocart.notselected=商品が選択されていません。

error.checkout.noselected=商品が選択されていません。

error.search.notfound=見つかりませんでした。

BookStore\src\main\java\bookstore\action\bean

AddToCartActionFormBean.java

package bookstore.action.bean;

import org.apache.struts.action.ActionForm;

public class AddToCartActionFormBean extends ActionForm{

	private String[] selecteditems;
	
	public String[] getSelecteditems(){
		return( this.selecteditems );
	}
	public void setSelecteditems( String[] inSelecteditems ){
		this.selecteditems = inSelecteditems;
	}
}

CreateUserActionFormBean.java

package bookstore.action.bean;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

public class CreateUserActionFormBean extends ActionForm{
	
	private String account;
	private String name;
	private String email;
	private String passwd;
	private String passwd2;
	
	public String getAccount() {
		return account;
	}
	public void setAccount(String account) {
		this.account = account;
	}
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	
	public String getPasswd() {
		return passwd;
	}
	public void setPasswd(String passwd) {
		this.passwd = passwd;
	}
	
	public String getPasswd2() {
		return passwd2;
	}
	public void setPasswd2(String passwd2) {
		this.passwd2 = passwd2;
	}
	
	public ActionErrors validate( ActionMapping mapping,
								   HttpServletRequest req ){
		
		ActionErrors errors = null;
		if( getName().equals( "" ) || getName() == null ||
			getEmail().equals( "" ) || getEmail() == null ||
			getAccount().equals( "" ) || getAccount() == null ||
			getPasswd().equals( "" ) || getPasswd() == null ||
			getPasswd2().equals( "" ) || getPasswd2() == null ){
		
			errors = new ActionErrors();
		
			errors.add( "illegalcreateuser",
					new ActionMessage( "error.createuser.hasempty" ) );
		}
		
		return( errors );
	}
}

LoginActionFormBean.java

package bookstore.action.bean;

import org.apache.struts.action.ActionForm;

public class LoginActionFormBean extends ActionForm{
	
	private String account;
	private String passwd;
	
	public String getAccount() {
		return account;
	}
	public void setAccount(String account) {
		this.account = account;
	}
	
	public String getPasswd() {
		return passwd;
	}
	public void setPasswd(String passwd) {
		this.passwd = passwd;
	}
}

SearchActionFormBean.java

package bookstore.action.bean;

import org.apache.struts.action.ActionForm;

public class SearchActionFormBean extends ActionForm{
	
	private String keyword;

	public String getKeyword() {
		return keyword;
	}

	public void setKeyword(String keyword) {
		this.keyword = keyword;
	}
}

BookStore\src\main\java\bookstore\action

AddToCartAction.java

package bookstore.action;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

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

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 bookstore.action.bean.AddToCartActionFormBean;
import bookstore.logic.BookLogic;
import bookstore.vbean.VBook;

public class AddToCartAction extends Action {
	
	BookLogic bookLogic;
	
	public ActionForward execute( ActionMapping mapping,
								   ActionForm form,
								   HttpServletRequest req,
								   HttpServletResponse res ){

		HttpSession httpSession = req.getSession( false );
		if( httpSession == null ){
			return( mapping.findForward( "illegalSession" ) );
		}
		
		List<String> cart = (List<String>) httpSession.getAttribute( "Cart" );
		if( cart == null ){
			cart = new ArrayList<String>();
		}
		
		AddToCartActionFormBean atcafb = (AddToCartActionFormBean)form;
		
		List<String> productList = (List<String>)httpSession.getAttribute( "ProductList" );
		
		String[] selecteItemsArray = atcafb.getSelecteditems();
		List<String> selectedItems = null;

		if( selecteItemsArray != null &&
				selecteItemsArray.length != 0 ){
			selectedItems = Arrays.asList( atcafb.getSelecteditems() );
		}
		
		List<String> newCart = bookLogic.createCart( productList,
													 selectedItems,
													 cart );
		
		httpSession.setAttribute( "Cart", newCart );

		List<String> productListAll = bookLogic.getAllBookISBNs();
		List<VBook> vProductList = bookLogic.createVBookList(
										productListAll, newCart );
		
		httpSession.setAttribute( "ProductList", productListAll );
		httpSession.setAttribute( "ProductListView", vProductList );
		
		return( mapping.findForward( "Continue" ) );
	}
	

	public void setBookLogic( BookLogic bookLogic ){
		this.bookLogic = bookLogic;
	}
}

CheckoutAction.java

package bookstore.action;

import java.util.List;

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

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 bookstore.logic.BookLogic;

public class CheckoutAction extends Action {
	
	BookLogic bookLogic;
	
	public ActionForward execute( ActionMapping mapping,
								   ActionForm form,
								   HttpServletRequest req,
								   HttpServletResponse res ){

		HttpSession httpSession = req.getSession( false );
		if( httpSession == null ){
			return( mapping.findForward( "illegalSession" ) );
		}
		
		List<String> selectedItems = (List<String>)httpSession.getAttribute( "Cart" );

		if( selectedItems == null || selectedItems.size() == 0 ){

			ActionMessages errors = new ActionMessages();
			
			errors.add( "productalart",
					new ActionMessage( "error.checkout.noselected" ) );
			saveMessages( req, errors );
			return( mapping.findForward( "illegalCheckout" ) );
		}
		
		httpSession.setAttribute( "ItemsToBuy",
					bookLogic.createVCheckout( selectedItems ) );
		
		return( mapping.findForward( "ToCheck" ) );
	}
	

	public void setBookLogic( BookLogic bookLogic ){
		this.bookLogic = bookLogic;
	}
}

CreateUserAction.java

package bookstore.action;

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

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 bookstore.action.bean.CreateUserActionFormBean;
import bookstore.logic.CustomerLogic;

public class CreateUserAction extends Action{
	
	CustomerLogic customerLogic;
	
	public ActionForward execute( ActionMapping mapping,
								   ActionForm form,
								   HttpServletRequest req,
								   HttpServletResponse res ){
		
		CreateUserActionFormBean cuafb = (CreateUserActionFormBean)form;

		String passwd = cuafb.getPasswd();
		String passwd2 = cuafb.getPasswd2();

		ActionMessages errors;

		if( passwd.equals( passwd2 ) == false ){
			// passwd and passwd2 not matched
			errors = new ActionMessages();
			errors.add( "illegalcreateuser",
				new ActionMessage( "error.createuser.pass2inmatch" ) );
			saveMessages( req, errors );
			return( mapping.findForward( "illegalCreateUser" ) );
		}

		String account = cuafb.getAccount();
		
		if( customerLogic.isAlreadyExsited( account ) ){
			// user has already exsited
			errors = new ActionMessages();
			errors.add( "illegalcreateuser",
					new ActionMessage( "error.createuser.useralreadyexist" ) );
			saveMessages( req, errors );
			return( mapping.findForward( "illegalCreateUser" ) );
		}
	
		if( !customerLogic.createCustomer( account,
										  passwd, 
										  cuafb.getName(), 
										  cuafb.getEmail() ) ){
			// user was not created
			errors = new ActionMessages();
			errors.add( "illegalcreateuser",
					new ActionMessage( "error.createuser.cannotcreate" ) );
			saveMessages( req, errors );
			return( mapping.findForward( "illegalCreateUser" ) );
		}
		
		return( mapping.findForward( "UserCreated" ) );
	}

	
	public void setCustomerLogic(CustomerLogic inCustomerLogic) {
		this.customerLogic = inCustomerLogic;
	}
}

LoginAction.java

package bookstore.action;

import java.util.List;

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

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 bookstore.action.bean.LoginActionFormBean;
import bookstore.logic.BookLogic;
import bookstore.logic.CustomerLogic;
import bookstore.vbean.VBook;

public class LoginAction extends Action{
	
	CustomerLogic customerLogic;
	BookLogic bookLogic;
	
	public ActionForward execute( ActionMapping mapping,
								   ActionForm form,
								   HttpServletRequest req,
								   HttpServletResponse res ){
		
		LoginActionFormBean lafb = (LoginActionFormBean)form;

		// password match
		if( !customerLogic.isPasswordMatched(
							lafb.getAccount(),
							lafb.getPasswd() ) ){
			// Account Mismached
			ActionMessages errors = new ActionMessages();
			errors.add( "illegallogin",
				new ActionMessage( "error.login.pwmismatch" ) );
			saveMessages( req, errors );
			return( mapping.findForward( "illegalLogin" ) );
		}
		
		// getSession()
		HttpSession httpSession = req.getSession( false );
		if( httpSession != null ){
			httpSession.invalidate();
		}

		httpSession = req.getSession();
		
		httpSession.setAttribute( "Login", lafb.getAccount() );
		
		List<String> productListAll = bookLogic.getAllBookISBNs();
		List<VBook> vProductList = bookLogic.createVBookList(
										 productListAll, null );
		
		httpSession.setAttribute( "ProductList", productListAll );
		httpSession.setAttribute( "ProductListView", vProductList );
		
		return( mapping.findForward( "LoginSuccess" ) );
	}

	
	public void setCustomerLogic(CustomerLogic customerLogic) {
		this.customerLogic = customerLogic;
	}


	public void setBookLogic(BookLogic bookLogic) {
		this.bookLogic = bookLogic;
	}
}

OrderAction.java

package bookstore.action;

import java.util.List;

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

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 bookstore.logic.CustomerLogic;
import bookstore.logic.OrderLogic;

public class OrderAction extends Action{
	
	OrderLogic orderLogic;
	CustomerLogic customerLogic;
	
	public ActionForward execute( ActionMapping mapping,
								   ActionForm form,
								   HttpServletRequest req,
								   HttpServletResponse res ){

		HttpSession httpSession = req.getSession( false );
		if( httpSession == null ){
			return( mapping.findForward( "illegalSession" ) );
		}
		
		String uid = (String)httpSession.getAttribute( "Login" );
		List<String> cart = (List<String>)httpSession.getAttribute( "Cart" );
		
		orderLogic.orderBooks( uid, cart );
		
		req.setAttribute( "Customer",
				customerLogic.createVCustomer( uid ) );
		
		return( mapping.findForward( "OrderSuccess" ) );
	}

	
	public void setOrderLogic(OrderLogic orderLogic) {
		this.orderLogic = orderLogic;
	}


	public void setCustomerLogic(CustomerLogic customerLogic) {
		this.customerLogic = customerLogic;
	}
}

SearchAction.java

package bookstore.action;

import java.util.List;

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

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 bookstore.action.bean.SearchActionFormBean;
import bookstore.logic.BookLogic;
import bookstore.vbean.VBook;

public class SearchAction extends Action{

	BookLogic bookLogic;
	
	public ActionForward execute( ActionMapping mapping,
								   ActionForm form,
								   HttpServletRequest req,
								   HttpServletResponse res ){

		HttpSession httpSession = req.getSession( false );
		if( httpSession == null ){
			return( mapping.findForward( "illegalSession" ) );
		}
		
		ActionMessages errors;
		
		List<String> cart = (List<String>) httpSession.getAttribute( "Cart" );
		
		SearchActionFormBean safb = (SearchActionFormBean)form;
		
		List<String> foundBooks = bookLogic.retrieveBookISBNsByKeyword( safb.getKeyword() );
		
		if( foundBooks == null || foundBooks.size() == 0 ){
			
			foundBooks = bookLogic.getAllBookISBNs();
			
			errors = new ActionMessages();
			errors.add( "productalart",
					new ActionMessage( "error.search.notfound" ) );
			saveMessages( req, errors );
		}
		
		List<VBook> vProductList = bookLogic.createVBookList(
											foundBooks, cart );

		httpSession.setAttribute( "ProductList", foundBooks );
		httpSession.setAttribute( "ProductListView", vProductList );		
				
		return( mapping.findForward( "SearchSuccess" ) );
	}
	
	
	public void setBookLogic(BookLogic bookLogic) {
		this.bookLogic = bookLogic;
	}
}

BookStore\src\main\java\bookstore\dao\hibernate

BookDAOImpl.java

package bookstore.dao.hibernate;

import java.util.List;
import java.util.regex.Pattern;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.annotation.Propagation;

import bookstore.dao.BookDAO;
import bookstore.pbean.TBook;

@Transactional(readOnly = true, propagation = Propagation.REQUIRED)
public class BookDAOImpl extends HibernateDaoSupport
											implements BookDAO{

	public int getPriceByISBNs(final List<String> inISBNList) {

		HibernateTemplate ht = getHibernateTemplate();
		
		return( ((Long)ht.execute(new HibernateCallback() {

			public Object doInHibernate(Session session)
								throws HibernateException {
				
				Query priceQuery = session
					.createQuery("select sum( book.price ) from TBook book where book.isbn in ( :SELECTED_ITEMS )");
				priceQuery.setParameterList("SELECTED_ITEMS", inISBNList);

				return( (Long)priceQuery.uniqueResult() );
			}
		} )).intValue() );
	}


	public List<TBook> retrieveBooksByKeyword(String inKeyword) {
		String escapedKeyword = Pattern.compile( "([%_])" )
											.matcher( inKeyword )
											.replaceAll( "\\\\$1" );
		String[] keywords = { "%" + escapedKeyword + "%",
							  "%" + escapedKeyword + "%",
							  "%" + escapedKeyword + "%" };
		
		List<TBook> booksList = getHibernateTemplate().find( 
					"from TBook book where book.author like ?" +
					"or book.title like ? or book.publisher like ?" ,
					keywords );
		
		return( booksList );
	}


	public List<TBook> retrieveBooksByISBNs( final List<String> inISBNList ){

		HibernateTemplate ht = getHibernateTemplate();
		
		if( inISBNList == null ){
			return( ht.find( "from TBook book" ) );
		}else{
		
			return( ((List<TBook>)ht.execute(new HibernateCallback() {

				public Object doInHibernate(Session session)
									throws HibernateException {
				
					Query retrieveQuery = session
						.createQuery("from TBook book where book.isbn in ( :ISBNS )");
					retrieveQuery.setParameterList("ISBNS", inISBNList);

					return( retrieveQuery.list() );
				}
			} )));
		}
	}
}

CustomerDAOImpl.java

package bookstore.dao.hibernate;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.annotation.Propagation;

import bookstore.dao.CustomerDAO;
import bookstore.pbean.TCustomer;

@Transactional(readOnly = true, propagation = Propagation.REQUIRED)
public class CustomerDAOImpl extends HibernateDaoSupport implements CustomerDAO{
	
	public	int	getCustomerNumberByUid( final String inUid ){
		
		HibernateTemplate ht = getHibernateTemplate();
		
		return( ((Long)ht.execute(new HibernateCallback() {

			public Object doInHibernate(Session session)
								throws HibernateException {
				
				Query numQuery = session
					.createQuery( "select count(*) from TCustomer customer where customer.uid like :UID" );
						numQuery.setString( "UID", inUid );

				return( (Long) numQuery.uniqueResult() );
			}
		} )).intValue() );
	}
	
	
	public TCustomer findCustomerByUid(final String inUid) {

		HibernateTemplate ht = getHibernateTemplate();
		
		return( ((TCustomer)ht.execute(new HibernateCallback() {

			public Object doInHibernate(Session session)
								throws HibernateException {
				
				Query priceQuery = session
					.createQuery("from TCustomer customer where customer.uid like :UID" );
				priceQuery.setString( "UID", inUid );

				return( (TCustomer)priceQuery.uniqueResult() );
			}
		} )));
	}

	
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public void saveCustomer( String inUid,
							   String inPasswordMD5,
							   String inName,
							   String inEmail ){
		TCustomer saveCustomer = new TCustomer();
		
		saveCustomer.setUid( inUid );
		saveCustomer.setPasswordmd5( inPasswordMD5 );
		saveCustomer.setName( inName );
		saveCustomer.setEmail( inEmail );
		
		getHibernateTemplate().save( saveCustomer );
	}
}

OrderDAOImpl.java

package bookstore.dao.hibernate;

import java.util.Calendar;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.annotation.Propagation;

import bookstore.dao.OrderDAO;
import bookstore.pbean.TCustomer;
import bookstore.pbean.TOrder;

@Transactional(readOnly = true, propagation = Propagation.REQUIRED)
public class OrderDAOImpl extends HibernateDaoSupport
										implements OrderDAO{

	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public TOrder createOrder(TCustomer inCustomer){

		TOrder saveOrder = new TOrder();
		saveOrder.setTCustomer( inCustomer );
		saveOrder.setOrderday( Calendar.getInstance().getTime() );
		
		getHibernateTemplate().save( saveOrder );
		
		return( saveOrder );
	}
}

OrderDetailDAOImpl.java

package bookstore.dao.hibernate;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.annotation.Propagation;

import bookstore.dao.OrderDetailDAO;
import bookstore.pbean.TBook;
import bookstore.pbean.TOrder;
import bookstore.pbean.TOrderDetail;

@Transactional(readOnly = true, propagation = Propagation.REQUIRED)
public class OrderDetailDAOImpl extends HibernateDaoSupport
										implements OrderDetailDAO{

	
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public void createOrderDetail(TOrder inOrder, TBook inBook) {
	
		TOrderDetail saveOrderDetail = new TOrderDetail();
		
		saveOrderDetail.setTOrder( inOrder );
		saveOrderDetail.setTBook( inBook );
		
		getHibernateTemplate().save( saveOrderDetail );
	}
}

BookStore\src\main\java\bookstore\dao

BookDAO.java

package bookstore.dao;

import java.util.List;

import bookstore.pbean.TBook;

public interface BookDAO{
	public int getPriceByISBNs( List<String> inISBNList );
	public List<TBook> retrieveBooksByKeyword( String inKeyword );
	public List<TBook> retrieveBooksByISBNs( List<String> inISBNList );
}

CustomerDAO.java

package bookstore.dao;

import bookstore.pbean.TCustomer;

public interface CustomerDAO{
	public int			getCustomerNumberByUid( String inUid );
	public TCustomer	findCustomerByUid( String inUid );
	public void		saveCustomer( String inUid,
									  String inPasswordMD5,
									  String inName,
									  String inEmail );
}

OrderDAO.java

package bookstore.dao;

import bookstore.pbean.TCustomer;
import bookstore.pbean.TOrder;

public interface OrderDAO{
	public TOrder createOrder( TCustomer inCustomer );
}

OrderDetailDAO.java

package bookstore.dao;

import bookstore.pbean.TBook;
import bookstore.pbean.TOrder;

public interface OrderDetailDAO{
	public void createOrderDetail( TOrder inOrder, TBook inBook );
}

BookStore\src\main\java\bookstore\logic

BookLogic.java

package bookstore.logic;

import java.util.List;

import bookstore.vbean.VBook;
import bookstore.vbean.VCheckout;

public interface BookLogic{
	public List<String>	getAllBookISBNs();
	public List<String>	retrieveBookISBNsByKeyword( String inKeyword );
	public List<VBook>	createVBookList(List<String> inProductList,
								  		List<String> inSelectedList );
	public VCheckout	createVCheckout( List<String> inSelectedList );
	public List<String> createCart( List<String> inProductList, 
									 List<String> inSelectedList,
									 List<String> inCart  );
}

BookLogicImpl.java

package bookstore.logic;

import java.util.ArrayList;
import java.util.List;

import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.annotation.Propagation;

import bookstore.dao.BookDAO;
import bookstore.pbean.TBook;
import bookstore.vbean.VBook;
import bookstore.vbean.VCheckout;


@Transactional(readOnly = true, propagation = Propagation.REQUIRED)
public class BookLogicImpl implements BookLogic{

	BookDAO bookdao;

	public List<String> getAllBookISBNs() {
		List<String> isbns = new ArrayList<String>();
		
		for( TBook bookIter :  bookdao.retrieveBooksByISBNs( null ) ){
			isbns.add( bookIter.getIsbn() );
		}
		
		return( isbns );
	}
	

	public List<String> retrieveBookISBNsByKeyword(String inKeyword) {
		List<String> isbns = new ArrayList<String>();
		
		for( TBook bookIter :  bookdao.retrieveBooksByKeyword( inKeyword ) ){
			isbns.add( bookIter.getIsbn() );
		}
		
		return( isbns );
	}


	public List<VBook> createVBookList(List<String> inProductList, List<String> inSelectedList) {
		List<VBook> vArrayList = new ArrayList<VBook>();
		
		for( TBook bookIter :  bookdao.retrieveBooksByISBNs( inProductList ) ){
			VBook currentVBook = new VBook( bookIter );
			currentVBook.setSelected( false );

			if( inSelectedList != null && inSelectedList.size() != 0 ){
				if( inSelectedList.contains( bookIter.getIsbn() ) ){
					currentVBook.setSelected( true );
				}
			}
			
			vArrayList.add( currentVBook );
		}

		return( vArrayList );
	}
	
	
	public VCheckout createVCheckout( List<String> inSelectedList ){
		VCheckout vc = new VCheckout();
		vc.setTotal( bookdao.getPriceByISBNs( inSelectedList ) );

		List<VBook> viewList = new ArrayList<VBook>();
		
		for( TBook iterBook : bookdao.retrieveBooksByISBNs( inSelectedList ) ){
			VBook vb = new VBook( iterBook );
			vb.setSelected(true );
			
			viewList.add( vb );			
		}
		
		
		vc.setSelecteditems( viewList );
		
		return( vc );
	}

	
	public List<String> createCart( List<String> inProductList, 
							 List<String> inSelectedList,
							 List<String> inCart ){

		inCart.removeAll( inProductList );
		if( inSelectedList != null &&
			inSelectedList.size() != 0 ){
			inCart.addAll( inSelectedList );
		}
			
		return( inCart );
	}


	public void setBookdao( BookDAO bookdao ){
		this.bookdao = bookdao;
	}
}

CustomerLogic.java

package bookstore.logic;

import bookstore.vbean.VCustomer;

public interface CustomerLogic {
	public boolean isAlreadyExsited( String inSid );
	public boolean createCustomer( String inUid,
									 String inPassword,
									 String inName,
									 String inEmail );
	public boolean isPasswordMatched( String inUid,
										String inPassword );
	public VCustomer createVCustomer( String inUid );
}

CustomerLogicImpl.java

package bookstore.logic;

import bookstore.dao.CustomerDAO;
import bookstore.pbean.TCustomer;
import bookstore.vbean.VCustomer;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;


@Transactional(readOnly = true, propagation = Propagation.REQUIRED)
public class CustomerLogicImpl implements CustomerLogic {
	
	CustomerDAO customerdao;
	
	public boolean isAlreadyExsited( String inUid ){
		
		int customernum = customerdao.getCustomerNumberByUid( inUid );
		
		if( customernum != 0 ){
			return( true );
		}else{
			return( false );
		}
	}

	
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public boolean createCustomer( String inUid,
									 String inPassword,
									 String inName,
									 String inEmail ){

		if( isAlreadyExsited( inUid ) ){
			return( false );
		}
		
		String passwordMD5 = getStringDigest( inPassword );
		
		customerdao.saveCustomer( inUid, passwordMD5, inName, inEmail );
		
		return( true );
	}
	

	public boolean isPasswordMatched(String inUid, String inPassword) {

		if( !isAlreadyExsited( inUid ) ){
			return( false );			
		}
		
		TCustomer customer = customerdao.findCustomerByUid( inUid );
		if( customer.getPasswordmd5()
				.equals( getStringDigest( inPassword ) ) == false ){
			return( false );	
		}
		return( true );
	}

	
	public VCustomer createVCustomer(String inUid) {
		return( new VCustomer( 
					customerdao.findCustomerByUid( inUid ) ) );
	}
	
	
	private static String getStringDigest( String inString ){
		return( DigestUtils.md5Hex( inString + "digested" ) );
	}


	public void setCustomerdao( CustomerDAO inCdao ){
		this.customerdao = inCdao;
	}
}

OrderLogic.java

package bookstore.logic;

import java.util.List;

public interface OrderLogic {
	public void orderBooks( String inUid, List<String> inISBNs );
}

OrderLogicImpl.java

package bookstore.logic;

import java.util.List;

import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.annotation.Propagation;

import bookstore.dao.BookDAO;
import bookstore.dao.CustomerDAO;
import bookstore.dao.OrderDAO;
import bookstore.dao.OrderDetailDAO;
import bookstore.pbean.TBook;
import bookstore.pbean.TCustomer;
import bookstore.pbean.TOrder;


@Transactional(readOnly = true, propagation = Propagation.REQUIRED)
public class OrderLogicImpl implements OrderLogic {

	BookDAO bookdao;
	CustomerDAO customerdao;
	OrderDAO orderdao;
	OrderDetailDAO odetaildao;
	
	
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public void orderBooks( String inUid, List<String> inISBNs ){
		TCustomer customer = customerdao.findCustomerByUid( inUid );
		TOrder order = orderdao.createOrder( customer );

		for( TBook iterBook : bookdao.retrieveBooksByISBNs( inISBNs ) ){
			odetaildao.createOrderDetail( order, iterBook );
		}		
	}

	
	public void setBookdao( BookDAO bookdao ){
		this.bookdao = bookdao;
	}


	public void setCustomerdao( CustomerDAO customerdao ){
		this.customerdao = customerdao;
	}


	public void setOrderdao( OrderDAO orderdao ){
		this.orderdao = orderdao;
	}


	public void setOrderdetaildao( OrderDetailDAO odetaildao ){
		this.odetaildao = odetaildao;
	}
}

BookStore\src\main\java\bookstore\pbean

TBook.java

package bookstore.pbean;

// Generated 2008/12/06 0:12:15 by Hibernate Tools 3.2.1.GA

import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
import org.hibernate.annotations.GenericGenerator;

/**
 * TBook generated by hbm2java
 */
@Entity
@Table(name = "t_book", schema = "public", uniqueConstraints = @UniqueConstraint(columnNames = "isbn"))
public class TBook implements java.io.Serializable {

	private int id;
	private String isbn;
	private String title;
	private String author;
	private String publisher;
	private int price;
	private Set<TOrderDetail> TOrderDetails = new HashSet<TOrderDetail>(0);

	public TBook() {
	}

	public TBook(int id, String isbn, String title, String author,
			String publisher, int price) {
		this.id = id;
		this.isbn = isbn;
		this.title = title;
		this.author = author;
		this.publisher = publisher;
		this.price = price;
	}

	public TBook(int id, String isbn, String title, String author,
			String publisher, int price, Set<TOrderDetail> TOrderDetails) {
		this.id = id;
		this.isbn = isbn;
		this.title = title;
		this.author = author;
		this.publisher = publisher;
		this.price = price;
		this.TOrderDetails = TOrderDetails;
	}

	@Id
	@GeneratedValue(generator="hibernate_sequence")
	@GenericGenerator(name="hibernate_sequence", strategy = "native")
	@Column(name="id", unique=true, nullable=false)
	public int getId() {
		return this.id;
	}

	public void setId(int id) {
		this.id = id;
	}

	@Column(name = "isbn", unique = true, nullable = false)
	public String getIsbn() {
		return this.isbn;
	}

	public void setIsbn(String isbn) {
		this.isbn = isbn;
	}

	@Column(name = "title", nullable = false)
	public String getTitle() {
		return this.title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	@Column(name = "author", nullable = false)
	public String getAuthor() {
		return this.author;
	}

	public void setAuthor(String author) {
		this.author = author;
	}

	@Column(name = "publisher", nullable = false)
	public String getPublisher() {
		return this.publisher;
	}

	public void setPublisher(String publisher) {
		this.publisher = publisher;
	}

	@Column(name = "price", nullable = false)
	public int getPrice() {
		return this.price;
	}

	public void setPrice(int price) {
		this.price = price;
	}

	@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "TBook")
	public Set<TOrderDetail> getTOrderDetails() {
		return this.TOrderDetails;
	}

	public void setTOrderDetails(Set<TOrderDetail> TOrderDetails) {
		this.TOrderDetails = TOrderDetails;
	}

}

TCustomer.java

package bookstore.pbean;

// Generated 2008/12/06 0:12:15 by Hibernate Tools 3.2.1.GA

import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
import org.hibernate.annotations.GenericGenerator;

/**
 * TCustomer generated by hbm2java
 */
@Entity
@Table(name = "t_customer", schema = "public", uniqueConstraints = @UniqueConstraint(columnNames = "uid"))
public class TCustomer implements java.io.Serializable {

	private int id;
	private String uid;
	private String passwordmd5;
	private String name;
	private String email;
	private Set<TOrder> TOrders = new HashSet<TOrder>(0);

	public TCustomer() {
	}

	public TCustomer(int id, String uid, String passwordmd5, String name,
			String email) {
		this.id = id;
		this.uid = uid;
		this.passwordmd5 = passwordmd5;
		this.name = name;
		this.email = email;
	}

	public TCustomer(int id, String uid, String passwordmd5, String name,
			String email, Set<TOrder> TOrders) {
		this.id = id;
		this.uid = uid;
		this.passwordmd5 = passwordmd5;
		this.name = name;
		this.email = email;
		this.TOrders = TOrders;
	}

	@Id
	@GeneratedValue(generator="hibernate_sequence")
	@GenericGenerator(name="hibernate_sequence", strategy = "native")
	@Column(name="id", unique=true, nullable=false)
	public int getId() {
		return this.id;
	}

	public void setId(int id) {
		this.id = id;
	}

	@Column(name = "uid", unique = true, nullable = false)
	public String getUid() {
		return this.uid;
	}

	public void setUid(String uid) {
		this.uid = uid;
	}

	@Column(name = "passwordmd5", nullable = false)
	public String getPasswordmd5() {
		return this.passwordmd5;
	}

	public void setPasswordmd5(String passwordmd5) {
		this.passwordmd5 = passwordmd5;
	}

	@Column(name = "name", nullable = false)
	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Column(name = "email", nullable = false)
	public String getEmail() {
		return this.email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "TCustomer")
	public Set<TOrder> getTOrders() {
		return this.TOrders;
	}

	public void setTOrders(Set<TOrder> TOrders) {
		this.TOrders = TOrders;
	}

}

TOrder.java

package bookstore.pbean;

// Generated 2008/12/06 0:12:15 by Hibernate Tools 3.2.1.GA

import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import org.hibernate.annotations.GenericGenerator;

/**
 * TOrder generated by hbm2java
 */
@Entity
@Table(name = "t_order", schema = "public")
public class TOrder implements java.io.Serializable {

	private int id;
	private TCustomer TCustomer;
	private Date orderday;
	private Set<TOrderDetail> TOrderDetails = new HashSet<TOrderDetail>(0);

	public TOrder() {
	}

	public TOrder(int id, TCustomer TCustomer, Date orderday) {
		this.id = id;
		this.TCustomer = TCustomer;
		this.orderday = orderday;
	}

	public TOrder(int id, TCustomer TCustomer, Date orderday,
			Set<TOrderDetail> TOrderDetails) {
		this.id = id;
		this.TCustomer = TCustomer;
		this.orderday = orderday;
		this.TOrderDetails = TOrderDetails;
	}

	@Id
	@GeneratedValue(generator="hibernate_sequence")
	@GenericGenerator(name="hibernate_sequence", strategy = "native")
	@Column(name="id", unique=true, nullable=false)
	public int getId() {
		return this.id;
	}

	public void setId(int id) {
		this.id = id;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "customer_id_fk", nullable = false)
	public TCustomer getTCustomer() {
		return this.TCustomer;
	}

	public void setTCustomer(TCustomer TCustomer) {
		this.TCustomer = TCustomer;
	}

	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "orderday", nullable = false, length = 35)
	public Date getOrderday() {
		return this.orderday;
	}

	public void setOrderday(Date orderday) {
		this.orderday = orderday;
	}

	@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "TOrder")
	public Set<TOrderDetail> getTOrderDetails() {
		return this.TOrderDetails;
	}

	public void setTOrderDetails(Set<TOrderDetail> TOrderDetails) {
		this.TOrderDetails = TOrderDetails;
	}

}

TOrderDetail.java

package bookstore.pbean;

// Generated 2008/12/06 0:12:15 by Hibernate Tools 3.2.1.GA

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import org.hibernate.annotations.GenericGenerator;

/**
 * TOrderDetail generated by hbm2java
 */
@Entity
@Table(name = "t_order_detail", schema = "public")
public class TOrderDetail implements java.io.Serializable {

	private int id;
	private TOrder TOrder;
	private TBook TBook;

	public TOrderDetail() {
	}

	public TOrderDetail(int id, TOrder TOrder, TBook TBook) {
		this.id = id;
		this.TOrder = TOrder;
		this.TBook = TBook;
	}

	@Id
	@GeneratedValue(generator="hibernate_sequence")
	@GenericGenerator(name="hibernate_sequence", strategy = "native")
	@Column(name="id", unique=true, nullable=false)
	public int getId() {
		return this.id;
	}

	public void setId(int id) {
		this.id = id;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "order_id_fk", nullable = false)
	public TOrder getTOrder() {
		return this.TOrder;
	}

	public void setTOrder(TOrder TOrder) {
		this.TOrder = TOrder;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "book_id_fk", nullable = false)
	public TBook getTBook() {
		return this.TBook;
	}

	public void setTBook(TBook TBook) {
		this.TBook = TBook;
	}

}

BookStore\src\main\java\bookstore\vbean

VBook.java

package bookstore.vbean;

import bookstore.pbean.TBook;

public class VBook{
	
	     private String isbn;
	     private String title;
	     private String author;
	     private String publisher;
	     private int price;
	     private boolean selected;

	    public VBook(){}
	    
	    public VBook( TBook book ){
	    	this.isbn = book.getIsbn();
	    	this.title = book.getTitle();
	    	this.author = book.getAuthor();
	    	this.publisher = book.getPublisher();
	    	this.price = book.getPrice();
	    	this.selected = false;
	    }
	    
	    public String getIsbn() {
	        return this.isbn;
	    }	    
	    public void setIsbn(String isbn) {
	        this.isbn = isbn;
	    }

	    public String getTitle() {
	        return this.title;
	    }
	    public void setTitle(String title) {
	        this.title = title;
	    }

	    public String getAuthor() {
	        return this.author;
	    }
	    public void setAuthor(String author) {
	        this.author = author;
	    }

	    public String getPublisher() {
	        return this.publisher;
	    }
	    public void setPublisher(String publisher) {
	        this.publisher = publisher;
	    }

	    public int getPrice() {
	        return this.price;
	    }
	    public void setPrice(int price) {
	        this.price = price;
	    }

		public boolean isSelected() {
			return selected;
		}
		public void setSelected(boolean selected) {
			this.selected = selected;
		}
}

VCheckout.java

package bookstore.vbean;

import java.util.List;


public class VCheckout{
	
	private int total;
	private List selecteditems;

	public int getTotal(){
		return( this.total );
	}
	public void setTotal( int inTotal ){
		this.total = inTotal;
	}
	
	public List getSelecteditems() {
		return selecteditems;
	}
	public void setSelecteditems(List inSelecteditems) {
		this.selecteditems = inSelecteditems;
	}
}

VCustomer.java

package bookstore.vbean;

import bookstore.pbean.TCustomer;

public class VCustomer{

	private String uid;
	private String name;
	private String email;
	
	public VCustomer(){}
	
	public VCustomer( TCustomer customer ){
		uid = customer.getUid();
		name = customer.getName();
		email = customer.getEmail();
	}
	
	public String getUid() {
		return uid;
	}
	public void setUid(String uid) {
		this.uid = uid;
	}

	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}	
	
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
}

BookStore\src\main\resources

MessageResources.properties

errors.header=<font color="red">
errors.footer=</font>

error.login.pwmismatch=\u30ed\u30b0\u30a4\u30f3\u540d\u3068\u30d1\u30b9\u30ef\u30fc\u30c9\u304c\u4e00\u81f4\u3057\u307e\u305b\u3093\u3002

error.createuser.pass2inmatch=\u78ba\u8a8d\u7528\u30d1\u30b9\u30ef\u30fc\u30c9\u304c\u4e00\u81f4\u3057\u307e\u305b\u3093\u3002
error.createuser.useralreadyexist=\u3053\u306e\u30a2\u30ab\u30a6\u30f3\u30c8\u306f\u3059\u3067\u306b\u5229\u7528\u3055\u308c\u3066\u3044\u307e\u3059\u3002
error.createuser.cannotcreate=\u30e6\u30fc\u30b6\u304c\u4f5c\u6210\u3067\u304d\u307e\u305b\u3093\u3067\u3057\u305f\u3002
error.createuser.hasempty=\u5165\u529b\u6b04\u306b\u7a7a\u6b04\u304c\u3042\u308a\u307e\u3057\u305f\u3002

error.addtocart.notselected=\u5546\u54c1\u304c\u9078\u629e\u3055\u308c\u3066\u3044\u307e\u305b\u3093\u3002

error.checkout.noselected=\u5546\u54c1\u304c\u9078\u629e\u3055\u308c\u3066\u3044\u307e\u305b\u3093\u3002

error.search.notfound=\u898b\u3064\u304b\u308a\u307e\u305b\u3093\u3067\u3057\u305f\u3002

(欲知后事如何,请见

代码来自日本的技术图书http://www.shuwasystem.co.jp/products/7980html/2197.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值