Skip to content

Commit

Permalink
55 html groovy template (#92)
Browse files Browse the repository at this point in the history
HTML + Groovy template #55
  • Loading branch information
soraksh authored and Andrey Subbotin committed Nov 14, 2018
1 parent d48155b commit 687c664
Show file tree
Hide file tree
Showing 7 changed files with 146 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

/**
* This interface describes report template document.
*/
*/
public interface ReportTemplate extends Serializable {
String DEFAULT_TEMPLATE_CODE = "DEFAULT";

Expand All @@ -47,6 +47,13 @@ public interface ReportTemplate extends Serializable {
*/
String getOutputNamePattern();

/**
* @return if it is groovy html template.
*/
default boolean isGroovy() {
return false;
}

/**
* @return if report is defined by custom class.
* In this case band data will be passed in com.haulmont.yarg.structure.ReportTemplate#getCustomReport() object and it will generate binary.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import freemarker.ext.beans.BeansWrapper;
import freemarker.ext.beans.MapModel;
import freemarker.template.*;
import groovy.text.GStringTemplateEngine;
import groovy.text.Template;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
Expand Down Expand Up @@ -177,18 +179,31 @@ protected void loadFontsFromDirectory(HtmlToPdfConverter converter, File fontsDi
protected void writeHtmlDocument(BandData rootBand, OutputStream outputStream) {
Map templateModel = getTemplateModel(rootBand);

Template htmlTemplate = getTemplate();
Writer htmlWriter = new OutputStreamWriter(outputStream);

try {
htmlTemplate.process(templateModel, htmlWriter);
htmlWriter.close();
} catch (TemplateException fmException) {
throw wrapWithReportingException("FreeMarkerException: " + fmException.getMessage());
} catch (ReportingException e) {
throw e;
} catch (Exception e) {
throw wrapWithReportingException("An error occurred while rendering html document.", e);
if (reportTemplate.isGroovy()) {
groovy.text.Template htmlTemplate = getGroovyTemplate();
Writer htmlWriter = new OutputStreamWriter(outputStream);
try {
htmlTemplate.make(templateModel).writeTo(htmlWriter);
htmlWriter.close();
} catch (ReportingException e) {
throw e;
} catch (Exception e) {
throw wrapWithReportingException("An error occurred while rendering html document.", e);
}
} else {
freemarker.template.Template htmlTemplate = getFreemarkerTemplate();
Writer htmlWriter = new OutputStreamWriter(outputStream);

try {
htmlTemplate.process(templateModel, htmlWriter);
htmlWriter.close();
} catch (TemplateException fmException) {
throw wrapWithReportingException("FreeMarkerException: " + fmException.getMessage());
} catch (ReportingException e) {
throw e;
} catch (Exception e) {
throw wrapWithReportingException("An error occurred while rendering html document.", e);
}
}
}

Expand Down Expand Up @@ -217,7 +232,7 @@ protected Map getBandModel(BandData band) {
return model;
}

protected Template getTemplate() {
protected freemarker.template.Template getFreemarkerTemplate() {
try {
String templateContent = IOUtils.toString(reportTemplate.getDocumentContent(), StandardCharsets.UTF_8);
StringTemplateLoader stringLoader = new StringTemplateLoader();
Expand All @@ -227,11 +242,22 @@ protected Template getTemplate() {
fmConfiguration.setTemplateLoader(stringLoader);
fmConfiguration.setDefaultEncoding("UTF-8");

Template htmlTemplate = fmConfiguration.getTemplate(reportTemplate.getDocumentName());
freemarker.template.Template htmlTemplate = fmConfiguration.getTemplate(reportTemplate.getDocumentName());
htmlTemplate.setObjectWrapper(objectWrapper);
return htmlTemplate;
} catch (Exception e) {
throw wrapWithReportingException("An error occurred while creating freemarker template", e);
}
}

protected groovy.text.Template getGroovyTemplate() {
try {
GStringTemplateEngine templateEngine = new GStringTemplateEngine();
String templateContent = IOUtils.toString(reportTemplate.getDocumentContent(), StandardCharsets.UTF_8);
groovy.text.Template htmlTemplate = templateEngine.createTemplate(templateContent);
return htmlTemplate;
} catch (Exception e) {
throw wrapWithReportingException("An error occurred while creating groovy template", e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ public class ReportTemplateImpl implements ReportTemplate {
protected ReportOutputType reportOutputType;
protected String outputNamePattern;

protected boolean groovy = false;

protected CustomReport customReport;
protected boolean custom = false;

Expand All @@ -52,6 +54,11 @@ public ReportTemplateImpl(String code, String documentName, String documentPath,
validate();
}

public ReportTemplateImpl(String code, String documentName, String documentPath, ReportOutputType reportOutputType, boolean groovy) throws IOException {
this(code, documentName, documentPath, reportOutputType);
this.groovy = groovy;
}

void validate() {
if (!isCustom()) {
Preconditions.checkNotNull(this.code, "\"code\" parameter can not be null");
Expand Down Expand Up @@ -92,6 +99,11 @@ public String getOutputNamePattern() {
return outputNamePattern;
}

@Override
public boolean isGroovy() {
return groovy;
}

@Override
public boolean isCustom() {
return custom;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ public String getOutputNamePattern() {
return null;
}

@Override
public boolean isGroovy() {
return false;
}

@Override
public boolean isCustom() {
return false;
Expand Down
47 changes: 47 additions & 0 deletions core/modules/core/test/integration/HtmlGroovyIntegrationTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package integration;

import com.haulmont.yarg.formatters.ReportFormatter;
import com.haulmont.yarg.formatters.factory.DefaultFormatterFactory;
import com.haulmont.yarg.formatters.factory.FormatterFactoryInput;
import com.haulmont.yarg.structure.BandData;
import com.haulmont.yarg.structure.BandOrientation;
import com.haulmont.yarg.structure.ReportOutputType;
import com.haulmont.yarg.structure.impl.ReportTemplateImpl;
import junit.framework.Assert;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.junit.Test;

import java.io.File;
import java.io.FileOutputStream;
import java.util.HashSet;

public class HtmlGroovyIntegrationTest {
@Test
public void testHtmlFormatter() throws Exception {
BandData root = new BandData("Root", null, BandOrientation.HORIZONTAL);

BandData userBand = new BandData("User", root, BandOrientation.HORIZONTAL);
userBand.addData("active", true);
userBand.addData("login", "admin");
root.addChild(userBand);
root.setFirstLevelBandDefinitionNames(new HashSet<String>());
root.getFirstLevelBandDefinitionNames().add("User");

FileOutputStream outputStream = new FileOutputStream("./result/integration/html-groovy-test-result.html");

ReportFormatter formatter = new DefaultFormatterFactory().createFormatter(new FormatterFactoryInput("html", root,
new ReportTemplateImpl("", "./modules/core/test/integration/html-groovy-test-template.html",
"./modules/core/test/integration/html-groovy-test-template.html", ReportOutputType.html, true), outputStream));

formatter.renderDocument();

IOUtils.closeQuietly(outputStream);

File sample = new File("./modules/core/test/integration/html-groovy-test-template-result.html");
File result = new File("./result/integration/html-groovy-test-result.html");
boolean isTwoEqual = FileUtils.contentEqualsIgnoreEOL(sample, result, null);

Assert.assertTrue("Files are not equal", isTwoEqual);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http:https://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http:https://www.w3.org/1999/xhtml" xml:lang="ru">
<head>
<title> Report User </title>
<style type="text/css">
body {font-family: 'Charis SIL', sans-serif;}
tbody tr {height:20px; min-height:20px}

</style>
</head>
<body>

<p>Login: admin</p>
<p>Active: true</p>
</body>
</html>
17 changes: 17 additions & 0 deletions core/modules/core/test/integration/html-groovy-test-template.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http:https://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http:https://www.w3.org/1999/xhtml" xml:lang="ru">
<head>
<title> Report User </title>
<style type="text/css">
body {font-family: 'Charis SIL', sans-serif;}
tbody tr {height:20px; min-height:20px}

</style>
</head>
<body>
<% def user = Root.bands.User %>
<p>Login: ${user.fields.login.first()}</p>
<p>Active: ${user.fields.active.first()}</p>
</body>
</html>

0 comments on commit 687c664

Please sign in to comment.