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() );
}
}
}