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.endow.web.struts; 017 018 import java.io.BufferedInputStream; 019 import java.io.ByteArrayOutputStream; 020 import java.io.FileOutputStream; 021 import java.io.OutputStream; 022 import java.util.ArrayList; 023 import java.util.List; 024 import java.util.zip.ZipEntry; 025 import java.util.zip.ZipOutputStream; 026 027 import javax.servlet.ServletOutputStream; 028 import javax.servlet.http.HttpServletRequest; 029 import javax.servlet.http.HttpServletResponse; 030 031 import org.apache.commons.lang.StringUtils; 032 import org.apache.struts.action.ActionForm; 033 import org.apache.struts.action.ActionForward; 034 import org.apache.struts.action.ActionMapping; 035 import org.kuali.kfs.module.endow.report.service.TrialBalanceReportService; 036 import org.kuali.kfs.module.endow.report.util.EndowmentReportHeaderDataHolder; 037 import org.kuali.kfs.module.endow.report.util.TrialBalanceReportDataHolder; 038 import org.kuali.kfs.module.endow.report.util.TrialBalanceReportPrint; 039 import org.kuali.kfs.sys.KFSConstants; 040 import org.kuali.kfs.sys.context.SpringContext; 041 import org.kuali.rice.kns.util.WebUtils; 042 043 public class TrialBalanceAction extends EndowmentReportBaseAction { 044 045 private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(TrialBalanceAction.class); 046 047 private final String REPORT_NAME = "Trial Balance"; 048 private final String REPORT_FILE_NAME = "TrialBalanceReport.pdf"; 049 050 public TrialBalanceAction() { 051 super(); 052 } 053 054 /** 055 * Directs to the start page 056 * 057 * @param mapping 058 * @param form 059 * @param request 060 * @param response 061 * @return 062 * @throws Exception 063 */ 064 public ActionForward start(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { 065 return mapping.findForward(KFSConstants.MAPPING_BASIC); 066 } 067 068 /** 069 * Clears the form when the "clear" button is pressed 070 * 071 * @param mapping 072 * @param form 073 * @param request 074 * @param response 075 * @return 076 * @throws Exception 077 */ 078 public ActionForward clear(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { 079 TrialBalanceForm trialBalanceForm = (TrialBalanceForm) form; 080 trialBalanceForm.clear(); 081 return mapping.findForward(KFSConstants.MAPPING_BASIC); 082 } 083 084 /** 085 * Cancels the current page and goes to the start page 086 * 087 * @param mapping 088 * @param form 089 * @param request 090 * @param response 091 * @return 092 * @throws Exception 093 */ 094 public ActionForward cancel(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { 095 return mapping.findForward(KFSConstants.MAPPING_BASIC); 096 } 097 098 /** 099 * Generates Trial Balance in the PDF form 100 * 101 * @param mapping 102 * @param form 103 * @param request 104 * @param response 105 * @return 106 * @throws Exception 107 */ 108 public ActionForward print(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { 109 110 TrialBalanceReportService trialBalanceReportService = SpringContext.getBean(TrialBalanceReportService.class); 111 112 // get all the value strings from the form 113 TrialBalanceForm trialBalanceForm = (TrialBalanceForm) form; 114 String kemids = trialBalanceForm.getKemid(); 115 String benefittingOrganziationCampuses = trialBalanceForm.getBenefittingOrganziationCampus(); 116 String benefittingOrganziationCharts = trialBalanceForm.getBenefittingOrganziationChart(); 117 String benefittingOrganziations = trialBalanceForm.getBenefittingOrganziation(); 118 String typeCodes = trialBalanceForm.getTypeCode(); 119 String purposeCodes = trialBalanceForm.getPurposeCode(); 120 String combineGroupCodes = trialBalanceForm.getCombineGroupCode(); 121 String asOfDate = trialBalanceForm.getAsOfDate(); 122 String endowmentOption = trialBalanceForm.getEndowmentOption(); 123 String listKemidsInHeader = trialBalanceForm.getListKemidsInHeader(); 124 String closedIndicator = trialBalanceForm.getClosedIndicator(); 125 String message = trialBalanceForm.getMessage(); 126 127 List<TrialBalanceReportDataHolder> trialBalanceReportDataHolders = null; 128 129 /* 130 * Creates the report data based on the selected criteria. 131 * The criteria are selected as follows. 132 * 1. Kemid and the other criteria cannot be selected at the same time. 133 * 2. If none of them are selected, all kemids will be selected. 134 * 3. The other criteria other than kemid are AND-combined. 135 * 4. All the criteria in the text input can be multiple by the use of wild card or the separator ('&' for kemid, ',' for the others) 136 */ 137 if (StringUtils.isNotBlank(kemids)) { 138 139 if (( StringUtils.isNotBlank(benefittingOrganziationCampuses) 140 || StringUtils.isNotBlank(benefittingOrganziationCharts) 141 || StringUtils.isNotBlank(benefittingOrganziations) 142 || StringUtils.isNotBlank(typeCodes) 143 || StringUtils.isNotBlank(purposeCodes) 144 || StringUtils.isNotBlank(combineGroupCodes) )) { 145 146 // kemid and the other criteria cannot be selected at the same time 147 trialBalanceForm.setMessage(ERROR_REPORT_KEMID_WITH_OTHER_CRITERIA); 148 return mapping.findForward(KFSConstants.MAPPING_BASIC); 149 150 } else { 151 // by kemid only 152 List<String> kemidList = parseValueString(kemids, KEMID_SEPERATOR); 153 trialBalanceReportDataHolders = trialBalanceReportService.getTrialBalanceReportsByKemidByIds(kemidList, endowmentOption, closedIndicator); 154 } 155 } else { 156 if (( StringUtils.isBlank(benefittingOrganziationCampuses) 157 && StringUtils.isBlank(benefittingOrganziationCharts) 158 && StringUtils.isBlank(benefittingOrganziations) 159 && StringUtils.isBlank(typeCodes) 160 && StringUtils.isBlank(purposeCodes) 161 && StringUtils.isBlank(combineGroupCodes) )) { 162 163 // for all kemids 164 trialBalanceReportDataHolders = trialBalanceReportService.getTrialBalanceReportForAllKemids(endowmentOption, closedIndicator); 165 166 } else { 167 // by other criteria 168 trialBalanceReportDataHolders = trialBalanceReportService.getTrialBalanceReportsByOtherCriteria( 169 parseValueString(benefittingOrganziationCampuses, OTHER_CRITERIA_SEPERATOR), 170 parseValueString(benefittingOrganziationCharts, OTHER_CRITERIA_SEPERATOR), 171 parseValueString(benefittingOrganziations, OTHER_CRITERIA_SEPERATOR), 172 parseValueString(typeCodes, OTHER_CRITERIA_SEPERATOR), 173 parseValueString(purposeCodes, OTHER_CRITERIA_SEPERATOR), 174 parseValueString(combineGroupCodes, OTHER_CRITERIA_SEPERATOR), 175 endowmentOption, 176 closedIndicator); 177 } 178 } 179 180 // Check to see if you have something to print 181 if (trialBalanceReportDataHolders != null && !trialBalanceReportDataHolders.isEmpty()) { 182 183 // prepare the header sheet data 184 EndowmentReportHeaderDataHolder reportRequestHeaderDataHolder = trialBalanceReportService.createReportHeaderSheetData( 185 getKemidsSelected(trialBalanceReportDataHolders), 186 parseValueString(benefittingOrganziationCampuses, OTHER_CRITERIA_SEPERATOR), 187 parseValueString(benefittingOrganziationCharts, OTHER_CRITERIA_SEPERATOR), 188 parseValueString(benefittingOrganziations, OTHER_CRITERIA_SEPERATOR), 189 parseValueString(typeCodes, OTHER_CRITERIA_SEPERATOR), 190 parseValueString(purposeCodes, OTHER_CRITERIA_SEPERATOR), 191 parseValueString(combineGroupCodes, OTHER_CRITERIA_SEPERATOR), 192 REPORT_NAME, 193 endowmentOption, 194 null); 195 196 // generate the report in PDF 197 ByteArrayOutputStream pdfStream = new TrialBalanceReportPrint().printTrialBalanceReport(reportRequestHeaderDataHolder, trialBalanceReportDataHolders, listKemidsInHeader); 198 if (pdfStream != null) { 199 trialBalanceForm.setMessage("Reports Generated"); 200 WebUtils.saveMimeOutputStreamAsFile(response, "application/pdf", pdfStream, REPORT_FILE_NAME); 201 return null; 202 } 203 } 204 205 // No report was generated 206 if (StringUtils.isBlank(kemids)) { 207 trialBalanceForm.setMessage("Report was not generated."); 208 } else { 209 trialBalanceForm.setMessage("Report was not generated for " + kemids + "."); 210 } 211 212 return mapping.findForward(KFSConstants.MAPPING_BASIC); 213 } 214 215 /** 216 * Retrieves all the kemids used for the report 217 * 218 * @param trialBalanceReportDataHolder 219 * @return 220 */ 221 protected List<String> getKemidsSelected(List<TrialBalanceReportDataHolder> trialBalanceReportDataHolder) { 222 List<String> kemids = new ArrayList<String>(); 223 for (TrialBalanceReportDataHolder dataHolder : trialBalanceReportDataHolder) { 224 kemids.add(dataHolder.getKemid()); 225 } 226 return kemids; 227 } 228 229 }