YCP Logo Assignment 5: Web Bookstore

Due: Friday, April 29th Wednesday, May 4th by 11:59 PM

Updated 4/29 - corrected mistake in the Inserting the book (and author if needed) section, extended due date

Getting Started

Download CS350_Assign5.zip. Import it into your Eclipse workspace (File->Import...->General->Existing projects into workspace->Archive File). You will see a project called CS350_Assign5 in the Package Explorer.

The initial project is a simple web bookstore application. You can run it by right-clicking on BookStore.java and choosing Run As->Web Application. Note that you need to have the Google Eclipse Plugin installed.

Note that the user interface has been divided into two tabs, called Search and Add Book.

Your Task

There are two tasks

  1. Modify the book search so that searching by title is supported. You can use the same approach that you used in Lab 7.
  2. Add support for adding new books to the database.

Adding Books

Implement the createSearchUI() method so that it returns a FlowPanel containing a user interface for adding books to the database. It should look something like this:

figures/addBook.png

Once a book has been added to the database, it should be possible to retrieve the book information using the Search tab:

figures/addBookResults.png

Hints

Inserting the book (and author if needed)

To insert a new book into the database, you must first know the unique integer id of the author. If the author does not yet exist, you will need to add the author the authors relation.

Here is a suggested approach:

  1. Execute a select query to find the author id (based on the author last name and first name of the book to be added.) If the query returns a ResultSet with at least one tuple, then the author already exists.
  2. If the author id is not found, use an SQL insert statement to add the author. Note that you should not provide a value for the author_id attribute: instead, the database will generate one for you.
  3. You can execute the query select last_insert_rowid() to get the author id following the insertion of a new author.

Once you know the author id, inserting a new tuple into the books relation should be straightforward.

Important: Note that to execute an SQL insert statement, you will need to use the execute() method of the PreparedStatement class, and not the executeQuery() method.

Making sure database resources are closed

Your application will need to ensure that any database resources, such as ResultSet, PreparedStatement, and Connection objects, are closed when they are no longer needed. You can use the DBUtil.closeQuietly methods to close them.

Make sure that you use try/finally blocks to ensure that resources are closed even if an exception occurs.

Adding a new RPC service

You will probably want to add a new RPC service for adding books. Here is a suggested approach.

1. Add the synchronous interface:

package edu.ycp.cs350.books.client;

import com.google.gwt.user.client.rpc.RemoteService;
import com.google.gwt.user.client.rpc.RemoteServiceRelativePath;

import edu.ycp.cs350.books.shared.Book;

@RemoteServiceRelativePath("addBook")
public interface AddBookService extends RemoteService {
        public Boolean addBook(Book book);
}

2. Add the asynchronous interface. The Google Eclipse plugin will do this automatically if you use the quick fix for the error reported for the synchronous interface ("Missing asynchronous interface...").

3. Add the server implementation:

package edu.ycp.cs350.books.server;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

import edu.ycp.cs350.books.client.AddBookService;
import edu.ycp.cs350.books.shared.Book;

public class AddBookServiceImpl extends RemoteServiceServlet implements AddBookService {
        private static final long serialVersionUID = 1L;

        @Override
        public Boolean addBook(Book book) {
                // your code...
        }
}

4. Modify war/WEB-INF/web.xml. You will need a servlet tag and a servlet-mapping tag:

The servlet tag:

<servlet>
  <servlet-name>AddBookService</servlet-name>
  <servlet-class>
    edu.ycp.cs350.books.server.AddBookServiceImpl
  </servlet-class>
</servlet>

The servlet-mapping tag:

<servlet-mapping>
      <servlet-name>AddBookService</servlet-name>
      <url-pattern>/bookstore/addBook</url-pattern>
</servlet-mapping>

Note that all of the servlet tags must preceed all of the servlet-mapping tags.

5. Add the service object to the BookStore entry point class:

private final AddBookServiceAsync addBookService =
        GWT.create(AddBookService.class);

You can put this underneath the already-present searchService field.

Grading criteria

  • Search by title: 40%
  • Insert book for existing author: 20%
  • Insert book for new author: 30%
  • Coding style: 10%
  • Support partial last name and title searches: 5%

Submitting

Export your finished project to a zip file (right-click on the project, then Export...->Archive File).

Since the resulting zip file will be too large to upload to Marmoset, please email it to me:

dhovemey@ycp.edu