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

📄 netstoreserviceimpl.java

📁 基于servlet/xml开发的网上书店 下载有需写web.xml配置
💻 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
import net.sf.hibernate.*;

public class NetstoreServiceImpl implements INetstoreService{
  ServletContext servletContext = null;

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

  public void setServletContext( ServletContext ctx ){
    this.servletContext = ctx;
  }

  public ServletContext getServletContext(){
    return servletContext;
  }

  /**
   * Return a list of items.
   */
  public List getItems(int beginIndex,int length) throws DatastoreException {
    Session session = HibernateUtil.getSession();
    Transaction tx = null;
    try {
      tx = session.beginTransaction();
      Query query = session.createQuery("from Item i order by i.basePrice asc");
      query.setFirstResult(beginIndex);
      query.setMaxResults(length);
      List result = query.list();
      tx.commit();

      return toGBEncoding(result);
    }catch (Exception ex) {
      HibernateUtil.rollbackTransaction(tx);
      throw DatastoreException.datastoreError(ex);
    } finally {
      // No matter what, close the session
      HibernateUtil.closeSession(session);
    }
  }

  public Item getItemById( Long id)throws DatastoreException{
    Session session = HibernateUtil.getSession();
    Transaction tx = null;
    try {
      tx = session.beginTransaction();
      Item item =(Item)session.get(Item.class,id);;
      tx.commit();

      return toGBEncoding(item);
    }catch (Exception ex) {
      HibernateUtil.rollbackTransaction(tx);
      throw DatastoreException.datastoreError(ex);
    } finally {
      // No matter what, close the session
      HibernateUtil.closeSession(session);
    }
  }

  /**
   * 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 saveOrUpdateCustomer(Customer customer) throws
    DatastoreException{

    Session session = HibernateUtil.getSession();
    Transaction tx = null;
    try {
      tx = session.beginTransaction();
      session.saveOrUpdate(customer);
      tx.commit();

    }catch (Exception ex) {
      HibernateUtil.rollbackTransaction(tx);
      throw DatastoreException.datastoreError(ex);
    } finally {
      // No matter what, close the session
      HibernateUtil.closeSession(session);
    }
  }

  public void saveOrder(Order order) throws DatastoreException{

    Session session = HibernateUtil.getSession();
    Transaction tx = null;
    try {
      tx = session.beginTransaction();
      session.save(order);
      tx.commit();

   }catch (Exception ex) {
      HibernateUtil.rollbackTransaction(tx);
      throw DatastoreException.datastoreError(ex);
    } finally {
      // No matter what, close the session
      HibernateUtil.closeSession(session);
    }
  }

  public Customer getCustomerById(Long id) throws DatastoreException{

    Session session = HibernateUtil.getSession();
    Transaction tx = null;
    try {
      tx = session.beginTransaction();
      Query query = session.createQuery("from Customer c left outer join fetch c.orders where c.id=:id");
      query.setLong("id",id.longValue());
      Customer customer =(Customer) query
                          .uniqueResult();

      tx.commit();

      return customer;

    }catch (Exception ex) {
      HibernateUtil.rollbackTransaction(tx);
      throw DatastoreException.datastoreError(ex);
    } finally {
      // No matter what, close the session
      HibernateUtil.closeSession(session);
    }
  }

  /**
   * Log the user out of the system.
   */
  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());
     
    
  }

  private List toGBEncoding(List items){
    Iterator it=items.iterator();
    while(it.hasNext()){
     toGBEncoding((Item)it.next());
    }
    return items;
  }
  private Item toGBEncoding(Item item){
    item.setName(getGBString(item.getName()));
    item.setDescription(getGBString(item.getDescription()));
    item.setFeature(getGBString(item.getFeature()));

    return item;
  }

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

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

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

⌨️ 快捷键说明

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