001 /* 002 * Copyright 2011 The Kuali Foundation. 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 package org.kuali.kfs.module.purap.document.web.struts; 017 018 import java.io.ByteArrayOutputStream; 019 020 import javax.servlet.ServletOutputStream; 021 import javax.servlet.http.HttpServletRequest; 022 import javax.servlet.http.HttpServletResponse; 023 024 import org.apache.commons.lang.StringUtils; 025 import org.apache.struts.action.ActionForm; 026 import org.apache.struts.action.ActionForward; 027 import org.apache.struts.action.ActionMapping; 028 import org.kuali.kfs.module.purap.businessobject.PurchaseOrderVendorQuote; 029 import org.kuali.kfs.module.purap.document.PurchaseOrderDocument; 030 import org.kuali.kfs.module.purap.document.authorization.DocumentInitiationException; 031 import org.kuali.kfs.module.purap.document.service.PurchaseOrderService; 032 import org.kuali.kfs.sys.KFSConstants; 033 import org.kuali.kfs.sys.KFSKeyConstants; 034 import org.kuali.kfs.sys.context.SpringContext; 035 import org.kuali.rice.kns.document.authorization.DocumentAuthorizer; 036 import org.kuali.rice.kns.service.DataDictionaryService; 037 import org.kuali.rice.kns.service.DocumentHelperService; 038 import org.kuali.rice.kns.service.DocumentService; 039 import org.kuali.rice.kns.util.GlobalVariables; 040 import org.kuali.rice.kns.web.struts.action.KualiAction; 041 042 /** 043 * Struts Action for printing Purap documents outside of a document action 044 */ 045 public class PrintAction extends KualiAction { 046 private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(PrintAction.class); 047 048 @Override 049 public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { 050 //get parameters 051 String poDocNumber = request.getParameter("poDocNumber"); 052 Integer vendorQuoteId = new Integer(request.getParameter("vendorQuoteId")); 053 if (StringUtils.isEmpty(poDocNumber) || StringUtils.isEmpty(poDocNumber)) { 054 throw new RuntimeException(); 055 } 056 057 // doc service - get this doc 058 PurchaseOrderDocument po = (PurchaseOrderDocument) SpringContext.getBean(DocumentService.class).getByDocumentHeaderId(poDocNumber); 059 DocumentAuthorizer documentAuthorizer = SpringContext.getBean(DocumentHelperService.class).getDocumentAuthorizer(po); 060 061 if (!documentAuthorizer.canInitiate(KFSConstants.FinancialDocumentTypeCodes.PURCHASE_ORDER, GlobalVariables.getUserSession().getPerson())) { 062 throw new DocumentInitiationException(KFSKeyConstants.AUTHORIZATION_ERROR_DOCUMENT, new String[]{GlobalVariables.getUserSession().getPerson().getPrincipalName(), "print", "Purchase Order"}); 063 } 064 065 // get the vendor quote 066 PurchaseOrderVendorQuote poVendorQuote = null; 067 for (PurchaseOrderVendorQuote vendorQuote : po.getPurchaseOrderVendorQuotes()) { 068 if (vendorQuote.getPurchaseOrderVendorQuoteIdentifier().equals(vendorQuoteId)) { 069 poVendorQuote = vendorQuote; 070 break; 071 } 072 } 073 074 if (poVendorQuote == null) { 075 throw new RuntimeException(); 076 } 077 078 ByteArrayOutputStream baosPDF = new ByteArrayOutputStream(); 079 poVendorQuote.setTransmitPrintDisplayed(false); 080 try { 081 StringBuffer sbFilename = new StringBuffer(); 082 sbFilename.append("PURAP_PO_QUOTE_"); 083 sbFilename.append(po.getPurapDocumentIdentifier()); 084 sbFilename.append("_"); 085 sbFilename.append(System.currentTimeMillis()); 086 sbFilename.append(".pdf"); 087 088 // call the print service 089 boolean success = SpringContext.getBean(PurchaseOrderService.class).printPurchaseOrderQuotePDF(po, poVendorQuote, baosPDF); 090 091 if (!success) { 092 poVendorQuote.setTransmitPrintDisplayed(true); 093 if (baosPDF != null) { 094 baosPDF.reset(); 095 } 096 return mapping.findForward(KFSConstants.MAPPING_BASIC); 097 } 098 response.setHeader("Cache-Control", "max-age=30"); 099 response.setContentType("application/pdf"); 100 StringBuffer sbContentDispValue = new StringBuffer(); 101 // sbContentDispValue.append("inline"); 102 sbContentDispValue.append("attachment"); 103 sbContentDispValue.append("; filename="); 104 sbContentDispValue.append(sbFilename); 105 106 response.setHeader("Content-disposition", sbContentDispValue.toString()); 107 108 response.setContentLength(baosPDF.size()); 109 110 ServletOutputStream sos; 111 112 sos = response.getOutputStream(); 113 114 baosPDF.writeTo(sos); 115 116 sos.flush(); 117 118 } 119 finally { 120 if (baosPDF != null) { 121 baosPDF.reset(); 122 } 123 } 124 125 return null; 126 } 127 128 } 129