PageRenderTime 71ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/src/main/java/org/comsoft/reporting/engine/JUniPrintReportsEngine.java

https://bitbucket.org/openicar/juniprint-reports-engine
Java | 218 lines | 154 code | 36 blank | 28 comment | 25 complexity | 98873ba74938bc6cc5d14d4be5beed31 MD5 | raw file
Possible License(s): Apache-2.0
  1. /*-
  2. * #%L
  3. * JUniPrint based reports engine
  4. * %%
  5. * Copyright (C) 2011 - 2017 COMSOFT, JSC
  6. * %%
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. * #L%
  19. */
  20. package org.comsoft.reporting.engine;
  21. import java.io.FileInputStream;
  22. import java.io.OutputStream;
  23. import java.util.ArrayList;
  24. import java.util.Calendar;
  25. import java.util.Date;
  26. import java.util.Iterator;
  27. import java.util.List;
  28. import java.util.Map;
  29. import org.apache.poi.hssf.usermodel.HSSFCell;
  30. import org.apache.poi.hssf.usermodel.HSSFName;
  31. import org.apache.poi.hssf.usermodel.HSSFRichTextString;
  32. import org.apache.poi.hssf.usermodel.HSSFRow;
  33. import org.apache.poi.hssf.usermodel.HSSFSheet;
  34. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  35. import org.apache.poi.hssf.util.AreaReference;
  36. import org.apache.poi.poifs.filesystem.POIFSFileSystem;
  37. import org.apache.poi.ss.util.CellReference;
  38. import org.comsoft.juniprint.JUniPrint;
  39. import org.comsoft.juniprint.utils.ExcelUtils;
  40. import org.comsoft.reporting.QueryField;
  41. import org.comsoft.reporting.ReportDesc;
  42. import org.comsoft.reporting.api.ReportOutputFormat;
  43. import org.comsoft.reporting.api.ReportQuery;
  44. import org.comsoft.reporting.api.ReportRenderer;
  45. import org.jboss.seam.annotations.Name;
  46. import org.jboss.seam.framework.Controller;
  47. /**
  48. * Отчетный движок на основе библиотеки JUniPrint
  49. *
  50. * @author <a href="mailto:faa@comsoft-corp.ru">Фомичев Артем</a> <br>
  51. *
  52. */
  53. @Name("jUniPrintReportsEngine")
  54. public class JUniPrintReportsEngine extends Controller implements ReportRenderer {
  55. private static final long serialVersionUID = 1L;
  56. @Override
  57. public void begin() {
  58. // TODO Auto-generated method stub
  59. }
  60. @Override
  61. public void end() {
  62. // TODO Auto-generated method stub
  63. }
  64. ReportDesc reportDesc;
  65. ReportQuery reportQuery;
  66. String templateFile;
  67. @Override
  68. public void open(ReportDesc reportDesc, ReportQuery reportQuery, String outputFormat, String templateFile) {
  69. this.reportDesc = reportDesc;
  70. this.reportQuery = reportQuery;
  71. this.templateFile = templateFile;
  72. }
  73. @Override
  74. public void render(OutputStream out) {
  75. try {
  76. FileInputStream templateFileStream = new FileInputStream(templateFile);
  77. try {
  78. POIFSFileSystem fs = new POIFSFileSystem(templateFileStream);
  79. HSSFWorkbook wb = new HSSFWorkbook(fs);
  80. for (Map.Entry<String, Object> param : reportQuery.getParams().entrySet()) {
  81. HSSFName paramCellName = ExcelUtils.getNamedRange(wb, param.getKey());
  82. if (paramCellName != null) {
  83. HSSFSheet paramCellSheet = wb.getSheet(paramCellName.getSheetName());
  84. AreaReference paramCellAreaRef = new AreaReference(paramCellName.getRefersToFormula());
  85. CellReference paramCellAreaRefFirstCell = paramCellAreaRef.getFirstCell();
  86. HSSFCell paramCell = getCell(paramCellSheet, paramCellAreaRefFirstCell.getRow(), paramCellAreaRefFirstCell.getCol());
  87. setCellValue(paramCell, param.getValue());
  88. }
  89. }
  90. String dataBegFieldName = "DataBeg";
  91. HSSFName nameDataBeg = ExcelUtils.getNamedRange(wb, dataBegFieldName);
  92. String nameShData = nameDataBeg.getSheetName();
  93. HSSFSheet shData = wb.getSheet(nameShData);
  94. AreaReference areaDataBeg = new AreaReference(nameDataBeg.getRefersToFormula());
  95. int dataBegRow = areaDataBeg.getFirstCell().getRow();
  96. int dataBegCol = areaDataBeg.getFirstCell().getCol();
  97. Iterator<?> rowsIterator = reportQuery.getResultList().iterator();
  98. int i = 0;
  99. while (rowsIterator.hasNext()) {
  100. Object rowObject = rowsIterator.next();
  101. int rowIndex = dataBegRow + i;
  102. HSSFRow row = getRow(shData, rowIndex);
  103. List<QueryField> fields = reportQuery.getQueryDesc().getFields();
  104. int j = 0;
  105. for (QueryField queryField : fields) {
  106. int colIndex = dataBegCol + j;
  107. HSSFCell cell = getCell(row, colIndex);
  108. Object value = reportQuery.getFieldValue(rowObject, queryField.getFieldName());
  109. if(value != null){
  110. setCellValue(cell, value);
  111. }
  112. j++;
  113. }
  114. i++;
  115. }
  116. HSSFName aSelf_FormatName = ExcelUtils.getNamedRange(wb, "Self_Format");
  117. if (aSelf_FormatName == null) {
  118. JUniPrint jUniPrint = new JUniPrint(wb);
  119. jUniPrint.init(null, null, dataBegFieldName);
  120. jUniPrint.uniPrint(false);
  121. }
  122. wb.write(out);
  123. } finally {
  124. templateFileStream.close();
  125. }
  126. } catch (Exception e) {
  127. throw new RuntimeException(e);
  128. }
  129. }
  130. protected void setCellValue(HSSFCell cell, Object value) {
  131. if (value == null) return;
  132. if (value instanceof Number) {
  133. cell.setCellValue(((Number)value).doubleValue());
  134. } else if (value instanceof Date) {
  135. cell.setCellValue((Date)value);
  136. } else if (value instanceof Calendar) {
  137. cell.setCellValue((Calendar)value);
  138. } else if (value instanceof Boolean) {
  139. cell.setCellValue(Boolean.TRUE.equals(value));
  140. } else {
  141. cell.setCellValue(new HSSFRichTextString(value.toString()));
  142. }
  143. }
  144. private static List<ReportOutputFormat> AVAILABLE_OUTPUT_FORMATS;
  145. static {
  146. AVAILABLE_OUTPUT_FORMATS = new ArrayList<ReportOutputFormat>();
  147. AVAILABLE_OUTPUT_FORMATS.add(new ReportOutputFormatImpl("xls", "MS Office Spreadsheet", ".xls", "application/vnd.ms-excel"));
  148. }
  149. @Override
  150. public List<ReportOutputFormat> getAvailableOutputFormats() {
  151. return AVAILABLE_OUTPUT_FORMATS;
  152. }
  153. @Override
  154. public void setOutputFormat(String outputFormat) {
  155. // do nothing :)))
  156. }
  157. @Override
  158. public ReportOutputFormat getOutputFormat() {
  159. return AVAILABLE_OUTPUT_FORMATS.get(0);
  160. }
  161. @Override
  162. public String getDefaultOutputFormat() {
  163. return "xls";
  164. }
  165. private HSSFRow getRow(HSSFSheet sh, int rowIndex){
  166. if(sh==null) return null;
  167. HSSFRow r = sh.getRow(rowIndex);
  168. if(r != null) return r;
  169. r = sh.createRow(rowIndex);
  170. return r;
  171. }
  172. private HSSFCell getCell(HSSFRow row, int columnIndex){
  173. if(row==null) return null;
  174. HSSFCell c = row.getCell(columnIndex);
  175. if(c != null) return c;
  176. c = row.createCell(columnIndex);
  177. return c;
  178. }
  179. private HSSFCell getCell(HSSFSheet sh, int rowIndex, int columnIndex){
  180. return getCell(getRow(sh, rowIndex), columnIndex);
  181. }
  182. }