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.ar.web.struts; 017 018 import java.io.ByteArrayOutputStream; 019 import java.io.File; 020 import java.util.ArrayList; 021 import java.util.HashMap; 022 import java.util.List; 023 import java.util.Set; 024 025 import javax.servlet.ServletOutputStream; 026 import javax.servlet.http.HttpServletRequest; 027 import javax.servlet.http.HttpServletResponse; 028 029 import org.apache.commons.lang.StringUtils; 030 import org.apache.struts.action.ActionForm; 031 import org.apache.struts.action.ActionForward; 032 import org.apache.struts.action.ActionMapping; 033 import org.kuali.kfs.module.ar.report.service.AccountsReceivableReportService; 034 import org.kuali.kfs.sys.KFSConstants; 035 import org.kuali.kfs.sys.context.SpringContext; 036 import org.kuali.rice.kns.web.struts.action.KualiAction; 037 038 import com.lowagie.text.Document; 039 import com.lowagie.text.pdf.PdfCopy; 040 import com.lowagie.text.pdf.PdfImportedPage; 041 import com.lowagie.text.pdf.PdfReader; 042 import com.lowagie.text.pdf.SimpleBookmark; 043 044 /** 045 * This class handles Actions for lookup flow 046 */ 047 048 public class CustomerStatementAction extends KualiAction { 049 private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(CustomerStatementAction.class); 050 051 public CustomerStatementAction() { 052 super(); 053 } 054 055 public ActionForward start(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { 056 return mapping.findForward(KFSConstants.MAPPING_BASIC); 057 } 058 059 public ActionForward clear(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { 060 CustomerStatementForm csForm = (CustomerStatementForm)form; 061 csForm.clear(); 062 return mapping.findForward(KFSConstants.MAPPING_BASIC); 063 } 064 065 public ActionForward cancel(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { 066 return mapping.findForward(KFSConstants.MAPPING_BASIC); 067 } 068 069 public ActionForward print(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { 070 String basePath = getBasePath(request); 071 CustomerStatementForm csForm = (CustomerStatementForm) form; 072 String chartCode = csForm.getChartCode(); 073 String orgCode = csForm.getOrgCode(); 074 String customerNumber = csForm.getCustomerNumber(); 075 String accountNumber = csForm.getAccountNumber(); 076 077 HashMap<String, String> params = new HashMap<String, String>(); 078 if(!StringUtils.isBlank(chartCode)) { 079 params.put("chartCode", chartCode); 080 } 081 if(!StringUtils.isBlank(orgCode)) { 082 params.put("orgCode", orgCode); 083 } 084 if(!StringUtils.isBlank(customerNumber)) { 085 params.put("customerNumber", customerNumber); 086 } 087 if(!StringUtils.isBlank(accountNumber)) { 088 params.put("accountNumber", accountNumber); 089 } 090 091 092 String methodToCallPrintPDF = "printStatementPDF"; 093 String methodToCallStart = "start"; 094 String printPDFUrl = getUrlForPrintStatement(basePath, methodToCallPrintPDF, params); 095 String displayTabbedPageUrl = getUrlForPrintStatement(basePath, methodToCallStart, params); 096 097 request.setAttribute("printPDFUrl", printPDFUrl); 098 request.setAttribute("displayTabbedPageUrl", displayTabbedPageUrl); 099 if(!StringUtils.isBlank(chartCode)) { 100 request.setAttribute("chartCode", chartCode); 101 } 102 if(!StringUtils.isBlank(orgCode)) { 103 request.setAttribute("orgCode", orgCode); 104 } 105 if(!StringUtils.isBlank(customerNumber)) { 106 request.setAttribute("customerNumber", customerNumber); 107 } 108 if(!StringUtils.isBlank(accountNumber)) { 109 request.setAttribute("accountNumber", accountNumber); 110 } 111 request.setAttribute("printLabel", "Customer Statement"); 112 return mapping.findForward("arPrintPDF"); 113 114 } 115 116 public ActionForward printStatementPDF(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { 117 118 CustomerStatementForm csForm = (CustomerStatementForm)form; 119 String chartCode = request.getParameter("chartCode"); 120 chartCode = chartCode==null?"":chartCode; 121 String orgCode = request.getParameter("orgCode"); 122 orgCode = orgCode==null?"":orgCode; 123 String customerNumber = request.getParameter("customerNumber"); 124 customerNumber = customerNumber==null?"":customerNumber; 125 String accountNumber = request.getParameter("accountNumber"); 126 accountNumber = accountNumber==null?"":accountNumber; 127 AccountsReceivableReportService reportService = SpringContext.getBean(AccountsReceivableReportService.class); 128 List<File> reports = new ArrayList<File>(); 129 130 StringBuilder fileName = new StringBuilder(); 131 String contentDisposition = ""; 132 133 if ( !StringUtils.isBlank(chartCode) && !StringUtils.isBlank(orgCode)) { 134 reports = reportService.generateStatementByBillingOrg(chartCode, orgCode); 135 fileName.append(chartCode); 136 fileName.append(orgCode); 137 } else if (!StringUtils.isBlank(customerNumber)) { 138 reports = reportService.generateStatementByCustomer(customerNumber.toUpperCase()); 139 fileName.append(customerNumber); 140 } else if (!StringUtils.isBlank(accountNumber)) { 141 reports = reportService.generateStatementByAccount(accountNumber); 142 fileName.append(accountNumber); 143 } 144 if (reports.size() !=0) { 145 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 146 try { 147 int pageOffset = 0; 148 ArrayList master = new ArrayList(); 149 int f = 0; 150 // File file = new File(fileName); 151 Document document = null; 152 PdfCopy writer = null; 153 for (File file : reports) { 154 // we create a reader for a certain document 155 String reportName = file.getAbsolutePath(); 156 PdfReader reader = new PdfReader(reportName); 157 reader.consolidateNamedDestinations(); 158 // we retrieve the total number of pages 159 int n = reader.getNumberOfPages(); 160 List bookmarks = SimpleBookmark.getBookmark(reader); 161 if (bookmarks != null) { 162 if (pageOffset != 0) { 163 SimpleBookmark.shiftPageNumbers(bookmarks, pageOffset, null); 164 } 165 master.addAll(bookmarks); 166 } 167 pageOffset += n; 168 169 if (f == 0) { 170 // step 1: creation of a document-object 171 document = new Document(reader.getPageSizeWithRotation(1)); 172 // step 2: we create a writer that listens to the document 173 writer = new PdfCopy(document, baos); 174 // step 3: we open the document 175 document.open(); 176 } 177 // step 4: we add content 178 PdfImportedPage page; 179 for (int i = 0; i < n; ) { 180 ++i; 181 page = writer.getImportedPage(reader, i); 182 writer.addPage(page); 183 } 184 writer.freeReader(reader); 185 f++; 186 } 187 if (!master.isEmpty()) 188 writer.setOutlines(master); 189 // step 5: we close the document 190 191 document.close(); 192 // csForm.setReports(file); 193 194 StringBuffer sbContentDispValue = new StringBuffer(); 195 String useJavascript = request.getParameter("useJavascript"); 196 if (useJavascript == null || useJavascript.equalsIgnoreCase("false")) { 197 sbContentDispValue.append("attachment"); 198 } 199 else { 200 sbContentDispValue.append("inline"); 201 } 202 sbContentDispValue.append("; filename="); 203 sbContentDispValue.append(fileName); 204 205 contentDisposition = sbContentDispValue.toString(); 206 } 207 catch(Exception e) { 208 e.printStackTrace(); 209 } 210 211 fileName.append("-StatementBatchPDFs.pdf"); 212 213 response.setContentType("application/pdf"); 214 response.setHeader("Content-disposition", contentDisposition); 215 response.setHeader("Expires", "0"); 216 response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0"); 217 response.setHeader("Pragma", "public"); 218 response.setContentLength(baos.size()); 219 220 // write to output 221 ServletOutputStream sos = response.getOutputStream(); 222 baos.writeTo(sos); 223 sos.flush(); 224 sos.close(); 225 226 return null; 227 } 228 csForm.setMessage("No Reports Generated"); 229 return mapping.findForward(KFSConstants.MAPPING_BASIC); 230 } 231 /** 232 * Creates a URL to be used in printing the purchase order. 233 * 234 * @param basePath String: The base path of the current URL 235 * @param methodToCall String: The name of the method that will be invoked to do this particular print 236 * @return The URL 237 */ 238 private String getUrlForPrintStatement(String basePath, String methodToCall, HashMap<String, String> params) { 239 StringBuffer result = new StringBuffer(basePath); 240 result.append("/arCustomerStatement.do?methodToCall=").append(methodToCall); 241 Set<String> keys = params.keySet(); 242 for(String key : keys) { 243 result.append("&").append(key).append("=").append(params.get(key)); 244 } 245 246 return result.toString(); 247 } 248 249 }