Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Sample to show lineage on a pentaho reporting parameter. This is meant to be run from within the reporting-engine-core tests, as then you'll have all the environment set up correctly.
/*
* This program is free software; you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License, version 2.1 as published by the Free Software
* Foundation.
*
* You should have received a copy of the GNU Lesser General Public License along with this
* program; if not, you can obtain a copy at http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
* or from the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
*
* Copyright (c) 2006 - 2015 Pentaho Corporation.. All rights reserved.
*/
package org.pentaho.reporting.engine.classic.core.parameter;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Locale;
import java.util.Set;
import java.util.TimeZone;
import javax.naming.spi.NamingManager;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.pentaho.reporting.engine.classic.core.ClassicEngineBoot;
import org.pentaho.reporting.engine.classic.core.CompoundDataFactory;
import org.pentaho.reporting.engine.classic.core.DataFactory;
import org.pentaho.reporting.engine.classic.core.DataFactoryContext;
import org.pentaho.reporting.engine.classic.core.MasterReport;
import org.pentaho.reporting.engine.classic.core.designtime.datafactory.DesignTimeDataFactoryContext;
import org.pentaho.reporting.engine.classic.core.modules.misc.datafactory.sql.JndiConnectionProvider;
import org.pentaho.reporting.engine.classic.core.modules.misc.datafactory.sql.SQLReportDataFactory;
import org.pentaho.reporting.engine.classic.core.parameters.DefaultListParameter;
import org.pentaho.reporting.engine.classic.core.parameters.DefaultParameterContext;
import org.pentaho.reporting.engine.classic.core.parameters.ModifiableReportParameterDefinition;
import org.pentaho.reporting.engine.classic.core.parameters.ParameterAttributeNames;
import org.pentaho.reporting.engine.classic.core.parameters.ParameterDefinitionEntry;
import org.pentaho.reporting.engine.classic.core.parameters.ReportParameterDefinition;
import org.pentaho.reporting.engine.classic.core.testsupport.DebugJndiContextFactoryBuilder;
import org.pentaho.reporting.libraries.formula.parser.ParseException;
import org.pentaho.reporting.libraries.formula.util.FormulaUtil;
public class ParameterLineageTest
{
@Before
public void setUp() throws Exception
{
// standard test case setup here ...
Locale.setDefault(Locale.US);
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
// enforce binary compatibility for the xml-files so that comparing them can be faster.
ClassicEngineBoot.getInstance().start();
if (NamingManager.hasInitialContextFactoryBuilder() == false)
{
NamingManager.setInitialContextFactoryBuilder(new DebugJndiContextFactoryBuilder());
}
}
public Set<String> performLineage() throws Exception
{
MasterReport report = definedReport();
ReportParameterDefinition pdef = report.getParameterDefinition();
ParameterDefinitionEntry p = pdef.getParameterDefinition(0);
DefaultParameterContext pc = new DefaultParameterContext(report);
// parameter can reference fields in several places
// in default- and post-processing formulas
String def = p.getParameterAttribute(ParameterAttributeNames.Core.NAMESPACE, ParameterAttributeNames.Core.DEFAULT_VALUE_FORMULA, pc);
String post = p.getParameterAttribute(ParameterAttributeNames.Core.NAMESPACE, ParameterAttributeNames.Core.POST_PROCESSOR_FORMULA, pc);
Set<String> fields = new HashSet<String>();
fields.addAll(getFields(def));
fields.addAll(getFields(post));
if (p instanceof DefaultListParameter)
{
String query = ((DefaultListParameter) p).getQueryName();
CompoundDataFactory normalize = CompoundDataFactory.normalize(report.getDataFactory());
normalize.initialize(new DesignTimeDataFactoryContext(report));
try
{
DataFactory dataFactoryForQuery = normalize.getDataFactoryForQuery(query);
String[] referencedFields = dataFactoryForQuery.getMetaData().getReferencedFields(dataFactoryForQuery, query, pc.getParameterValues());
if (referencedFields == null) {
// we cannot tell ...
return null;
}
fields.addAll(Arrays.asList(referencedFields));
}
finally
{
normalize.close();
}
}
return fields;
}
@Test
public void testLineage() throws Exception
{
Set<String> x = performLineage();
System.out.println(x);
Assert.assertEquals(new HashSet<String>(Arrays.asList("param", "::org.pentaho.reporting::query-limit")), x);
}
private Set<String> getFields(String formula) throws ParseException
{
HashSet<String> retval = new HashSet<String>();
if (formula != null)
{
final Object[] objects = FormulaUtil.getReferences(formula);
for (Object object : objects)
{
if (object instanceof String)
{
retval.add((String) object);
}
}
}
return retval;
}
protected MasterReport definedReport()
{
JndiConnectionProvider sampleData = new JndiConnectionProvider();
sampleData.setConnectionPath("SampleData");
SQLReportDataFactory df = new SQLReportDataFactory(sampleData);
df.setQuery("With Params", "SELECT * FROM CUSTOMERS WHERE CUSTOMERNUMBER=${param}");
MasterReport report = new MasterReport();
report.setDataFactory(df);
DefaultListParameter p = new DefaultListParameter("With Params", "CUSTOMERNUMBER", "CUSTOMERNAME", "p", false, true, BigInteger.class);
ModifiableReportParameterDefinition pdef = report.getModifiableParameterDefinition();
pdef.addParameterDefinition(p);
return report;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.