/*
* 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) 2008 - 2009 Pentaho Corporation, . All rights reserved.
*/
package org.pentaho.reporting.engine.classic.extensions.datasources.kettle;
import java.io.File;
import java.util.ArrayList;
import org.pentaho.di.core.exception.KettleException;
import org.pentaho.di.core.xml.XMLHandler;
import org.pentaho.di.repository.Repository;
import org.pentaho.di.trans.TransMeta;
import org.pentaho.reporting.engine.classic.core.ParameterMapping;
import org.pentaho.reporting.engine.classic.core.ReportDataFactoryException;
import org.pentaho.reporting.libraries.resourceloader.Resource;
import org.pentaho.reporting.libraries.resourceloader.ResourceException;
import org.pentaho.reporting.libraries.resourceloader.ResourceKey;
import org.pentaho.reporting.libraries.resourceloader.ResourceKeyCreationException;
import org.pentaho.reporting.libraries.resourceloader.ResourceManager;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
/**
* This class requires access to the system-properties and the local filesystem. I do not believe
* that this code can be safely executed under a restrictive security manager rule.
*
* @author Thomas Morgner
*/
public class KettleTransFromFileProducer extends AbstractKettleTransformationProducer
{
private String transformationFile;
public KettleTransFromFileProducer(final String repositoryName,
final String transformationFile,
final String stepName,
final String username,
final String password,
final String[] definedArgumentNames,
final ParameterMapping[] definedVariableNames)
{
super(repositoryName, stepName, username, password, definedArgumentNames, definedVariableNames);
this.transformationFile = transformationFile;
}
public KettleTransFromFileProducer(final String transformationFile,
final String stepName,
final String[] definedArgumentNames,
final ParameterMapping[] definedVariableNames)
{
this("", transformationFile, stepName, null, null, definedArgumentNames, definedVariableNames);
}
public String getTransformationFile()
{
return transformationFile;
}
private ResourceKey createKey(final ResourceManager resourceManager,
final ResourceKey contextKey) throws ResourceKeyCreationException
{
try
{
return resourceManager.deriveKey(contextKey, transformationFile);
}
catch (ResourceKeyCreationException e)
{
// failure is expected ..
}
return resourceManager.createKey(new File(transformationFile));
}
protected String computeFullFilename(ResourceKey key)
{
while (key != null)
{
final Object identifier = key.getIdentifier();
if (identifier instanceof File)
{
final File file = (File) identifier;
return file.getAbsolutePath();
}
key = key.getParent();
}
return null;
}
protected TransMeta loadTransformation(final Repository repository,
final ResourceManager resourceManager,
final ResourceKey contextKey)
throws ReportDataFactoryException, KettleException
{
if (transformationFile == null)
{
throw new ReportDataFactoryException("No Transformation file given");
}
if (resourceManager == null || contextKey == null)
{
return new TransMeta(transformationFile, repository);
}
try
{
final ResourceKey resourceKey = createKey(resourceManager, contextKey);
final Resource resource = resourceManager.create(resourceKey, contextKey, Document.class);
final Document document = (Document) resource.getResource();
final Node node = XMLHandler.getSubNode(document, TransMeta.XML_TAG);
final TransMeta meta = new TransMeta();
meta.loadXML(node, repository, true, null, null);
final String filename = computeFullFilename(resourceKey);
if (filename != null)
{
meta.setFilename(filename);
}
else
{
meta.setFilename(transformationFile);
}
return meta;
}
catch (ResourceException re)
{
throw new ReportDataFactoryException("Unable to load Kettle-Transformation", re);
}
}
public Object getQueryHash(final ResourceManager resourceManager, final ResourceKey resourceKey)
{
final ArrayList<Object> retval = internalGetQueryHash();
final String fullName = computeFullFilename(resourceKey);
if (fullName != null)
{
retval.add(fullName);
}
else
{
retval.add(resourceKey);
}
retval.add(transformationFile);
return retval;
}
}