Library Reservation System end to end scenario
Author: Libor Martinek, Lukas JungmannLast update: $Date: 2009/10/29 16:50:43 $, $Revision: 1.1.1.1 $
Introduction
This end to end scenario discribes developing of very simple Library Reservation System. It covers j2ee features which are possibe to use in NetBeans IDE 5.5 (version for NetBeans 4.1 is here). This application consists of EJB module with one web service, which is used for reservation of book in libarry. We will create database tables in bundled Java DB database, then we will generated CMP Entity Beans from this database and finally we will create web service.Library sources are available here.
Database setup
- Start bundled Java DB. Tools | Java DB Database | Start Java DB Server
- Create new database .
Tools | Java DB Database | Create Java DB Database, call it
libraryand usetesteras user name as well as password. - Create tables with sample data. Click on Open button and open library-javadb.sql. Choose connection to created library DB and click on 'Execute All' button.
Creating IDE project
- Create new J2EE 1.4 EJB Module project with name
"Library".
- Generate CMP Entity Beans from
Database. Right-click on Library project and select New ->
CMP Entity Beans from Database. Create new Data source
jdbc/libraryforjdbc:derby://localhost:1527/libraryconnection, Add All tables and click on Next, enter packagelibraryand let both checkboxes selected and click on Finish. Four Entity Beans are created. - Create Book DTO. Right-click on BookEB and select Generated DTO from popup menu.
- Create Service Locator. Open
New File Wizard, select category Enterprise, file type
Caching Service Locator, enter
ServiceLocatoras locator Name, packageserviceand press Finish. - Create Web Service. Open New File Wizard, select category Web Services, file type Web Service, enter "ReservationService" as Web Service Name, package "service" and press Finish.
Writing Web Service
- Call EJB. Select "Enterprise Resources -> Call Enterprise Bean" from popup menu and select BookEB. Do the same for BookCopyEB, ReservationEB and LibraryUserEB.
- Write login method.
private LibraryUserLocal login(String username, String password) throws java.rmi.RemoteException { LibraryUserLocal user; try { user = lookupLibraryUserBean().findByPrimaryKey(username); } catch (FinderException e) { throw new RemoteException("Authentication failed: Invalid username or password."); } if (user == null || !user.getPassword().equals(password)) { throw new RemoteException("Authentication failed: Invalid username or password."); } return user; } - Write convertToBookDTOArray
method.
private library.BookDTO[] convertToBookDTOArray(Collection collection) { if (collection == null || collection.isEmpty()) { return null; } BookDTO books[] = new BookDTO[collection.size()]; int index = 0; Iterator it = collection.iterator(); while (it.hasNext()) { BookLocal bookBean = (BookLocal)(it.next()); BookDTO bookDTO = new BookDTO(); bookDTO.populate(bookBean); books[index++] = bookDTO; } return books; } - Create getBooksByTitle operation.
Select "Web Service -> Add Operation" from
popup menu. Create this operation:
public library.BookDTO[] getBooksByTitle(String username, String password, String title) throws javax.ejb.FinderException, java.rmi.RemoteException {and write code:
public library.BookDTO[] getBooksByTitle(String username, String password, String title) throws javax.ejb.FinderException, java.rmi.RemoteException { // TODO implement operation login(username, password); Collection collection = lookupBookBean().findByTitle(title); return convertToBookDTOArray(collection); } - Create getBooksByAuthor operation.
Select "Web Service -> Add Operation" from
popup menu. Create this operation:
public library.BookDTO[] getBooksByAuthor(String username, String password, String author) throws javax.ejb.FinderException, java.rmi.RemoteException {and write code:
public library.BookDTO[] getBooksByTitle(String username, String password, String author) throws javax.ejb.FinderException, java.rmi.RemoteException { // TODO implement operation login(username, password); Collection collection = lookupBookBean().findByAuthor(author); return convertToBookDTOArray(collection); } - Add Finder Method to
BookCopyEB. Open BookCopyEB and select "EJB Methods -> Add Finder
Method" from popup menu. Call it
findByISBN, use following EJBQL:SELECT OBJECT(o) FROM BookCopy o WHERE o.isbn.isbn = ?1, addString isbnparameter and press OK. - Add Finder Method to
BookCopyEB. Open BookCopyEB and select "EJB Methods -> Add Finder
Method" from popup menu. Call it
findReservedBooks, use following EJBQL:SELECT DISTINCT OBJECT(o) FROM BookCopy o, IN (o.reservationBeanCollection) AS r WHERE o.isbn.isbn = ?1 AND ((r.reservedFrom <= ?2 AND r.reservedTo >= ?2) OR (r.reservedFrom <= ?3 AND r.reservedTo >= ?3) OR (r.reservedFrom >= ?2 AND r.reservedTo <= ?3)), addString isbn, java.sql.Date from, java.sql.Date toparameter and press OK. - Add Select method. Open
ReservationEB and select "EJB Methods -> Add Select Method" from
popup menu. Call it
ejbSelectMaxId, use following EJBQL:SELECT MAX(o.id) FROM Reservation oand press OK. - Add Home method. Open
ReservationEB and select "EJB Methods -> Add Home Method" from popup
menu. Write name "maxId", return type "int". Write method code:
public int ejbHomeMaxId() { //TODO implement ejbHomeMaxId try { return ejbSelectMaxId(); } catch (FinderException e) { return 0; } - Add Create method. Open
ReservationEB and select "EJB Methods -> Add Create Method" from popup
menu, add
Integer id, Date from, Date to, BookCopyLocal bookCopy, LibraryUserLocal userparameters, press OK and implement added create and postCreate methods:public Integer ejbCreate(Integer id, Date from, Date to, BookCopyLocal bookCopy, LibraryUserLocal user) throws CreateException { //TODO implement ejbCreate setId(id); setReservedFrom(from); setReservedTo(to); setReturned(null); return null; } public void ejbPostCreate(Integer id, Date from, Date to, BookCopyLocal bookCopy, LibraryUserLocal user) throws CreateException { //TODO implement ejbPostCreate setUsername(user); setBookcopyId(bookCopy); } - Create operation. Open
ReservationService, add new operation there:
public boolean reserve(String username, String password, String isbn, String from, String to) throws javax.ejb.FinderException, javax.ejb.CreateException, java.rmi.RemoteException {and implement it:public boolean reserve(String username, String password, String isbn, String from, String to) throws javax.ejb.FinderException, javax.ejb.CreateException, java.rmi.RemoteException { // TODO implement operation LibraryUserLocal user = login(username, password); try { DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.US); java.sql.Date f = new java.sql.Date(df.parse(from).getTime()); java.sql.Date t = new java.sql.Date(df.parse(to).getTime()); Collection reservedBooks = lookupBookCopyBean().findReservedBooksEntity(isbn, f, t); // get all books for this isbn Iterator it = lookupBookCopyBean().findByISBN(isbn).iterator(); while(it.hasNext()){ BookCopyLocal bookCopy = (BookCopyLocal)it.next(); if (!reservedBooks.contains(bookCopy)){ ReservationLocal reservation = lookupReservationBean() .create( new Integer(lookupReservationBean().maxId()+1), f, t, bookCopy, user); return true; } } return false; } catch (ParseException e) { throw new RemoteException("Date parse error: "+e.getMessage()); } } - Create Message Handler. Open New File Wizard, select Web Services category and create new Message Handler. Name it ReservationMessageHandler, and create it in package service.
- Set Message Handler. Right-click on ReservationService node and select Configure Handlers popup menu. Add newly created Message Handler.
Test functionality
- Build project
- Deploy project
- Create new temporary web/ejb project with web service client for ReservationService
- Web Service References -> ReservationService -> ReservationServiceSEIPort.
- Double-click on getBooksByAuthor node.
- Enter parameters: "Tom", "tom", "Douglas Adams".
- Click on submit. You will get all books from Douglas Adams. Clock
dialog.
- Double-click on reserver.
- Enter parameters: "Tom", "tom", "0345391802", "Jul 1, 2005", "Jul 10, 2005".
- Click on submit. You will get result "true", which means that reservation was successfully processed.
- Enter parameters: "Tom", "tom", "0345418905", "Aug 1, 2005", "Aug 10, 2005".
- Click on submit. You will get result "false", which means that
reservation was NOT successfully processed. This book is not available
at this time.
