5 Replies Latest reply on Nov 14, 2014 2:57 AM by kavnico

    iText Nested DataModels

    clerum

      I'm trying to use itext to generate a PDF invoice. The layout is going to be something like this.


      Account
      --Site
      ----Line Items


      I have the following page working.


      <p:document
           xmlns:p="http://jboss.com/products/seam/pdf"
          xmlns:ui="http://java.sun.com/jsf/facelets"
          xmlns:f="http://java.sun.com/jsf/core">
         <p:font name="courier"> 
         <f:facet name="header">
              <p:font size="20">
                  <p:footer borderWidthTop="1" borderColorTop="green" borderWidthBottom="0" alignment="center">Clearfly Communications - <p:pageNumber /></p:footer>
              </p:font>
         </f:facet>
         <p:image value="/img/logo.gif" scalePercent="10" alignment="left" /> 
           
         <p:table columns="2" widthPercentage="100">
                   <f:facet name="defaultCell">
                  <p:cell borderWidth="0" />
              </f:facet>
              <p:cell><p:font style="bold"><p:paragraph>#{selinv.accountName}</p:paragraph></p:font></p:cell>
              <p:cell horizontalAlignment="right"><p:paragraph><p:text value="#{selinv.invoiceDate}"><f:convertDateTime type="both" pattern="MM/dd/yyyy"/></p:text></p:paragraph></p:cell>
              <p:cell><p:font style="bold"><p:paragraph>Account: #{selinv.accountCode} </p:paragraph></p:font></p:cell>
              <p:cell horizontalAlignment="right"><p:paragraph>Invoice: #{selinv.invoiceNumber}</p:paragraph></p:cell>
         </p:table>   
        </p:font> 
        <p:table columns="1" widthPercentage="100">
        <ui:repeat value="#{siteList}" var="site">       
               <p:cell><p:font style="bold"><p:paragraph>#{site.sitename}</p:paragraph></p:font></p:cell>
                <p:table columns="2" widthPercentage="100">
                  <ui:repeat value="#{productList}" var="product">
                       <p:cell><p:paragraph>#{product.description}</p:paragraph></p:cell>
                         <p:cell><p:paragraph>#{product.recAmount}</p:paragraph></p:cell>
                  </ui:repeat>
                </p:table>         
        </ui:repeat>
        </p:table>
        
        
       <p:newPage />
        <p:barCode type="code128" code="A775-0243" barHeight="15" />
      </p:document>



      The issue is with the productList datamodel. It appears that it only gets called to be created once. Which makes because the productList variable is outjected so all my sites get the prodcuts for the first site listed. Is there another way to call the datamodel with parameters so that it gets recreated everytime the

      <ui:repeat>

      cycles?




      import java.util.List;
      
      import javax.ejb.Remove;
      import javax.ejb.Stateful;
      import javax.persistence.EntityManager;
      
      import org.jboss.seam.annotations.Factory;
      import org.jboss.seam.annotations.In;
      import org.jboss.seam.annotations.Name;
      import org.jboss.seam.annotations.datamodel.DataModel;
      
      @Stateful
      @Name("itemutil")
      public class ItemAction implements ItemUtil  {
           
           @In
           EntityManager entityManager;
           
           @In(required=false)
           Site site;
           
           @DataModel
           List <MainTable> productList;
           
           @SuppressWarnings("unchecked")
           @Factory("productList")
           public void listProducts()
           {
                System.out.println("Selecting products for Account: " + site.getAccountcode() + " and Site: " + site.getId());
                productList = (List<MainTable>) entityManager.createQuery("select maintable from MainTable maintable where accountcode=? and site_id=?")
                .setParameter(1, site.getAccountcode())
                .setParameter(2, site.getId())
                .getResultList();
           }
      
           @Remove
           public void destroy()
           {
                
           }
      
      }




        • 1. Re: iText Nested DataModels
          norman

          To be sure, are you saying you are seeing a different behavior than you see if you were to use the same code in a plain html page?

          • 2. Re: iText Nested DataModels
            clerum

            Not sure I'm seeing anything different. I can't say I'm even coding this correctly...


            Are their any examples of a nested datamodel.

            • 3. Re: iText Nested DataModels
              clerum

              OK I figured it out.


              Instead of using the datamodel with factory to return the list to the page I'm just call it directly and void the bijection which is causing my issue. This way the listProducts method is called directly with variables from the previous datatable each time.


              The more I realize that this isn't magic the more it makes sense...


              @Stateful
              @Name("itemutil")
              public class ItemAction implements ItemUtil  {
                   
                   @In
                   EntityManager entityManager;
              
                   @SuppressWarnings("unchecked")
                   public List<MainTable> listProducts(String account_code, int site_id)
                   {
                        System.out.println("Selecting products for Account: " + account_code + " and Site: " + site_id);
                        List <MainTable> productList = (List<MainTable>) entityManager.createQuery("select maintable from MainTable maintable where accountcode=? and site_id=?")
                        .setParameter(1, account_code)
                        .setParameter(2, site_id)
                        .getResultList();
                        return productList;
                   }
              
                   @Remove
                   public void destroy()
                   {
                        
                   }
              



              <p:document
                   xmlns:p="http://jboss.com/products/seam/pdf"
                  xmlns:ui="http://java.sun.com/jsf/facelets"
                  xmlns:f="http://java.sun.com/jsf/core">
                 <p:font name="courier"> 
                 <f:facet name="header">
                      <p:font size="20">
                          <p:footer borderWidthTop="1" borderColorTop="green" borderWidthBottom="0" alignment="center">Clearfly Communications - <p:pageNumber /></p:footer>
                      </p:font>
                 </f:facet>
                 <p:image value="/img/logo.gif" scalePercent="10" alignment="left" /> 
                   
                 <p:table columns="2" widthPercentage="100">
                           <f:facet name="defaultCell">
                          <p:cell borderWidth="0" />
                      </f:facet>
                      <p:cell><p:font style="bold"><p:paragraph>#{selinv.accountName}</p:paragraph></p:font></p:cell>
                      <p:cell horizontalAlignment="right"><p:paragraph><p:text value="#{selinv.invoiceDate}"><f:convertDateTime type="both" pattern="MM/dd/yyyy"/></p:text></p:paragraph></p:cell>
                      <p:cell><p:font style="bold"><p:paragraph>Account: #{selinv.accountCode} </p:paragraph></p:font></p:cell>
                      <p:cell horizontalAlignment="right"><p:paragraph>Invoice: #{selinv.invoiceNumber}</p:paragraph></p:cell>
                 </p:table>   
                </p:font> 
                <p:table columns="1" widthPercentage="100">
                <ui:repeat value="#{siteList}" var="site">       
                       <p:cell><p:font style="bold"><p:paragraph>#{site.sitename}</p:paragraph></p:font></p:cell>
                        <p:table columns="2" widthPercentage="100">
                          <ui:repeat value="#{itemutil.listProducts(site.accountcode, site.id)}" var="product">
                               <p:cell><p:paragraph>#{product.description}</p:paragraph></p:cell>
                                 <p:cell><p:paragraph>#{product.recAmount}</p:paragraph></p:cell>
                          </ui:repeat>
                        </p:table>         
                </ui:repeat>
                </p:table>
                
                
               <p:newPage />
                <p:barCode type="code128" code="#{selinv.accountCode}" barHeight="15" />
              </p:document>
              

              • 4. Re: iText Nested DataModels
                passimage

                They've kind of made it to where the book "iText in Action" is really the place to go, but there are some good example sites out there:  roseindia.net/java/itext/index.shtml vogella.de/articles/JavaPDF/article.html, javaboutique.internet.com/tutorials/iText, tutorials.jenkov.com/java-itext/getting-started.html, geek-tutorials.com/java/itext/itext_index.php  They also do have their JavaDoc out there: api.itextpdf.com/index.html?overview-summary.html also share with you generating code 128 barcode java.

                • 5. Re: Re: iText Nested DataModels
                  kavnico

                  I want to generate code 128 in java progress. Can you give me any idea?

                  Appreciated in advance.