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

📄 netstoreserviceimpl.java

📁 网上商店 新增功能: 1
💻 JAVA
字号:
package netstore.service;

import java.sql.Timestamp;
import java.util.List;
import java.util.Iterator;
import java.util.ArrayList;
import netstore.framework.security.IAuthentication;
import netstore.businessobjects.*;

// Import the exceptions used
import netstore.framework.exceptions.DatastoreException;
import netstore.framework.exceptions.InvalidLoginException;
import netstore.framework.exceptions.ExpiredPasswordException;
import netstore.framework.exceptions.AccountLockedException;
import javax.servlet.ServletContext;

// Import the implementation specific packages
//hibernate 3.0
import org.hibernate.*;

import org.apache.log4j.*;

/**
 * 业务代理实现
 * @author huangyongfeng
 *
 */
public class NetstoreServiceImpl implements INetstoreService{
  private static Logger logger = Logger.getLogger(NetstoreServiceImpl.class.getName());	
  
  ServletContext servletContext = null;
  public int rowCount;

  /**
   * Create the service *
   */
  public NetstoreServiceImpl() throws DatastoreException {
    super();
  }

  /**
   * Servlet上下文
   */
  public void setServletContext( ServletContext ctx ){
    this.servletContext = ctx;
  }

  public ServletContext getServletContext(){
    return servletContext;
  }

  /**
   * Authenticate the user's credentials and either return a User for the
   * user or throw one of the security exceptions.
   * 实现认证接口中认证方法
   */
  public Customer authenticate(String email, String password) throws
    InvalidLoginException,DatastoreException{

    Session session = HibernateUtil.getSession();
    Transaction tx = null;
    try {
      tx = session.beginTransaction();
      Query query = session.createQuery("from Customer c where c.email=:email and c.password=:password");
      query.setString("email",email);
      query.setString("password",password);
      List result = query.list();
      tx.commit();

      //抛出登录无效异常
      if(result.isEmpty())
        throw new InvalidLoginException();

      //返回客户对象
      return (Customer)result.iterator().next();

   }catch (HibernateException ex) {
      HibernateUtil.rollbackTransaction(tx);
      //抛出数据存取异常
      throw DatastoreException.datastoreError(ex);
    } finally {
      // No matter what, close the session
      HibernateUtil.closeSession(session);
    }
  }

  /**
   * 注销
   */
  public void logout(String email){
    // Do nothing with right now, but might want to log it for auditing reasons
	//这里什么也没有做,实际上可以记录下客户的注销信息
  }

  public void destroy(){
     HibernateUtil.close();
  }

  /*
   * test NetstoreServiceImpl
   * 测试一下
   */
  public static void main(String args[])throws Exception{
    NetstoreServiceImpl service=new  NetstoreServiceImpl();

     Customer customer=service.authenticate("tom@yahoo.com","1234");
     System.out.println(customer.getName());
    
  }

  /**
   * 编码产品集合
   * @param items
   * @return
   */
  protected List toGBEncoding(List items){
    Iterator it=items.iterator();
    while(it.hasNext()){
     toGBEncoding((Item)it.next());
    }
    return items;
  }
  
  /**
   * 编码单个产品
   * @param item
   * @return
   */
  protected Item toGBEncoding(Item item){
    item.setName(getGBString(item.getName()));
    item.setDescription(getGBString(item.getDescription()));
    item.setFeature(getGBString(item.getFeature()));

    return item;
  }

  protected Item toISOEncoding(Item item){
    item.setName(getISOString(item.getName()));
    item.setDescription(getISOString(item.getDescription()));
    item.setFeature(getISOString(item.getFeature()));
    return item;
  }

  protected  String getGBString(String s){
      /*
     try{
       return new String(s.getBytes("ISO-8859-1"),"GB2312");
     }catch(Exception e){return null;}
     */
      return s;
  }

  protected  String getISOString(String s){
       /*
     try{
       return new String(s.getBytes("GB2312"),"ISO-8859-1");
     }catch(Exception e){return null;}
     */
       return s;
  }

	public int getRowCount() {
		return rowCount;
	}
	
	public void setRowCount(int rowCount) {
		this.rowCount = rowCount;
	}
}

⌨️ 快捷键说明

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