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.TransactionSummaryReportService;
032    import org.kuali.kfs.module.endow.report.util.EndowmentReportHeaderDataHolder;
033    import org.kuali.kfs.module.endow.report.util.TransactionSummaryReportDataHolder;
034    import org.kuali.kfs.module.endow.report.util.TransactionSummaryReportPrint;
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 TransactionSummaryAction extends EndowmentReportBaseAction {
040    
041        private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(TransactionSummaryAction.class);
042        
043        private final String REPORT_NAME = "Transaction Summary";
044        private final String REPORT_FILE_NAME = "TransactionSummaryReport.pdf";
045        
046        public TransactionSummaryAction() {
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            return mapping.findForward(KFSConstants.MAPPING_BASIC);
062        }
063    
064        /**
065         * Clears the form when the "clear" button is pressed
066         * 
067         * @param mapping
068         * @param form
069         * @param request
070         * @param response
071         * @return
072         * @throws Exception
073         */
074        public ActionForward clear(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
075            TransactionSummaryForm transactionStatementForm = (TransactionSummaryForm) form;
076            transactionStatementForm.clear();
077            return mapping.findForward(KFSConstants.MAPPING_BASIC);
078        }
079    
080        /**
081         * Cancels the current page and goes to the start page
082         * 
083         * @param mapping
084         * @param form
085         * @param request
086         * @param response
087         * @return
088         * @throws Exception
089         */
090        public ActionForward cancel(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
091            return mapping.findForward(KFSConstants.MAPPING_BASIC);
092        }
093    
094        /**
095         * Generates Transaction Statement in the PDF form
096         * 
097         * @param mapping
098         * @param form
099         * @param request
100         * @param response
101         * @return
102         * @throws Exception
103         */
104        public ActionForward print(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
105            
106            TransactionSummaryReportService transactionSummaryReportService = SpringContext.getBean(TransactionSummaryReportService.class);
107            
108            // get all the value strings from the form
109            TransactionSummaryForm transactionSummaryForm = (TransactionSummaryForm) form;
110            String kemids = transactionSummaryForm.getKemid();
111            String benefittingOrganziationCampuses = transactionSummaryForm.getBenefittingOrganziationCampus();
112            String benefittingOrganziationCharts = transactionSummaryForm.getBenefittingOrganziationChart();
113            String benefittingOrganziations = transactionSummaryForm.getBenefittingOrganziation();
114            String typeCodes = transactionSummaryForm.getTypeCode();
115            String purposeCodes = transactionSummaryForm.getPurposeCode();
116            String combineGroupCodes = transactionSummaryForm.getCombineGroupCode();
117            String beginningDate = transactionSummaryForm.getBeginningDate();
118            String endingDate = transactionSummaryForm.getEndingDate();
119            String endowmentOption = transactionSummaryForm.getEndowmentOption();
120            String reportOption = transactionSummaryForm.getReportOption();
121            String listKemidsOnHeader = transactionSummaryForm.getListKemidsInHeader();
122            String summaryTotalsOnly = transactionSummaryForm.getSummaryTotalsOnly();
123            String closedIndicator = transactionSummaryForm.getClosedIndicator();
124            String message = transactionSummaryForm.getMessage();
125    
126            // check to see if the ending date is greater than the beginning date
127            SimpleDateFormat df = new SimpleDateFormat("MM/dd/yyyy");
128            try {
129                java.util.Date beginDate = df.parse(beginningDate);
130                java.util.Date endDate = df.parse(endingDate);
131    
132                if (beginDate.compareTo(endDate) >= 0) {
133                    transactionSummaryForm.setMessage(ERROR_REPORT_ENDING_DATE_NOT_GREATER_THAN_BEGINNING_DATE);
134                    return mapping.findForward(KFSConstants.MAPPING_BASIC);
135                }
136            } catch (ParseException e) {
137                transactionSummaryForm.setMessage(e.getMessage());
138                return mapping.findForward(KFSConstants.MAPPING_BASIC);
139            }
140    
141            List<TransactionSummaryReportDataHolder> transactionSummaryReportList = null;
142    
143            /*
144             * Creates the report data based on the selected criteria.
145             * The criteria are selected as follows.
146             * 1. Kemid and the other criteria cannot be selected at the same time.
147             * 2. If none of them are selected, all kemids will be selected.
148             * 3. The other criteria other than kemid are "OR" combined.
149             * 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) 
150             * 5. Beginning Date and Ending Date are required.
151             */
152            
153            if (StringUtils.isNotBlank(beginningDate) && StringUtils.isNotBlank(endingDate)) {
154                if (StringUtils.isNotBlank(kemids)) {
155                    
156                    if (( StringUtils.isNotBlank(benefittingOrganziationCampuses) 
157                        || StringUtils.isNotBlank(benefittingOrganziationCharts)
158                        || StringUtils.isNotBlank(benefittingOrganziations)
159                        || StringUtils.isNotBlank(typeCodes)
160                        || StringUtils.isNotBlank(purposeCodes) 
161                        || StringUtils.isNotBlank(combineGroupCodes) )) {
162                    
163                        // kemid and the other criteria cannot be selected at the same time 
164                        transactionSummaryForm.setMessage(ERROR_REPORT_KEMID_WITH_OTHER_CRITERIA);
165                        return mapping.findForward(KFSConstants.MAPPING_BASIC);
166                        
167                    } else {
168                        // by kemid only
169                        List<String> kemidList = parseValueString(kemids, KEMID_SEPERATOR);                
170                        transactionSummaryReportList = transactionSummaryReportService.getTransactionSummaryReportsByKemidByIds(kemidList, beginningDate, endingDate, endowmentOption, closedIndicator, reportOption);
171                    }
172                } else {
173                    if (( StringUtils.isBlank(benefittingOrganziationCampuses) 
174                            && StringUtils.isBlank(benefittingOrganziationCharts)
175                            && StringUtils.isBlank(benefittingOrganziations)
176                            && StringUtils.isBlank(typeCodes)
177                            && StringUtils.isBlank(purposeCodes) 
178                            && StringUtils.isBlank(combineGroupCodes) )) {
179        
180                        // for all kemids
181                        transactionSummaryReportList = transactionSummaryReportService.getTransactionSummaryReportForAllKemids(beginningDate, endingDate, endowmentOption, closedIndicator, reportOption);
182                        
183                    } else {
184                        // by other criteria
185                        transactionSummaryReportList = transactionSummaryReportService.getTransactionSummaryReportsByOtherCriteria(
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                            beginningDate,
193                            endingDate,
194                            endowmentOption,
195                            closedIndicator, reportOption);
196                    }
197                }
198            } else {
199                transactionSummaryForm.setMessage(ERROR_BOTH_BEGINNING_AND_ENDING_DATE_REQUIRED);
200                return mapping.findForward(KFSConstants.MAPPING_BASIC);
201            }
202                   
203            // see if you have something to print        
204            if (transactionSummaryReportList != null && !transactionSummaryReportList.isEmpty()) {
205                // prepare the header sheet data
206                EndowmentReportHeaderDataHolder reportRequestHeaderDataHolder = transactionSummaryReportService.createReportHeaderSheetData(
207                        getKemidsSelected(transactionSummaryReportList),
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 TransactionSummaryReportPrint().printTransactionSummaryReport(reportRequestHeaderDataHolder, transactionSummaryReportList, listKemidsOnHeader, reportOption, summaryTotalsOnly);            
220                if (pdfStream != null) {
221                    transactionSummaryForm.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            transactionSummaryForm.setMessage("Report was not generated for " + kemids + ".");
229            
230            return mapping.findForward(KFSConstants.MAPPING_BASIC);
231            
232        }
233        
234        /**
235         * Retrieves all the kemids used for the report 
236         * 
237         * @param transactionStatementReportDataHolder
238         * @return
239         */
240        protected List<String> getKemidsSelected(List<TransactionSummaryReportDataHolder> transactionSummaryReportList) {
241            
242            List<String> kemids = new ArrayList<String>();
243            for (TransactionSummaryReportDataHolder dataHolder : transactionSummaryReportList) {
244                kemids.add(dataHolder.getKemid());
245            }
246            
247            return kemids;        
248        }
249        
250    }