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.tem.batch.service.impl;
20  
21  import java.io.File;
22  import java.io.FileNotFoundException;
23  import java.io.PrintStream;
24  import java.text.MessageFormat;
25  import java.util.ArrayList;
26  import java.util.LinkedHashMap;
27  import java.util.List;
28  import java.util.Map;
29  
30  import org.apache.commons.lang.StringUtils;
31  import org.apache.commons.lang.text.StrBuilder;
32  import org.kuali.kfs.module.tem.TemConstants;
33  import org.kuali.kfs.module.tem.batch.service.DataReportService;
34  import org.kuali.kfs.module.tem.util.MessageUtils;
35  import org.kuali.kfs.sys.KFSConstants;
36  import org.kuali.kfs.sys.MessageBuilder;
37  import org.kuali.kfs.sys.report.BusinessObjectReportHelper;
38  import org.kuali.rice.core.api.datetime.DateTimeService;
39  import org.kuali.rice.krad.bo.BusinessObject;
40  import org.kuali.rice.krad.util.ErrorMessage;
41  
42  public class DataReportServiceImpl implements DataReportService {
43      public static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(DataReportServiceImpl.class);
44  
45      public final static String REPORT_FILE_NAME_PATTERN = "{0}/{1}_{2}{3}";
46  
47      private DateTimeService dateTimeService;
48  
49      /**
50       * @see org.kuali.kfs.module.tem.batch.service.DataReportService#writeToReport(java.io.PrintStream, org.kuali.kfs.module.tem.businessobject.AgencyStagingData, java.lang.String, org.kuali.kfs.sys.report.BusinessObjectReportHelper)
51       */
52      @Override
53      public <T extends BusinessObject> void writeToReport(PrintStream reportDataStream,T tableData, List<ErrorMessage> errors, BusinessObjectReportHelper reportHelper) {
54          String reportEntry = formatMessage(tableData, getMessageAsString(errors), reportHelper);
55          reportDataStream.println(reportEntry);
56      }
57  
58      /**
59       *  Return a String for the report
60       *
61       *  Using the report helper to display the table header , and then the table data values
62       *  If the errors string is not empty, additional errors (formated) will be displayed under the table
63       *
64       * @param tableData
65       * @param errors
66       * @param reportHelper
67       * @return
68       */
69      private <T extends BusinessObject> String formatMessage(T tableData, String errors, BusinessObjectReportHelper reportHelper) {
70          StringBuilder body = new StringBuilder();
71          Map<String, String> tableDefinition=new LinkedHashMap<String, String>();
72          List<String> propertyList =new ArrayList<String>();
73          tableDefinition = reportHelper.getTableDefinition();
74          propertyList = reportHelper.getTableCellValues(tableData, false);
75  
76          String tableCellFormat = tableDefinition.get(KFSConstants.ReportConstants.TABLE_CELL_FORMAT_KEY);
77          String fieldLine = String.format(tableCellFormat, propertyList.toArray());
78          //create the table fields
79          body.append(fieldLine);
80          body.append(BusinessObjectReportHelper.LINE_BREAK);
81  
82          if (StringUtils.isNotEmpty(errors)){
83              //append the error messages
84              body.append("**** ERROR(S): **** ")
85                  .append(BusinessObjectReportHelper.LINE_BREAK)
86                  .append(errors)
87                  .append(BusinessObjectReportHelper.LINE_BREAK);
88          }
89          return body.toString();
90      }
91  
92      /**
93       *
94       * @param reportDataStream
95       * @param fileName
96       * @param importBy
97       */
98      @Override
99      public void writeReportHeader(PrintStream reportDataStream, String fileName, String reportHeader, BusinessObjectReportHelper reportHelper) {
100         StringBuilder header = new StringBuilder();
101         header.append(MessageBuilder.buildMessageWithPlaceHolder(reportHeader, BusinessObjectReportHelper.LINE_BREAK, fileName));
102         header.append(BusinessObjectReportHelper.LINE_BREAK);
103         header.append(BusinessObjectReportHelper.LINE_BREAK);
104         header.append(BusinessObjectReportHelper.LINE_BREAK);
105 
106         Map<String, String> tableDefinition = reportHelper.getTableDefinition();
107         String tableHeaderFormat = tableDefinition.get(KFSConstants.ReportConstants.TABLE_HEADER_LINE_KEY);
108         header.append(tableHeaderFormat);
109         reportDataStream.print(header);
110     }
111 
112     /**
113      * get print stream for report
114      */
115     @Override
116     public PrintStream getReportPrintStream(String directory, String filePrefix) {
117         String dateTime = dateTimeService.toDateTimeStringForFilename(dateTimeService.getCurrentSqlDate());
118         String reportFileName = MessageFormat.format(REPORT_FILE_NAME_PATTERN, directory, filePrefix, dateTime, TemConstants.TEXT_FILE_SUFFIX);
119 
120         File outputfile = new File(reportFileName);
121 
122         try {
123             return new PrintStream(outputfile);
124         }
125         catch (FileNotFoundException e) {
126             String errorMessage = "Cannot find the output file: " + reportFileName;
127             LOG.error(errorMessage);
128             throw new RuntimeException(errorMessage, e);
129         }
130     }
131 
132     /**
133      * @see org.kuali.kfs.module.tem.batch.service.DataReportService#getMessageAsString(java.util.List)
134      */
135     @Override
136     public String getMessageAsString(List<ErrorMessage> errorMessages){
137 
138         List<String> messageList = new ArrayList<String>();
139         for (ErrorMessage error : errorMessages){
140             messageList.add(MessageUtils.getErrorMessage(error));
141         }
142         StrBuilder builder = new StrBuilder();
143         builder.appendWithSeparators(messageList, BusinessObjectReportHelper.LINE_BREAK);
144        return  builder.toString();
145     }
146 
147     public DateTimeService getDateTimeService() {
148         return dateTimeService;
149     }
150 
151     public void setDateTimeService(DateTimeService dateTimeService) {
152         this.dateTimeService = dateTimeService;
153     }
154 
155 }