中规模应用的开发(一)

阅读更多

BookStore

pom.xml


  4.0.0
  bookstore
  BookStore
  war
  1.0-SNAPSHOT
  BookStore Maven Webapp
  http://maven.apache.org
  
    
      junit
      junit
      3.8.1
      test
    
    
    	org.hibernate
    	hibernate-core
    	3.3.1.GA
    
    
    	org.hibernate
    	hibernate-annotations
    	3.4.0.GA
    
        
      postgresql
      postgresql
      8.3-603.jdbc3
    
    
      org.springframework
      spring-orm
      2.5.6
    
    
      org.springframework
      spring-aop
      2.5.6
    
    
      org.springframework
      spring-jdbc
      2.5.6
    
    
      org.springframework
      spring-webmvc-struts
      2.5.6
    
	
      org.apache.struts
      struts-core
      1.3.9
    
	
		org.apache.velocity
		velocity
		1.6
	
	
		org.apache.velocity
		velocity-tools
		1.3
	
	
		commons-dbcp
		commons-dbcp
		1.2.2
	
	
		commons-codec
		commons-codec
		1.3
	
	    
    	org.slf4j
    	slf4j-jdk14
    	1.5.2
    
    
    	javassist
    	javassist
    	3.8.0.GA
    
  
  
    BookStore
  

BookStore\init

message.txt

errors.header=
errors.footer=

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 cart = (List) httpSession.getAttribute( "Cart" );
		if( cart == null ){
			cart = new ArrayList();
		}
		
		AddToCartActionFormBean atcafb = (AddToCartActionFormBean)form;
		
		List productList = (List)httpSession.getAttribute( "ProductList" );
		
		String[] selecteItemsArray = atcafb.getSelecteditems();
		List selectedItems = null;

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

		List productListAll = bookLogic.getAllBookISBNs();
		List 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 selectedItems = (List)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 productListAll = bookLogic.getAllBookISBNs();
		List 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 cart = (List)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 cart = (List) httpSession.getAttribute( "Cart" );
		
		SearchActionFormBean safb = (SearchActionFormBean)form;
		
		List 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 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 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 retrieveBooksByKeyword(String inKeyword) {
		String escapedKeyword = Pattern.compile( "([%_])" )
											.matcher( inKeyword )
											.replaceAll( "\\\\$1" );
		String[] keywords = { "%" + escapedKeyword + "%",
							  "%" + escapedKeyword + "%",
							  "%" + escapedKeyword + "%" };
		
		List booksList = getHibernateTemplate().find( 
					"from TBook book where book.author like ?" +
					"or book.title like ? or book.publisher like ?" ,
					keywords );
		
		return( booksList );
	}


	public List retrieveBooksByISBNs( final List inISBNList ){

		HibernateTemplate ht = getHibernateTemplate();
		
		if( inISBNList == null ){
			return( ht.find( "from TBook book" ) );
		}else{
		
			return( ((List)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 inISBNList );
	public List retrieveBooksByKeyword( String inKeyword );
	public List retrieveBooksByISBNs( List 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	getAllBookISBNs();
	public List	retrieveBookISBNsByKeyword( String inKeyword );
	public List	createVBookList(List inProductList,
								  		List inSelectedList );
	public VCheckout	createVCheckout( List inSelectedList );
	public List createCart( List inProductList, 
									 List inSelectedList,
									 List 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 getAllBookISBNs() {
		List isbns = new ArrayList();
		
		for( TBook bookIter :  bookdao.retrieveBooksByISBNs( null ) ){
			isbns.add( bookIter.getIsbn() );
		}
		
		return( isbns );
	}
	

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


	public List createVBookList(List inProductList, List inSelectedList) {
		List vArrayList = new ArrayList();
		
		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 inSelectedList ){
		VCheckout vc = new VCheckout();
		vc.setTotal( bookdao.getPriceByISBNs( inSelectedList ) );

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

	
	public List createCart( List inProductList, 
							 List inSelectedList,
							 List 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 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 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 TOrderDetails = new HashSet(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 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 getTOrderDetails() {
		return this.TOrderDetails;
	}

	public void setTOrderDetails(Set 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 TOrders = new HashSet(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 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 getTOrders() {
		return this.TOrders;
	}

	public void setTOrders(Set 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 TOrderDetails = new HashSet(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 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 getTOrderDetails() {
		return this.TOrderDetails;
	}

	public void setTOrderDetails(Set 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=
errors.footer=

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

你可能感兴趣的:(Struts,Hibernate,Java,Apache,DAO)