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.ByteArrayOutputStream;
019 import java.text.ParseException;
020 import java.text.SimpleDateFormat;
021 import java.util.ArrayList;
022 import java.util.List;
023
024 import javax.servlet.http.HttpServletRequest;
025 import javax.servlet.http.HttpServletResponse;
026
027 import org.apache.commons.lang.StringUtils;
028 import org.apache.struts.action.ActionForm;
029 import org.apache.struts.action.ActionForward;
030 import org.apache.struts.action.ActionMapping;
031 import org.kuali.kfs.module.endow.report.service.TransactionStatementReportService;
032 import org.kuali.kfs.module.endow.report.util.EndowmentReportHeaderDataHolder;
033 import org.kuali.kfs.module.endow.report.util.TransactionStatementReportDataHolder;
034 import org.kuali.kfs.module.endow.report.util.TransactionStatementReportPrint;
035 import org.kuali.kfs.sys.KFSConstants;
036 import org.kuali.kfs.sys.context.SpringContext;
037 import org.kuali.rice.kns.util.WebUtils;
038
039 public class TransactionStatementAction extends EndowmentReportBaseAction {
040
041 private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(TransactionStatementAction.class);
042
043 private final String REPORT_NAME = "Transaction Statement";
044 private final String REPORT_FILE_NAME = "TransactionStatementReport.pdf";
045
046 public TransactionStatementAction() {
047 super();
048 }
049
050 /**
051 * Directs to the start page
052 *
053 * @param mapping
054 * @param form
055 * @param request
056 * @param response
057 * @return
058 * @throws Exception
059 */
060 public ActionForward start(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
061 TransactionStatementForm transactionStatementForm = (TransactionStatementForm)form;
062 return mapping.findForward(KFSConstants.MAPPING_BASIC);
063 }
064
065 /**
066 * Clears the form when the "clear" button is pressed
067 *
068 * @param mapping
069 * @param form
070 * @param request
071 * @param response
072 * @return
073 * @throws Exception
074 */
075 public ActionForward clear(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
076 TransactionStatementForm transactionStatementForm = (TransactionStatementForm) form;
077 transactionStatementForm.clear();
078 return mapping.findForward(KFSConstants.MAPPING_BASIC);
079 }
080
081 /**
082 * Cancels the current page and goes to the start page
083 *
084 * @param mapping
085 * @param form
086 * @param request
087 * @param response
088 * @return
089 * @throws Exception
090 */
091 public ActionForward cancel(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
092 return mapping.findForward(KFSConstants.MAPPING_BASIC);
093 }
094
095 /**
096 * Generates Transaction Statement in the PDF form
097 *
098 * @param mapping
099 * @param form
100 * @param request
101 * @param response
102 * @return
103 * @throws Exception
104 */
105 public ActionForward print(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
106
107 TransactionStatementReportService transactionStatementReportService = SpringContext.getBean(TransactionStatementReportService.class);
108
109 // get all the value strings from the form
110 TransactionStatementForm transactionStatementForm = (TransactionStatementForm) form;
111 String kemids = transactionStatementForm.getKemid();
112 String benefittingOrganziationCampuses = transactionStatementForm.getBenefittingOrganziationCampus();
113 String benefittingOrganziationCharts = transactionStatementForm.getBenefittingOrganziationChart();
114 String benefittingOrganziations = transactionStatementForm.getBenefittingOrganziation();
115 String typeCodes = transactionStatementForm.getTypeCode();
116 String purposeCodes = transactionStatementForm.getPurposeCode();
117 String combineGroupCodes = transactionStatementForm.getCombineGroupCode();
118 String beginningDate = transactionStatementForm.getBeginningDate();
119 String endingDate = transactionStatementForm.getEndingDate();
120 String endowmentOption = transactionStatementForm.getEndowmentOption();
121 String listKemidsInHeader = transactionStatementForm.getListKemidsInHeader();
122 String closedIndicator = transactionStatementForm.getClosedIndicator();
123 String message = transactionStatementForm.getMessage();
124
125 List<TransactionStatementReportDataHolder> transactionStatementReportDataHolders = null;
126
127 // check to see if the ending date is greater than the beginning date
128 SimpleDateFormat df = new SimpleDateFormat("MM/dd/yyyy");
129 try {
130 java.util.Date beginDate = df.parse(beginningDate);
131 java.util.Date endDate = df.parse(endingDate);
132
133 if (beginDate.compareTo(endDate) >= 0) {
134 transactionStatementForm.setMessage(ERROR_REPORT_ENDING_DATE_NOT_GREATER_THAN_BEGINNING_DATE);
135 return mapping.findForward(KFSConstants.MAPPING_BASIC);
136 }
137 } catch (ParseException e) {
138 transactionStatementForm.setMessage(e.getMessage());
139 return mapping.findForward(KFSConstants.MAPPING_BASIC);
140 }
141
142 /*
143 * Creates the report data based on the selected criteria.
144 * The criteria are selected as follows.
145 * 1. Kemid and the other criteria cannot be selected at the same time.
146 * 2. If none of them are selected, all kemids will be selected.
147 * 3. The other criteria other than kemid are "OR" combined.
148 * 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)
149 * 5. Beginning Date and Ending Date are required.
150 */
151
152 if (StringUtils.isNotBlank(beginningDate) && StringUtils.isNotBlank(endingDate)) {
153 if (StringUtils.isNotBlank(kemids)) {
154
155 if (( StringUtils.isNotBlank(benefittingOrganziationCampuses)
156 || StringUtils.isNotBlank(benefittingOrganziationCharts)
157 || StringUtils.isNotBlank(benefittingOrganziations)
158 || StringUtils.isNotBlank(typeCodes)
159 || StringUtils.isNotBlank(purposeCodes)
160 || StringUtils.isNotBlank(combineGroupCodes) )) {
161
162 // kemid and the other criteria cannot be selected at the same time
163 transactionStatementForm.setMessage(ERROR_REPORT_KEMID_WITH_OTHER_CRITERIA);
164 return mapping.findForward(KFSConstants.MAPPING_BASIC);
165
166 } else {
167 // by kemid only
168 List<String> kemidList = parseValueString(kemids, KEMID_SEPERATOR);
169 transactionStatementReportDataHolders = transactionStatementReportService.getTransactionStatementReportsByKemidByIds(kemidList, beginningDate, endingDate, endowmentOption, closedIndicator);
170 }
171 } else {
172 if (( StringUtils.isBlank(benefittingOrganziationCampuses)
173 && StringUtils.isBlank(benefittingOrganziationCharts)
174 && StringUtils.isBlank(benefittingOrganziations)
175 && StringUtils.isBlank(typeCodes)
176 && StringUtils.isBlank(purposeCodes)
177 && StringUtils.isBlank(combineGroupCodes) )) {
178
179 // for all kemids
180 transactionStatementReportDataHolders = transactionStatementReportService.getTransactionStatementReportForAllKemids(beginningDate, endingDate, endowmentOption, closedIndicator);
181
182 } else {
183 // by other criteria
184 transactionStatementReportDataHolders = transactionStatementReportService.getTransactionStatementReportsByOtherCriteria(
185 parseValueString(benefittingOrganziationCampuses, OTHER_CRITERIA_SEPERATOR),
186 parseValueString(benefittingOrganziationCharts, OTHER_CRITERIA_SEPERATOR),
187 parseValueString(benefittingOrganziations, OTHER_CRITERIA_SEPERATOR),
188 parseValueString(typeCodes, OTHER_CRITERIA_SEPERATOR),
189 parseValueString(purposeCodes, OTHER_CRITERIA_SEPERATOR),
190 parseValueString(combineGroupCodes, OTHER_CRITERIA_SEPERATOR),
191 beginningDate,
192 endingDate,
193 endowmentOption,
194 closedIndicator);
195 }
196 }
197 } else {
198 transactionStatementForm.setMessage(ERROR_BOTH_BEGINNING_AND_ENDING_DATE_REQUIRED);
199 return mapping.findForward(KFSConstants.MAPPING_BASIC);
200
201 }
202
203 // See to see if you have something to print
204 if (transactionStatementReportDataHolders != null && !transactionStatementReportDataHolders.isEmpty()) {
205 // prepare the header sheet data
206 EndowmentReportHeaderDataHolder reportRequestHeaderDataHolder = transactionStatementReportService.createReportHeaderSheetData(
207 getKemidsSelected(transactionStatementReportDataHolders),
208 parseValueString(benefittingOrganziationCampuses, OTHER_CRITERIA_SEPERATOR),
209 parseValueString(benefittingOrganziationCharts, OTHER_CRITERIA_SEPERATOR),
210 parseValueString(benefittingOrganziations, OTHER_CRITERIA_SEPERATOR),
211 parseValueString(typeCodes, OTHER_CRITERIA_SEPERATOR),
212 parseValueString(purposeCodes, OTHER_CRITERIA_SEPERATOR),
213 parseValueString(combineGroupCodes, OTHER_CRITERIA_SEPERATOR),
214 REPORT_NAME,
215 endowmentOption,
216 null);
217
218 // generate the report in PDF
219 ByteArrayOutputStream pdfStream = new TransactionStatementReportPrint().printTransactionStatementReport(reportRequestHeaderDataHolder, transactionStatementReportDataHolders, listKemidsInHeader);
220 if (pdfStream != null) {
221 transactionStatementForm.setMessage("Reports Generated");
222 WebUtils.saveMimeOutputStreamAsFile(response, "application/pdf", pdfStream, REPORT_FILE_NAME);
223 return null;
224 }
225 }
226
227 // No report was generated
228 if (StringUtils.isBlank(kemids)) {
229 transactionStatementForm.setMessage("Report was not generated.");
230 } else {
231 transactionStatementForm.setMessage("Report was not generated for " + kemids + ".");
232 }
233
234 return mapping.findForward(KFSConstants.MAPPING_BASIC);
235 }
236
237 /**
238 * Retrieves all the kemids used for the report
239 *
240 * @param transactionStatementReportDataHolder
241 * @return
242 */
243 protected List<String> getKemidsSelected(List<TransactionStatementReportDataHolder> transactionStatementReportDataHolder) {
244
245 List<String> kemids = new ArrayList<String>();
246 for (TransactionStatementReportDataHolder dataHolder : transactionStatementReportDataHolder) {
247 kemids.add(dataHolder.getKemid());
248 }
249
250 return kemids;
251 }
252
253 }