1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.ole.module.purap.document.web.struts;
17
18 import org.apache.commons.lang.StringUtils;
19 import org.apache.struts.action.ActionForm;
20 import org.apache.struts.action.ActionForward;
21 import org.apache.struts.action.ActionMapping;
22 import org.kuali.ole.module.purap.businessobject.PurchaseOrderVendorQuote;
23 import org.kuali.ole.module.purap.document.PurchaseOrderDocument;
24 import org.kuali.ole.module.purap.document.authorization.DocumentInitiationException;
25 import org.kuali.ole.module.purap.document.service.PurchaseOrderService;
26 import org.kuali.ole.sys.OLEConstants;
27 import org.kuali.ole.sys.OLEKeyConstants;
28 import org.kuali.ole.sys.context.SpringContext;
29 import org.kuali.rice.kns.document.authorization.DocumentAuthorizer;
30 import org.kuali.rice.kns.service.DocumentHelperService;
31 import org.kuali.rice.kns.web.struts.action.KualiAction;
32 import org.kuali.rice.krad.service.DocumentService;
33 import org.kuali.rice.krad.util.GlobalVariables;
34
35 import javax.servlet.ServletOutputStream;
36 import javax.servlet.http.HttpServletRequest;
37 import javax.servlet.http.HttpServletResponse;
38 import java.io.ByteArrayOutputStream;
39
40
41
42
43 public class PrintAction extends KualiAction {
44 private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(PrintAction.class);
45
46 @Override
47 public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
48
49 String poDocNumber = request.getParameter("poDocNumber");
50 Integer vendorQuoteId = new Integer(request.getParameter("vendorQuoteId"));
51 if (StringUtils.isEmpty(poDocNumber) || StringUtils.isEmpty(poDocNumber)) {
52 throw new RuntimeException();
53 }
54
55
56 PurchaseOrderDocument po = (PurchaseOrderDocument) SpringContext.getBean(DocumentService.class).getByDocumentHeaderId(poDocNumber);
57 DocumentAuthorizer documentAuthorizer = SpringContext.getBean(DocumentHelperService.class).getDocumentAuthorizer(po);
58
59 if (!documentAuthorizer.canInitiate(OLEConstants.FinancialDocumentTypeCodes.PURCHASE_ORDER, GlobalVariables.getUserSession().getPerson())) {
60 throw new DocumentInitiationException(OLEKeyConstants.AUTHORIZATION_ERROR_DOCUMENT, new String[]{GlobalVariables.getUserSession().getPerson().getPrincipalName(), "print", "Purchase Order"});
61 }
62
63
64 PurchaseOrderVendorQuote poVendorQuote = null;
65 for (PurchaseOrderVendorQuote vendorQuote : po.getPurchaseOrderVendorQuotes()) {
66 if (vendorQuote.getPurchaseOrderVendorQuoteIdentifier().equals(vendorQuoteId)) {
67 poVendorQuote = vendorQuote;
68 break;
69 }
70 }
71
72 if (poVendorQuote == null) {
73 throw new RuntimeException();
74 }
75
76 ByteArrayOutputStream baosPDF = new ByteArrayOutputStream();
77 poVendorQuote.setTransmitPrintDisplayed(false);
78 try {
79 StringBuffer sbFilename = new StringBuffer();
80 sbFilename.append("PURAP_PO_QUOTE_");
81 sbFilename.append(po.getPurapDocumentIdentifier());
82 sbFilename.append("_");
83 sbFilename.append(System.currentTimeMillis());
84 sbFilename.append(".pdf");
85
86
87 boolean success = SpringContext.getBean(PurchaseOrderService.class).printPurchaseOrderQuotePDF(po, poVendorQuote, baosPDF);
88
89 if (!success) {
90 poVendorQuote.setTransmitPrintDisplayed(true);
91 if (baosPDF != null) {
92 baosPDF.reset();
93 }
94 return mapping.findForward(OLEConstants.MAPPING_BASIC);
95 }
96 response.setHeader("Cache-Control", "max-age=30");
97 response.setContentType("application/pdf");
98 StringBuffer sbContentDispValue = new StringBuffer();
99
100 sbContentDispValue.append("attachment");
101 sbContentDispValue.append("; filename=");
102 sbContentDispValue.append(sbFilename);
103
104 response.setHeader("Content-disposition", sbContentDispValue.toString());
105
106 response.setContentLength(baosPDF.size());
107
108 ServletOutputStream sos;
109
110 sos = response.getOutputStream();
111
112 baosPDF.writeTo(sos);
113
114 sos.flush();
115
116 } finally {
117 if (baosPDF != null) {
118 baosPDF.reset();
119 }
120 }
121
122 return null;
123 }
124
125 }
126