BAEL-617: added dependencies and report generation code

This commit is contained in:
Gian Mario Contessa 2019-03-29 18:30:28 +00:00
parent c16db46236
commit d973b289c4
105 changed files with 245 additions and 358 deletions

Binary file not shown.

View File

@ -9,19 +9,18 @@
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<description>Module For Spring Boot Integration with BIRT</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
@ -50,35 +49,13 @@
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!-- Birt dependencies -->
<!-- https://mvnrepository.com/artifact/org.eclipse.platform/org.eclipse.core.runtime -->
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.eclipse.platform</groupId>
<artifactId>org.eclipse.core.runtime</artifactId>
<version>3.14.0</version>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.6</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.eclipse.birt.runtime/org.eclipse.datatools.connectivity.oda -->
<dependency>
<groupId>org.eclipse.birt.runtime</groupId>
<artifactId>org.eclipse.datatools.connectivity.oda</artifactId>
<version>3.5.0.201603142002</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.ibm.icu/icu4j -->
<dependency>
<groupId>com.ibm.icu</groupId>
<artifactId>icu4j</artifactId>
<version>58.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.eclipse.birt.runtime/org.mozilla.javascript -->
<dependency>
<groupId>org.eclipse.birt.runtime</groupId>
<artifactId>org.mozilla.javascript</artifactId>
<version>1.7.5.v201504281450</version>
</dependency>
</dependencies>
@ -87,11 +64,6 @@
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
<configuration>
<mainClass>com.baeldung.springbootmvc.SpringBootMvcApplication</mainClass>
<layout>JAR</layout>
</configuration>
</plugin>
</plugins>
</build>

View File

@ -1,11 +1,48 @@
<?xml version="1.0" encoding="UTF-8"?>
<report xmlns="http://www.eclipse.org/birt/2005/design" version="3.2.23" id="1">
<property name="createdBy">Eclipse BIRT Designer Version 4.7.0.v201706222054</property>
<text-property name="title">new_report</text-property>
<list-property name="propertyBindings">
<structure>
<property name="name">queryText</property>
<property name="id">5</property>
</structure>
<structure>
<property name="name">HOME</property>
<property name="id">4</property>
</structure>
<structure>
<property name="name">URI</property>
<property name="id">4</property>
</structure>
<structure>
<property name="name">DELIMTYPE</property>
<property name="id">4</property>
</structure>
<structure>
<property name="name">CHARSET</property>
<property name="id">4</property>
</structure>
<structure>
<property name="name">INCLCOLUMNNAME</property>
<property name="id">4</property>
</structure>
<structure>
<property name="name">INCLTYPELINE</property>
<property name="id">4</property>
</structure>
<structure>
<property name="name">TRAILNULLCOLS</property>
<property name="id">4</property>
</structure>
<structure>
<property name="name">OdaConnProfileName</property>
<property name="id">4</property>
</structure>
<structure>
<property name="name">OdaConnProfileStorePath</property>
<property name="id">4</property>
</structure>
</list-property>
<property name="units">in</property>
<property name="iconFile">/templates/blank_report.gif</property>
@ -13,7 +50,7 @@
<property name="imageDPI">96</property>
<data-sources>
<oda-data-source extensionID="org.eclipse.datatools.connectivity.oda.flatfile" name="Data Source" id="4">
<property name="HOME">.</property>
<property name="URI">reports/data.csv</property>
<property name="DELIMTYPE">COMMA</property>
<property name="CHARSET">UTF-8</property>
<property name="INCLCOLUMNNAME">YES</property>
@ -27,18 +64,22 @@
<list-property name="columnHints">
<structure>
<property name="columnName">Student</property>
<property name="analysis">dimension</property>
<text-property name="heading">Student</text-property>
</structure>
<structure>
<property name="columnName">Math</property>
<property name="analysis">measure</property>
<text-property name="heading">Math</text-property>
</structure>
<structure>
<property name="columnName">Geography</property>
<property name="analysis">measure</property>
<text-property name="heading">Geography</text-property>
</structure>
<structure>
<property name="columnName">History</property>
<property name="analysis">measure</property>
<text-property name="heading">History</text-property>
</structure>
</list-property>
@ -98,7 +139,7 @@
<property name="nativeDataType">12</property>
</structure>
</list-property>
<xml-property name="queryText"><![CDATA[select "Student", "Math", "Geography", "History" from "./data.csv" : {"Student","Student",STRING;"Math","Math",STRING;"Geography","Geography",STRING;"History","History",STRING}]]></xml-property>
<xml-property name="queryText"><![CDATA[select "Student", "Math", "Geography", "History" from "reports/data.csv" : {"Student","Student",STRING;"Math","Math",STRING;"Geography","Geography",STRING;"History","History",STRING}]]></xml-property>
<xml-property name="designerValues"><![CDATA[<?xml version="1.0" encoding="UTF-8"?>
<model:DesignValues xmlns:design="http://www.eclipse.org/datatools/connectivity/oda/design" xmlns:model="http://www.eclipse.org/birt/report/model/adapter/odaModel">
<Version>2.0</Version>
@ -176,6 +217,21 @@
</model:DesignValues>]]></xml-property>
</oda-data-set>
</data-sets>
<styles>
<style name="table" id="49">
<property name="borderBottomStyle">solid</property>
<property name="borderLeftStyle">solid</property>
<property name="borderRightStyle">solid</property>
<property name="borderTopStyle">solid</property>
<property name="textAlign">center</property>
</style>
<style name="table-header" id="50">
<property name="borderBottomStyle">solid</property>
<property name="borderLeftStyle">solid</property>
<property name="borderRightStyle">solid</property>
<property name="borderTopStyle">solid</property>
</style>
</styles>
<page-setup>
<simple-master-page name="Simple MasterPage" id="2"/>
</page-setup>
@ -227,7 +283,7 @@
<Visible>true</Visible>
<Label>
<Caption>
<Value>Student results</Value>
<Value>Exam results</Value>
<Font>
<Size>16.0</Size>
<Bold>true</Bold>

View File

@ -1,27 +1,37 @@
package com.baeldung.birtintegration;
package com.baeldung.birt.designer;
import com.ibm.icu.util.ULocale;
import org.apache.log4j.Logger;
import org.eclipse.birt.core.exception.BirtException;
import org.eclipse.birt.core.framework.Platform;
import org.eclipse.birt.report.model.api.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import javax.annotation.PostConstruct;
import java.io.File;
import java.io.IOException;
public class ReportDesignHelper {
@SpringBootApplication
public class ReportDesignHelper implements CommandLineRunner {
private static final Logger log = Logger.getLogger(ReportDesignHelper.class);
public static final String REPORTS_FOLDER = System.getProperty("user.dir") + "/reports";
@Value("${reports.relative.path}")
private String REPORTS_FOLDER;
public static void main(String[] args) {
try {
new ReportDesignHelper().buildReport();
} catch (IOException | BirtException e) {
log.error("Exception while building report", e);
}
new SpringApplicationBuilder(ReportDesignHelper.class).web(WebApplicationType.NONE).build().run(args);
}
private void buildReport() throws IOException, BirtException {
@Override public void run(String... args) throws Exception {
buildReport();
}
public void buildReport() throws IOException, BirtException {
final DesignConfig config = new DesignConfig();
final IDesignEngine engine;
@ -90,4 +100,5 @@ public class ReportDesignHelper {
design.close();
log.info("Report generated");
}
}

View File

@ -0,0 +1,29 @@
package com.baeldung.birt.engine;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@SpringBootApplication
@EnableWebMvc
public class BirtIntegrationApplication implements WebMvcConfigurer {
@Value("${reports.relative.path}")
private String reportsPath;
@Value("${images.relative.path}")
private String imagesPath;
public static void main(final String[] args) {
SpringApplication.run(BirtIntegrationApplication.class, args);
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry
.addResourceHandler(reportsPath + imagesPath + "/**")
.addResourceLocations("file:///" + System.getProperty("user.dir") + "/" + reportsPath + imagesPath);
}
}

View File

@ -1,29 +1,20 @@
package com.baeldung.birtintegration.controller;
package com.baeldung.birt.engine.controller;
import com.baeldung.birtintegration.dto.Report;
import com.baeldung.birtintegration.service.BirtReportService;
import com.baeldung.birt.engine.dto.OutputType;
import com.baeldung.birt.engine.dto.Report;
import com.baeldung.birt.engine.service.BirtReportService;
import org.apache.log4j.Logger;
import org.eclipse.birt.report.engine.api.EngineException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
/**
* Report controller class
*
* @author koolshams
*
*/
@Controller
public class BirtReportController {
private static final Logger log = Logger.getLogger(BirtReportController.class);
@ -53,16 +44,16 @@ public class BirtReportController {
return ResponseEntity.ok().build();
}
/**
* Generate full report
*/
@RequestMapping(method = RequestMethod.GET, value = "/report/{name}")
@ResponseBody
public void generateFullReport(HttpServletResponse response, HttpServletRequest request,
@PathVariable("name") String name) throws EngineException, IOException {
@PathVariable("name") String name, @RequestParam("output") String output) throws EngineException, IOException {
log.info("Generating full report: " + name);
reportService.generateMainReport(name, response, request);
OutputType format = OutputType.from(output);
reportService.generateMainReport(name, format, response, request);
}
}

View File

@ -0,0 +1,21 @@
package com.baeldung.birt.engine.dto;
import org.eclipse.birt.report.engine.api.IRenderOption;
public enum OutputType {
HTML(IRenderOption.OUTPUT_FORMAT_HTML),
PDF(IRenderOption.OUTPUT_FORMAT_PDF),
INVALID("invalid");
String val;
OutputType(String val) {
this.val = val;
}
public static OutputType from(String text) {
for (OutputType output : values()) {
if(output.val.equalsIgnoreCase(text)) return output;
}
return INVALID;
}
}

View File

@ -0,0 +1,39 @@
package com.baeldung.birt.engine.dto;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.RequiredArgsConstructor;
import java.util.ArrayList;
import java.util.List;
/**
* Report DTO class
*/
@Data
@NoArgsConstructor
public class Report {
private String title;
private String name;
private List<Parameter> parameters;
public Report(String title, String name) {
this.title = title;
this.name = name;
}
@Data
@NoArgsConstructor
@AllArgsConstructor
public static class Parameter {
private String title;
private String name;
private ParameterType type;
}
public enum ParameterType {
INT, STRING;
}
}

View File

@ -1,14 +1,16 @@
package com.baeldung.birtintegration.service;
package com.baeldung.birt.engine.service;
import com.baeldung.birtintegration.ReportDesignHelper;
import com.baeldung.birtintegration.dto.Report;
import com.baeldung.birt.engine.dto.OutputType;
import com.baeldung.birt.engine.dto.Report;
import org.eclipse.birt.core.exception.BirtException;
import org.eclipse.birt.core.framework.Platform;
import org.eclipse.birt.report.engine.api.*;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
@ -20,17 +22,24 @@ import java.util.*;
@Service
public class BirtReportService implements ApplicationContextAware, DisposableBean {
@Value("${reports.relative.path}")
private String reportsPath;
@Value("${images.relative.path}")
private String imagesPath;
private HTMLServerImageHandler htmlImageHandler = new HTMLServerImageHandler();
@Autowired
private ResourceLoader resourceLoader;
@Autowired
private ServletContext servletContext;
private IReportEngine birtEngine;
private ApplicationContext context;
private String imageFolder;
private Map<String, IReportRunnable> reports = new HashMap<>();
private static final String IMAGE_FOLDER = "/images";
@SuppressWarnings("unchecked")
@PostConstruct
protected void initialize() throws BirtException {
@ -40,6 +49,7 @@ public class BirtReportService implements ApplicationContextAware, DisposableBea
IReportEngineFactory factory = (IReportEngineFactory) Platform
.createFactoryObject(IReportEngineFactory.EXTENSION_REPORT_ENGINE_FACTORY);
birtEngine = factory.createReportEngine(config);
imageFolder = System.getProperty("user.dir") + File.separatorChar + reportsPath + imagesPath;
loadReports();
}
@ -53,7 +63,7 @@ public class BirtReportService implements ApplicationContextAware, DisposableBea
*
*/
public void loadReports() throws EngineException {
File folder = new File(ReportDesignHelper.REPORTS_FOLDER);
File folder = new File(reportsPath);
for (String file : Objects.requireNonNull(folder.list())) {
if (!file.endsWith(".rptdesign")) {
continue;
@ -88,8 +98,17 @@ public class BirtReportService implements ApplicationContextAware, DisposableBea
return Report.ParameterType.STRING;
}
public void generateMainReport(String reportName, HttpServletResponse response, HttpServletRequest request) {
generateHTMLReport(reports.get(reportName), response, request);
public void generateMainReport(String reportName, OutputType output, HttpServletResponse response, HttpServletRequest request) {
switch (output) {
case HTML:
generateHTMLReport(reports.get(reportName), response, request);
break;
case PDF:
generatePDFReport(reports.get(reportName), response, request);
break;
default:
throw new IllegalArgumentException("Output type not recognized:" + output);
}
}
/**
@ -102,6 +121,9 @@ public class BirtReportService implements ApplicationContextAware, DisposableBea
IRenderOption options = new RenderOption();
HTMLRenderOption htmlOptions = new HTMLRenderOption(options);
htmlOptions.setOutputFormat("html");
htmlOptions.setBaseImageURL("/" + reportsPath + imagesPath);
htmlOptions.setImageDirectory(imageFolder);
htmlOptions.setImageHandler(htmlImageHandler);
runAndRenderTask.setRenderOption(htmlOptions);
runAndRenderTask.getAppContext().put(EngineConstants.APPCONTEXT_BIRT_VIEWER_HTTPSERVET_REQUEST, request);
@ -115,6 +137,25 @@ public class BirtReportService implements ApplicationContextAware, DisposableBea
}
}
private void generatePDFReport(IReportRunnable report, HttpServletResponse response, HttpServletRequest request) {
IRunAndRenderTask runAndRenderTask = birtEngine.createRunAndRenderTask(report);
response.setContentType(birtEngine.getMIMEType("pdf"));
IRenderOption options = new RenderOption();
PDFRenderOption pdfRenderOption = new PDFRenderOption(options);
pdfRenderOption.setOutputFormat("pdf");
runAndRenderTask.setRenderOption(pdfRenderOption);
runAndRenderTask.getAppContext().put(EngineConstants.APPCONTEXT_PDF_RENDER_CONTEXT, request);
try {
pdfRenderOption.setOutputStream(response.getOutputStream());
runAndRenderTask.run();
} catch (Exception e) {
throw new RuntimeException(e.getMessage(), e);
} finally {
runAndRenderTask.close();
}
}
@Override public void destroy() {
birtEngine.destroy();
Platform.shutdown();

View File

@ -1,55 +0,0 @@
package com.baeldung.birtintegration;
import org.eclipse.birt.core.exception.BirtException;
import org.eclipse.birt.core.framework.Platform;
import org.eclipse.birt.report.engine.api.EngineConfig;
import org.eclipse.birt.report.engine.api.IReportEngine;
import org.eclipse.birt.report.engine.api.IReportEngineFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
//@Configuration
public class BirtConfiguration implements ApplicationContextAware, DisposableBean {
private ApplicationContext applicationContext;
private IReportEngine birtEngine;
@Bean
public IReportEngine getObject(){
Logger logger = LoggerFactory.getLogger(IReportEngine.class);
EngineConfig config = new EngineConfig();
//This line injects the Spring Context into the BIRT Context
config.getAppContext().put("spring", this.applicationContext );
// config.setLogConfig(logger. , this.logLevel);
try {
Platform.startup( config );
}
catch ( BirtException e ) {
throw new RuntimeException ( "Could not start the Birt engine!", e) ;
}
IReportEngineFactory factory = (IReportEngineFactory) Platform.createFactoryObject( IReportEngineFactory.EXTENSION_REPORT_ENGINE_FACTORY );
IReportEngine be = factory.createReportEngine( config );
this.birtEngine = be ;
return be ;
}
@Override public void destroy() throws Exception {
birtEngine.destroy();
Platform.shutdown() ;
}
@Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
}

View File

@ -1,11 +0,0 @@
package com.baeldung.birtintegration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class BirtIntegrationApplication {
public static void main(final String[] args) {
SpringApplication.run(BirtIntegrationApplication.class, args);
}
}

View File

@ -1,105 +0,0 @@
package com.baeldung.birtintegration.dto;
import java.util.ArrayList;
import java.util.List;
/**
* Report DTO class
*/
public class Report {
private String title;
private String name;
private List<Parameter> parameters;
public String getTitle() {
return title;
}
public Report() {
super();
parameters = new ArrayList<>();
}
public Report(String title, String name) {
this();
this.title = title;
this.name = name;
}
public void setTitle(String title) {
this.title = title;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Parameter> getParameters() {
return parameters;
}
public void setParameters(List<Parameter> parameters) {
this.parameters = parameters;
}
/**
* Parameter DTO class
*
* @author koolshams
*/
public static class Parameter {
private String title;
private String name;
private ParameterType type;
public Parameter() {
super();
}
public Parameter(String title, String name, ParameterType type) {
this();
this.title = title;
this.name = name;
this.type = type;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public ParameterType getType() {
return type;
}
public void setType(ParameterType type) {
this.type = type;
}
}
/**
* Parameter Type ENUM, currently support INT and STRING
*
* @author koolshams
*/
public enum ParameterType {
INT, STRING;
}
}

View File

@ -0,0 +1,2 @@
reports.relative.path=reports/
images.relative.path=images/

View File

@ -1,4 +1,6 @@
log4j.rootLogger=DEBUG, STDOUT
log4j.appender.STDOUT=org.apache.log4j.ConsoleAppender
log4j.appender.STDOUT.layout=org.apache.log4j.PatternLayout
log4j.appender.STDOUT.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n
log4j.appender.STDOUT.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n
log4j.logger.org.springframework=debug
log4j.logger.org.springframework.boot.autoconfigure.logging.ConditionEvaluationReportLoggingListener=info

View File

@ -1,9 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<faces-config xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd"
version="2.2">
<application>
<el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
</application>
</faces-config>

View File

@ -1,21 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>
<context-param>
<param-name>com.sun.faces.forceLoadConfiguration</param-name>
<param-value>true</param-value>
</context-param>
</web-app>

View File

@ -1,20 +0,0 @@
<f:view xmlns="http://www.w3c.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html">
<h:head>
<meta charset="utf-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
<title>TO-DO application</title>
</h:head>
<h:body>
<div>
<p>Welcome in the TO-DO application!</p>
<p style="height:50px">
This is a static message rendered from xhtml.
<h:form>
<h:commandButton value="Load To-do page!" action="#{jsfController.loadTodoPage}" />
</h:form>
</p>
</div>
</h:body>
</f:view>

Some files were not shown because too many files have changed in this diff Show More