1 Reply Latest reply on Apr 13, 2010 7:11 PM by Matt Campbell

    Hibernate flush error when using Render.instance().render() to get pdf for persisting

    Matt Campbell Newbie

      I am trying to generate and save a pdf and after the pdf is saved, also send it to the browser for the user to view. I am getting a hibernate exception (using as JPA provider) that says the following: org.hibernate.AssertionFailure: collection [<insert collection name here>] was not processed by flush().


      I tried setting cascade to ALL for that collection, but then the problem just moves to another collection. I followed this until I had cascade all for every collection in the given package of Entities and the error was still occurring! This leads me to believe that it might be something with the way my entity is getting used.  Here's what is happening.


      I have a button on my Home.xhtml page that triggers an action method on a controller component. That component takes the EntityHome for my entity as an in parameter. The action method sets up the call to ender.instance().render() and then gets the DocumentStore and gets the bytes for the generated document out of it and sets them to the variable where they are stored on the entity. The .xhtml file that renders the PDF also uses the same EntityHome and Entity instance to render the pdf. Could this be part of the problem?


      I have included my controller class with the action method below. The action method that is being called is generateAndDisplayFile(). I am using Seam 2.2.0.EAP5 (the version that ships with JBoss EAP 5)


      package com.citytechinc.crp.web.quote.action;
      
      import java.io.IOException;
      
      import javax.faces.context.FacesContext;
      
      import org.jboss.seam.annotations.In;
      import org.jboss.seam.annotations.Logger;
      import org.jboss.seam.annotations.Name;
      import org.jboss.seam.core.Conversation;
      import org.jboss.seam.core.Manager;
      import org.jboss.seam.document.ByteArrayDocumentData;
      import org.jboss.seam.document.DocumentData;
      import org.jboss.seam.document.DocumentStore;
      import org.jboss.seam.faces.FacesMessages;
      import org.jboss.seam.faces.RedirectException;
      import org.jboss.seam.faces.Renderer;
      import org.jboss.seam.framework.Controller;
      import org.jboss.seam.log.Log;
      
      import com.citytechinc.crp.domain.quote.Quote;
      import com.citytechinc.crp.web.quote.QuoteHome;
      
      @Name("quoteFileAction")
      public class QuoteFileAction extends Controller {
      
          private static final String QUOTE_PDF_VIEW_ID = "/quote/QuotePdf.xhtml";
      
          private static final long serialVersionUID = 1L;
      
          @Logger
          private Log log;
      
          @In
          private QuoteHome quoteHome;
      
          @In
          private Manager manager;
      
          @In
          private Conversation conversation;
      
          @In(value = "org.jboss.seam.document.documentStore", create = true)
          private DocumentStore documentStore;
      
          @In
          private FacesContext facesContext;
      
          public boolean quoteHasFile() {
              return quoteHome.getInstance().hasQuoteFile();
          }
      
          public void generateAndDisplayFile() {
      
              //        EmptyFacesContext emptyFacesContext = new EmptyFacesContext();
              boolean newConversation = conversation.begin();
              try {
                  log.info("calling quote home to generate file...");
                  if (!generateQuoteFile()) {
                      throw new IllegalStateException("Failed to generate quote file!");
                  }
      
              } catch (Exception ex) {
                  log.error("Error when trying to get the content of the pdf in bytes", ex);
              } finally {
                  if (newConversation) {
                      conversation.end();
                  }
                  //            emptyFacesContext.restore();
              }
      
              displayFile();
          }
      
          private boolean generateQuoteFile() throws Exception {
          
              if (!quoteHome.allowFileCreation()) {
                  FacesMessages.createFacesMessage(javax.faces.application.FacesMessage.SEVERITY_INFO,
                  "You cannot render a new PDF for this Quote at this time. Please try again or contact support.");
                  return false;
              }
          
              log.info("getting ready to generate document bytes");
              ByteArrayDocumentData byteData = null;
              byteData = createFile(byteData);
          
              ByteArrayDocumentData file = byteData;
              log.info("document bytes generated file is " + (file != null ? "not null" : "null"));
          
              if (file != null) {
          
                  quoteHome.getInstance().setDocumentBytes(file.getData());
                  quoteHome.getInstance().setDocumentMimeType(file.getDocumentType().getMimeType());
                  quoteHome.getInstance().setDocumentExtension(file.getDocumentType().getExtension());
          
                  return true;
              } else {
                  FacesMessages.createFacesMessage(javax.faces.application.FacesMessage.SEVERITY_ERROR,
                  "There was a problem rendering the PDF for Quote #{quoteHome.instance.id}. Please try again or contact support.");
                  return false;
              }
          }
      
          private ByteArrayDocumentData createFile(ByteArrayDocumentData byteData) throws Exception {
              Renderer.instance().render(QUOTE_PDF_VIEW_ID);
              DocumentStore doc = DocumentStore.instance();
          
              if (doc != null) {
                  DocumentData data = doc.getDocumentData("1");
                  if (data instanceof ByteArrayDocumentData) {
                      byteData = (ByteArrayDocumentData) data;
                  } else {
                      throw new Exception("Couldnt get the bytes from the pdf document, unkown class " + data.getClass().getName());
                  }
              }
              return byteData;
          }
      
          public void displayFile() {
              Quote instance = quoteHome.getInstance();
              byte[] binaryData = instance.getDocumentBytes();
      
              log.info("getting ready to send document bytes to browser");
              DocumentData.DocumentType documentType = new DocumentData.DocumentType(instance.getDocumentExtension(), instance.getDocumentMimeType());
              ByteArrayDocumentData data = new ByteArrayDocumentData(quoteHome.quoteFileNameBase(), documentType, binaryData);
              String docId = documentStore.newId();
              documentStore.saveData(docId, data);
              String documentUrl = documentStore.preferredUrlForContent(data.getBaseName(), data.getDocumentType().getExtension(), docId);
              redirect(documentUrl);
      
              log.info("redirect to document processed");
          }
      
          protected void redirect(String url) {
              try {
                  facesContext.getExternalContext().redirect(manager.encodeConversationId(url));
              } catch (IOException ioe) {
                  throw new RedirectException(ioe);
              }
          }
      }