📄 booksession.java
字号:
package de.laliluna.tutorial.library.session.ejb;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.FinderException;
import javax.ejb.RemoveException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import de.laliluna.tutorial.library.entity.interfaces.BookLocal;
import de.laliluna.tutorial.library.entity.interfaces.BookLocalHome;
import de.laliluna.tutorial.library.entity.interfaces.BookValue;
import de.laliluna.tutorial.library.entity.interfaces.UserLocal;
import de.laliluna.tutorial.library.entity.interfaces.UserLocalHome;
import de.laliluna.tutorial.library.view.BookView;
/**
* XDoclet-based session bean. The class must be declared
* public according to the EJB specification.
*
* To generate the EJB related files to this EJB:
* - Add Standard EJB module to XDoclet project properties
* - Customize XDoclet configuration for your appserver
* - Run XDoclet
*
* Below are the xdoclet-related tags needed for this EJB.
*
* @ejb.bean name="BookSession"
* display-name="Name for BookSession"
* description="Description for BookSession"
* jndi-name="ejb/BookSessionHome"
* type="Stateless"
* view-type="remote"
*/
public class BookSession implements SessionBean {
/**
* This method return all books as a collection
*
* Diese Methode liest alle B點her aus und gibt
* diese als Collection zur點k
*
* @ejb.interface-method view-type = "both"
*/
public BookView[] getAllBooks() throws EJBException {
try {
InitialContext context = new InitialContext();
//Get the home interface with JNDI from the application server
//Home Interface 黚er JNDI beim Appliacation Server holen
BookLocalHome bookLocalHome = (BookLocalHome)context.lookup(BookLocalHome.JNDI_NAME);
//get all books with the local home interface
//bestimme alle B點her mit dem Local Home Interface
Collection collection = bookLocalHome.findAll();
//define an ArrayList
//definieren einer ArrayList
ArrayList arrayList = new ArrayList();
//loop over the collection
//schleife 黚er die collection
for (Iterator iter = collection.iterator(); iter.hasNext();) {
BookLocal element = (BookLocal) iter.next();
//define a new BookView object
//definiere ein neues BookView object
BookView bookView = new BookView();
//set the BookValue in bookView
//setze das BookValue in bookView
bookView.setBookValue(element.getBookValue());
//set the UserValue in bookView
//setze das UserValue in bookView
if(element.getUser() != null)
bookView.getUserView().setUserValue(element.getUser().getUserValue());
//add the bookView object to the ArrayList
//f黦e das bookView zur ArrayList hinzu
arrayList.add(bookView);
}
//return the array of BookView
//gibt das Array von BookView zur點k
return (BookView[])arrayList.toArray(new BookView[0]);
} catch (NamingException e) {
throw new EJBException(e.getMessage());
} catch (FinderException e) {
throw new EJBException(e.getMessage());
}
}
/**
* This methode get a book by primary key
*
* Diese Methode liest ein Buch anhand seines Prim鋜schl黶sels aus.
*
* @ejb.interface-method view-type = "both"
*/
public BookView getBookByPrimaryKey(Integer primaryKey) throws EJBException {
try {
InitialContext context = new InitialContext();
BookView bookView = new BookView();
//Get the home interface with JNDI from the application server
//Home Interface 黚er JNDI beim Appliacation Server holen
BookLocalHome bookLocalHome = (BookLocalHome)context.lookup(BookLocalHome.JNDI_NAME);
//search for a book with the primary key
//suche ein Buch anhand des Prim鋜schl黶sels
BookLocal bookLocal = bookLocalHome.findByPrimaryKey(primaryKey);
//set the BookValue object in BookView
//setze das BookValue Objekt in BookView
bookView.setBookValue(bookLocal.getBookValue());
//set the UserValue in BookView
//setze das UserValue in BookView
if(bookLocal.getUser() != null)
bookView.getUserView().setUserValue(bookLocal.getUser().getUserValue());
//return the book value object
//das Book Value Objekt zur點kgeben
return bookView;
} catch (NamingException e) {
throw new EJBException(e.getMessage());
} catch (FinderException e) {
throw new EJBException(e.getMessage());
}
}
/**
* This methode return a book from a user by primary key
*
* Diese Methode gibt ein ausgeliehenes Buch eines
* Benutzers zur點k.
*
* @ejb.interface-method view-type = "both"
*/
public void returnBook(Integer primaryKey) throws EJBException {
try {
InitialContext context = new InitialContext();
//Get the home interface with JNDI from the application server
//Home Interface 黚er JNDI beim Appliacation Server holen
BookLocalHome bookLocalHome = (BookLocalHome)context.lookup(BookLocalHome.JNDI_NAME);
//find a book by primary key
//suche das Buch anhand seines Prim鋜schl黶sels
BookLocal bookLocal = bookLocalHome.findByPrimaryKey(primaryKey);
//remove the relation between the user
//entferne die Beziehung zwischen dem Benutzer
bookLocal.setUser(null);
} catch (NamingException e) {
throw new EJBException(e.getMessage());
} catch (FinderException e) {
throw new EJBException(e.getMessage());
}
}
/**
* This methode borrow a book for a user
*
* Diese Methode verleiht ein Buch an einen Benutzer
*
* @ejb.interface-method view-type = "both"
*/
public void borrowBook(Integer primaryKey, Integer userPrimaryKey) throws EJBException {
try {
InitialContext context = new InitialContext();
//Get the home interface with JNDI from the application server
//Home Interface 黚er JNDI beim Appliacation Server holen
BookLocalHome bookLocalHome = (BookLocalHome)context.lookup(BookLocalHome.JNDI_NAME);
UserLocalHome userLocalHome = (UserLocalHome)context.lookup(UserLocalHome.JNDI_NAME);
//find the book by primary key
//suche das Buch anhand seines Prim鋜schl黶sels
BookLocal bookLocal = bookLocalHome.findByPrimaryKey(primaryKey);
//find the user by primary key
//suche den Benutzer anhand seines Prim鋜schl黶sels
UserLocal userLocal = userLocalHome.findByPrimaryKey(userPrimaryKey);
//set the local inferface of user to assign him to a book
//setze das Local Interface des Benutzers um den Benutzer dem Buch zuzuweisen
bookLocal.setUser(userLocal);
} catch (NamingException e) {
throw new EJBException(e.getMessage());
} catch (FinderException e) {
throw new EJBException(e.getMessage());
}
}
/**
* This method removes a book from the database
*
* Diese Methode entfernt ein Buch aus der Datenbank
*
* @ejb.interface-method view-type = "both"
*/
public void removeBookByPrimaryKey(Integer primaryKey) throws EJBException {
try {
InitialContext context = new InitialContext();
//Get the home interface with JNDI from the application server
//Home Interface 黚er JNDI beim Appliacation Server holen
BookLocalHome bookLocalHome = (BookLocalHome)context.lookup(BookLocalHome.JNDI_NAME);
//find the book by primary key
//suche das Buch anhand seines Prim鋜schl黶sels
BookLocal bookLocal = bookLocalHome.findByPrimaryKey(primaryKey);
//remove the book
//entferne das buch
bookLocal.remove();
} catch (RemoveException e) {
throw new EJBException(e.getMessage());
} catch (NamingException e) {
throw new EJBException(e.getMessage());
} catch (FinderException e) {
throw new EJBException(e.getMessage());
}
}
/**
* This method save the value object of the book
*
* Diese Methode speichert das Value Objekt des Buches
*
* @ejb.interface-method view-type = "both"
*/
public void saveBook(BookValue bookValue) throws EJBException {
try {
InitialContext context = new InitialContext();
//Get the home interface with JNDI from the application server
//Home Interface 黚er JNDI beim Appliacation Server holen
BookLocalHome bookLocalHome = (BookLocalHome)context.lookup(BookLocalHome.JNDI_NAME);
//check if the book can be found for update, otherwise insert it as a new book
//黚erpr黤en ob das Buch f黵 die Aktualisierung gefunden wird, ansonsten erstelle eine Neues
BookLocal bookLocal = null;
try {
if(bookValue.getId()==null)bookValue.setId(new Integer(0));
bookLocal = bookLocalHome.findByPrimaryKey(bookValue.getId());
} catch (FinderException e1) {
bookLocal = bookLocalHome.create();
}
//update the values of the book
//aktualsiere die Werte des Buches
bookLocal.setBookValue(bookValue);
} catch (NamingException e) {
throw new EJBException(e.getMessage());
} catch (CreateException e) {
throw new EJBException(e.getMessage());
}
}
/** The session context */
private SessionContext context;
public BookSession() {
// TODO Auto-generated constructor stub
}
public void ejbActivate() throws EJBException, RemoteException {
// TODO Auto-generated method stub
}
public void ejbPassivate() throws EJBException, RemoteException {
// TODO Auto-generated method stub
}
public void ejbRemove() throws EJBException, RemoteException {
// TODO Auto-generated method stub
}
/**
* Set the associated session context. The container calls this method
* after the instance creation.
*
* The enterprise bean instance should store the reference to the context
* object in an instance variable.
*
* This method is called with no transaction context.
*
* @throws EJBException Thrown if method fails due to system-level error.
*/
public void setSessionContext(SessionContext newContext)
throws EJBException {
context = newContext;
}
/**
* An example business method
*
* @ejb.interface-method view-type = "both"
*
* @throws EJBException Thrown if method fails due to system-level error.
*/
public void replaceWithRealBusinessMethod() throws EJBException {
// rename and start putting your business logic here
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -