PageRenderTime 53ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

/modules/dxp/apps/portal-reports-engine-console/portal-reports-engine-console-jasper/src/main/java/com/liferay/portal/reports/engine/console/jasper/internal/fill/manager/BaseReportFillManager.java

http://github.com/liferay/liferay-portal
Java | 203 lines | 141 code | 45 blank | 17 comment | 11 complexity | b689f5336d0ea3f3e440b557715ddf47 MD5 | raw file
Possible License(s): LGPL-2.0
  1. /**
  2. * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
  3. *
  4. * The contents of this file are subject to the terms of the Liferay Enterprise
  5. * Subscription License ("License"). You may not use this file except in
  6. * compliance with the License. You can obtain a copy of the License by
  7. * contacting Liferay, Inc. See the License for the specific language governing
  8. * permissions and limitations under the License, including but not limited to
  9. * distribution rights of the Software.
  10. *
  11. *
  12. *
  13. */
  14. package com.liferay.portal.reports.engine.console.jasper.internal.fill.manager;
  15. import com.liferay.petra.string.StringPool;
  16. import com.liferay.portal.kernel.dao.jdbc.DataAccess;
  17. import com.liferay.portal.kernel.log.Log;
  18. import com.liferay.portal.kernel.log.LogFactoryUtil;
  19. import com.liferay.portal.kernel.util.DateFormatFactoryUtil;
  20. import com.liferay.portal.kernel.util.GetterUtil;
  21. import com.liferay.portal.kernel.util.ListUtil;
  22. import com.liferay.portal.kernel.util.StringUtil;
  23. import com.liferay.portal.kernel.util.Validator;
  24. import com.liferay.portal.reports.engine.ReportRequest;
  25. import com.liferay.portal.reports.engine.ReportRequestContext;
  26. import java.io.ByteArrayInputStream;
  27. import java.io.InputStream;
  28. import java.sql.Connection;
  29. import java.text.DateFormat;
  30. import java.text.ParseException;
  31. import java.util.ArrayList;
  32. import java.util.Date;
  33. import java.util.HashMap;
  34. import java.util.List;
  35. import java.util.Map;
  36. import net.sf.jasperreports.engine.JRDataSource;
  37. import net.sf.jasperreports.engine.JRException;
  38. import net.sf.jasperreports.engine.JRParameter;
  39. import net.sf.jasperreports.engine.JasperFillManager;
  40. import net.sf.jasperreports.engine.JasperPrint;
  41. import net.sf.jasperreports.engine.JasperReport;
  42. /**
  43. * @author Gavin Wan
  44. * @author Brian Wing Shun Chan
  45. */
  46. public abstract class BaseReportFillManager implements ReportFillManager {
  47. @Override
  48. public JasperPrint fillReport(
  49. JasperReport jasperReport, ReportRequest reportRequest)
  50. throws JRException {
  51. Connection connection = null;
  52. try {
  53. connection = getConnection(reportRequest);
  54. }
  55. catch (JRException jrException) {
  56. throw jrException;
  57. }
  58. catch (Exception exception) {
  59. throw new JRException(exception);
  60. }
  61. Map<String, Object> reportParameters = getReportParameters(
  62. jasperReport, reportRequest);
  63. if (connection != null) {
  64. try {
  65. return JasperFillManager.fillReport(
  66. jasperReport, reportParameters, connection);
  67. }
  68. finally {
  69. DataAccess.cleanUp(connection);
  70. }
  71. }
  72. JRDataSource jrDataSource = null;
  73. try {
  74. jrDataSource = getJRDataSource(reportRequest);
  75. }
  76. catch (JRException jrException) {
  77. throw jrException;
  78. }
  79. catch (Exception exception) {
  80. throw new JRException(exception);
  81. }
  82. if (jrDataSource != null) {
  83. return JasperFillManager.fillReport(
  84. jasperReport, reportParameters, jrDataSource);
  85. }
  86. return JasperFillManager.fillReport(jasperReport, reportParameters);
  87. }
  88. protected Connection getConnection(ReportRequest reportRequest)
  89. throws Exception {
  90. return null;
  91. }
  92. protected InputStream getDataSourceByteArrayInputStream(
  93. ReportRequest reportRequest) {
  94. ReportRequestContext reportRequestContext =
  95. reportRequest.getReportRequestContext();
  96. byte[] dataSourceByteArray = (byte[])reportRequestContext.getAttribute(
  97. ReportRequestContext.DATA_SOURCE_BYTE_ARRAY);
  98. return new ByteArrayInputStream(dataSourceByteArray);
  99. }
  100. protected String getDataSourceCharSet(ReportRequest reportRequest) {
  101. ReportRequestContext reportRequestContext =
  102. reportRequest.getReportRequestContext();
  103. return GetterUtil.getString(
  104. (String)reportRequestContext.getAttribute(
  105. ReportRequestContext.DATA_SOURCE_CHARSET),
  106. StringPool.UTF8);
  107. }
  108. protected String[] getDataSourceColumnNames(ReportRequest reportRequest) {
  109. ReportRequestContext reportRequestContext =
  110. reportRequest.getReportRequestContext();
  111. String dataSourceColumnNames =
  112. (String)reportRequestContext.getAttribute(
  113. ReportRequestContext.DATA_SOURCE_COLUMN_NAMES);
  114. if (Validator.isNotNull(dataSourceColumnNames)) {
  115. return StringUtil.split(dataSourceColumnNames, StringPool.COMMA);
  116. }
  117. return null;
  118. }
  119. protected JRDataSource getJRDataSource(ReportRequest reportRequest)
  120. throws Exception {
  121. return null;
  122. }
  123. protected Map<String, Object> getReportParameters(
  124. JasperReport jasperReport, ReportRequest reportRequest) {
  125. Map<String, Object> reportParameters = new HashMap<>();
  126. Map<String, String> stringReportParameters =
  127. reportRequest.getReportParameters();
  128. JRParameter[] jrParameters = jasperReport.getParameters();
  129. for (JRParameter jrParameter : jrParameters) {
  130. String name = jrParameter.getName();
  131. Class<?> clazz = jrParameter.getValueClass();
  132. String stringValue = stringReportParameters.get(name);
  133. Object value = stringValue;
  134. if (clazz.equals(Date.class)) {
  135. DateFormat dateFormat =
  136. DateFormatFactoryUtil.getSimpleDateFormat("yyyy-MM-dd");
  137. try {
  138. value = dateFormat.parse(stringValue);
  139. }
  140. catch (ParseException parseException) {
  141. _log.error(
  142. stringValue + " is not yyyy-MM-dd", parseException);
  143. }
  144. }
  145. else if (clazz.equals(List.class)) {
  146. List<String> listValue = ListUtil.fromArray(
  147. StringUtil.split(stringValue));
  148. value = new ArrayList<>(listValue);
  149. }
  150. if (value != null) {
  151. reportParameters.put(name, value);
  152. }
  153. }
  154. return reportParameters;
  155. }
  156. private static final Log _log = LogFactoryUtil.getLog(
  157. BaseReportFillManager.class);
  158. }