// $Id: NewBookFromEditionDetailsAction.java 14032 2011-11-29 20:02:11Z dgorsline $
package org.CLIENT.cmsui.booksearch.action;
import java.util.ArrayList;
import java.util.Collections;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.CLIENT.CLIENTConstants;
import org.CLIENT.bakertaylor.BakerTaylorAPI;
import org.CLIENT.bakertaylor.BakerTaylorEditionInfo;
import org.CLIENT.book.BookProcessingStatus;
import org.CLIENT.book.BookUtility;
import org.CLIENT.cmsui.action.CMSAction;
import org.CLIENT.cmsui.storyeditor.PresentationConstants;
import org.CLIENT.cmsui.util.CMSUtil;
import org.CLIENT.dbobjects.Book;
import org.CLIENT.dbobjects.ParentAssociationCollection;
import org.CLIENT.dbobjects.Person;
import org.CLIENT.dbobjects.StoryThing;
import org.CLIENT.exception.CLIENTException;
import org.CLIENT.logfiles.CLIENTLogger;
import org.CLIENT.misc.SimplePublishableItem;
import org.CLIENT.publish.PublishUtil;
/**
* Looks up full edition details (from Baker & Taylor) for the specified ISBN
* (by query string parameter isbn
);
* uses this info to populate the current page thing in the session
* (which happens to be a new book). Returns status information via the
* request attribute SUCCESS_MESSAGE
.
*/
public class NewBookFromEditionDetailsAction extends CMSAction implements PresentationConstants
{
private static Logger logger = CLIENTLogger.getLogger(NewBookFromEditionDetailsAction.class.getName());
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception
{
HttpSession session = request.getSession(false);
if (session == null)
{
throw new CLIENTException("No session variable");
}
String isbn = request.getParameter("isbn");
Book sessionBook = (Book) CMSUtil.getSessionPageThing(request);
int userId = CMSUtil.getCurrentUserId(request);
//perform the B&T lookup
BakerTaylorAPI bakerTaylorAPI = new BakerTaylorAPI();
BakerTaylorEditionInfo info = null;
try
{
info = bakerTaylorAPI.getDetailsByISBN(isbn);
}
catch (Exception e)
{
request.setAttribute(ERROR_MESSAGE, "Internal error contacting Baker&Taylor: " + e.getMessage());
logger.error("Internal error contacting Baker&Taylor", e);
return mapping.findForward(FORWARD_ERROR);
}
//sweep B&T info into the Book object and its related objects
BookProcessingStatus bookStatus = BookUtility.createBookObjects(sessionBook, info, userId);
if (!bookStatus.getSuccess())
{
String errorMessage = "Cannot import book edition information";
for (String error : bookStatus.getErrors())
{
errorMessage += "; " + error;
}
request.setAttribute(ERROR_MESSAGE, errorMessage);
return mapping.findForward(FORWARD_ERROR);
}
//update the application-memory list of all library subjects
ArrayList librarySubjects = bookStatus.getLibrarySubjects();
if (librarySubjects.size() > 0)
{
ArrayList allLibrarySubjects =
(ArrayList) request.getSession().getServletContext().getAttribute(PresentationConstants.ATT_ALL_LIBRARY_SUBJECTS);
for (SimplePublishableItem librarySubject : librarySubjects)
{
if (!allLibrarySubjects.contains(librarySubject))
{
allLibrarySubjects.add(librarySubject);
}
}
Collections.sort(allLibrarySubjects);
}
//load the parentCollection to include the new genres
ParentAssociationCollection parentCollection = new ParentAssociationCollection(sessionBook.getId());
session.setAttribute(ATT_STORY_EDITOR_PARENTS, parentCollection);
//pop book info into session variables
ArrayList authorList = sessionBook.getAuthors();
session.setAttribute(ATT_WEB_FEATURE_AUTHORS, authorList);
//publish and unlock newly-created authors, if any
ArrayList authorsCreated = bookStatus.getAuthors();
if (authorsCreated != null)
{
for (Person author : authorsCreated)
{
PublishUtil publisher = PublishUtil.getForThing(author, null, userId);
publisher.publish();
author.unlock();
}
}
//publish and unlock newly-created excerpts, if any
ArrayList excerptsCreated = bookStatus.getExcerpts();
if (excerptsCreated != null)
{
for (StoryThing excerpt : excerptsCreated)
{
PublishUtil publisher = PublishUtil.getForThing(excerpt, null, userId);
publisher.publish();
//SFW-6218: temporary
//override the publish date
java.sql.Date date = new java.sql.Date(110, 0, 1);
excerpt.setThingDate(date);
excerpt.setThingPubDate(date);
excerpt.saveToDb();
//end SFW-6218
excerpt.unlock();
}
}
request.setAttribute(SUCCESS_MESSAGE, bookStatus.toJSON());
return mapping.findForward(FORWARD_SUCCESS);
}
public int getActionSecurityLevel()
{
return CLIENTConstants.FUNCTION_FOR_EVERYONE;
}
}