/**
* OpenCPS is the open source Core Public Services software
* Copyright (C) 2016-present OpenCPS community

* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.

* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
*/

package org.opencps.jasperreport.util;

import java.util.Map;

import org.opencps.jasperreport.compile.JRReportTemplate;
import org.opencps.jasperreport.datasource.JRJSONDataSource;

import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.util.StringPool;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;

/**
 * @author trungnt
 *
 */
public class JRReportUtil {

	public static String createReportPDFfFile(
	    String jrxmlTemplate, String jsonData, Map<String, Object> parameters,
	    String outputDestination, String exportName) {

		String sourceFileName = outputDestination + exportName;
		try {
			JRReportTemplate reportTemplate = JRReportTemplate
			    .compile(jrxmlTemplate);
			JRJSONDataSource dataSource = JRJSONDataSource
			    .getInstance(jsonData);
			JasperPrint jasperPrint =
			    getJasperPrint(reportTemplate, parameters, dataSource);

			return exportReportToPdfFile(jasperPrint, sourceFileName);
		}
		catch (Exception e) {
			_log
			    .error(e);

			return StringPool.BLANK;

		}

	}

	protected static JasperPrint getJasperPrint(
	    JRReportTemplate jrReportTemplate, Map<String, Object> parameters,
	    JRJSONDataSource dataSource)
	    throws JRException {

		JasperPrint jasperPrint = JasperFillManager
		    .fillReport(jrReportTemplate, null, dataSource);

		return jasperPrint;
	}

	protected static String exportReportToPdfFile(
	    JasperPrint jasperPrint, String sourceFileName)
	    throws JRException {

		return JasperExportManager
		    .exportReportToHtmlFile(sourceFileName);

	}

	private static Log _log = LogFactoryUtil
	    .getLog(JRReportUtil.class
	        .getName());
}

@@ -739,17 +739,16 @@
 es/%3E%0A*/%0A
-%0A
 package 
@@ -1273,16 +1273,65 @@
 erPrint;
+%0Aimport net.sf.jasperreports.engine.JasperReport;
 %0A%0A/**%0A *
@@ -1351,11 +1351,8 @@
 gnt%0A
- *%0A
  */%0A
@@ -1388,16 +1388,29 @@
 %09public 
+synchronized 
 static S
@@ -1636,31 +1636,27 @@
 y %7B%0A%09%09%09J
-R
+asper
 Report
-Template
  reportT
@@ -1690,23 +1690,31 @@
 %09%09%09    .
-compile
+getJasperReport
 (jrxmlTe
@@ -1908,32 +1908,24 @@
 eturn export
-ReportTo
 PdfFile(jasp
@@ -2039,24 +2039,327 @@
 ;%0A%0A%09%09%7D%0A%0A%09%7D%0A%0A
+%09protected static JasperPrint getJasperPrint(%0A%09    JasperReport jrReportTemplate, Map%3CString, Object%3E parameters,%0A%09    JRJSONDataSource dataSource)%0A%09    throws JRException %7B%0A%0A%09%09JasperPrint jasperPrint = JasperFillManager%0A%09%09    .fillReport(jrReportTemplate, null, dataSource);%0A%0A%09%09return jasperPrint;%0A%09%7D%0A%0A
 %09protected s
@@ -2684,24 +2684,16 @@
 g export
-ReportTo
 PdfFile(
@@ -2775,23 +2775,16 @@
 on %7B%0A%0A%09%09
-return 
 JasperEx
@@ -2820,17 +2820,29 @@
 rtTo
-Html
+Pdf
 File(
+jasperPrint, 
 sour
@@ -2855,16 +2855,42 @@
 Name);%0A%0A
+%09%09return sourceFileName;%0A%0A
 %09%7D%0A%0A%09pri

Update method exportReportToPDF