001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 018 package org.apache.geronimo.samples.bank.ejb; 019 020 import java.rmi.RemoteException; 021 import java.util.Collection; 022 import java.util.Iterator; 023 import java.util.List; 024 import java.util.ArrayList; 025 026 import javax.persistence.PersistenceUnit; 027 import javax.ejb.EJBException; 028 import javax.ejb.Stateless; 029 import javax.naming.Context; 030 import javax.naming.InitialContext; 031 import javax.naming.NamingException; 032 import javax.persistence.EntityManager; 033 import javax.persistence.EntityManagerFactory; 034 035 @Stateless 036 public class BankManagerFacadeBean implements BankManagerFacadeLocal { 037 038 @PersistenceUnit(unitName="BankPU") 039 protected EntityManagerFactory emf; 040 041 public BankManagerFacadeBean() { 042 043 } 044 045 public List<Account> getAccountInformation(String customerId) { 046 EntityManager em = emf.createEntityManager(); 047 String query = "SELECT * FROM Account WHERE customerId='" + customerId + "'"; 048 List<Account> accountBeanList = (List<Account>)em.createNativeQuery( query, Account.class ).getResultList(); 049 em.close(); 050 return accountBeanList; 051 } 052 053 public List<ExchangeRate> getExchangeRates() { 054 EntityManager em = emf.createEntityManager(); 055 String query = "SELECT * FROM ExchangeRate"; 056 List<ExchangeRate> rateList = (List<ExchangeRate>)em.createNativeQuery( query, ExchangeRate.class ).getResultList(); 057 em.close(); 058 return rateList; 059 } 060 061 public Customer getCustomer(String customerCode){ 062 EntityManager em = emf.createEntityManager(); 063 064 Customer customer = (Customer)em.find(Customer.class, customerCode); 065 em.close(); 066 return customer; 067 } 068 069 070 public void changeAccountBalance(String accountNo,Double balance) { 071 EntityManager em = emf.createEntityManager(); 072 // get the Account 073 Account account = (Account)em.find(Account.class, accountNo); 074 075 // update the Account object 076 account.setBalance(balance); 077 em.persist(em); 078 079 em.close(); 080 } 081 082 public Double getAccountBalance(String accountNo){ 083 EntityManager em = emf.createEntityManager(); 084 085 // get Account 086 Account account = (Account)em.find(Account.class, accountNo); 087 088 em.close(); 089 090 // return the balance as an object 091 return new Double(account.getBalance()); 092 } 093 }