View Javadoc
1   /*
2    * The Kuali Financial System, a comprehensive financial management system for higher education.
3    * 
4    * Copyright 2005-2014 The Kuali Foundation
5    * 
6    * This program is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU Affero General Public License as
8    * published by the Free Software Foundation, either version 3 of the
9    * License, or (at your option) any later version.
10   * 
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU Affero General Public License for more details.
15   * 
16   * You should have received a copy of the GNU Affero General Public License
17   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  package org.kuali.kfs.module.ar.web.struts;
20  
21  import java.io.ByteArrayOutputStream;
22  import java.io.File;
23  import java.nio.file.Files;
24  import java.util.HashMap;
25  import java.util.Map;
26  
27  import javax.servlet.http.HttpServletRequest;
28  import javax.servlet.http.HttpServletResponse;
29  
30  import org.apache.commons.lang.StringUtils;
31  import org.apache.struts.action.ActionForm;
32  import org.apache.struts.action.ActionForward;
33  import org.apache.struts.action.ActionMapping;
34  import org.kuali.kfs.integration.cg.ContractsAndGrantsBillingAgency;
35  import org.kuali.kfs.integration.cg.ContractsAndGrantsBillingAward;
36  import org.kuali.kfs.module.ar.ArConstants;
37  import org.kuali.kfs.module.ar.ArPropertyConstants;
38  import org.kuali.kfs.module.ar.report.service.ContractsGrantsInvoiceReportService;
39  import org.kuali.kfs.module.ar.report.service.FederalFinancialReportService;
40  import org.kuali.kfs.module.ar.service.AccountsReceivablePdfHelperService;
41  import org.kuali.kfs.sys.KFSConstants;
42  import org.kuali.kfs.sys.KFSPropertyConstants;
43  import org.kuali.kfs.sys.context.SpringContext;
44  import org.kuali.kfs.sys.util.KfsWebUtils;
45  import org.kuali.rice.kns.web.struts.action.KualiAction;
46  import org.kuali.rice.krad.service.KualiModuleService;
47  import org.kuali.rice.krad.util.ObjectUtils;
48  
49  /**
50   * Action class for Federal Financial Report service.
51   */
52  public class FederalFinancialReportAction extends KualiAction {
53      private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(FederalFinancialReportAction.class);
54  
55      private static volatile FederalFinancialReportService federalFinancialReportService;
56  
57      /**
58       * @param mapping
59       * @param form
60       * @param request
61       * @param response
62       * @return
63       * @throws Exception
64       */
65      public ActionForward start(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
66          return mapping.findForward(KFSConstants.MAPPING_BASIC);
67      }
68  
69      /**
70       * @param mapping
71       * @param form
72       * @param request
73       * @param response
74       * @return
75       * @throws Exception
76       */
77      public ActionForward cancel(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
78          return mapping.findForward(KFSConstants.MAPPING_CANCEL);
79      }
80  
81      /**
82       * @param mapping
83       * @param form
84       * @param request
85       * @param response
86       * @return
87       * @throws Exception
88       */
89      public ActionForward clear(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
90          FederalFinancialReportForm ffrForm = (FederalFinancialReportForm) form;
91          ffrForm.setReportingPeriod(null);
92          ffrForm.setFiscalYear(null);
93          ffrForm.setProposalNumber(null);
94          return mapping.findForward(KFSConstants.MAPPING_BASIC);
95      }
96  
97      /**
98       * This method receives the print action and forwards it accordingly.
99       *
100      * @param mapping
101      * @param form
102      * @param request
103      * @param response
104      * @return
105      * @throws Exception
106      */
107     public ActionForward print(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
108         FederalFinancialReportForm ffrForm = (FederalFinancialReportForm) form;
109         String message = getFederalFinancialReportService().validate(ffrForm.getFederalForm(), ffrForm.getProposalNumber(), ffrForm.getFiscalYear(), ffrForm.getReportingPeriod(), ffrForm.getAgencyNumber());
110         if (StringUtils.isEmpty(message)) {
111             String basePath = getApplicationBaseUrl();
112             String docId = ffrForm.getProposalNumber();
113             String agencyNumber = ffrForm.getAgencyNumber();
114             String formType = ffrForm.getFederalForm();
115             String period = ffrForm.getReportingPeriod();
116             String year = ffrForm.getFiscalYear();
117 
118             String printInvoicePDFUrl = getFederalFinancialReportService().getUrlForPrintInvoice(basePath, docId, period, year, agencyNumber, formType, ArConstants.PRINT_INVOICE_PDF_METHOD);
119             String displayInvoiceTabbedPageUrl = getFederalFinancialReportService().getUrlForPrintInvoice(basePath, "", "", "", "", "", KFSConstants.START_METHOD);
120 
121             request.setAttribute(ArPropertyConstants.PRINT_PDF_URL, printInvoicePDFUrl);
122             request.setAttribute(ArPropertyConstants.DISPLAY_TABBED_PAGE_URL, displayInvoiceTabbedPageUrl);
123             return mapping.findForward(ArConstants.MAPPING_PRINT_PDF);
124         }
125         else {
126             ffrForm.setError(message);
127             return mapping.findForward(KFSConstants.MAPPING_BASIC);
128         }
129     }
130 
131     /**
132      * This method generates the pdf file and provides it to the user to Print it.
133      *
134      * @param mapping
135      * @param form
136      * @param request
137      * @param response
138      * @return
139      * @throws Exception
140      */
141     public ActionForward printInvoicePDF(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
142         String proposalNumber = request.getParameter(KFSConstants.PARAMETER_DOC_ID);
143         String period = request.getParameter(FederalFinancialReportService.REPORTING_PERIOD);
144         String year = request.getParameter(FederalFinancialReportService.FISCAL_YEAR);
145         String formType = request.getParameter(FederalFinancialReportService.FEDERAL_FORM);
146         String agencyNumber = request.getParameter(KFSPropertyConstants.AGENCY_NUMBER);
147         Map<String, Object> map = new HashMap<String, Object>();
148         map.put(KFSPropertyConstants.PROPOSAL_NUMBER, proposalNumber);
149         ContractsAndGrantsBillingAward award = SpringContext.getBean(KualiModuleService.class).getResponsibleModuleService(ContractsAndGrantsBillingAward.class).getExternalizableBusinessObject(ContractsAndGrantsBillingAward.class, map);
150         map = new HashMap<String, Object>();
151         map.put(KFSPropertyConstants.AGENCY_NUMBER, agencyNumber);
152         ContractsAndGrantsBillingAgency agency = SpringContext.getBean(KualiModuleService.class).getResponsibleModuleService(ContractsAndGrantsBillingAgency.class).getExternalizableBusinessObject(ContractsAndGrantsBillingAgency.class, map);
153 
154         if (ObjectUtils.isNotNull(award) || ObjectUtils.isNotNull(agency)) {
155             ContractsGrantsInvoiceReportService reportService = SpringContext.getBean(ContractsGrantsInvoiceReportService.class);
156             File report = reportService.generateFederalFinancialForm(award, period, year, formType, agency);
157 
158             byte[] content = Files.readAllBytes(report.toPath());
159             ByteArrayOutputStream baos = SpringContext.getBean(AccountsReceivablePdfHelperService.class).buildPdfOutputStream(content);
160 
161             StringBuilder fileName = new StringBuilder();
162             fileName.append(formType);
163             fileName.append(KFSConstants.DASH);
164             fileName.append(period);
165             fileName.append(KFSConstants.DASH);
166             if (StringUtils.equals(formType, ArConstants.FEDERAL_FORM_425)) {
167                 fileName.append(proposalNumber);
168             } else {
169                 fileName.append(agencyNumber);
170             }
171             fileName.append(KFSConstants.ReportGeneration.PDF_FILE_EXTENSION);
172 
173             KfsWebUtils.saveMimeOutputStreamAsFile(response, KFSConstants.ReportGeneration.PDF_MIME_TYPE, baos, fileName.toString(), Boolean.parseBoolean(request.getParameter(KFSConstants.ReportGeneration.USE_JAVASCRIPT)));
174         }
175         return null;
176 
177     }
178 
179     public FederalFinancialReportService getFederalFinancialReportService() {
180         if (federalFinancialReportService == null) {
181             federalFinancialReportService = SpringContext.getBean(FederalFinancialReportService.class);
182         }
183         return federalFinancialReportService;
184     }
185 }