View Javadoc

1   package org.kuali.ole.deliver.loan.printSlip;
2   
3   
4   
5   import com.lowagie.text.*;
6   import com.lowagie.text.Font;
7   import com.lowagie.text.pdf.PdfPCell;
8   import com.lowagie.text.pdf.PdfPTable;
9   import com.lowagie.text.pdf.PdfPageEventHelper;
10  import com.lowagie.text.pdf.PdfWriter;
11  import org.apache.log4j.Logger;
12  import org.kuali.ole.OLEConstants;
13  import org.kuali.ole.deliver.circulationdesk.bo.OleCirculationDesk;
14  import org.kuali.ole.deliver.loan.LoanProcessor;
15  import org.kuali.ole.deliver.loan.bo.OleLoanDocument;
16  import org.kuali.ole.deliver.request.bo.OleDeliverRequestBo;
17  import org.kuali.ole.docstore.discovery.service.QueryServiceImpl;
18  import org.kuali.ole.docstore.model.xmlpojo.work.instance.oleml.Item;
19  import org.kuali.ole.location.bo.OleLocation;
20  import org.kuali.ole.patron.bo.OlePatronDocument;
21  import org.kuali.rice.kim.impl.identity.address.EntityAddressBo;
22  import org.kuali.rice.kim.impl.identity.email.EntityEmailBo;
23  import org.kuali.rice.kim.impl.identity.entity.EntityBo;
24  import org.kuali.rice.kim.impl.identity.name.EntityNameBo;
25  
26  import javax.servlet.ServletOutputStream;
27  import javax.servlet.http.HttpServletResponse;
28  import java.awt.*;
29  import java.io.ByteArrayOutputStream;
30  import java.io.OutputStream;
31  import java.text.SimpleDateFormat;
32  import java.util.*;
33  import java.util.List;
34  
35  /**
36   * The OlePrintSlip  class used to create pdf document .
37   */
38  public class OlePrintSlip extends PdfPageEventHelper {
39  
40      private static final Logger LOG = Logger.getLogger(OlePrintSlip.class);
41  
42      private Map<String, Font> printFontMap = new HashMap<String, Font>();
43  
44      private Map<String, Font> fontMap = new HashMap<String, Font>();
45      private Map<String, Color> colorMap = new HashMap<String, Color>();
46      private Map<String, Color> printColorMap = new HashMap<String, Color>();
47  
48  
49      public void populateFontMap() {
50          fontMap.put("COURIER", new Font(Font.COURIER));
51          fontMap.put("BOLD", new Font(Font.BOLD));
52          fontMap.put("BOLDITALIC", new Font(Font.BOLDITALIC));
53          fontMap.put("DEFAULTSIZE", new Font(Font.DEFAULTSIZE));
54          fontMap.put("HELVETICA", new Font(Font.HELVETICA));
55          fontMap.put("ITALIC", new Font(Font.ITALIC));
56          fontMap.put("NORMAL", new Font(Font.NORMAL));
57          fontMap.put("STRIKETHRU", new Font(Font.STRIKETHRU));
58          fontMap.put("SYMBOL", new Font(Font.SYMBOL));
59          fontMap.put("TIMES_ROMAN", new Font(Font.TIMES_ROMAN));
60          fontMap.put("UNDEFINED", new Font(Font.UNDEFINED));
61          fontMap.put("UNDERLINE", new Font(Font.UNDERLINE));
62          fontMap.put("ZAPFDINGBATS", new Font(Font.ZAPFDINGBATS));
63  
64      }
65  
66      public void populateColorMap() {
67          colorMap.put("WHITE", Color.WHITE);
68          colorMap.put("YELLOW", Color.YELLOW);
69          colorMap.put("BLACK", Color.BLACK);
70          colorMap.put("BLUE", Color.BLUE);
71          colorMap.put("CYAN", Color.CYAN);
72          colorMap.put("DARK_GRAY", Color.DARK_GRAY);
73          colorMap.put("GRAY", Color.GRAY);
74          colorMap.put("GREEN", Color.GREEN);
75          colorMap.put("LIGHT_GRAY", Color.LIGHT_GRAY);
76          colorMap.put("MAGENTA", Color.MAGENTA);
77          colorMap.put("ORANGE", Color.ORANGE);
78          colorMap.put("PINK", Color.PINK);
79          colorMap.put("RED", Color.RED);
80          colorMap.put("PINK", Color.PINK);
81      }
82  
83  
84      /**
85       *  Used to create pdf document for hold slip
86       * @param oleLoanDocument
87       * @return Void
88       */
89      public void createPdfForPrintingSlip( OleLoanDocument oleLoanDocument,HttpServletResponse response) {
90          LOG.debug("Initialize Normal pdf Template");
91          LoanProcessor loanProcessor = new LoanProcessor();
92          OleDeliverRequestBo oleDeliverRequestBo = oleLoanDocument.getOleDeliverRequestBo();
93          OlePatronDocument oleRequestPatronDocument = oleDeliverRequestBo!=null?loanProcessor.getOlePatronDocument(oleDeliverRequestBo.getBorrowerId()):null;
94              EntityNameBo nameBo = oleRequestPatronDocument!=null?oleRequestPatronDocument.getEntity().getNames().get(0):null;
95              String patronName = nameBo!=null? nameBo.getFirstName()+","+nameBo.getLastName():null;
96              Date expirationDate =  oleRequestPatronDocument!=null?oleRequestPatronDocument.getExpirationDate():null;
97          OlePatronDocument olePatronDocument = oleLoanDocument!=null?loanProcessor.getOlePatronDocument(oleLoanDocument.getPatronId()):null;
98              OleLocation oleLocation=null;
99              OleCirculationDesk oleCirculationDesk = null;
100             try {
101                 if(oleLoanDocument.getCirculationLocationId() != null){
102                     oleCirculationDesk = loanProcessor.getOleCirculationDesk(oleLoanDocument.getCirculationLocationId());
103                     oleLocation =oleCirculationDesk!=null?oleCirculationDesk.getOleCirculationDeskLocations().get(0).getLocation():null;
104                 }
105                 String locationName = oleLocation!=null?oleLocation.getLocationName():null;
106                 String itemBarcode = oleLoanDocument.getItemId();
107                 String itemCallNumber = null;
108                 String copyNumber=null;
109                 Item oleItem = oleLoanDocument.getOleItem();
110                 if(oleItem!=null){
111                     itemCallNumber = loanProcessor.getItemCallNumber(oleItem,oleLoanDocument.getInstanceUuid());
112                     copyNumber = oleItem.getCopyNumber();
113 
114                 }else{
115                     Map itemUuid = QueryServiceImpl.getInstance().getItemDetails(itemBarcode,null);
116                     Map bibInformation = QueryServiceImpl.getInstance().getTitleAndAuthorfromBib((String) itemUuid.get(OLEConstants.BIB_UUID));
117                     String itemXml = null;
118                     oleLoanDocument.setInstanceUuid((String) itemUuid.get(OLEConstants.INSTANCE_UUID));
119                     oleLoanDocument.setItemUuid((String) itemUuid.get(OLEConstants.ITEM_UUID));
120                     oleLoanDocument.setTitle((String)bibInformation.get(OLEConstants.TITLE));
121                     oleLoanDocument.setAuthor((String)bibInformation.get(OLEConstants.AUTHOR));
122                     itemXml = loanProcessor.getItemXML(oleLoanDocument.getItemUuid());
123                     oleItem = loanProcessor.getItemPojo(itemXml);
124                     itemCallNumber = loanProcessor.getItemCallNumber(oleItem,oleLoanDocument.getInstanceUuid());
125                     copyNumber = oleItem.getCopyNumber();
126                 }
127                 String itemTitle = oleLoanDocument.getTitle();
128                 String requestedBy =null;
129                 if(oleDeliverRequestBo!=null){
130                     requestedBy= oleDeliverRequestBo.getBorrowerName();
131                 }
132                 String routeTo = oleLoanDocument.getRouteToLocation()!=null?oleLoanDocument.getRouteToLocation():oleLoanDocument.getItemLocation();
133                 String checkInNote =oleItem!=null?oleItem.getCheckinNote():null;
134                 String itemStatusCode = oleLoanDocument.getItemStatusCode()!=null?oleLoanDocument.getItemStatusCode():"";
135                 boolean checkInNoteDisplay =checkInNote!=null && !checkInNote.trim().isEmpty() && !oleLoanDocument.getItemStatusCode().contains(OLEConstants.ITEM_STATUS_IN_TRANSIT) && oleLoanDocument.getRouteToLocation()!=null && !oleLoanDocument.getRouteToLocation().trim().isEmpty();
136                 boolean holdSlip = itemStatusCode.equalsIgnoreCase(OLEConstants.ITEM_STATUS_ON_HOLD);
137                 boolean inTransitSlip = itemStatusCode.equalsIgnoreCase(OLEConstants.ITEM_STATUS_IN_TRANSIT);
138                 boolean inTransitForHold = itemStatusCode.equalsIgnoreCase(OLEConstants.ITEM_STATUS_IN_TRANSIT_HOLD);
139                 boolean inTransitPerStaff = itemStatusCode.equalsIgnoreCase(OLEConstants.ITEM_STATUS_IN_TRANSIT_STAFF);
140                 boolean missingPieceCheck = itemStatusCode.equalsIgnoreCase(OLEConstants.ITEM_STATUS_RETURNED_MISSING);
141                 boolean returnedDamaged = itemStatusCode.equalsIgnoreCase(OLEConstants.ITEM_STATUS_RETURNED_DAMAGED);
142                 boolean claimsReturned = oleLoanDocument.isNoticeForClaimsReturned();
143                 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
144                 populateColorMap();
145                 populateFontMap();
146                 response.setContentType("application/pdf");
147                 OutputStream os = null;
148                 Document document = this.getDocument(0, 0, 5, 5);
149                 if(missingPieceCheck){
150                     os = response.getOutputStream();
151                     PdfWriter.getInstance(document, os);
152                 }
153                 document.open();
154                 document.newPage();
155                 Font boldFont = new Font(Font.TIMES_ROMAN, 15, Font.BOLD);
156                 PdfPTable pdfTable =new PdfPTable(3);
157                 Paragraph paraGraph = new Paragraph();
158                 paraGraph.setAlignment(Element.ALIGN_CENTER);
159                 if(holdSlip){
160                     paraGraph.add(new Chunk("Hold Slip",boldFont));
161                 }else if(inTransitSlip){
162                     paraGraph.add(new Chunk("Routing Slip In-Transit",boldFont));
163                 }else if(inTransitForHold){
164                     paraGraph.add(new Chunk("Routing Slip In-Transit For Hold",boldFont));
165                 }else if(inTransitPerStaff){
166                     paraGraph.add(new Chunk("Routing Slip In-Transit Per Staff Request",boldFont));
167                 }else if(missingPieceCheck){
168                     paraGraph.add(new Chunk("Missing Pieces Notice",boldFont));
169                 }else if (returnedDamaged){
170                     paraGraph.add(new Chunk("Returned Damaged",boldFont));
171                 }else if(claimsReturned){
172                     paraGraph.add(new Chunk("Claims Returned Notice",boldFont));
173                 }else if(checkInNoteDisplay){
174                     paraGraph.add(new Chunk("Routing Slip",boldFont));
175                 }else {
176                     paraGraph.add(new Chunk("Receipt(CheckIn) Slip",boldFont));
177                 }
178                 paraGraph.add(Chunk.NEWLINE);
179                 paraGraph.add(Chunk.NEWLINE);
180                 paraGraph.add(Chunk.NEWLINE);
181                 if(holdSlip){
182                     pdfTable.addCell(getPdfPCellInJustified("Patron Name"));
183                     pdfTable.addCell(getPdfPCellInLeft(":"));
184                     pdfTable.addCell(getPdfPCellInJustified(patronName));
185 
186                     pdfTable.addCell(getPdfPCellInJustified("Expiration Date"));
187                     pdfTable.addCell(getPdfPCellInLeft(":"));
188                     pdfTable.addCell(getPdfPCellInJustified(expirationDate.toString()));
189 
190                 } else if(inTransitSlip || checkInNoteDisplay){
191 
192                     pdfTable.addCell(getPdfPCellInJustified("Route To"));
193                     pdfTable.addCell(getPdfPCellInLeft(":"));
194                     pdfTable.addCell(getPdfPCellInJustified(routeTo));
195 
196                     pdfTable.addCell(getPdfPCellInJustified("Date/Time "));
197                     pdfTable.addCell(getPdfPCellInLeft(":"));
198                     pdfTable.addCell(getPdfPCellInJustified(oleLoanDocument.getCheckInDate().toString()));
199 
200                 } else if(inTransitForHold){
201 
202                     pdfTable.addCell(getPdfPCellInJustified("Route To"));
203                     pdfTable.addCell(getPdfPCellInLeft(":"));
204                     pdfTable.addCell(getPdfPCellInJustified(routeTo));
205 
206                     pdfTable.addCell(getPdfPCellInJustified("Place on hold for"));
207                     pdfTable.addCell(getPdfPCellInLeft(":"));
208                     pdfTable.addCell(getPdfPCellInJustified(requestedBy));
209 
210 
211                     pdfTable.addCell(getPdfPCellInJustified("Date/Time "));
212                     pdfTable.addCell(getPdfPCellInLeft(":"));
213                     pdfTable.addCell(getPdfPCellInJustified(oleLoanDocument.getCheckInDate().toString()));
214 
215                 } else if(inTransitPerStaff){
216 
217                     pdfTable.addCell(getPdfPCellInJustified("Route To"));
218                     pdfTable.addCell(getPdfPCellInLeft(":"));
219                     pdfTable.addCell(getPdfPCellInJustified(routeTo));
220 
221                     pdfTable.addCell(getPdfPCellInJustified("Requested By"));
222                     pdfTable.addCell(getPdfPCellInLeft(":"));
223                     pdfTable.addCell(getPdfPCellInJustified(requestedBy));
224 
225 
226                     pdfTable.addCell(getPdfPCellInJustified("Date/Time "));
227                     pdfTable.addCell(getPdfPCellInLeft(":"));
228                     pdfTable.addCell(getPdfPCellInJustified(oleLoanDocument.getCheckInDate().toString()));
229 
230                 } else if(returnedDamaged){
231                     pdfTable.addCell(getPdfPCellInJustified("Route To"));
232                     pdfTable.addCell(getPdfPCellInLeft(":"));
233                     pdfTable.addCell(getPdfPCellInJustified(routeTo));
234                 }
235                 else if (missingPieceCheck){
236                     document.add(paraGraph);
237                     //Circulation Desk
238                     pdfTable.addCell(getPdfPCellInJustified("Circulation Location / Library Name"));
239                     pdfTable.addCell(getPdfPCellInLeft(":"));
240                     pdfTable.addCell(getPdfPCellInJustified(oleCirculationDesk!=null?oleCirculationDesk.getCirculationDeskPublicName():""));
241 
242                     pdfTable.addCell(getPdfPCellInJustified("Address"));
243                     pdfTable.addCell(getPdfPCellInLeft(":"));
244                     pdfTable.addCell(getPdfPCellInJustified(""));
245 
246                     pdfTable.addCell(getPdfPCellInJustified("Email"));
247                     pdfTable.addCell(getPdfPCellInLeft(":"));
248                     pdfTable.addCell(getPdfPCellInJustified("kuali.ole@org.com"));
249 
250                     pdfTable.addCell(getPdfPCellInJustified("Phone #"));
251                     pdfTable.addCell(getPdfPCellInLeft(":"));
252                     pdfTable.addCell(getPdfPCellInJustified("111-5555"));
253 
254                     document.add(pdfTable);
255                     paraGraph = new Paragraph();
256                     paraGraph.add(Chunk.NEWLINE);
257                     document.add(paraGraph);
258 
259                     //Patron
260                     paraGraph = new Paragraph();
261                     paraGraph.add(new Chunk("Addressee",boldFont));
262                     paraGraph.add(Chunk.NEWLINE);
263                     document.add(paraGraph);
264 
265                     pdfTable =new PdfPTable(3);
266                     pdfTable.addCell(getPdfPCellInJustified("Borrower Name"));
267                     pdfTable.addCell(getPdfPCellInLeft(":"));
268                     pdfTable.addCell(getPdfPCellInJustified(patronName));
269 
270                     pdfTable.addCell(getPdfPCellInJustified("Address"));
271                     pdfTable.addCell(getPdfPCellInLeft(":"));
272                     pdfTable.addCell(getPdfPCellInJustified(""));
273 
274                     pdfTable.addCell(getPdfPCellInJustified("Email"));
275                     pdfTable.addCell(getPdfPCellInLeft(":"));
276                     pdfTable.addCell(getPdfPCellInJustified(olePatronDocument!=null && olePatronDocument.getEmailAddress()!=null?olePatronDocument.getEmailAddress():""));
277 
278                     pdfTable.addCell(getPdfPCellInJustified("Phone #"));
279                     pdfTable.addCell(getPdfPCellInLeft(":"));
280                     pdfTable.addCell(getPdfPCellInJustified(olePatronDocument!=null &&olePatronDocument.getPhoneNumber()!=null?olePatronDocument.getPhoneNumber():""));
281 
282                     document.add(pdfTable );
283                     //Notice Type
284                     paraGraph = new Paragraph();
285                     paraGraph.add(new Chunk("Return with Missing Piece(s) Notice",boldFont));
286                     paraGraph.setAlignment(Element.ALIGN_CENTER);
287                     paraGraph.add(Chunk.NEWLINE);
288                     document.add(paraGraph);
289 
290 
291                     //Notice-specific text
292                     paraGraph = new Paragraph();
293                     paraGraph.add(new Chunk("The following item(s) returned by you is missing one or more of its pieces.Please return the missing piece(s) to the library shown above or contact the library about this matter to avoid incurring any penalties.",boldFont));
294                     paraGraph.setAlignment(Element.ALIGN_CENTER);
295                     paraGraph.add(Chunk.NEWLINE);
296                     document.add(paraGraph);
297 
298                     //Title/item information
299                     paraGraph = new Paragraph();
300                     paraGraph.add(new Chunk("Title/item information",boldFont));
301                     paraGraph.add(Chunk.NEWLINE);
302                     document.add(paraGraph);
303 
304                     pdfTable =new PdfPTable(3);
305                     pdfTable.addCell(getPdfPCellInJustified("Title"));
306                     pdfTable.addCell(getPdfPCellInLeft(":"));
307                     pdfTable.addCell(getPdfPCellInJustified(itemTitle));
308 
309                     pdfTable.addCell(getPdfPCellInJustified("Author"));
310                     pdfTable.addCell(getPdfPCellInLeft(":"));
311                     pdfTable.addCell(getPdfPCellInJustified(oleLoanDocument.getAuthor()));
312 
313                     pdfTable.addCell(getPdfPCellInJustified("Volume/Issue/Copy #"));
314                     pdfTable.addCell(getPdfPCellInLeft(":"));
315                     pdfTable.addCell(getPdfPCellInJustified(copyNumber));
316 
317                     pdfTable.addCell(getPdfPCellInJustified("Library shelving location "));
318                     pdfTable.addCell(getPdfPCellInLeft(":"));
319                     pdfTable.addCell(getPdfPCellInJustified(locationName));
320 
321                     pdfTable.addCell(getPdfPCellInJustified("Call #"));
322                     pdfTable.addCell(getPdfPCellInLeft(":"));
323                     pdfTable.addCell(getPdfPCellInJustified(itemCallNumber));
324 
325                     pdfTable.addCell(getPdfPCellInJustified("Item barcode"));
326                     pdfTable.addCell(getPdfPCellInLeft(":"));
327                     pdfTable.addCell(getPdfPCellInJustified(itemBarcode));
328                     document.add(pdfTable);
329 
330                 }
331                 if (!missingPieceCheck){
332                     pdfTable.addCell(getPdfPCellInJustified("Circulation Location"));
333                     pdfTable.addCell(getPdfPCellInLeft(":"));
334                     pdfTable.addCell(getPdfPCellInJustified(locationName));
335 
336                     pdfTable.addCell(getPdfPCellInJustified("Item Barcode"));
337                     pdfTable.addCell(getPdfPCellInLeft(":"));
338                     pdfTable.addCell(getPdfPCellInJustified(itemBarcode));
339 
340                     pdfTable.addCell(getPdfPCellInJustified("Title"));
341                     pdfTable.addCell(getPdfPCellInLeft(":"));
342                     pdfTable.addCell(getPdfPCellInJustified(itemTitle));
343 
344                     pdfTable.addCell(getPdfPCellInJustified("Call Number"));
345                     pdfTable.addCell(getPdfPCellInLeft(":"));
346                     pdfTable.addCell(getPdfPCellInJustified(itemCallNumber));
347 
348                     pdfTable.addCell(getPdfPCellInJustified("Copy Number"));
349                     pdfTable.addCell(getPdfPCellInLeft(":"));
350                     pdfTable.addCell(getPdfPCellInJustified(copyNumber));
351                     if(!(holdSlip | inTransitSlip | inTransitForHold | inTransitPerStaff | checkInNoteDisplay)){
352                         pdfTable.addCell(getPdfPCellInJustified("Check-in Date"));
353                         pdfTable.addCell(getPdfPCellInLeft(":"));
354                         pdfTable.addCell(getPdfPCellInJustified(oleLoanDocument.getCheckInDate().toString()));
355 
356                         pdfTable.addCell(getPdfPCellInJustified("Patron Barcode"));
357                         pdfTable.addCell(getPdfPCellInLeft(":"));
358                         pdfTable.addCell(getPdfPCellInJustified(olePatronDocument!=null && olePatronDocument.getBarcode()!=null? olePatronDocument.getBarcode():""));
359                     }
360                 }
361                 response.setContentType("application/pdf");
362                 if (!missingPieceCheck){
363                     os =response.getOutputStream();
364                     PdfWriter.getInstance(document, os);
365                     document.open();
366                     document.add(paraGraph);
367                     document.add(pdfTable);
368                     document.close();
369                 }  else {
370                     document.close();
371                 }
372                 byteArrayOutputStream.flush();
373                 byteArrayOutputStream.close();
374                 os.flush();
375                 os.close();
376             } catch (Exception e) {
377                 e.printStackTrace();
378             }
379     }
380 
381     /**
382      *  Used to create pdf document for checkout slip
383      * @param oleLoanDocument
384      * @return Void
385      */
386     public void createPdfForBackGroundCheckOut( OleLoanDocument oleLoanDocument,HttpServletResponse response) {
387         LOG.debug("Initialize Normal pdf Template");
388         LoanProcessor loanProcessor = new LoanProcessor();
389         OlePatronDocument olePatronDocument = oleLoanDocument!=null?loanProcessor.getOlePatronDocument(oleLoanDocument.getPatronId()):null;
390         if(olePatronDocument!=null){
391             EntityBo entityBo = olePatronDocument.getEntity();
392             EntityNameBo nameBo = entityBo!=null?entityBo.getNames().get(0):null;
393             String patronName = nameBo!=null? nameBo.getFirstName()+" "+nameBo.getLastName():null;
394             List<EntityEmailBo> entityEmailBos =entityBo!=null && entityBo.getEntityTypeContactInfos().size()>0?entityBo.getEntityTypeContactInfos().get(0).getEmailAddresses():new ArrayList<EntityEmailBo>();
395             List<EntityAddressBo> entityAddressBos =entityBo!=null && entityBo.getEntityTypeContactInfos().size()>0?entityBo.getEntityTypeContactInfos().get(0).getAddresses():new ArrayList<EntityAddressBo>();
396             String  defaultEmailAddress = null;
397             String defaultAddress = "";
398             for(EntityEmailBo entityEmailBo :entityEmailBos){
399                 if(entityEmailBo.isDefaultValue()){
400                     defaultEmailAddress = entityEmailBo.getEmailAddress();
401                     break;
402                 }
403             }
404             String line1 = "";
405             String line2 = "";
406             String line3 = "";
407             String city ="";
408             String stateProvisionalCode = "";
409             String countryCode = "";
410             String postalCode = "";
411             for(EntityAddressBo entityAddressBo : entityAddressBos){
412                 if(entityAddressBo.isDefaultValue()){
413                     line1= entityAddressBo.getLine1()!=null && !entityAddressBo.getLine1().isEmpty() ? entityAddressBo.getLine1()+OLEConstants.COMMA :"";
414                     line2= entityAddressBo.getLine2()!=null && !entityAddressBo.getLine2().isEmpty() ? entityAddressBo.getLine2()+OLEConstants.COMMA :"";
415                     line3= entityAddressBo.getLine3()!=null && !entityAddressBo.getLine3().isEmpty() ? entityAddressBo.getLine3()+OLEConstants.COMMA :"";
416                     city= entityAddressBo.getCity()!=null && !entityAddressBo.getCity().isEmpty() ? entityAddressBo.getCity()+OLEConstants.COMMA :"";
417                     stateProvisionalCode= entityAddressBo.getStateProvinceCode()!=null && !entityAddressBo.getStateProvinceCode().isEmpty() ? entityAddressBo.getStateProvinceCode()+OLEConstants.COMMA :"";
418                     countryCode= entityAddressBo.getCountryCode()!=null && !entityAddressBo.getCountryCode().isEmpty() ? entityAddressBo.getCountryCode()+OLEConstants.COMMA :"";
419                     postalCode= entityAddressBo.getPostalCode()!=null && !entityAddressBo.getPostalCode().isEmpty() ? entityAddressBo.getPostalCode():"";
420                 }
421             }
422             defaultAddress = line1+line2+line3+city+stateProvisionalCode+countryCode+postalCode;
423                 try{
424                 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
425                 populateColorMap();
426                 populateFontMap();
427                 response.setContentType("application/pdf");
428                 OutputStream os = null;
429                 Document document = this.getDocument(0, 0, 5, 5);
430                 document.open();
431                 document.newPage();
432                 Font boldFont = new Font(Font.TIMES_ROMAN, 15, Font.BOLD);
433                 PdfPTable pdfTable =new PdfPTable(1);
434                 Paragraph paraGraph = new Paragraph();
435                 paraGraph.setAlignment(Element.ALIGN_CENTER);
436                 paraGraph.add(new Chunk("Mailing Label",boldFont));
437                 paraGraph.add(Chunk.NEWLINE);
438                 paraGraph.add(Chunk.NEWLINE);
439                 paraGraph.add(Chunk.NEWLINE);
440                     pdfTable.setHorizontalAlignment(Element.ALIGN_CENTER);
441                     pdfTable.addCell(getPdfPCellInJustified(patronName));
442                     if(defaultAddress!=null && !defaultAddress.trim().isEmpty()){
443                        if(!line1.isEmpty()) pdfTable.addCell(getPdfPCellInJustified(line1));
444                        if(!line2.isEmpty()) pdfTable.addCell(getPdfPCellInJustified(line2));
445                        if(!line3.isEmpty()) pdfTable.addCell(getPdfPCellInJustified(line3));
446                        if(!city.isEmpty()) pdfTable.addCell(getPdfPCellInJustified(city));
447                        if(!stateProvisionalCode.isEmpty()) pdfTable.addCell(getPdfPCellInJustified(stateProvisionalCode));
448                        if(!countryCode.isEmpty()) pdfTable.addCell(getPdfPCellInJustified(countryCode));
449                        if(!postalCode.isEmpty()) pdfTable.addCell(getPdfPCellInJustified(postalCode));
450                     }
451                     else{
452                         if(defaultEmailAddress!=null && !defaultEmailAddress.trim().isEmpty()){
453                             pdfTable.addCell(getPdfPCellInJustified(defaultEmailAddress));
454                         }
455                     }
456                     document.add(pdfTable);
457                 response.setContentType("application/pdf");
458                 os =response.getOutputStream();
459                 PdfWriter.getInstance(document, os);
460                 document.open();
461                 document.add(paraGraph);
462                 document.add(pdfTable);
463                 document.close();
464                 byteArrayOutputStream.flush();
465                 byteArrayOutputStream.close();
466                 os.flush();
467                 os.close();
468             } catch (Exception e) {
469                 e.printStackTrace();
470             }
471         }
472     }
473 
474 
475     public void createDueDateSlipPdf(List<OleLoanDocument> oleLoanDocument, HttpServletResponse response) {
476         ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
477 
478         try {
479             populateColorMap();
480             populateFontMap();
481             SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yy");
482             String date = simpleDateFormat.format(System.currentTimeMillis());
483             response.setContentType("application/pdf");
484             Document document = this.getDocument(0, 0, 5, 5);
485             PdfWriter writer = PdfWriter.getInstance(document, byteArrayOutputStream);
486             document.open();
487             document.newPage();
488             Font boldFont = new Font(Font.TIMES_ROMAN, 15, Font.BOLD);
489             PdfPTable pdfTable =new PdfPTable(3);
490             Paragraph paraGraph = new Paragraph();
491             paraGraph.setAlignment(Element.ALIGN_CENTER);
492             paraGraph.add(new Chunk("Due Date Slip",boldFont));
493             paraGraph.add(Chunk.NEWLINE);
494             paraGraph.add(Chunk.NEWLINE);
495             paraGraph.add(Chunk.NEWLINE);
496             LoanProcessor loanProcessor = new LoanProcessor();
497             OleLocation oleLocation=null;
498             try {
499                 OleCirculationDesk oleCirculationDesk = null;
500                 if(oleLoanDocument.get(0).getCirculationLocationId() != null){
501                    oleCirculationDesk = loanProcessor.getOleCirculationDesk(oleLoanDocument.get(0).getCirculationLocationId());
502                 }
503                 oleLocation = oleCirculationDesk.getOleCirculationDeskLocations().get(0).getLocation();
504                // oleLocation = loanProcessor.getLocationByLocationId(oleLoanDocument.get(0).getCirculationLocationId());
505             } catch (Exception e) {
506                 e.printStackTrace();
507             }
508             String locationName = oleLocation!=null?oleLocation.getLocationName():null;
509 
510 
511             for(int dueDateSlip=0;dueDateSlip<oleLoanDocument.size();dueDateSlip++){
512 
513                 pdfTable.addCell(getPdfPCellInJustified("Circulation Location/Library Name"));
514                 pdfTable.addCell(getPdfPCellInLeft(":"));
515                 pdfTable.addCell(getPdfPCellInJustified(locationName));
516 
517                 pdfTable.addCell(getPdfPCellInJustified("Item Barcode"));
518                 pdfTable.addCell(getPdfPCellInLeft(":"));
519                 pdfTable.addCell(getPdfPCellInJustified(oleLoanDocument.get(dueDateSlip).getItemId()));
520 
521                 pdfTable.addCell(getPdfPCellInJustified("Title"));
522                 pdfTable.addCell(getPdfPCellInLeft(":"));
523                 pdfTable.addCell(getPdfPCellInJustified(oleLoanDocument.get(dueDateSlip).getTitle()));
524 
525                 pdfTable.addCell(getPdfPCellInJustified("Call Number"));
526                 pdfTable.addCell(getPdfPCellInLeft(":"));
527                 pdfTable.addCell(getPdfPCellInJustified(oleLoanDocument.get(dueDateSlip).getItemCallNumber()));
528 
529                 pdfTable.addCell(getPdfPCellInJustified("Copy Number"));
530                 pdfTable.addCell(getPdfPCellInLeft(":"));
531                 pdfTable.addCell(getPdfPCellInJustified(oleLoanDocument.get(dueDateSlip).getItemCopyNumber()));
532 
533                 pdfTable.addCell(getPdfPCellInJustified("Due Date"));
534                 pdfTable.addCell(getPdfPCellInLeft(":"));
535                 pdfTable.addCell(getPdfPCellInJustified(oleLoanDocument.get(dueDateSlip).getLoanDueDate().toString()));
536 
537                 pdfTable.addCell(getPdfPCellInJustified("Patron Barcode"));
538                 pdfTable.addCell(getPdfPCellInLeft(":"));
539                 pdfTable.addCell(getPdfPCellInJustified(oleLoanDocument.get(dueDateSlip).getPatronBarcode()));
540 
541 
542 
543                 /*paraGraph.add(Chunk.NEWLINE);
544                 paraGraph.add(Chunk.NEWLINE);
545 */
546                 pdfTable.addCell(getEmptyCell());
547                 pdfTable.addCell(getEmptyCell());
548                 pdfTable.addCell(getEmptyCell());
549 
550                 pdfTable.addCell(getEmptyCell());
551                 pdfTable.addCell(getEmptyCell());
552                 pdfTable.addCell(getEmptyCell());
553             }
554             response.setContentType("application/pdf");
555             ServletOutputStream sos = response.getOutputStream();
556             PdfWriter.getInstance(document, sos);
557             document.open();
558             document.add(paraGraph);
559             document.add(pdfTable);
560             document.close();
561             String fileName = "Due date slip_"+date;
562             ///OutputStream outputStream = new FileOutputStream(""+fileName+".pdf");
563             // byteArrayOutputStream.writeTo(outputStream);
564             byteArrayOutputStream.flush();
565             byteArrayOutputStream.close();
566             sos.flush();
567             sos.close();
568             // outputStream.close();
569         } catch (Exception e) {
570             e.printStackTrace();
571         }
572     }
573 
574     private PdfPCell getEmptyCell(){
575         PdfPCell pdfPCell = new PdfPCell(new Paragraph(Chunk.NEWLINE));
576         pdfPCell.setBorder(pdfPCell.NO_BORDER);
577         pdfPCell.setHorizontalAlignment(pdfPCell.ALIGN_JUSTIFIED);
578         return pdfPCell;
579     }
580 
581     private PdfPCell getPdfPCellInJustified(String chunk){
582         PdfPCell pdfPCell = new PdfPCell(new Paragraph(new Chunk(chunk)));
583         pdfPCell.setBorder(pdfPCell.NO_BORDER);
584         pdfPCell.setHorizontalAlignment(pdfPCell.ALIGN_JUSTIFIED);
585         return pdfPCell;
586     }
587     private PdfPCell getPdfPCellInLeft(String chunk){
588         PdfPCell pdfPCell = new PdfPCell(new Paragraph(new Chunk(chunk)));
589         pdfPCell.setBorder(pdfPCell.NO_BORDER);
590         pdfPCell.setHorizontalAlignment(pdfPCell.ALIGN_LEFT);
591         return pdfPCell;
592     }
593     public Document getDocument(float f1, float f2, float f3, float f4) {
594         Document document = new Document(PageSize.A4);
595         document.setMargins(f1, f2, f3, f4);
596         return document;
597     }
598 }
599 
600