11a37d047SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
31a37d047SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
41a37d047SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
51a37d047SAndrew Rist  * distributed with this work for additional information
61a37d047SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
71a37d047SAndrew Rist  * to you under the Apache License, Version 2.0 (the
81a37d047SAndrew Rist  * "License"); you may not use this file except in compliance
91a37d047SAndrew Rist  * with the License.  You may obtain a copy of the License at
101a37d047SAndrew Rist  *
111a37d047SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
121a37d047SAndrew Rist  *
131a37d047SAndrew Rist  * Unless required by applicable law or agreed to in writing,
141a37d047SAndrew Rist  * software distributed under the License is distributed on an
151a37d047SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
161a37d047SAndrew Rist  * KIND, either express or implied.  See the License for the
171a37d047SAndrew Rist  * specific language governing permissions and limitations
181a37d047SAndrew Rist  * under the License.
191a37d047SAndrew Rist  *
201a37d047SAndrew Rist  *************************************************************/
211a37d047SAndrew Rist 
221a37d047SAndrew Rist 
23cdf0e10cSrcweir package com.sun.star.report.pentaho;
24cdf0e10cSrcweir 
25cdf0e10cSrcweir import com.sun.star.report.DataSourceFactory;
26cdf0e10cSrcweir import com.sun.star.report.ImageService;
27cdf0e10cSrcweir import com.sun.star.report.InputRepository;
28cdf0e10cSrcweir import com.sun.star.report.JobDefinitionException;
29cdf0e10cSrcweir import com.sun.star.report.JobProgressIndicator;
30cdf0e10cSrcweir import com.sun.star.report.JobProperties;
31cdf0e10cSrcweir import com.sun.star.report.OutputRepository;
32cdf0e10cSrcweir import com.sun.star.report.ParameterMap;
33cdf0e10cSrcweir import com.sun.star.report.ReportEngineParameterNames;
34cdf0e10cSrcweir import com.sun.star.report.ReportExecutionException;
35cdf0e10cSrcweir import com.sun.star.report.ReportJob;
36cdf0e10cSrcweir import com.sun.star.report.ReportJobDefinition;
37cdf0e10cSrcweir import com.sun.star.report.SDBCReportDataFactory;
38cdf0e10cSrcweir import com.sun.star.report.pentaho.loader.InputRepositoryLoader;
39cdf0e10cSrcweir import com.sun.star.report.pentaho.model.OfficeDetailSection;
40cdf0e10cSrcweir import com.sun.star.report.pentaho.model.OfficeDocument;
41cdf0e10cSrcweir import com.sun.star.report.pentaho.model.OfficeGroup;
42cdf0e10cSrcweir import com.sun.star.report.pentaho.model.OfficeReport;
43cdf0e10cSrcweir import com.sun.star.report.pentaho.output.chart.ChartRawReportProcessor;
44cdf0e10cSrcweir import com.sun.star.report.pentaho.output.spreadsheet.SpreadsheetRawReportProcessor;
45cdf0e10cSrcweir import com.sun.star.report.pentaho.output.text.TextRawReportProcessor;
46cdf0e10cSrcweir 
47cdf0e10cSrcweir import java.io.IOException;
48cdf0e10cSrcweir 
49cdf0e10cSrcweir import java.lang.Integer;
50cdf0e10cSrcweir 
51cdf0e10cSrcweir import java.util.ArrayList;
52cdf0e10cSrcweir import java.util.List;
53cdf0e10cSrcweir 
54cdf0e10cSrcweir import org.apache.commons.logging.Log;
55cdf0e10cSrcweir import org.apache.commons.logging.LogFactory;
56cdf0e10cSrcweir 
57cdf0e10cSrcweir import org.jfree.report.expressions.Expression;
58cdf0e10cSrcweir import org.jfree.report.expressions.FormulaExpression;
59cdf0e10cSrcweir import org.jfree.report.flow.DefaultReportJob;
60cdf0e10cSrcweir import org.jfree.report.flow.ReportProcessor;
61cdf0e10cSrcweir import org.jfree.report.flow.raw.XmlPrintReportProcessor;
62cdf0e10cSrcweir import org.jfree.report.structure.Node;
63cdf0e10cSrcweir import org.jfree.report.structure.Section;
64cdf0e10cSrcweir import org.jfree.report.util.ReportParameters;
65cdf0e10cSrcweir 
66cdf0e10cSrcweir import org.pentaho.reporting.libraries.formula.lvalues.ContextLookup;
67cdf0e10cSrcweir import org.pentaho.reporting.libraries.formula.lvalues.FormulaFunction;
68cdf0e10cSrcweir import org.pentaho.reporting.libraries.formula.lvalues.LValue;
69cdf0e10cSrcweir import org.pentaho.reporting.libraries.formula.lvalues.Term;
70cdf0e10cSrcweir import org.pentaho.reporting.libraries.formula.parser.FormulaParser;
71cdf0e10cSrcweir import org.pentaho.reporting.libraries.formula.parser.ParseException;
72cdf0e10cSrcweir import org.pentaho.reporting.libraries.resourceloader.Resource;
73cdf0e10cSrcweir import org.pentaho.reporting.libraries.resourceloader.ResourceException;
74cdf0e10cSrcweir import org.pentaho.reporting.libraries.resourceloader.ResourceManager;
75cdf0e10cSrcweir 
76cdf0e10cSrcweir 
77cdf0e10cSrcweir /**
78cdf0e10cSrcweir  * ToDo: Allow interrupting of jobs and report the report progress
79cdf0e10cSrcweir  */
80cdf0e10cSrcweir public class PentahoReportJob implements ReportJob
81cdf0e10cSrcweir {
82cdf0e10cSrcweir 
83cdf0e10cSrcweir     private static final Log LOGGER = LogFactory.getLog(PentahoReportJob.class);
84cdf0e10cSrcweir     private boolean finished;
85cdf0e10cSrcweir     private final List listeners;
86cdf0e10cSrcweir     private final DataSourceFactory dataSourceFactory;
87cdf0e10cSrcweir     private final OutputRepository outputRepository;
88cdf0e10cSrcweir     private final JobProperties jobProperties;
89cdf0e10cSrcweir     private OfficeDocument report;
90cdf0e10cSrcweir     private final ResourceManager resourceManager;
91cdf0e10cSrcweir     private final String outputName;
92cdf0e10cSrcweir     private final ImageService imageService;
93cdf0e10cSrcweir     private final InputRepository inputRepository;
94cdf0e10cSrcweir     private final ReportJobDefinition definition;
95cdf0e10cSrcweir     private final List masterValues;
96cdf0e10cSrcweir     private final List detailColumns;
97cdf0e10cSrcweir 
getDefinition()98cdf0e10cSrcweir     public ReportJobDefinition getDefinition()
99cdf0e10cSrcweir     {
100cdf0e10cSrcweir         return definition;
101cdf0e10cSrcweir     }
102cdf0e10cSrcweir 
PentahoReportJob(final ReportJobDefinition definition)103cdf0e10cSrcweir     public PentahoReportJob(final ReportJobDefinition definition)
104cdf0e10cSrcweir             throws JobDefinitionException
105cdf0e10cSrcweir     {
106cdf0e10cSrcweir         if (definition == null)
107cdf0e10cSrcweir         {
108cdf0e10cSrcweir             throw new NullPointerException();
109cdf0e10cSrcweir         }
110cdf0e10cSrcweir 
111cdf0e10cSrcweir         this.definition = definition;
112cdf0e10cSrcweir         this.listeners = new ArrayList();
113cdf0e10cSrcweir         this.jobProperties = definition.getProcessingParameters().copy();
114cdf0e10cSrcweir 
115cdf0e10cSrcweir         this.dataSourceFactory = (DataSourceFactory) jobProperties.getProperty(ReportEngineParameterNames.INPUT_DATASOURCE_FACTORY);
116cdf0e10cSrcweir         if (this.dataSourceFactory == null)
117cdf0e10cSrcweir         {
118cdf0e10cSrcweir             throw new JobDefinitionException("DataSourceFactory must not be null.");
119cdf0e10cSrcweir         }
120cdf0e10cSrcweir 
121cdf0e10cSrcweir         this.outputRepository = (OutputRepository) jobProperties.getProperty(ReportEngineParameterNames.OUTPUT_REPOSITORY);
122cdf0e10cSrcweir         if (this.outputRepository == null)
123cdf0e10cSrcweir         {
124cdf0e10cSrcweir             throw new JobDefinitionException("OutputRepository must not be null.");
125cdf0e10cSrcweir         }
126cdf0e10cSrcweir 
127cdf0e10cSrcweir         this.inputRepository =
128cdf0e10cSrcweir                 (InputRepository) jobProperties.getProperty(ReportEngineParameterNames.INPUT_REPOSITORY);
129cdf0e10cSrcweir         if (inputRepository == null)
130cdf0e10cSrcweir         {
131cdf0e10cSrcweir             throw new JobDefinitionException("InputRepository must not be null.");
132cdf0e10cSrcweir         }
133cdf0e10cSrcweir 
134cdf0e10cSrcweir         this.outputName = (String) jobProperties.getProperty(ReportEngineParameterNames.OUTPUT_NAME);
135cdf0e10cSrcweir         if (outputName == null)
136cdf0e10cSrcweir         {
137cdf0e10cSrcweir             throw new JobDefinitionException("OutputName must not be null");
138cdf0e10cSrcweir         }
139cdf0e10cSrcweir 
140cdf0e10cSrcweir         this.imageService = (ImageService) jobProperties.getProperty(ReportEngineParameterNames.IMAGE_SERVICE);
141cdf0e10cSrcweir         if (imageService == null)
142cdf0e10cSrcweir         {
143cdf0e10cSrcweir             throw new JobDefinitionException("A valid image-service implementation must be given.");
144cdf0e10cSrcweir         }
145cdf0e10cSrcweir 
146cdf0e10cSrcweir         this.masterValues = (ArrayList) jobProperties.getProperty(ReportEngineParameterNames.INPUT_MASTER_VALUES);
147cdf0e10cSrcweir         this.detailColumns = (ArrayList) jobProperties.getProperty(ReportEngineParameterNames.INPUT_DETAIL_COLUMNS);
148cdf0e10cSrcweir         Integer maxRows=(Integer) jobProperties.getProperty(ReportEngineParameterNames.MAXROWS);
149cdf0e10cSrcweir 
150cdf0e10cSrcweir         this.resourceManager = new ResourceManager();
151cdf0e10cSrcweir         this.resourceManager.registerDefaults();
152cdf0e10cSrcweir         this.resourceManager.registerLoader(new InputRepositoryLoader(inputRepository));
153cdf0e10cSrcweir 
154cdf0e10cSrcweir         try
155cdf0e10cSrcweir         {
156cdf0e10cSrcweir             this.report = parseReport(definition);
157cdf0e10cSrcweir         }
158cdf0e10cSrcweir         catch (ResourceException e)
159cdf0e10cSrcweir         {
160cdf0e10cSrcweir             throw new JobDefinitionException("Failed to parse the report.", e);
161cdf0e10cSrcweir         }
162cdf0e10cSrcweir     }
163cdf0e10cSrcweir 
parseReport(final ReportJobDefinition definition)164cdf0e10cSrcweir     private OfficeDocument parseReport(final ReportJobDefinition definition)
165cdf0e10cSrcweir             throws ResourceException, JobDefinitionException
166cdf0e10cSrcweir     {
167cdf0e10cSrcweir         final String reportResource = (String) this.jobProperties.getProperty(ReportEngineParameterNames.INPUT_NAME);
168cdf0e10cSrcweir         if (reportResource == null)
169cdf0e10cSrcweir         {
170cdf0e10cSrcweir             throw new JobDefinitionException("Report definition name must be given");
171cdf0e10cSrcweir         }
172cdf0e10cSrcweir 
173cdf0e10cSrcweir         final Resource res = resourceManager.createDirectly("sun:oo://" + reportResource, OfficeDocument.class);
174cdf0e10cSrcweir         final OfficeDocument tempReport = (OfficeDocument) res.getResource();
175cdf0e10cSrcweir         tempReport.setDataFactory(new StarReportDataFactory(dataSourceFactory));
176cdf0e10cSrcweir         tempReport.setJobProperties(definition.getProcessingParameters().copy());
177cdf0e10cSrcweir         final ReportParameters inputParameters = tempReport.getInputParameters();
178cdf0e10cSrcweir 
179cdf0e10cSrcweir         final ParameterMap queryParameters = definition.getQueryParameters();
180cdf0e10cSrcweir         final String[] paramKeys = queryParameters.keys();
181cdf0e10cSrcweir         for (int i = 0; i < paramKeys.length; i++)
182cdf0e10cSrcweir         {
183cdf0e10cSrcweir             final String key = paramKeys[i];
184cdf0e10cSrcweir             inputParameters.put(key, queryParameters.get(key));
185cdf0e10cSrcweir         }
186cdf0e10cSrcweir 
187cdf0e10cSrcweir         return tempReport;
188cdf0e10cSrcweir     }
189cdf0e10cSrcweir 
addProgressIndicator(final JobProgressIndicator indicator)190cdf0e10cSrcweir     public void addProgressIndicator(final JobProgressIndicator indicator)
191cdf0e10cSrcweir     {
192cdf0e10cSrcweir         listeners.add(indicator);
193cdf0e10cSrcweir     }
194cdf0e10cSrcweir 
195cdf0e10cSrcweir     /**
196cdf0e10cSrcweir      * Interrupt the job.
197cdf0e10cSrcweir      */
interrupt()198cdf0e10cSrcweir     public void interrupt()
199cdf0e10cSrcweir     {
200cdf0e10cSrcweir         // hey, not yet ..
201cdf0e10cSrcweir     }
202cdf0e10cSrcweir 
203cdf0e10cSrcweir     /**
204cdf0e10cSrcweir      * Queries the jobs result status.
205cdf0e10cSrcweir      *
206cdf0e10cSrcweir      * @return true, if the job is finished (or has been interrupted), false if the job
207cdf0e10cSrcweir      *         waits for activation.
208cdf0e10cSrcweir      */
isFinished()209cdf0e10cSrcweir     public boolean isFinished()
210cdf0e10cSrcweir     {
211cdf0e10cSrcweir         return finished;
212cdf0e10cSrcweir     }
213cdf0e10cSrcweir 
finish()214cdf0e10cSrcweir     public void finish()
215cdf0e10cSrcweir     {
216cdf0e10cSrcweir         finished = true;
217cdf0e10cSrcweir     }
218cdf0e10cSrcweir 
219cdf0e10cSrcweir     /**
220cdf0e10cSrcweir      * Queries the jobs execution status.
221cdf0e10cSrcweir      *
222cdf0e10cSrcweir      * @return true, if the job is currently running, false otherwise.
223cdf0e10cSrcweir      */
isRunning()224cdf0e10cSrcweir     public boolean isRunning()
225cdf0e10cSrcweir     {
226cdf0e10cSrcweir         return !finished;
227cdf0e10cSrcweir     }
228cdf0e10cSrcweir 
removeProgressIndicator(final JobProgressIndicator indicator)229cdf0e10cSrcweir     public void removeProgressIndicator(final JobProgressIndicator indicator)
230cdf0e10cSrcweir     {
231cdf0e10cSrcweir         listeners.remove(indicator);
232cdf0e10cSrcweir     }
233cdf0e10cSrcweir 
collectGroupExpressions(final Node[] nodes, final List expressions, final FormulaParser parser, final Expression reportFunctions[])234cdf0e10cSrcweir     private void collectGroupExpressions(final Node[] nodes, final List expressions, final FormulaParser parser, final Expression reportFunctions[])
235cdf0e10cSrcweir     {
236cdf0e10cSrcweir         for (int i = 0; i < nodes.length; i++)
237cdf0e10cSrcweir         {
238cdf0e10cSrcweir             final Node node = nodes[i];
239cdf0e10cSrcweir             if (node instanceof OfficeGroup)
240cdf0e10cSrcweir             {
241cdf0e10cSrcweir                 final OfficeGroup group = (OfficeGroup) node;
242cdf0e10cSrcweir                 final FormulaExpression exp = (FormulaExpression) group.getGroupingExpression();
243cdf0e10cSrcweir                 if (exp == null)
244cdf0e10cSrcweir                 {
245cdf0e10cSrcweir                     continue;
246cdf0e10cSrcweir                 }
247cdf0e10cSrcweir 
248cdf0e10cSrcweir                 try
249cdf0e10cSrcweir                 {
250cdf0e10cSrcweir                     final String expression = exp.getFormulaExpression();
251cdf0e10cSrcweir                     if (expression == null)
252cdf0e10cSrcweir                     {
253cdf0e10cSrcweir                         continue;
254cdf0e10cSrcweir                     }
255cdf0e10cSrcweir                     final FormulaFunction function = (FormulaFunction) parser.parse(expression);
256cdf0e10cSrcweir                     final LValue[] parameters = function.getChildValues();
257cdf0e10cSrcweir                     if (parameters.length > 0)
258cdf0e10cSrcweir                     {
259cdf0e10cSrcweir                         String name = parameters[0].toString();
260cdf0e10cSrcweir                         if (parameters[0] instanceof ContextLookup)
261cdf0e10cSrcweir                         {
262cdf0e10cSrcweir                             final ContextLookup context = (ContextLookup) parameters[0];
263cdf0e10cSrcweir                             name = context.getName();
264cdf0e10cSrcweir                         }
265cdf0e10cSrcweir                         for (int j = 0; j < reportFunctions.length; j++)
266cdf0e10cSrcweir                         {
267cdf0e10cSrcweir                             if (reportFunctions[j] instanceof FormulaExpression)
268cdf0e10cSrcweir                             {
269cdf0e10cSrcweir                                 final FormulaExpression reportExp = (FormulaExpression) reportFunctions[j];
270cdf0e10cSrcweir 
271cdf0e10cSrcweir                                 if (reportExp.getName().equals(name))
272cdf0e10cSrcweir                                 {
273cdf0e10cSrcweir                                     LValue val = parser.parse(reportExp.getFormulaExpression());
274cdf0e10cSrcweir                                     while( !(val instanceof ContextLookup))
275cdf0e10cSrcweir                                     {
276cdf0e10cSrcweir                                         if (val instanceof Term)
277cdf0e10cSrcweir                                         {
278cdf0e10cSrcweir                                             val = ((Term)val).getHeadValue();
279cdf0e10cSrcweir                                         }
280cdf0e10cSrcweir                                         else if (val instanceof FormulaFunction)
281cdf0e10cSrcweir                                         {
282cdf0e10cSrcweir                                             final FormulaFunction reportFunction = (FormulaFunction) val;
283cdf0e10cSrcweir                                             val = reportFunction.getChildValues()[0];
284cdf0e10cSrcweir                                         }
285cdf0e10cSrcweir                                     }
286cdf0e10cSrcweir                                     if (val instanceof ContextLookup)
287cdf0e10cSrcweir                                     {
288cdf0e10cSrcweir                                         final ContextLookup context = (ContextLookup) val;
289cdf0e10cSrcweir                                         name = context.getName();
290cdf0e10cSrcweir                                     }
291cdf0e10cSrcweir                                     break;
292cdf0e10cSrcweir                                 }
293cdf0e10cSrcweir                             }
294cdf0e10cSrcweir                         }
295cdf0e10cSrcweir 
296cdf0e10cSrcweir                         final Object[] pair = new Object[2];
297cdf0e10cSrcweir                         pair[0] = name;
298cdf0e10cSrcweir                         pair[1] = group.getAttribute(OfficeNamespaces.OOREPORT_NS, "sort-ascending");
299cdf0e10cSrcweir                         expressions.add(pair);
300cdf0e10cSrcweir                     }
301cdf0e10cSrcweir                 }
302cdf0e10cSrcweir                 catch (ParseException ex)
303cdf0e10cSrcweir                 {
304cdf0e10cSrcweir                     LOGGER.error("ReportProcessing failed", ex);
305cdf0e10cSrcweir                 }
306cdf0e10cSrcweir             }
307cdf0e10cSrcweir             else if (node instanceof OfficeDetailSection)
308cdf0e10cSrcweir             {
309cdf0e10cSrcweir                 return;
310cdf0e10cSrcweir             }
311cdf0e10cSrcweir             if (node instanceof Section)
312cdf0e10cSrcweir             {
313cdf0e10cSrcweir                 final Section section = (Section) node;
314cdf0e10cSrcweir                 collectGroupExpressions(section.getNodeArray(), expressions, parser, reportFunctions);
315cdf0e10cSrcweir             }
316cdf0e10cSrcweir         }
317cdf0e10cSrcweir     }
318cdf0e10cSrcweir 
setMetaDataProperties(DefaultReportJob job)319cdf0e10cSrcweir     private void setMetaDataProperties(DefaultReportJob job)
320cdf0e10cSrcweir     {
321cdf0e10cSrcweir         job.getConfiguration().setConfigProperty(ReportEngineParameterNames.AUTHOR, (String) jobProperties.getProperty(ReportEngineParameterNames.AUTHOR));
322cdf0e10cSrcweir         job.getConfiguration().setConfigProperty(ReportEngineParameterNames.TITLE, (String) jobProperties.getProperty(ReportEngineParameterNames.TITLE));
323cdf0e10cSrcweir     }
324cdf0e10cSrcweir 
325cdf0e10cSrcweir     /**
326cdf0e10cSrcweir      * Although we might want to run the job as soon as it has been created, sometimes it is
327cdf0e10cSrcweir      * wiser to let the user add some listeners first. If we execute at once, the user
328*9f813b30Smseidel      * either has to deal with threading code or won't receive any progress information in
329cdf0e10cSrcweir      * single threaded environments.
330cdf0e10cSrcweir      */
execute()331cdf0e10cSrcweir     public void execute()
332cdf0e10cSrcweir             throws ReportExecutionException, IOException
333cdf0e10cSrcweir     {
334cdf0e10cSrcweir         final DefaultReportJob job = new DefaultReportJob(report);
335cdf0e10cSrcweir         setMetaDataProperties(job);
336cdf0e10cSrcweir         final String contentType = (String) jobProperties.getProperty(ReportEngineParameterNames.CONTENT_TYPE);
337cdf0e10cSrcweir         //noinspection OverlyBroadCatchBlock
338cdf0e10cSrcweir         try
339cdf0e10cSrcweir         {
340cdf0e10cSrcweir             final ReportParameters parameters = job.getParameters();
341cdf0e10cSrcweir 
342cdf0e10cSrcweir             if (masterValues != null && detailColumns != null)
343cdf0e10cSrcweir             {
344cdf0e10cSrcweir                 parameters.put(SDBCReportDataFactory.MASTER_VALUES, masterValues);
345cdf0e10cSrcweir                 parameters.put(SDBCReportDataFactory.DETAIL_COLUMNS, detailColumns);
346cdf0e10cSrcweir             }
347cdf0e10cSrcweir 
348cdf0e10cSrcweir             final Node[] nodes = report.getNodeArray();
349cdf0e10cSrcweir 
350cdf0e10cSrcweir             final FormulaParser parser = new FormulaParser();
351cdf0e10cSrcweir             final ArrayList expressions = new ArrayList();
352cdf0e10cSrcweir             final OfficeReport officeReport = (OfficeReport) ((Section) nodes[0]).getNode(0);
353cdf0e10cSrcweir             final Section reportBody = (Section) officeReport.getBodySection();
354cdf0e10cSrcweir             collectGroupExpressions(reportBody.getNodeArray(), expressions, parser, officeReport.getExpressions());
355cdf0e10cSrcweir             parameters.put(SDBCReportDataFactory.GROUP_EXPRESSIONS, expressions);
356cdf0e10cSrcweir             final String command = (String) officeReport.getAttribute(OfficeNamespaces.OOREPORT_NS, "command");
357cdf0e10cSrcweir             final String commandType = (String) officeReport.getAttribute(OfficeNamespaces.OOREPORT_NS, SDBCReportDataFactory.COMMAND_TYPE);
358cdf0e10cSrcweir             final String escapeProcessing = (String) officeReport.getAttribute(OfficeNamespaces.OOREPORT_NS, SDBCReportDataFactory.ESCAPE_PROCESSING);
359cdf0e10cSrcweir             report.setQuery(command);
360cdf0e10cSrcweir             parameters.put(SDBCReportDataFactory.COMMAND_TYPE, commandType);
361cdf0e10cSrcweir             parameters.put(SDBCReportDataFactory.ESCAPE_PROCESSING, !("false".equals(escapeProcessing)));
362cdf0e10cSrcweir 
363cdf0e10cSrcweir             final String filter = (String) officeReport.getAttribute(OfficeNamespaces.OOREPORT_NS, "filter");
364cdf0e10cSrcweir             parameters.put(SDBCReportDataFactory.UNO_FILTER, filter);
365cdf0e10cSrcweir 
366cdf0e10cSrcweir             parameters.put(ReportEngineParameterNames.MAXROWS, report.getJobProperties().getProperty(ReportEngineParameterNames.MAXROWS));
367cdf0e10cSrcweir 
368cdf0e10cSrcweir             final long startTime = System.currentTimeMillis();
369cdf0e10cSrcweir             final ReportProcessor rp = getProcessorForContentType(contentType);
370cdf0e10cSrcweir             rp.processReport(job);
371cdf0e10cSrcweir             job.close();
372cdf0e10cSrcweir             final long endTime = System.currentTimeMillis();
373cdf0e10cSrcweir             LOGGER.debug("Report processing time: " + (endTime - startTime));
374cdf0e10cSrcweir         }
375cdf0e10cSrcweir         catch (final Exception e)
376cdf0e10cSrcweir         {
377cdf0e10cSrcweir             String message = e.getMessage();
378cdf0e10cSrcweir             if (message == null || message.length() == 0)
379cdf0e10cSrcweir             {
380cdf0e10cSrcweir                 message = "Failed to process the report";
381cdf0e10cSrcweir             }
382cdf0e10cSrcweir             throw new ReportExecutionException(message, e);
383cdf0e10cSrcweir         }
384cdf0e10cSrcweir 
385cdf0e10cSrcweir     }
386cdf0e10cSrcweir 
getProcessorForContentType(final String mimeType)387cdf0e10cSrcweir     protected ReportProcessor getProcessorForContentType(final String mimeType)
388cdf0e10cSrcweir             throws ReportExecutionException
389cdf0e10cSrcweir     {
390cdf0e10cSrcweir         final ReportProcessor ret;
391cdf0e10cSrcweir 
392cdf0e10cSrcweir         if (PentahoReportEngineMetaData.OPENDOCUMENT_SPREADSHEET.equals(mimeType))
393cdf0e10cSrcweir         {
394cdf0e10cSrcweir             ret = new SpreadsheetRawReportProcessor(inputRepository, outputRepository, outputName, imageService, dataSourceFactory);
395cdf0e10cSrcweir         }
396cdf0e10cSrcweir         else if (PentahoReportEngineMetaData.OPENDOCUMENT_TEXT.equals(mimeType))
397cdf0e10cSrcweir         {
398cdf0e10cSrcweir             ret = new TextRawReportProcessor(inputRepository, outputRepository, outputName, imageService, dataSourceFactory);
399cdf0e10cSrcweir         }
400cdf0e10cSrcweir         else if (PentahoReportEngineMetaData.OPENDOCUMENT_CHART.equals(mimeType))
401cdf0e10cSrcweir         {
402cdf0e10cSrcweir             ret = new ChartRawReportProcessor(inputRepository, outputRepository, outputName, imageService, dataSourceFactory);
403cdf0e10cSrcweir         }
404cdf0e10cSrcweir         else if (PentahoReportEngineMetaData.DEBUG.equals(mimeType))
405cdf0e10cSrcweir         {
406cdf0e10cSrcweir             ret = new XmlPrintReportProcessor(System.out, "ISO-8859-1");
407cdf0e10cSrcweir         }
408cdf0e10cSrcweir         else
409cdf0e10cSrcweir         {
410cdf0e10cSrcweir             throw new ReportExecutionException("Invalid mime-type");
411cdf0e10cSrcweir         }
412cdf0e10cSrcweir 
413cdf0e10cSrcweir         return ret;
414cdf0e10cSrcweir     }
415cdf0e10cSrcweir }
416