1 package org.kuali.ole.deliver.util.printSlip;
2
3 import com.itextpdf.text.*;
4 import com.itextpdf.text.pdf.PdfPTable;
5 import com.itextpdf.text.pdf.PdfWriter;
6
7 import org.apache.commons.lang.StringUtils;
8 import org.apache.log4j.Logger;
9 import org.kuali.ole.OLEConstants;
10 import org.kuali.ole.deliver.bo.OleItemSearch;
11 import org.kuali.ole.deliver.drools.CheckedInItem;
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 import java.util.Iterator;
20
21
22
23
24 public abstract class OlePrintSlipUtil {
25 private static final Logger LOG = Logger.getLogger(OlePrintSlipUtil.class);
26
27
28 private OleItemSearch oleItemSearch;
29 private OleItemRecordForCirc oleItemRecordForCirc;
30 SimpleDateFormat simpleDateFormat;
31 SimpleDateFormat dateFormat;
32 private DocstoreUtil docstoreUtil;
33 private OutputStream outputStream;
34
35 public SimpleDateFormat getSimpleDateFormat() {
36 if (null == simpleDateFormat) {
37 simpleDateFormat = new SimpleDateFormat(OLEConstants.TIMESTAMP);
38 }
39 return simpleDateFormat;
40 }
41
42 public SimpleDateFormat getDateFormat() {
43 if (null == dateFormat) {
44 dateFormat = new SimpleDateFormat(OLEConstants.DATEFORMAT);
45 }
46 return dateFormat;
47 }
48
49 public OleItemRecordForCirc getOleItemRecordForCirc() {
50 return oleItemRecordForCirc;
51 }
52
53 public OleItemSearch getOleItemSearch() {
54 return oleItemSearch;
55 }
56
57 public void createPdfForPrintingSlip(OleItemRecordForCirc oleItemRecordForCirc,HttpServletResponse response) {
58 LOG.debug("Initialize Normal pdf Template");
59 try {
60 this.oleItemRecordForCirc = oleItemRecordForCirc;
61 this.oleItemSearch = getDocstoreUtil().getOleItemSearchList(oleItemRecordForCirc.getItemUUID());
62 boolean missingPieceCheck = oleItemRecordForCirc.getItemRecord().isMissingPieceFlag();
63
64 getPdfFormatUtil().populateColorMap();
65 getPdfFormatUtil().populateFontMap();
66 response.setContentType("application/pdf");
67 Document document= getPdfFormatUtil().getDocument(0, 0, 5, 5);
68 document.open();
69 PdfPTable pdfTable = getPdfPTable();
70 Paragraph paraGraph = new Paragraph();
71 paraGraph.setAlignment(Element.ALIGN_CENTER);
72
73 populateHeader(paraGraph);
74
75 paraGraph.add(Chunk.NEWLINE);
76 paraGraph.add(Chunk.NEWLINE);
77 paraGraph.add(Chunk.NEWLINE);
78
79 populateBody(pdfTable);
80
81
82 if (!missingPieceCheck) {
83 populateContentForSlip(pdfTable);
84 }
85 if(null == outputStream){
86 outputStream = response.getOutputStream();
87 }
88 if (!missingPieceCheck) {
89 PdfWriter.getInstance(document, outputStream);
90 document.open();
91 document.add(paraGraph);
92 document.add(pdfTable);
93 document.close();
94 } else {
95 document.close();
96 }
97 outputStream.flush();
98 outputStream.close();
99 } catch (Exception e) {
100 LOG.error("Exception while creating pdf for printing slip", e);
101 }
102
103 }
104
105 public void createPdfForEndSessionPrintSlip(java.util.List<CheckedInItem> checkedInItems,HttpServletResponse response){
106
107 LOG.debug("Initialize Receipt pdf Template");
108 try {
109
110 getPdfFormatUtil().populateColorMap();
111 getPdfFormatUtil().populateFontMap();
112 response.setContentType("application/pdf");
113 Document document= getPdfFormatUtil().getDocument(0, 0, 5, 5);
114 document.open();
115 if(null == outputStream){
116 outputStream = response.getOutputStream();
117 }
118 PdfWriter.getInstance(document, outputStream);
119 document.open();
120 for(CheckedInItem checkedInItem : checkedInItems){
121 if(StringUtils.isNotBlank(checkedInItem.getItemForCircRecord().getItemStatusToBeUpdatedTo()) && checkedInItem.getItemForCircRecord().getItemStatusToBeUpdatedTo().equals(OLEConstants.ITEM_STATUS_ON_HOLD)){
122 this.oleItemRecordForCirc = checkedInItem.getItemForCircRecord();
123 this.oleItemSearch = getDocstoreUtil().getOleItemSearchList(oleItemRecordForCirc.getItemUUID());
124 PdfPTable pdfTable = getPdfPTable();
125 Paragraph paraGraph = new Paragraph();
126 paraGraph.setAlignment(Element.ALIGN_CENTER);
127 populateHeader(paraGraph);
128 paraGraph.add(Chunk.NEWLINE);
129 populateBody(pdfTable);
130 populateContentForSlip(pdfTable);
131 document.add(paraGraph);
132 document.add(pdfTable);
133 Paragraph paragraphForSpace = new Paragraph();
134 paragraphForSpace.add(Chunk.NEWLINE);
135 document.add(paragraphForSpace);
136 }
137 }
138
139 document.close();
140 outputStream.flush();
141 outputStream.close();
142 } catch (Exception e) {
143 LOG.error("Exception while creating pdf for printing slip", e);
144 }
145 }
146
147 private DocstoreUtil getDocstoreUtil() {
148 if (null == docstoreUtil) {
149 docstoreUtil = new DocstoreUtil();
150 }
151 return docstoreUtil;
152 }
153
154 public void setDocstoreUtil(DocstoreUtil docstoreUtil) {
155 this.docstoreUtil = docstoreUtil;
156 }
157
158 public OutputStream getOutputStream() {
159 return outputStream;
160 }
161
162 public void setOutputStream(OutputStream outputStream) {
163 this.outputStream = outputStream;
164 }
165
166 public abstract PdfFormatUtil getPdfFormatUtil();
167
168
169 protected abstract void populateHeader(Paragraph paraGraph);
170
171 protected abstract void populateBody(PdfPTable pdfTable);
172
173 protected abstract void populateContentForSlip(PdfPTable pdfTable);
174
175 protected abstract PdfPTable getPdfPTable();
176
177
178
179 }