|
EJB 2 - Les Entreprise Java Bean (JavaBeans)
6.4.Développement des Session
Bean
Lors de la conception, nous avons défini deux services,
représenté par deux session bean. Pour des raisons de
simplification, nous allons présenter qu’un seul bean
combinant l’ensemble des fonctionnalités.
Voici les fonctionnalités que ce service propose :
-
Récupérer l’ensemble des produits
-
Récupérer l’ensemble des produits à
partir d’une famille
-
Récupérer l’ensemble des produits ayant un
fournisseur d’un pays précis
-
Récupérer l’ensemble des familles de produit
-
Ajouter un produit
-
Supprimer un produit
-
Ajouter une famille de produit
-
Supprimer une famille de produit
-
Ajouter un fournisseur
-
Supprimer un fournisseur
-
Ajouter un pays
-
Supprimer un pays
-
Ajouter une ville
-
Supprimer une ville
Voici le code de ce bean :
package
com.society.stockmanager.ejb;
import
java.rmi.RemoteException;
import
java.util.Collection;
import
java.util.HashSet;
import
java.util.Iterator;
import
javax.ejb.CreateException;
import
javax.ejb.EJBException;
import
javax.ejb.FinderException;
import
javax.ejb.SessionBean;
import
javax.ejb.SessionContext;
import
javax.naming.Context;
import
javax.naming.InitialContext;
import
javax.rmi.PortableRemoteObject;
import
com.society.stockmanager.exceptions.NotFindException;
import
com.society.stockmanager.interfaces.CityLocalHome;
import
com.society.stockmanager.interfaces.CountryLocal;
import
com.society.stockmanager.interfaces.CountryLocalHome;
import
com.society.stockmanager.interfaces.ProductFamilyLocal;
import
com.society.stockmanager.interfaces.ProductFamilyLocalHome;
import
com.society.stockmanager.interfaces.ProductLocal;
import
com.society.stockmanager.interfaces.ProductLocalHome;
import
com.society.stockmanager.interfaces.ProviderLocal;
import
com.society.stockmanager.interfaces.ProviderLocalHome;
import
com.society.stockmanager.vo.ProductData;
import
com.society.stockmanager.vo.ProductFamilyData;
import
com.society.stockmanager.vo.ProviderData;
/**
*
@ejb.bean name="StorageService"
*
display-name="Name for StorageService"
*
description="Description for StorageService"
*
jndi-name="ejb/StorageService"
*
type="Stateless"
*
view-type="remote"
*
*
*
@ejb.ejb-ref ejb-name = "Product" view-type = "local"
*
*
@ejb.ejb-ref ejb-name = "ProductFamily" view-type = "local"
*
*
@ejb.ejb-ref ejb-name = "Provider" view-type = "local"
*
*
@ejb.ejb-ref ejb-name = "City" view-type = "local"
*
*
@ejb.ejb-ref ejb-name = "Country" view-type = "local"
*
*/
public
class StorageServiceBean implements SessionBean {
public
StorageServiceBean() {
super();
}
public
void setSessionContext(SessionContext ctx)
throws
EJBException,
RemoteException
{
}
public
void ejbRemove() throws EJBException, RemoteException {
}
public
void ejbActivate() throws EJBException, RemoteException {
}
public
void ejbPassivate() throws EJBException, RemoteException {
}
/**
* Retourne l'interface home associÈe ‡ un nom JNDI. Il
est ‡ noter qu'on
* obtient toujours l'interface locale aprËs un narrow.
*
* @param jndiNaming
* @param interfaceClassNarrowed
* @return
* @throws Exception
*/
private
Object getHomeInterface(String jndiNaming,
Class
interfaceClassNarrowed) throws Exception {
Context
ctx = new InitialContext();
Object
ref = ctx.lookup(jndiNaming);
Object
interfaceNarrowed = PortableRemoteObject.narrow(ref,
interfaceClassNarrowed);
return
interfaceNarrowed;
}
/**
* Get an objet ProductLocalHome by lookup.
*
* @return interface ProductLocalHome
* @throws Exception
*/
private
ProductLocalHome getProductHome() throws Exception {
return
(ProductLocalHome) getHomeInterface("java:comp/env/"
+
ProductLocalHome.JNDI_NAME, ProductLocalHome.class);
}
/**
* Get an objet ProductFamilyLocalHome by lookup
*
* @return interface ProductFamilyLocalHome
* @throws Exception
*/
private
ProductFamilyLocalHome getProductFamilyHome() throws Exception {
return
(ProductFamilyLocalHome) getHomeInterface("java:comp/env/"
+
ProductFamilyLocalHome.JNDI_NAME, ProductFamilyLocalHome.class);
}
/**
* Get an objet ProviderLocalHome by lookup
*
* @return interface ProviderLocalHome
* @throws Exception
*/
private
ProviderLocalHome getProviderHome() throws Exception {
return
(ProviderLocalHome) getHomeInterface("java:comp/env/"
+
ProviderLocalHome.JNDI_NAME, ProviderLocalHome.class);
}
/**
* Get an objet CityLocalHome by lookup
*
* @return interface CityLocalHome
* @throws Exception
*/
private
CityLocalHome getCityHome() throws Exception {
return
(CityLocalHome) getHomeInterface("java:comp/env/"
+
CityLocalHome.JNDI_NAME, CityLocalHome.class);
}
/**
* Get an objet CityLocalHome by lookup
*
* @return interface CityLocalHome
* @throws Exception
*/
private
CountryLocalHome getCountryHome() throws Exception {
return
(CountryLocalHome) getHomeInterface("java:comp/env/"
+
CountryLocalHome.JNDI_NAME, CountryLocalHome.class);
}
/**
* Default create method
*
* @throws CreateException
* @ejb.create-method
*/
public
void ejbCreate() throws CreateException {
}
/**
* Business method
* @ejb.interface-method view-type = "remote"
*/
public
String sayHello() {
return
"say Hello ";
}
/**
* Business method
* @ejb.interface-method view-type = "remote"
*/
public
Integer[] getAllProductIds() {
Integer[]
result = null;
try
{
System.out.println("Debut
");
Collection
allProducts = getProductHome().findAll();
System.out.println("Size
: " + allProducts.size());
result
= new Integer[allProducts.size()];
Iterator
it = allProducts.iterator();
int
index = 0;
while(it.hasNext())
{
ProductLocal
product = (ProductLocal)it.next();
result[index]
= product.getId();
index++;
}
}
catch(Exception
e) {
}
return
result;
}
/**
* Business method
* @ejb.interface-method view-type = "remote"
*/
public
Collection getAllProductData() throws NotFindException {
Collection
result = new HashSet();
try
{
Collection
allProducts = getProductHome().findAll();
Iterator
it = allProducts.iterator();
while(it.hasNext())
{
ProductLocal
product = (ProductLocal)it.next();
result.add(product.getData());
}
}
catch(Exception
e) {
throw
new NotFindException(e);
}
return
result;
}
/**
* Business method
* @ejb.interface-method view-type = "remote"
*/
public
Collection getAllProductFamily() {
Collection
returnAll = new HashSet ();
try
{
System.out.println("Debut
");
Collection
allProductFamily = getProductFamilyHome().findAll();
System.out.println("Size
: " + allProductFamily.size());
Iterator
it = allProductFamily.iterator();
int
index = 0;
while(it.hasNext())
{
ProductFamilyLocal
productFamily = (ProductFamilyLocal)it.next();
returnAll.add(productFamily.getData());
}
}
catch(Exception
e) {
System.out.println("Erreur
: " + e);
}
return
returnAll;
}
/**
* Business method
* @ejb.interface-method view-type = "remote"
*/
public
void addProduct(ProductData datas, String codeFamily) {
try
{
ProductLocal
product = getProductHome().create(datas,
getProductFamilyHome().findByPrimaryKey(codeFamily));
System.out.println("
Add product with datas : " + datas);
}
catch(CreateException
e) {
System.out.println("
Error during Add product with data : " + datas + " / "
+ e);
}
catch(FinderException
e) {
System.out.println("
Error during finding productFamily with id : " + codeFamily + "
/ " + e);
}
catch(Exception
e) {
System.out.println(e);
}
}
/**
* Business method
* @ejb.interface-method view-type = "remote"
*/
public
void removeProduct(Integer productId) {
try
{
ProductLocal
product = getProductHome().findByPrimaryKey(productId);
product.remove();
System.out.println("
Remove product with id : " + productId);
return;
}
catch(FinderException
e) {
System.out.println("
Error during finding product with id : " + productId + " /
" + e);
}
catch(Exception
e) {
System.out.println(e);
}
}
/**
* Business method
* @ejb.interface-method view-type = "remote"
*/
public
void addProductFamily(String code, String designation) {
try
{
getProductFamilyHome().create(code,
designation);
System.out.println("
Add productFamily with id : " + code);
}
catch(Exception
e) {
System.out.println("
Error during Add product family with id : " + code + " / "
+ e);
}
}
/**
* Business method
* @ejb.interface-method view-type = "remote"
*/
public
void removeProductFamily(String code) {
try
{
ProductFamilyLocal
prodFamily = getProductFamilyHome().findByPrimaryKey(code);
System.out.println("
Remove productFamily with id : " + code);
}
catch(Exception
e) {
System.out.println("
Error during remove product family with id : " + code + " /
" + e);
}
}
/**
* Business method
* @ejb.interface-method view-type = "remote"
*/
public
void addProvider(ProviderData datas, Integer cityId, Collection
productIds) {
try
{
//
Prepare products collection
Collection
products = new HashSet();
Iterator
itProducts = productIds.iterator();
while(itProducts.hasNext())
{
Integer
productId = (Integer) itProducts.next();
products.add(getProductHome().findByPrimaryKey(productId));
}
System.out.println("List
of products : " + products);
getProviderHome().create(datas,
getCityHome().findByPrimaryKey(cityId), products);
System.out.println("
Add provider with datas : " + datas);
}
catch(FinderException
e) {
System.out.println("Disable
to find product error : " + e);
}
catch(Exception
e) {
System.out.println("
Error during Add provider with datas : " + datas + " / "
+ e);
}
}
/**
* Business method
* @ejb.interface-method view-type = "remote"
*/
public
void removeProvider(Integer providerId) {
try
{
ProviderLocal
provider = getProviderHome().findByPrimaryKey(providerId);
System.out.println("
Remove provider with id : " + providerId);
}
catch(Exception
e) {
System.out.println("
Error during remove provider with id : " + providerId + " /
" + e);
}
}
/**
* Business method
* @ejb.interface-method view-type = "remote"
*/
public
void addCountry(Integer countryId, String name) {
try
{
getCountryHome().create(countryId,
name);
System.out.println("
Add country with id : " + countryId);
}
catch(Exception
e) {
System.out.println("
Error during Add country with id : " + countryId + " / "
+ e);
}
}
/**
* Business method
* @ejb.interface-method view-type = "remote"
*/
public
void removeCountry(Integer countryId) {
try
{
CountryLocal
country = getCountryHome().findByPrimaryKey(countryId);
System.out.println("
Remove country with id : " + countryId);
}
catch(Exception
e) {
System.out.println("
Error during remove country with id : " + countryId + " / "
+ e);
}
}
/**
* Business method
* @ejb.interface-method view-type = "remote"
*/
public
void addCity(Integer cityId, String name, Integer countryId) {
try
{
getCityHome().create(cityId,
name, getCountryHome().findByPrimaryKey(countryId));
System.out.println("
Add city with id : " + countryId);
}
catch(FinderException
e) {
System.out.println("Disable
to find country error : " + e);
}
catch(Exception
e) {
System.out.println("
Error during Add city with id : " + countryId + " / "
+ e);
}
}
/**
* Business method
* @ejb.interface-method view-type = "remote"
*/
public
void removeCity(Integer cityId) {
try
{
getCityHome().findByPrimaryKey(cityId);
System.out.println("
Remove city with id : " + cityId);
}
catch(FinderException
e) {
System.out.println("Disable
to find city error : " + e);
}
catch(Exception
e) {
System.out.println("
Error during remove city with id : " + cityId + " / "
+ e);
}
}
/**
* Business method
* @ejb.interface-method view-type = "remote"
*/
public
ProductFamilyData findProductFamily(String code) {
ProductFamilyData
product = null;
try
{
product
= getProductFamilyHome().findByPrimaryKey(code).getData();
System.out.println("
find productFamily with id : " + code);
}
catch(Exception
e) {
System.out.println("
Error during find product family with id : " + code + " / "
+ e);
}
return
product;
}
/**
* Business method
* @ejb.interface-method view-type = "remote"
*/
public
Collection findProductByCountryId(Integer countryId) {
Collection
result = new HashSet();
try
{
Collection
foundProducts = getProductHome().findAllByCountryId(countryId);
Iterator
it = foundProducts.iterator();
while(it.hasNext())
{
ProductLocal
product = (ProductLocal)it.next();
result.add(product.getPrimaryKey());
}
}
catch(Exception
e) {
System.out.println("
Error during find product with countryid : " + countryId + "
/ " + e);
}
return
result;
}
}
-
ejb.ejb-ref : déclare une référence vers
un autre EJB.
-
ejb-name : nom de l’EJB référence
-
view-type : type de vue de l’EJB
|
|
 |