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

📄 netstoreejbimpl.java

📁 基于servlet/xml开发的网上书店 下载有需写web.xml配置
💻 JAVA
字号:
package netstore.service.ejb;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import netstore.businessobjects.*;
import netstore.service.HibernateUtil;
import netstore.framework.exceptions.AccountLockedException;
import netstore.framework.exceptions.DatastoreException;
import netstore.framework.exceptions.ExpiredPasswordException;
import netstore.framework.exceptions.InvalidLoginException;
import java.rmi.RemoteException;

import net.sf.hibernate.*;
/**
 * This is a simple Session Bean implementation of the Netstore service
 */
public class NetstoreEJBImpl implements SessionBean, INetstore {
  SessionContext ctx;
  /**
   * Return a list of items.
   */
  public List getItems(int beginIndex,int length) throws DatastoreException ,RemoteException{
    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,RemoteException{
    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,RemoteException{

    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,RemoteException{

    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,RemoteException{

    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,RemoteException{

    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);
    }
  }

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

  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;}
  }
  public void ejbCreate(  ) throws CreateException {
  }

  public void ejbRemove() {
  }

  public void setSessionContext( SessionContext assignedContext ) {
    ctx = assignedContext;
  }

  public void ejbActivate(  ) {
    // Nothing to do for a stateless bean
  }

  public void ejbPassivate(  ) {
    // Nothing to do for a stateless bean
  }


}

⌨️ 快捷键说明

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