FileDocCategorySizeDatePackage
HibRentABike.javaAPI DocExample6594Mon Apr 25 16:35:12 BST 2005com.springbook

HibRentABike

public class HibRentABike extends org.springframework.orm.hibernate.support.HibernateDaoSupport implements RentABike

Fields Summary
private String
name
private org.springframework.transaction.PlatformTransactionManager
transactionManager
private Accounts
accountsFacade
private org.springframework.jms.core.JmsTemplate
jmsTemplate
private List
testBikes
int
curBike
Constructors Summary
Methods Summary
public voidaddReservation(Reservation reservation, double amount)

      try {
         System.out.println("1");
         MonetaryTransaction tx = new MonetaryTransaction(reservation.getReservationId(), amount);
         System.out.println("2");
         getHibernateTemplate().saveOrUpdate(reservation);
         System.out.println("3");
         accountsFacade.addTx(tx);
         System.out.println("4");
         jmsTemplate.send("TEST.FOO", new MessageCreator() {
            public Message createMessage(javax.jms.Session session)
                  throws JMSException {
               System.out.println("5");
               return session.createTextMessage("New Reservation: " +
                     reservation.toString());
            }
         });
      } catch (Exception ex) {
         System.out.println("JBG: exception: " + ex.getClass().getName());
         throw new AddReservationException();
      }
   
public voiddeleteBike(Bike bike)

        getHibernateTemplate().delete(bike);
    
public voiddeleteCustomer(Customer customer)

        getHibernateTemplate().delete(customer);
    
public AccountsgetAccountsFacade()

      return accountsFacade;
   
public BikegetBike(java.lang.String serialNo)

        Bike b = null;
        List bikes = getHibernateTemplate().find("from Bike where serialNo = '" + serialNo + "'");
        if(bikes.size() > 0) {
            b = (Bike)bikes.get(0);
        }
        return b;
    
public BikegetBike(int bikeId)

        return (Bike)getHibernateTemplate().load(Bike.class, new Integer(bikeId));
    
public java.util.ListgetBikes()


        return getHibernateTemplate().find("from Bike");
    
public java.util.ListgetBikesOldWay()

        List bikes = null;
        Session s = null;
        try {
            s = getHibernateTemplate().getSessionFactory().openSession();
            bikes = s.find("from Bike");
        }catch (Exception ex) {
            //handle exception gracefully
        }finally {
            s.close();
        }
        return bikes;
    
public CustomergetCustomer(int custId)

       return (Customer)getHibernateTemplate().load(Customer.class, new Integer(custId));
    
public java.util.ListgetCustomers()

       return getHibernateTemplate().find("from Customer");
    
public org.springframework.jms.core.JmsTemplategetJmsTemplate()

      return jmsTemplate;
   
public java.lang.StringgetName()

        return name;
    
public BikegetNewBikeFromQueue()

      
      try {
         ActiveMQTextMessage m = (ActiveMQTextMessage)testBikes.get(curBike);
         curBike++;
         Bike b = new Bike();
         String s = m.getText();
         String[] vals = s.split(":");
         b.setManufacturer(vals[0]);
         b.setModel(vals[1]);
         return b;
      } catch (Exception ex) {
         return null;

      }
   
public ReservationgetReservation(int resId)

        return (Reservation)getHibernateTemplate().load(Reservation.class, new Integer(resId));
    
public java.util.ListgetReservations()

        return getHibernateTemplate().find("from Reservation");
    
public java.util.ListgetReservations(Customer customer)

        return getHibernateTemplate().find("from Reservation where custId = " + customer.getCustId());
    
public java.util.ListgetReservations(Bike bike)

        return getHibernateTemplate().find("from Reservation where bikeId = " + bike.getBikeId());
    
public java.util.ListgetReservations(java.util.Date date)

        return getHibernateTemplate().find("from Reservation where resdate = ?", date);
    
public java.lang.StringgetStoreName()

        return this.name;
    
public java.util.ListgetTestBikes()

      return testBikes;
   
public org.springframework.transaction.PlatformTransactionManagergetTransactionManager()

      return transactionManager;
   
public voidsaveBike(Bike bike)

        System.out.println("To save or update: " + bike);
        getHibernateTemplate().saveOrUpdate(bike);
    
public voidsaveCustomer(Customer customer)

        getHibernateTemplate().saveOrUpdate(customer);
    
public voidsetAccountsFacade(Accounts accountsFacade)

      this.accountsFacade = accountsFacade;
   
public voidsetJmsTemplate(org.springframework.jms.core.JmsTemplate jmsTemplate)

      this.jmsTemplate = jmsTemplate;
   
public voidsetName(java.lang.String name)

        this.name = name;
    
public voidsetStoreName(java.lang.String name)

        this.name = name;
    
public voidsetTestBikes(java.util.List testBikes)

      this.testBikes = testBikes;
   
public voidsetTransactionManager(org.springframework.transaction.PlatformTransactionManager transactionManager)

      this.transactionManager = transactionManager;
   
public voidtransferReservation(Reservation oldRes, Reservation newRes)

      TransactionTemplate template = new TransactionTemplate(this.transactionManager);
      template.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
      try {
         template.execute(new TransactionCallbackWithoutResult() {
            protected void doInTransactionWithoutResult(TransactionStatus transactionStatus) {
               getHibernateTemplate().save(newRes);
               getHibernateTemplate().delete(oldRes);
            }
         });
      } catch (Exception ex) {
         throw new ReservationTransferException();
      }