package pt.webdetails.cda.dataaccess;

import org.dom4j.Element;
import org.pentaho.reporting.engine.classic.core.DataFactory;
import org.pentaho.reporting.engine.classic.extensions.datasources.kettle.KettleDataFactory;
import org.pentaho.reporting.libraries.resourceloader.ResourceKey;

import pt.webdetails.cda.connections.ConnectionCatalog.ConnectionType;
import pt.webdetails.cda.connections.InvalidConnectionException;
import pt.webdetails.cda.connections.kettle.KettleConnection;
import pt.webdetails.cda.settings.UnknownConnectionException;

/**
 * Todo: Document me!
 * <p/>
 * Date: 16.02.2010
 * Time: 13:20:39
 *
 * @author Thomas Morgner.
 */
public class KettleDataAccess extends PREDataAccess
{

  public KettleDataAccess(final Element element)
  {
    super(element);
  }

  public KettleDataAccess()
  {
  }

  public DataFactory getDataFactory() throws UnknownConnectionException, InvalidConnectionException
  {
    final KettleConnection connection = (KettleConnection) getCdaSettings().getConnection(getConnectionId());

    final KettleDataFactory dataFactory = new KettleDataFactory();
    dataFactory.setQuery("query", connection.createTransformationProducer(getQuery()));
    return dataFactory;
  }

  public String getType()
  {
    return "kettle";
  }

  @Override
  public ConnectionType getConnectionType()
  {
    return ConnectionType.KETTLE;
  }
  
  /**
   * ContextKey is used to resolve the transformation file, and so must be stored in the cache key.
   */
  public ResourceKey getExtraCacheKey(){
    return getCdaSettings().getContextKey();
  }
}

@@ -1,8 +1,9 @@
+%0A
 package 
@@ -280,16 +280,171 @@
 urceKey;
+%0Aimport org.pentaho.reporting.libraries.resourceloader.ResourceKeyCreationException;%0Aimport org.pentaho.reporting.libraries.resourceloader.ResourceManager;
 %0A%0Aimport
@@ -857,16 +857,50 @@
 Access%0A%7B
+%0A  %0A  private ResourceKey fileKey;
 %0A%0A  publ
@@ -1003,16 +1003,29 @@
 s()%0A  %7B%0A
+    super();%0A
   %7D%0A%0A  p
@@ -1577,16 +1577,526 @@
 %0A  %7D%0A  %0A
+  @Override %0A  public void setCdaSettings(pt.webdetails.cda.settings.CdaSettings cdaSettings) %7B%0A    super.setCdaSettings(cdaSettings);%0A    final ResourceManager resourceManager = new ResourceManager();%0A    resourceManager.registerDefaults();%0A    try %7B%0A      fileKey = resourceManager.deriveKey(getCdaSettings().getContextKey(), %22%22);%0A    %7D catch (ResourceKeyCreationException e) %7B%0A      fileKey = null;//will blow down the road%0A    %7D%0A  %7D;%0A  %0A  protected ResourceKey getResourceKey()%7B%0A    return fileKey;%0A  %7D%0A  %0A
   /**%0A  
@@ -2199,16 +2199,28 @@
 .%0A   */%0A
+  @Override%0A
   public
@@ -2270,32 +2270,16 @@
  get
-CdaSettings().getContext
+Resource
 Key(

set resourceKey to folder for some hope of cache reuse