Updates

parent 86925e9e
This diff is collapsed.
/**
* Copyright 2008-2018 MYSET SOLUTIONS
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ro.myset.reports;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.eclipse.birt.core.data.DataTypeUtil;
import org.eclipse.birt.core.exception.BirtException;
import org.eclipse.birt.core.framework.Platform;
import org.eclipse.birt.report.engine.api.*;
import org.eclipse.birt.report.model.api.elements.DesignChoiceConstants;
import org.eclipse.core.internal.registry.RegistryProviderFactory;
import ro.myset.reports.common.Constants;
import java.net.URL;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.logging.Handler;
import java.util.logging.Level;
/**
* ReportsEngine class
*/
public class Reports {
private static final Logger LOG = LogManager.getFormatterLogger(Reports.class);
private Properties properties = null;
private EngineConfig config = null;
private IReportEngineFactory factory = null;
private IReportEngine reportEngine = null;
// common
private final String BIRT_ROOT_LOGGER = "org.eclipse.birt.report";
// output formats
private final String OUTPUT_HTML = "html";
private final String OUTPUT_PDF = "pdf";
private final String OUTPUT_ODT = "odt";
private final String OUTPUT_DOC = "doc";
private final String OUTPUT_DOCX = "docx";
private final String OUTPUT_ODS = "ods";
private final String OUTPUT_XLS = "xls";
private final String OUTPUT_XLSX = "xlsx";
private final String OUTPUT_ODP = "odp";
private final String OUTPUT_PPT = "ppt";
private final String OUTPUT_PPTX = "pptx";
private final String OUTPUT_POSTSCRIPT = "postscript";
// reports engine configuration params
private final String REPORTS_LOGGER_LEVEL = "reports.logger.level";
private final String REPORTS_RESOURCES_PATH = "reports.resources.path";
private final String REPORTS_TEMP_PATH = "reports.temp.path";
private final String REPORTS_FONTS_URL = "reports.fonts.url";
// reports engine messages
private final String MESSAGE_01 = "Invalid configuration properties";
private final String MESSAGE_02 = "Invalid 'reports.resources.path' property value";
private final String MESSAGE_03 = "Invalid 'reports.temp.path' property value";
public Reports() {
}
public Reports(Properties properties) {
this.properties = properties;
}
public final void startup() throws Exception {
LOG.debug(Constants.EVENT_INVOKED);
if (properties == null)
throw new NullPointerException(MESSAGE_01);
config = new EngineConfig();
// jul to log4j
java.util.logging.Logger reportsLogger = java.util.logging.Logger.getLogger(BIRT_ROOT_LOGGER);
reportsLogger.setUseParentHandlers(false);
for (Handler handler : reportsLogger.getHandlers()) {
handler.setLevel(Level.OFF);
}
reportsLogger.addHandler(new ReportsLogHandler());
if (properties.getProperty(REPORTS_LOGGER_LEVEL) != null) {
reportsLogger.setLevel(Level.parse(properties.getProperty(REPORTS_LOGGER_LEVEL)));
}
config.setLogger(reportsLogger);
//resources
if (properties.getProperty(REPORTS_RESOURCES_PATH) == null)
throw new NullPointerException(MESSAGE_02);
config.setResourcePath(properties.getProperty(REPORTS_RESOURCES_PATH));
// temp
if (properties.getProperty(REPORTS_TEMP_PATH) == null)
throw new NullPointerException(MESSAGE_03);
config.setTempDir(properties.getProperty(REPORTS_TEMP_PATH));
// fonts
if (properties.getProperty(REPORTS_FONTS_URL) != null)
config.setFontConfig(new URL(properties.getProperty(REPORTS_FONTS_URL)));
// startup
Platform.startup(config);
factory = (IReportEngineFactory) Platform.createFactoryObject(IReportEngineFactory.EXTENSION_REPORT_ENGINE_FACTORY);
reportEngine = factory.createReportEngine(config);
LOG.debug(Constants.EVENT_COMPLETED);
}
public final void shutdown() throws Exception {
LOG.debug(Constants.EVENT_INVOKED);
reportEngine.destroy();
Platform.shutdown();
RegistryProviderFactory.releaseDefault();
LOG.debug(Constants.EVENT_COMPLETED);
}
/**
* Process report
*/
public void process(String report,
Map reportOptions,
Map reportParameters
) throws Exception {
LOG.debug(Constants.EVENT_INVOKED);
LOG.info("report - %s, reportOptions - %s,reportParameters - %s", report, reportOptions, reportParameters);
// init report execution
IReportRunnable reportDesign = null;
IRunAndRenderTask reportTask = null;
try {
reportDesign = reportEngine.openReportDesign(report);
reportTask = reportEngine.createRunAndRenderTask(reportDesign);
//report options
IRenderOption reportRenderOption = new RenderOption(reportOptions);
if (reportRenderOption.getOption(IRenderOption.OUTPUT_FORMAT).equals(OUTPUT_HTML))
reportRenderOption = new HTMLRenderOption(reportRenderOption);
if (reportRenderOption.getOption(IRenderOption.OUTPUT_FORMAT).equals(OUTPUT_PDF))
reportRenderOption = new PDFRenderOption(reportRenderOption);
if (reportRenderOption.getOption(IRenderOption.OUTPUT_FORMAT).equals(OUTPUT_XLS) ||
reportRenderOption.getOption(IRenderOption.OUTPUT_FORMAT).equals(OUTPUT_XLSX))
reportRenderOption = new EXCELRenderOption(reportRenderOption);
// report parameters
if (reportParameters != null) {
Iterator parametersNames = reportParameters.keySet().iterator();
String parameterName = null;
while (parametersNames.hasNext()) {
parameterName = (String) parametersNames.next();
reportTask.setParameterValue(parameterName, convertParameterType(reportParameters.get(parameterName)));
}
reportTask.validateParameters();
}
// report task run
reportTask.setRenderOption(reportRenderOption);
reportTask.run();
} catch (Exception e) {
throw e;
} finally {
if (reportTask != null)
reportTask.close();
}
LOG.debug(Constants.EVENT_COMPLETED);
}
/**
* Convert report parameters
*/
private final static Object convertParameterType(Object value) throws BirtException {
if (value == null)
return null;
String type = value.getClass().getSimpleName();
if (DesignChoiceConstants.PARAM_TYPE_STRING.equals(type)) {
return DataTypeUtil.toString(value);
} else if (DesignChoiceConstants.PARAM_TYPE_INTEGER.equals(type)) {
return DataTypeUtil.toInteger(value);
} else if (DesignChoiceConstants.PARAM_TYPE_DECIMAL.equals(type)) {
return DataTypeUtil.toBigDecimal(value);
} else if (DesignChoiceConstants.PARAM_TYPE_FLOAT.equals(type)) {
return DataTypeUtil.toDouble(value);
} else if (DesignChoiceConstants.PARAM_TYPE_DATETIME.equals(type)) {
return DataTypeUtil.toDate(value);
} else if (DesignChoiceConstants.PARAM_TYPE_DATE.equals(type)) {
return DataTypeUtil.toSqlDate(value);
} else if (DesignChoiceConstants.PARAM_TYPE_TIME.equals(type)) {
return DataTypeUtil.toSqlTime(value);
} else if (DesignChoiceConstants.PARAM_TYPE_BOOLEAN.equals(type)) {
return DataTypeUtil.toBoolean(value);
}
return value;
}
/**
* Sets reports properties
*/
public final void setProperties(Properties properties) {
this.properties = properties;
}
/**
* Gets reports properties
*/
public final Properties getProperties() {
return properties;
}
public IReportEngine getReportEngine() {
return reportEngine;
}
}
/**
* Copyright 2008-2018 MYSET SOLUTIONS
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ro.myset.reports;
/**
* Class for handling Reports exceptions.
*
* @author Cristian Gheorghe Florescu
* @version 1.0
*/
public class ReportsException extends Exception {
/**
*
*/
private static final long serialVersionUID = 1L;
/**
* Constructs a new exception with the specified message.
*
* @param msg
* The text message
*/
public ReportsException(String msg) {
super(msg);
}
/**
* Constructs a new exception using text message and StackTrace values of input Exception object.
*
* @param e
* The source exception
*/
public ReportsException(Exception e) {
super(e.getLocalizedMessage());
super.setStackTrace(e.getStackTrace());
}
}
/**
* Copyright 2008-2018 MYSET SOLUTIONS
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ro.myset.reports;
import org.apache.log4j.Priority;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.message.Message;
import java.text.MessageFormat;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.LogRecord;
/**
* ReportsLogHandler java logging bridge to log4j2
*/
public class ReportsLogHandler extends Handler {
private static final String LOGGER_UNKNOWN="UNKNOWN";
public void publish(LogRecord record) {
// Logger name
String loggerName = record.getLoggerName();
if (loggerName == null)
loggerName = record.getSourceClassName();
if (loggerName == null)
loggerName = LOGGER_UNKNOWN;
// Logger level
Logger LOG = LogManager.getFormatterLogger(loggerName);
Level level = record.getLevel();
// Logger message
String message =record.getMessage();
if (record.getParameters() != null && record.getParameters().length != 0) {
MessageFormat messageFormat = new MessageFormat(record.getMessage());
message = messageFormat.format(record.getParameters());
}
// Logger write
if (Level.SEVERE.equals(level)) {
LOG.error(message);
} else if (Level.WARNING.equals(level)) {
LOG.warn(message);
} else if (Level.INFO.equals(level)) {
LOG.info(message);
} else if (Level.FINE.equals(level)) {
LOG.debug(message);
} else if (Level.FINER.equals(level)) {
LOG.trace(message);
} else if (Level.FINEST.equals(level)) {
LOG.trace(message);
} else if (Level.ALL.equals(level)) {
LOG.trace(message);
}
}
@Override
public void flush() {
// nothing to do
}
@Override
public void close() throws SecurityException {
// nothing to do
}
}
/**
* Copyright 2008-2018 MYSET SOLUTIONS
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ro.myset.reports.common;
/**
* Constants class.
*
* @author Cristian Gheorghe Florescu
* @version 1.0
*/
public final class Constants {
//=====================
// UTILS
//=====================
// System
public static final String SYS_FILE_SEPARATOR = System.getProperty("file.separator");
public static final String SYS_LINE_SEPARATOR = System.getProperty("line.separator");
// Punctuation
public static final String EMPTY = "";
public static final String EQUAL = "=";
public static final String SPACE = " ";
public static final String COMMA = ",";
public static final String DOT = ".";
public static final String COLON = ":";
public static final String SEMICOLONS = ";";
public static final String SLASH = "/";
public static final String BACKSLASH = "\\";
public static final String UNDERLINE = "_";
public static final String DASH = "-";
public static final String QUOTE = "\"";
public static final String PIPE = "|";
public static final String APOSTROPHE = "'";
// Other
public static final String ELLIPSIS = "...";
public static final String CR = "\r";
public static final String CRLF = "\r\n";
// Date
public static final String DATE_FORMAT_ISO8601_DATE="yyyy-MM-dd'T'00:00:00.000";
public static final String DATE_FORMAT_ISO8601_FULL="yyyy-MM-dd'T'HH:mm:ss.SSSXXX";
// Simple events
public static final String EVENT_INVOKED="Invoked";
public static final String EVENT_COMPLETED="Completed";
public static final String EVENT_FAILED="Failed";
// Base numbers
public static final String ZERO = "0";
public static final String ONE = "1";
//=====================
// REPORTS
//=====================
}
/**
* Copyright 2008-2018 MYSET SOLUTIONS
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ro.myset.reports.grid.services;
import ro.myset.reports.Reports;
import java.util.Map;
import java.util.Properties;
/**
* ReportsProcessorService interface.
*
* @author Cristian Gheorghe Florescu
* @version 1.0
*/
public interface ReportsProcessorService {
/**
* Gets reports properties
*/
public Properties getProperties();
/**
* Sets reports properties
*/
public void setProperties(Properties properties);
/**
* Gets reports engine
*/
public Reports getReports();
/**
* Process report
*/
public void process(String report,
Map reportOptions,
Map reportParameters
) throws Exception;
/**
* Process report asynchronous
*/
public void processAsync(String report,
Map reportOptions,
Map reportParameters
) throws Exception;
}
/**
* Copyright 2008-2018 MYSET SOLUTIONS
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ro.myset.reports.grid.services;
import org.apache.ignite.Ignite;
import org.apache.ignite.resources.IgniteInstanceResource;
import org.apache.ignite.services.Service;
import org.apache.ignite.services.ServiceContext;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import ro.myset.reports.Reports;
import ro.myset.reports.common.Constants;
import java.util.Map;
import java.util.Properties;
/**
* DatabaseProcessorServiceImpl class.
*
* @author Cristian Gheorghe Florescu
* @version 2.0
*/
public class ReportsProcessorServiceImpl implements Service, ReportsProcessorService {
private static final Logger LOG = LogManager.getFormatterLogger(ReportsProcessorServiceImpl.class);
private Properties properties = null;
private Reports reports = null;
public ReportsProcessorServiceImpl() {
LOG.debug(this);
}
@IgniteInstanceResource
private Ignite ignite;
@Override
public void cancel(ServiceContext serviceContext) {
LOG.debug(Constants.EVENT_INVOKED);
LOG.debug("%s, %s", this, reports);
try {
reports.shutdown();
} catch (Exception e) {
LOG.error(Constants.EMPTY, e);
}
LOG.debug(Constants.EVENT_COMPLETED);
}
@Override
public void init(ServiceContext serviceContext) throws Exception {
LOG.debug(Constants.EVENT_INVOKED);
LOG.debug(this);
reports = new Reports(properties);
reports.startup();
LOG.debug(Constants.EVENT_COMPLETED);
}
@Override
public void execute(ServiceContext serviceContext) throws Exception {
LOG.debug(Constants.EVENT_INVOKED);
LOG.debug("%s, %s", this, reports);
LOG.debug(Constants.EVENT_COMPLETED);
}
@Override
public Properties getProperties() {
return properties;
}
@Override
public void setProperties(Properties properties) {
this.properties = properties;
}
@Override
public Reports getReports() {
return this.reports;
}
@Override
public final void process(String report,
Map reportOptions,
Map reportParameters) throws Exception {
LOG.debug(Constants.EVENT_INVOKED);
reports.process(report, reportOptions, reportParameters);
LOG.debug(Constants.EVENT_COMPLETED);
}
@Override
public final void processAsync(final String report,
final Map reportOptions,
final Map reportParameters) throws Exception {
LOG.debug(Constants.EVENT_INVOKED);
final Reports reportsFinal = reports;
Thread t = new Thread(new Runnable() {
@Override
public void run() {
try {
reportsFinal.process(report, reportOptions, reportParameters);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
});
t.setName(ReportsProcessorServiceImpl.class.getName().concat("-processAsync"));
t.start();
LOG.debug(Constants.EVENT_COMPLETED);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/DECORATION/1.7.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/DECORATION/1.7.0 http://maven.apache.org/xsd/decoration-1.7.0.xsd">
<skin>
<groupId>org.apache.maven.skins</groupId>
<artifactId>maven-fluido-skin</artifactId>
<version>1.6</version>
</skin>
<custom>
<fluidoSkin>
<topBarEnabled>false</topBarEnabled>
<sideBarEnabled>true</sideBarEnabled>
<leftColumnClass>span2</leftColumnClass>
<bodyColumnClass>span10</bodyColumnClass>
<copyrightClass>pull-right</copyrightClass>
</fluidoSkin>
</custom>
<bannerLeft>
<name>Site</name>
<src>http://maven.apache.org/images/apache-maven-project.png</src>
<href>http://www.myset.ro</href>
</bannerLeft>
<bannerRight>
<src>https://maven.apache.org/images/maven-logo-black-on-white.png</src>
</bannerRight>
<body>
<links>
<item name="Apache" href="https://www.apache.org"/>
</links>
<menu name="Project">
<item name="Introduction" href="project-summary.html"/>
<item name="Documentation" href="#"/>
<item name="Download" href="#"/>
<item name="Release Notes" href="#"/>
<item name="Road Map" href="#"/>
</menu>
<!-- standard left menu -->
<menu ref="reports"/>
</body>
<poweredBy>
<logo name="Maven"