...
8. Copy the following contents into Currency.java
. The Currency.java
uses various JPA annotations to mark the class as an entity and designates database table to persist the attribute values.
Code Block |
---|
| JAVA |
---|
| JAVA |
---|
title | Currency.java |
---|
borderStyle | solid | JAVA |
---|
|
package sample.jpa.currency;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="CurrencyRateTable")
public class Currency implements Serializable {
@Id
public String currencyName;
public String countryName;
public double rateAgstUSD;
public Currency(){
}
public String getCurrencyName() {
return currencyName;
}
public void setCurrencyName(String currencyName) {
this.currencyName = currencyName;
}
public String getCountryName() {
return countryName;
}
public void setCountryName(String countryName) {
this.countryName = countryName;
}
public double getRateAgstUSD() {
return rateAgstUSD;
}
public void setRateAgstUSD(double rateAgstUSD) {
this.rateAgstUSD = rateAgstUSD;
}
}
|
9. Similarly, create the interface CurrencyInterface.java
and copy the following contents. This is the
stateless session bean interface.
Code Block |
---|
| JAVA |
---|
| JAVA |
---|
title | CurrencyInterface.java |
---|
borderStyle | solid | JAVA |
---|
|
package sample.jpa.currency;
import java.util.List;
public interface CurrencyInterface {
public Currency createCurrency(String currencyName,
String countryName,
double rateAgstUSD);
public List listCurrencies();
public Currency updateCurrency(String currencyName,
double rateAgstUSD);
public void deleteCurrency(String currencyName);
public Currency retrieveCurrency(String currencyName);
}
|
10. Similarly, create CurrencyBean.java
and copy the following contents. This class is the bean class that implements CurrencyInterface.java
interface.
Code Block |
---|
| JAVA |
---|
| JAVA |
---|
title | CurrencyBean.java |
---|
borderStyle | solidJAVA |
---|
|
package sample.jpa.currency;
import java.util.List;
import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
@Stateless
@Local
public class CurrencyBean implements CurrencyInterface{
@PersistenceContext(unitName="CurrencyRateUnit")
private EntityManager em;
public Currency createCurrency(String currencyName,
String countryName,
double rateAgstUSD){
Currency crt1 = em.find(Currency.class, currencyName);
if(crt1 != null)
throw new IllegalArgumentException
("Currency already exists: Currency Name ("+currencyName+")");
Currency crt = new Currency();
crt.setCountryName(countryName);
crt.setCurrencyName(currencyName);
crt.setRateAgstUSD(rateAgstUSD);
System.out.println("Persisting Currency Rate entity:
(Currency = "+currencyName+")");
em.persist(crt);
System.out.println("Persisted successfully Currency entity:
(Currency = "+currencyName+")");
return crt;
}
public List listCurrencies(){
if(em == null) System.out.println("em is null!!!");
Query q = em.createQuery("SELECT crt FROM Currency crt");
List currList = q.getResultList();
return currList;
}
public Currency updateCurrency(String currencyName,
double rateAgstUSD){
if(em == null) System.out.println("em is null!!!");
Currency crt1 = em.find(Currency.class, currencyName);
if(crt1 == null) throw new IllegalArgumentException
("Currency not found: Currency Name ("+currencyName+")");
crt1.setRateAgstUSD(rateAgstUSD);
return crt1;
}
public void deleteCurrency(String currencyName){
if(em == null) System.out.println("em is null!!!");
Currency crt1 = em.find(Currency.class, currencyName);
if(crt1 == null) throw new
IllegalArgumentException
("Currency not found: Currency Name ("+currencyName+")");
em.remove(crt1);
}
public Currency retrieveCurrency(String currencyName){
Currency crt1 = em.find(Currency.class, currencyName);
return crt1;
}
}
|
...
Copy the below contents into CurrencyListJSFBean.java
Code Block |
---|
| JAVA |
---|
| JAVA |
---|
title | CurrencyListJSFBean.java |
---|
borderStyle | solidJAVA |
---|
|
package sample.jsf;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.ejb.EJB;
import javax.faces.component.UIParameter;
import javax.faces.event.ActionEvent;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.LinkRef;
import sample.jpa.currency.Currency;
import sample.jpa.currency.CurrencyInterface;
public class CurrencyListJSFBean {
private CurrencyInterface ci;
private Collection currencies;
public CurrencyListJSFBean() throws Exception{
Context ctx = new InitialContext();
System.out.println("Instantiating bean...");
Object obj = ctx.lookup("java:comp/env/ejb/CurrencyInterface");
System.out.println((obj instanceof LinkRef));
ci = (CurrencyInterface)obj;
System.out.println("Got the bean...");
init();
}
public Collection getCurrencies() {
return currencies;
}
public void setCurrencies(Collection currencies) {
this.currencies = currencies;
}
public void loadCurrencies()throws Exception{
init();
}
private void init()throws Exception{
List list = ci.listCurrencies();
Iterator it = list.iterator();
this.currencies = new ArrayList();
while (it.hasNext()){
Currency currency = (Currency)it.next();
CurrencyJSFBean cjb = new CurrencyJSFBean();
cjb.setCountryName(currency.getCountryName());
cjb.setCurrencyName(currency.getCurrencyName());
cjb.setRateAgstUSD(currency.getRateAgstUSD());
System.out.println(currency.getCountryName()+
" : " +currency.getCurrencyName()+ " : "+
currency.getRateAgstUSD());;
currencies.add(cjb);
}
}
public void deleteCurrency (ActionEvent event){
UIParameter component = (UIParameter) event.getComponent().findComponent("deleteCurrency");
String curencyNameDel = (String) component.getValue();
System.out.println("Currency Name to be deleted = "+curencyNameDel);
ci.deleteCurrency(curencyNameDel);
Iterator it = this.getCurrencies().iterator();
while (it.hasNext()){
CurrencyJSFBean cjb = (CurrencyJSFBean)it.next();
if(cjb.getCurrencyName().equals(curencyNameDel)){
it.remove();
}
}
}
}
|
Similarly, create CurrencyJSFBean.java
in the same package and copy the following contents into it.
Code Block |
---|
| JAVA |
---|
| JAVA |
---|
title | CurrencyJSFBean.java |
---|
borderStyle | solidJAVA |
---|
|
package sample.jsf;
import java.util.Iterator;
import java.util.Map;
import javax.faces.component.UIParameter;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.LinkRef;
import sample.jpa.currency.Currency;
import sample.jpa.currency.CurrencyInterface;
public class CurrencyJSFBean {
private CurrencyInterface ci;
private String currencyName;
private String countryName;
private double rateAgstUSD;
public CurrencyJSFBean()throws Exception{
Context ctx = new InitialContext();
System.out.println("Instantiating bean...");
Object obj = ctx.lookup("java:comp/env/ejb/CurrencyInterface");
System.out.println((obj instanceof LinkRef));
ci = (CurrencyInterface)obj;
System.out.println("Got the bean...");
}
public String getCurrencyName() {
return currencyName;
}
public void setCurrencyName(String currencyName) {
System.out.println("Set currencyName="+currencyName);
this.currencyName = currencyName;
}
public String getCountryName() {
return countryName;
}
public void setCountryName(String countryName) {
System.out.println("Set countryName="+countryName);
this.countryName = countryName;
}
public double getRateAgstUSD() {
return rateAgstUSD;
}
public void setRateAgstUSD(double rateAgstUSD) {
System.out.println("Set rateAgstUSD="+rateAgstUSD);
this.rateAgstUSD = rateAgstUSD;
}
public void selectCurrency(ActionEvent event){
UIParameter component = (UIParameter)
event.getComponent().findComponent("editCurrency");
String curencyName = (String) component.getValue();
Currency c = ci.retrieveCurrency(curencyName);
this.setCountryName(c.getCountryName());
this.setCurrencyName(c.getCurrencyName());
this.setRateAgstUSD(c.getRateAgstUSD());
}
public void saveCurrency(ActionEvent event){
ci.updateCurrency(this.getCurrencyName(), this.getRateAgstUSD());
}
public void initCurrency(ActionEvent event){
this.setCountryName("");
this.setCurrencyName("");
this.setRateAgstUSD(0.0);
}
public void insertCurrency(ActionEvent event){
ci.createCurrency(this.currencyName, countryName, rateAgstUSD);
}
}
|
...