View Javadoc

1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one or more
3    *  contributor license agreements.  See the NOTICE file distributed with
4    *  this work for additional information regarding copyright ownership.
5    *  The ASF licenses this file to You under the Apache License, Version 2.0
6    *  (the "License"); you may not use this file except in compliance with
7    *  the License.  You may obtain a copy of the License at
8    * 
9    *       http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
16   */
17  
18  package org.apache.geronimo.samples.bank.ejb;
19  
20  import java.rmi.RemoteException;
21  import java.util.Collection;
22  import java.util.Iterator;
23  import java.util.List;
24  import java.util.ArrayList;
25  
26  import javax.persistence.PersistenceUnit;
27  import javax.ejb.EJBException;
28  import javax.ejb.Stateless;
29  import javax.naming.Context;
30  import javax.naming.InitialContext;
31  import javax.naming.NamingException;
32  import javax.persistence.EntityManager;
33  import javax.persistence.EntityManagerFactory;
34  
35  @Stateless
36  public class BankManagerFacadeBean implements BankManagerFacadeLocal {
37  	
38  	@PersistenceUnit(unitName="BankPU")
39  	protected EntityManagerFactory emf;
40  
41  	public BankManagerFacadeBean() {
42  
43  	}
44  
45  	public List<Account> getAccountInformation(String customerId) {
46  		EntityManager em = emf.createEntityManager();
47  		String query = "SELECT * FROM Account WHERE customerId='" + customerId + "'";
48  		List<Account> accountBeanList = (List<Account>)em.createNativeQuery( query, Account.class ).getResultList();
49  		em.close();
50  		return accountBeanList;
51  	}
52  	
53  	public List<ExchangeRate> getExchangeRates() {
54  		EntityManager em = emf.createEntityManager();
55  		String query = "SELECT * FROM ExchangeRate";
56  		List<ExchangeRate> rateList = (List<ExchangeRate>)em.createNativeQuery( query, ExchangeRate.class ).getResultList();
57  		em.close();
58  		return rateList;
59  	}
60  
61  	public Customer getCustomer(String customerCode){
62  		EntityManager em = emf.createEntityManager();
63  
64  		Customer customer = (Customer)em.find(Customer.class, customerCode);
65  		em.close();
66  		return customer;
67  	}
68  
69  	
70  	public void changeAccountBalance(String accountNo,Double balance) {
71  		EntityManager em = emf.createEntityManager();
72  		// get the Account
73  		Account account = (Account)em.find(Account.class, accountNo);
74  
75  		// update the Account object
76  		account.setBalance(balance);
77  		em.persist(em);
78  
79  		em.close();
80  	}
81  	
82  	public Double getAccountBalance(String accountNo){
83  		EntityManager em = emf.createEntityManager();
84  
85  		// get Account
86  		Account account = (Account)em.find(Account.class, accountNo);
87  
88  		em.close();
89  
90  		// return the balance as an object
91  		return new Double(account.getBalance());
92  	}
93  }