001/* 002 * Copyright 2006-2008 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 */ 016package org.kuali.ole.module.purap.document.web.struts; 017 018import org.apache.commons.lang.StringUtils; 019import org.apache.struts.action.ActionForm; 020import org.apache.struts.action.ActionForward; 021import org.apache.struts.action.ActionMapping; 022import org.kuali.ole.module.purap.businessobject.PurchaseOrderVendorQuote; 023import org.kuali.ole.module.purap.document.PurchaseOrderDocument; 024import org.kuali.ole.module.purap.document.authorization.DocumentInitiationException; 025import org.kuali.ole.module.purap.document.service.PurchaseOrderService; 026import org.kuali.ole.sys.OLEConstants; 027import org.kuali.ole.sys.OLEKeyConstants; 028import org.kuali.ole.sys.context.SpringContext; 029import org.kuali.rice.kns.document.authorization.DocumentAuthorizer; 030import org.kuali.rice.kns.service.DocumentHelperService; 031import org.kuali.rice.kns.web.struts.action.KualiAction; 032import org.kuali.rice.krad.service.DocumentService; 033import org.kuali.rice.krad.util.GlobalVariables; 034 035import javax.servlet.ServletOutputStream; 036import javax.servlet.http.HttpServletRequest; 037import javax.servlet.http.HttpServletResponse; 038import java.io.ByteArrayOutputStream; 039 040/** 041 * Struts Action for printing Purap documents outside of a document action 042 */ 043public class PrintAction extends KualiAction { 044 private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(PrintAction.class); 045 046 @Override 047 public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { 048 //get parameters 049 String poDocNumber = request.getParameter("poDocNumber"); 050 Integer vendorQuoteId = new Integer(request.getParameter("vendorQuoteId")); 051 if (StringUtils.isEmpty(poDocNumber) || StringUtils.isEmpty(poDocNumber)) { 052 throw new RuntimeException(); 053 } 054 055 // doc service - get this doc 056 PurchaseOrderDocument po = (PurchaseOrderDocument) SpringContext.getBean(DocumentService.class).getByDocumentHeaderId(poDocNumber); 057 DocumentAuthorizer documentAuthorizer = SpringContext.getBean(DocumentHelperService.class).getDocumentAuthorizer(po); 058 059 if (!documentAuthorizer.canInitiate(OLEConstants.FinancialDocumentTypeCodes.PURCHASE_ORDER, GlobalVariables.getUserSession().getPerson())) { 060 throw new DocumentInitiationException(OLEKeyConstants.AUTHORIZATION_ERROR_DOCUMENT, new String[]{GlobalVariables.getUserSession().getPerson().getPrincipalName(), "print", "Purchase Order"}); 061 } 062 063 // get the vendor quote 064 PurchaseOrderVendorQuote poVendorQuote = null; 065 for (PurchaseOrderVendorQuote vendorQuote : po.getPurchaseOrderVendorQuotes()) { 066 if (vendorQuote.getPurchaseOrderVendorQuoteIdentifier().equals(vendorQuoteId)) { 067 poVendorQuote = vendorQuote; 068 break; 069 } 070 } 071 072 if (poVendorQuote == null) { 073 throw new RuntimeException(); 074 } 075 076 ByteArrayOutputStream baosPDF = new ByteArrayOutputStream(); 077 poVendorQuote.setTransmitPrintDisplayed(false); 078 try { 079 StringBuffer sbFilename = new StringBuffer(); 080 sbFilename.append("PURAP_PO_QUOTE_"); 081 sbFilename.append(po.getPurapDocumentIdentifier()); 082 sbFilename.append("_"); 083 sbFilename.append(System.currentTimeMillis()); 084 sbFilename.append(".pdf"); 085 086 // call the print service 087 boolean success = SpringContext.getBean(PurchaseOrderService.class).printPurchaseOrderQuotePDF(po, poVendorQuote, baosPDF); 088 089 if (!success) { 090 poVendorQuote.setTransmitPrintDisplayed(true); 091 if (baosPDF != null) { 092 baosPDF.reset(); 093 } 094 return mapping.findForward(OLEConstants.MAPPING_BASIC); 095 } 096 response.setHeader("Cache-Control", "max-age=30"); 097 response.setContentType("application/pdf"); 098 StringBuffer sbContentDispValue = new StringBuffer(); 099 // sbContentDispValue.append("inline"); 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