View Javadoc
1   package org.kuali.ole.deliver.util.printSlip;
2   
3   import com.itextpdf.text.Chunk;
4   import com.itextpdf.text.Document;
5   import com.itextpdf.text.Element;
6   import com.itextpdf.text.Paragraph;
7   import com.itextpdf.text.pdf.PdfPTable;
8   import com.itextpdf.text.pdf.PdfWriter;
9   import org.apache.log4j.Logger;
10  import org.kuali.ole.OLEConstants;
11  import org.kuali.ole.deliver.bo.OleItemSearch;
12  import org.kuali.ole.deliver.util.OleItemRecordForCirc;
13  import org.kuali.ole.deliver.util.PdfFormatUtil;
14  import org.kuali.ole.util.DocstoreUtil;
15  
16  import javax.servlet.http.HttpServletResponse;
17  import java.io.OutputStream;
18  import java.text.SimpleDateFormat;
19  
20  /**
21   * Created by pvsubrah on 9/3/15.
22   */
23  public abstract class OlePrintSlipUtil {
24      private static final Logger LOG = Logger.getLogger(OlePrintSlipUtil.class);
25  
26  
27      private OleItemSearch oleItemSearch;
28      private OleItemRecordForCirc oleItemRecordForCirc;
29      SimpleDateFormat simpleDateFormat;
30      SimpleDateFormat dateFormat;
31      private DocstoreUtil docstoreUtil;
32      private OutputStream outputStream;
33  
34      public SimpleDateFormat getSimpleDateFormat() {
35          if (null == simpleDateFormat) {
36              simpleDateFormat = new SimpleDateFormat(OLEConstants.TIMESTAMP);
37          }
38          return simpleDateFormat;
39      }
40  
41      public SimpleDateFormat getDateFormat() {
42          if (null == dateFormat) {
43              dateFormat = new SimpleDateFormat(OLEConstants.DATEFORMAT);
44          }
45          return dateFormat;
46      }
47  
48      public OleItemRecordForCirc getOleItemRecordForCirc() {
49          return oleItemRecordForCirc;
50      }
51  
52      public OleItemSearch getOleItemSearch() {
53          return oleItemSearch;
54      }
55  
56      public  void createPdfForPrintingSlip(OleItemRecordForCirc oleItemRecordForCirc,HttpServletResponse response) {
57          LOG.debug("Initialize Normal pdf Template");
58          try {
59              this.oleItemRecordForCirc = oleItemRecordForCirc;
60              this.oleItemSearch = getDocstoreUtil().getOleItemSearchList(oleItemRecordForCirc.getItemUUID());
61              boolean missingPieceCheck = oleItemRecordForCirc.getItemRecord().isMissingPieceFlag();
62  
63              getPdfFormatUtil().populateColorMap();
64              getPdfFormatUtil().populateFontMap();
65              response.setContentType("application/pdf");
66              Document document= getPdfFormatUtil().getDocument(0, 0, 5, 5);
67              document.open();
68              PdfPTable pdfTable = getPdfPTable();
69              Paragraph paraGraph = new Paragraph();
70              paraGraph.setAlignment(Element.ALIGN_CENTER);
71  
72              populateHeader(paraGraph);
73  
74              paraGraph.add(Chunk.NEWLINE);
75              paraGraph.add(Chunk.NEWLINE);
76              paraGraph.add(Chunk.NEWLINE);
77  
78              populateBody(pdfTable);
79  
80  
81              if (!missingPieceCheck) {
82                  populateContentForSlip(pdfTable);
83              }
84              if(null == outputStream){
85                  outputStream = response.getOutputStream();
86              }
87              if (!missingPieceCheck) {
88                  PdfWriter.getInstance(document, outputStream);
89                  document.open();
90                  document.add(paraGraph);
91                  document.add(pdfTable);
92                  document.close();
93              } else {
94                  document.close();
95              }
96              outputStream.flush();
97              outputStream.close();
98          } catch (Exception e) {
99              LOG.error("Exception while creating pdf for printing slip", e);
100         }
101 
102     }
103 
104     private DocstoreUtil getDocstoreUtil() {
105         if (null == docstoreUtil) {
106             docstoreUtil = new DocstoreUtil();
107         }
108         return docstoreUtil;
109     }
110 
111     public void setDocstoreUtil(DocstoreUtil docstoreUtil) {
112         this.docstoreUtil = docstoreUtil;
113     }
114 
115     public OutputStream getOutputStream() {
116         return outputStream;
117     }
118 
119     public void setOutputStream(OutputStream outputStream) {
120         this.outputStream = outputStream;
121     }
122 
123     public abstract PdfFormatUtil getPdfFormatUtil();
124 
125 
126     protected abstract void populateHeader(Paragraph paraGraph);
127 
128     protected abstract void populateBody(PdfPTable pdfTable);
129 
130     protected abstract void populateContentForSlip(PdfPTable pdfTable);
131 
132     protected abstract PdfPTable getPdfPTable();
133 
134 
135 
136 }