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.ar.web.struts;
017    
018    import java.io.ByteArrayOutputStream;
019    import java.io.File;
020    import java.sql.Date;
021    import java.util.ArrayList;
022    import java.util.Iterator;
023    import java.util.List;
024    
025    import javax.servlet.http.HttpServletRequest;
026    import javax.servlet.http.HttpServletResponse;
027    
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.ar.report.service.AccountsReceivableReportService;
032    import org.kuali.kfs.sys.KFSConstants;
033    import org.kuali.kfs.sys.context.SpringContext;
034    import org.kuali.rice.kns.util.WebUtils;
035    import org.kuali.rice.kns.web.struts.action.KualiAction;
036    
037    import com.lowagie.text.Document;
038    import com.lowagie.text.pdf.PdfCopy;
039    import com.lowagie.text.pdf.PdfImportedPage;
040    import com.lowagie.text.pdf.PdfReader;
041    import com.lowagie.text.pdf.SimpleBookmark;
042    
043    /**
044     * This class handles Actions for lookup flow
045     */
046    
047    public class CustomerInvoiceAction extends KualiAction {
048        private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(CustomerInvoiceAction.class);
049    
050        /**
051         * 
052         * Constructs a CustomerInvoiceAction.java.
053         */
054        public CustomerInvoiceAction() {
055            super();
056        }
057    
058        /**
059         * 
060         * This method...
061         * @param mapping
062         * @param form
063         * @param request
064         * @param response
065         * @return
066         * @throws Exception
067         */
068        public ActionForward start(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
069            return mapping.findForward(KFSConstants.MAPPING_BASIC);
070        }
071    
072        /**
073         * 
074         * This method...
075         * @param mapping
076         * @param form
077         * @param request
078         * @param response
079         * @return
080         * @throws Exception
081         */
082        public ActionForward cancel(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
083    
084            return mapping.findForward(KFSConstants.MAPPING_BASIC);
085        }   
086    
087        /**
088         * 
089         * This method...
090         * @param mapping
091         * @param form
092         * @param request
093         * @param response
094         * @return
095         * @throws Exception
096         */
097        public ActionForward clear(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
098            CustomerInvoiceForm ciForm = (CustomerInvoiceForm)form;
099            ciForm.setChartCode(null);
100            ciForm.setOrgCode(null);
101            ciForm.setOrgType(null); 
102            ciForm.setRunDate(null);
103            ciForm.setMessage(null);
104            return mapping.findForward(KFSConstants.MAPPING_BASIC);
105        }   
106    
107        /**
108         * 
109         * This method...
110         * @param mapping
111         * @param form
112         * @param request
113         * @param response
114         * @return
115         * @throws Exception
116         */
117        public ActionForward print(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
118            CustomerInvoiceForm ciForm = (CustomerInvoiceForm)form;
119    
120            String org = ciForm.getOrgCode();
121            String chart = ciForm.getChartCode();
122            Date date = ciForm.getRunDate();
123            
124            StringBuilder fileName = new StringBuilder();
125            
126            AccountsReceivableReportService reportService = SpringContext.getBean(AccountsReceivableReportService.class);
127            List<File> reports = new ArrayList<File>();
128            if (ciForm.getOrgType() != null && chart != null && org != null) {
129                if (ciForm.getOrgType().equals("B")) {
130                    reports = reportService.generateInvoicesByBillingOrg(chart, org, date);
131                }
132                else if (ciForm.getOrgType().equals("P")) {
133                    reports = reportService.generateInvoicesByProcessingOrg(chart, org, date);
134                }
135                fileName.append(chart);
136                fileName.append(org);
137                if (date != null) {
138                    fileName.append(date);  
139                }
140            } else if (ciForm.getUserId() != null) {
141                reports = reportService.generateInvoicesByInitiator(ciForm.getUserId(), date);
142                fileName.append(ciForm.getUserId());
143            }
144            if (reports.size()>0) {
145                ByteArrayOutputStream baos = new ByteArrayOutputStream();
146                try {
147                    int pageOffset = 0;
148                    ArrayList master = new ArrayList();
149                    int f = 0;
150                    Document document = null;
151                    PdfCopy  writer = null;
152                    for (Iterator<File> itr = reports.iterator(); itr.hasNext();) {
153                        // we create a reader for a certain document
154                        String reportName = itr.next().getAbsolutePath();
155                        PdfReader reader = new PdfReader(reportName);
156                        reader.consolidateNamedDestinations();
157                        // we retrieve the total number of pages
158                        int n = reader.getNumberOfPages();
159                        List bookmarks = SimpleBookmark.getBookmark(reader);
160                        if (bookmarks != null) {
161                            if (pageOffset != 0) {
162                                SimpleBookmark.shiftPageNumbers(bookmarks, pageOffset, null);
163                            }
164                            master.addAll(bookmarks);
165                        }
166                        pageOffset += n;
167    
168                        if (f == 0) {
169                            // step 1: creation of a document-object
170                            document = new Document(reader.getPageSizeWithRotation(1));
171                            // step 2: we create a writer that listens to the document
172                            writer = new PdfCopy(document, baos);
173                            // step 3: we open the document
174                            document.open();
175                        }
176                        // step 4: we add content
177                        PdfImportedPage page;
178                        for (int i = 0; i < n; ) {
179                            ++i;
180                            page = writer.getImportedPage(reader, i);
181                            writer.addPage(page);
182                        }
183                        writer.freeReader(reader);
184                        f++;
185                    }
186                    if (!master.isEmpty())
187                        writer.setOutlines(master);
188                    // step 5: we close the document
189    
190                    document.close();
191                    // csForm.setReports(file);
192                }
193                catch(Exception e) {
194                    e.printStackTrace();
195                } 
196                fileName.append("-InvoiceBatchPDFs.pdf");
197                
198    
199                WebUtils.saveMimeOutputStreamAsFile(response, "application/pdf", baos, fileName.toString());
200                ciForm.setMessage(reports.size()+" Reports Generated");
201                return null;
202            }
203            ciForm.setMessage("No Reports Generated");
204            return mapping.findForward(KFSConstants.MAPPING_BASIC);
205        }
206    
207    }