Library Reservation System end to end scenario

Author: Libor Martinek, Lukas Jungmann
Last 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

  1. Start bundled Java DB. Tools | Java DB Database | Start Java DB Server
  2. Create new database . Tools | Java DB Database | Create Java DB Database, call it library and use tester as user name as well as password.
  3. 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

  1. Create new J2EE 1.4 EJB Module project with name "Library".
  2. Generate CMP Entity Beans from Database. Right-click on Library project and select New -> CMP Entity Beans from Database. Create new Data source jdbc/library for jdbc:derby://localhost:1527/library connection, Add All tables and click on Next, enter package library and let both checkboxes selected and click on Finish. Four Entity Beans are created.
  3. Create Book DTO. Right-click on BookEB and select Generated DTO from popup menu.
  4. Create Service Locator. Open New File Wizard, select category Enterprise, file type Caching Service Locator, enter ServiceLocator as locator Name, package service and press Finish.
  5. 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

  1. Call EJB. Select "Enterprise Resources -> Call Enterprise Bean" from popup menu and select BookEB. Do the same for BookCopyEB, ReservationEB and LibraryUserEB.
  2. 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;
        }
                    
  3. 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;
        }
                    
  4. 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);
        }
                    
  5. 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);
        }
                    
  6. 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, add String isbn parameter and press OK.
  7. 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)), add String isbn, java.sql.Date from, java.sql.Date to parameter and press OK.
  8. 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 o and press OK.
  9. 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;
            }
                    
  10. 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 user parameters, 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);
        }
                    
  11. 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());
            }
        }
                    
  12. 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.
  13. Set Message Handler. Right-click on ReservationService node and select Configure Handlers popup menu. Add newly created Message Handler.

Test functionality

  1. Build project
  2. Deploy project
  3. Create new temporary web/ejb project with web service client for ReservationService
  4. Web Service References -> ReservationService -> ReservationServiceSEIPort.
  5. Double-click on getBooksByAuthor node.
  6. Enter parameters: "Tom", "tom", "Douglas Adams".
  7. Click on submit. You will get all books from Douglas Adams. Clock dialog.
  8. Double-click on reserver.
  9. Enter parameters: "Tom", "tom", "0345391802", "Jul 1, 2005", "Jul 10, 2005".
  10. Click on submit. You will get result "true", which means that reservation was successfully processed.
  11. Enter parameters: "Tom", "tom", "0345418905", "Aug 1, 2005", "Aug 10, 2005".
  12. Click on submit. You will get result "false", which means that reservation was NOT successfully processed. This book is not available at this time.



Project Features

About this Project

QA was started in November 2009, is owned by Jiří Kovalský, and has 97 members.
By use of this website, you agree to the NetBeans Policies and Terms of Use (revision 20160708.bf2ac18). © 2014, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo
 
 
Close
loading
Please Confirm
Close