protected void doPost( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException {
bzb = new BooksZenBooks( "en", dbConfigResource ); // @TODO language should be a request param
String forwardUrl;
String pageTitle;
int step = RequestHelper.getInt( "step", request );
RequestDispatcher dispatcher;
HashMap<String, String> formErrors = new HashMap<String, String>();
BookListing listing = ( BookListing ) request.getSession().getAttribute( "userListing" );
Book book;
/* Load necessary lexicons */
bzb.getLexicon().load( "global" );
bzb.getLexicon().load( "subject" );
bzb.getLexicon().load( "book" );
bzb.getLexicon().load( "listing" );
pageTitle = bzb.getLexicon().get( "addListing" );
/* Make sure user is logged in */
if( bzb.getAuthenticatedUser( request ) == null ) {
bzb.getLexicon().load( "error" );
forwardUrl = jspPath + "401.jsp";
pageTitle = bzb.getLexicon().get( "unauthorized" );
}
/* User has submitted the initial ISBN form */
else if( step == 2 ) {
/* Invalid ISBN */
if( !isValidISBN( RequestHelper.getString( "isbn", request ) ) ) {
forwardUrl = jspPath + "newListingStep1.jsp";
formErrors.put( "isbn", bzb.getLexicon().get( "invalidField", new String[][]{ { "field", bzb.getLexicon().get( "isbn" ) } } ) );
}
else {
forwardUrl = jspPath + "newListingStep2.jsp";
request.setAttribute( "languages", getLanguages() );
/* Add book data to request, if it exists in the DB */
request.setAttribute( "book", getBook( RequestHelper.getString( "isbn", request ) ) );
}
}
/* User has submitted the book form */
else if( step == 3 ) {
/* Existing book? */
if( RequestHelper.getString( "newBook", request ).equals( "false" ) ) {
/* Invalid ISBN */
if( !isValidISBN( RequestHelper.getString( "isbn", request ) ) ) {
forwardUrl = jspPath + "newListingStep1.jsp";
formErrors.put( "isbn", bzb.getLexicon().get( "invalidField", new String[][]{ { "field", bzb.getLexicon().get( "isbn" ) } } ) );
}
else {
/* Check to see if book exists in the DB */
if( ( book = getBook( RequestHelper.getString( "isbn", request ) ) ) != null ) {
forwardUrl = jspPath + "newListingStep3.jsp";
/* Create empty listing object for later */
if( listing == null ) {
listing = new BookListing();
}
listing.setBook( book );
/* Put listing object in SESSION object */
request.getSession().setAttribute( "userListing", listing );
request.setAttribute( "conditions", getConditions() );
}
else {
forwardUrl = jspPath + "newListingStep1.jsp";
formErrors.put( "isbn", bzb.getLexicon().get( "invalidField", new String[][]{ { "field", bzb.getLexicon().get( "isbn" ) } } ) );
}
}
}
/* New book being submitted */
else {
formErrors = checkBookForm( request);
/* Has errors - display book form again */
if( !formErrors.isEmpty() ) {
forwardUrl = jspPath + "newListingStep2.jsp";
request.setAttribute( "languages", getLanguages() );
}
/* No errors - display listing form */
else {
forwardUrl = jspPath + "newListingStep3.jsp";
if( listing == null ) {
listing = new BookListing();
}
/* Add listing to SESSION */
listing.setBook( getBookFromInput( request ) );
request.getSession().setAttribute( "userListing", listing );
request.setAttribute( "conditions", getConditions() );
}
}
}
/* Submitted the final listing form */
else if( step == 4 ) {
/* Hey, you can't skip to the end just yet! */
if( listing == null ) {
forwardUrl = jspPath + "newListingStep1.jsp";
formErrors.put( "page", bzb.getLexicon().get( "timeOut" ) );
}
/* Make sure form is valid */
else {
formErrors = checkListingForm( request );
/* Invalid - display listing form again */
if( !formErrors.isEmpty() ) {
forwardUrl = jspPath + "newListingStep3.jsp";
request.setAttribute( "conditions", getConditions() );
}
/* Create listing and display success page */
else {
saveListing( listing, request );
forwardUrl = jspPath + "newListingDone.jsp";
}
}
}
/* Display initial ISBN form */
else {
forwardUrl = jspPath + "newListingStep1.jsp";
}
/* Make lexicons and config settings available to JSP */
request.setAttribute( "config", bzb.getConfig().getSettings() );
request.setAttribute( "lexicon", bzb.getLexicon().getLexicons() );
request.setAttribute( "language", bzb.getLexicon().getLanguage() );
request.setAttribute( "subjects", bzb.getSubjects() );
request.setAttribute( "pageTitle", pageTitle );
request.setAttribute( "formErrors", formErrors );
/* Set up forward and display JSP */
dispatcher = getServletContext().getRequestDispatcher( forwardUrl );
dispatcher.forward( request, response );
}