001: /*
002: * Copyright (C) 2007 Erik Swenson - erik@oreports.com
003: *
004: * This program is free software; you can redistribute it and/or modify it
005: * under the terms of the GNU General Public License as published by the Free
006: * Software Foundation; either version 2 of the License, or (at your option)
007: * any later version.
008: *
009: * This program is distributed in the hope that it will be useful, but WITHOUT
010: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
011: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
012: * more details.
013: *
014: * You should have received a copy of the GNU General Public License along with
015: * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
016: * Place - Suite 330, Boston, MA 02111-1307, USA.
017: *
018: */
019: package org.efs.openreports.engine;
020:
021: import java.io.StringWriter;
022: import java.util.List;
023: import java.util.Map;
024: import java.util.Properties;
025: import org.apache.log4j.Logger;
026: import org.apache.velocity.Template;
027: import org.apache.velocity.VelocityContext;
028: import org.apache.velocity.app.Velocity;
029: import org.efs.openreports.ReportConstants.ExportType;
030: import org.efs.openreports.engine.input.ReportEngineInput;
031: import org.efs.openreports.engine.output.QueryEngineOutput;
032: import org.efs.openreports.engine.output.ReportEngineOutput;
033: import org.efs.openreports.objects.Report;
034: import org.efs.openreports.providers.DataSourceProvider;
035: import org.efs.openreports.providers.DirectoryProvider;
036: import org.efs.openreports.providers.PropertiesProvider;
037: import org.efs.openreports.providers.ProviderException;
038:
039: public class VelocityReportEngine extends ReportEngine {
040: protected static Logger log = Logger
041: .getLogger(VelocityReportEngine.class);
042:
043: public VelocityReportEngine(DataSourceProvider dataSourceProvider,
044: DirectoryProvider directoryProvider,
045: PropertiesProvider propertiesProvider) {
046: super (dataSourceProvider, directoryProvider, propertiesProvider);
047: }
048:
049: public ReportEngineOutput generateReport(ReportEngineInput input)
050: throws ProviderException {
051: Report report = input.getReport();
052: Map parameters = input.getParameters();
053:
054: try {
055: Properties properties = new Properties();
056: properties.setProperty("file.resource.loader.path",
057: directoryProvider.getReportDirectory());
058: properties.setProperty("runtime.log.logsystem.class",
059: "org.apache.velocity.runtime.log.NullLogSystem");
060:
061: Velocity.init(properties);
062:
063: VelocityContext context = new VelocityContext();
064: context.put("parameters", parameters);
065: context.put("report", report);
066:
067: /*
068: * if report has a query, process it like a QueryReport and
069: * put the results in the VelocityContext for use in templates
070: */
071: if (report.getQuery() != null
072: && report.getQuery().trim().length() > 0) {
073: QueryReportEngine queryReport = new QueryReportEngine(
074: dataSourceProvider, directoryProvider,
075: propertiesProvider);
076: QueryEngineOutput queryOutput = (QueryEngineOutput) queryReport
077: .generateReport(input);
078:
079: context.put("results", queryOutput.getResults());
080: context.put("properties", queryOutput.getProperties());
081: }
082:
083: StringWriter writer = new StringWriter();
084:
085: Template template = Velocity.getTemplate(report.getFile());
086: template.merge(context, writer);
087:
088: ReportEngineOutput output = new ReportEngineOutput();
089: output.setContent(writer.toString().getBytes());
090:
091: if (input.getExportType() == ExportType.HTML) {
092: output
093: .setContentType(ReportEngineOutput.CONTENT_TYPE_HTML);
094: } else {
095: output
096: .setContentType(ReportEngineOutput.CONTENT_TYPE_TEXT);
097: }
098:
099: return output;
100: } catch (Exception e) {
101: throw new ProviderException(e);
102: }
103: }
104:
105: public List buildParameterList(Report report)
106: throws ProviderException {
107: throw new ProviderException(
108: "VelocityReportEngine: buildParameterList not implemented.");
109: }
110: }
|