/**
* 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.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.opencps.dossiermgt.model.DossierFile;
import org.opencps.dossiermgt.model.DossierPart;
import org.opencps.dossiermgt.service.DossierFileLocalServiceUtil;
import org.opencps.dossiermgt.service.DossierPartLocalServiceUtil;
import org.opencps.jasperreport.util.JRReportUtil;

import com.liferay.portal.kernel.json.JSONFactoryUtil;
import com.liferay.portal.kernel.language.LanguageUtil;
import com.liferay.portal.kernel.util.ParamUtil;

/**
 * @author trungnt
 */
public class PreviewReportServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	protected synchronized void doGet(
	    HttpServletRequest request, HttpServletResponse response)
	    throws ServletException, IOException {

		long dossierFileId = ParamUtil
		    .getLong(request, "dossierFileId");
		PrintWriter writer = response
		    .getWriter();

		try {
			// Get dossier file
			DossierFile dossierFile = DossierFileLocalServiceUtil
			    .getDossierFile(dossierFileId);

			// Get dossier part
			DossierPart dossierPart = DossierPartLocalServiceUtil
			    .getDossierPart(dossierFile
			        .getDossierPartId());

			String formData = dossierFile
			    .getFormData();
			String jrxmlTemplate = dossierPart
			    .getFormReport();

			// Validate json string

			JSONFactoryUtil
			    .createJSONObject(formData);

			JRReportUtil
			    .renderReportHTMLStream(
			        response, writer, jrxmlTemplate, formData, null);

		}
		catch (Exception e) {
			writer.write("<div class=\"portlet-msg-alert\">" + LanguageUtil.get(request.getLocale(), "error-while-preview-report") + "</div>");
		}finally {
			writer.println();
			writer.close();
		}
	}

	@Override
	public void destroy() {

		// TODO Auto-generated method stub
		super.destroy();
	}
}

@@ -1334,24 +1334,74 @@
 actoryUtil;%0A
+import com.liferay.portal.kernel.json.JSONObject;%0A
 import com.l
@@ -2355,16 +2355,40 @@
 ing%0A%0A%09%09%09
+JSONObject jsonObject = 
 JSONFact
@@ -2391,24 +2391,27 @@
 FactoryUtil%0A
+%09%09%09
 %09%09%09    .crea
@@ -2433,16 +2433,144 @@
 rmData);
+%0A%09%09%09%0A%09%09%09/*JSONObject dataSource = JSONFactoryUtil%0A%09%09%09%09%09%09    .createJSONObject();%0A%09%09%09%0A%09%09%09dataSource.put(%22opencps%22, jsonObject);*/
 %0A%0A%09%09%09JRR
@@ -2655,24 +2655,37 @@
 mplate, 
-formData
+jsonObject.toString()
 , null);
@@ -2869,26 +2869,71 @@
 %0A%09%09%09
-writer.println();%0A
+if(writer != null)%7B%0A%09%09%09%09writer.println();%0A%09%09%09%09writer.flush();%0A%09
 %09%09%09w
@@ -2947,16 +2947,21 @@
 lose();%0A
+%09%09%09%7D%0A
 %09%09%7D%0A%09%7D%0A%0A

Update Preview report servlet