1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
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
59
60
61
62
63
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
71
72
73
74
75
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
83
84
85
86
87
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
99
100
101
102
103
104
105
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
133
134
135
136
137
138
139
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 }