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.bc.document.web.struts;
017
018 import java.io.ByteArrayOutputStream;
019 import java.util.ArrayList;
020 import java.util.Collection;
021 import java.util.HashMap;
022 import java.util.List;
023 import java.util.Locale;
024 import java.util.Map;
025 import java.util.ResourceBundle;
026
027 import javax.servlet.http.HttpServletRequest;
028 import javax.servlet.http.HttpServletResponse;
029
030 import net.sf.jasperreports.engine.JRParameter;
031
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.bc.BCConstants;
036 import org.kuali.kfs.module.bc.BudgetConstructionReportMode;
037 import org.kuali.kfs.module.bc.document.service.BudgetConstructionAccountMonthlyDetailReportService;
038 import org.kuali.kfs.module.bc.document.service.BudgetConstructionAccountSalaryDetailReportService;
039 import org.kuali.kfs.module.bc.document.service.BudgetConstructionDocumentAccountObjectDetailReportService;
040 import org.kuali.kfs.module.bc.document.service.BudgetConstructionReportsServiceHelper;
041 import org.kuali.kfs.module.bc.util.BudgetUrlUtil;
042 import org.kuali.kfs.sys.KFSConstants;
043 import org.kuali.kfs.sys.KFSPropertyConstants;
044 import org.kuali.kfs.sys.KFSConstants.ReportGeneration;
045 import org.kuali.kfs.sys.context.SpringContext;
046 import org.kuali.kfs.sys.service.ReportGenerationService;
047 import org.kuali.rice.kns.util.GlobalVariables;
048 import org.kuali.rice.kns.util.WebUtils;
049
050 /**
051 * Action class to display document reports and dumps menu
052 */
053 public class ReportRunnerAction extends BudgetExpansionAction {
054
055 /**
056 * Handles any special onetime setup when called from another screen action
057 *
058 * @param mapping
059 * @param form
060 * @param request
061 * @param response
062 * @return
063 * @throws Exception
064 */
065 public ActionForward loadExpansionScreen(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
066
067 ReportRunnerForm reportRunnerForm = (ReportRunnerForm) form;
068
069 return mapping.findForward(KFSConstants.MAPPING_BASIC);
070 }
071
072 /**
073 * Runs the reports or dump selected by the user using the BudgetConstructionDocumentReportMode to help determine the various
074 * objects needed to actually build the report data and render the report.
075 *
076 * @param mapping
077 * @param form
078 * @param request
079 * @param response
080 * @return
081 * @throws Exception
082 */
083 public ActionForward performReportDump(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
084
085 ReportRunnerForm reportRunnerForm = (ReportRunnerForm) form;
086 String principalName = GlobalVariables.getUserSession().getPerson().getPrincipalId();
087
088 int selectIndex = this.getSelectedLine(request);
089 String reportModeName = reportRunnerForm.getBudgetConstructionDocumentReportModes().get(selectIndex).getReportModeName();
090
091 Collection reportSet = new ArrayList();
092 String jasperFileName = null;
093 ByteArrayOutputStream baos = new ByteArrayOutputStream();
094 switch (selectIndex) {
095
096 case 0: {
097 jasperFileName = "BudgetAccountObjectDetail";
098 SpringContext.getBean(BudgetConstructionDocumentAccountObjectDetailReportService.class).updateDocumentAccountObjectDetailReportTable(principalName, reportRunnerForm.getDocumentNumber(), reportRunnerForm.getUniversityFiscalYear(), reportRunnerForm.getChartOfAccountsCode(), reportRunnerForm.getAccountNumber(), reportRunnerForm.getSubAccountNumber());
099 reportSet = SpringContext.getBean(BudgetConstructionDocumentAccountObjectDetailReportService.class).buildReports(principalName);
100 break;
101 }
102
103
104 case 1: {
105 jasperFileName = "BudgetAccountSalaryDetail";
106 reportSet = SpringContext.getBean(BudgetConstructionAccountSalaryDetailReportService.class).buildReports(reportRunnerForm.getUniversityFiscalYear(), reportRunnerForm.getChartOfAccountsCode(), reportRunnerForm.getAccountNumber(), reportRunnerForm.getSubAccountNumber());
107 break;
108 }
109
110
111 case 2: {
112 jasperFileName = "BudgetAccountMonthlyDetail";
113 reportSet = SpringContext.getBean(BudgetConstructionAccountMonthlyDetailReportService.class).buildReports(reportRunnerForm.getDocumentNumber(), reportRunnerForm.getUniversityFiscalYear(), reportRunnerForm.getChartOfAccountsCode(), reportRunnerForm.getAccountNumber(), reportRunnerForm.getSubAccountNumber());
114 break;
115 }
116 case 3: {
117 return new ActionForward(buildReportExportForwardURL(reportRunnerForm, mapping, BudgetConstructionReportMode.ACCOUNT_EXPORT.reportModeName), true);
118 }
119
120 case 4: {
121 return new ActionForward(buildReportExportForwardURL(reportRunnerForm, mapping, BudgetConstructionReportMode.FUNDING_EXPORT.reportModeName), true);
122 }
123
124 case 5: {
125 return new ActionForward(buildReportExportForwardURL(reportRunnerForm, mapping, BudgetConstructionReportMode.MONTHLY_EXPORT.reportModeName), true);
126 }
127
128 }
129
130 if (reportSet.isEmpty()) {
131 List<String> messageList = new ArrayList<String>();
132 messageList.add(BCConstants.Report.MSG_REPORT_NO_DATA);
133 SpringContext.getBean(BudgetConstructionReportsServiceHelper.class).generatePdf(messageList, baos);
134 WebUtils.saveMimeOutputStreamAsFile(response, ReportGeneration.PDF_MIME_TYPE, baos, jasperFileName + ReportGeneration.PDF_FILE_EXTENSION);
135 }
136 else {
137 ResourceBundle resourceBundle = ResourceBundle.getBundle(BCConstants.Report.REPORT_MESSAGES_CLASSPATH, Locale.getDefault());
138 Map<String, Object> reportData = new HashMap<String, Object>();
139 reportData.put(JRParameter.REPORT_RESOURCE_BUNDLE, resourceBundle);
140
141 SpringContext.getBean(ReportGenerationService.class).generateReportToOutputStream(reportData, reportSet, BCConstants.Report.REPORT_TEMPLATE_CLASSPATH + jasperFileName, baos);
142 WebUtils.saveMimeOutputStreamAsFile(response, ReportGeneration.PDF_MIME_TYPE, baos, jasperFileName + ReportGeneration.PDF_FILE_EXTENSION);
143 }
144 return null;
145 }
146
147 /**
148 * Builds URL for the report dump url.
149 */
150 private String buildReportExportForwardURL(ReportRunnerForm reportRunnerForm, ActionMapping mapping, String documentReportMode) {
151 Map<String, String> parameters = new HashMap<String, String>();
152
153 parameters.put(KFSPropertyConstants.CHART_OF_ACCOUNTS_CODE, reportRunnerForm.getChartOfAccountsCode());
154 parameters.put(KFSPropertyConstants.ACCOUNT_NUMBER, reportRunnerForm.getAccountNumber());
155 parameters.put(KFSPropertyConstants.SUB_ACCOUNT_NUMBER, reportRunnerForm.getSubAccountNumber());
156 parameters.put(BCConstants.Report.REPORT_MODE, documentReportMode);
157 parameters.put(BCConstants.IS_ORG_REPORT_REQUEST_PARAMETER, "false");
158
159 return BudgetUrlUtil.buildBudgetUrl(mapping, reportRunnerForm, BCConstants.REPORT_EXPORT_ACTION, parameters);
160 }
161 }