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.pmd.PmdConnectionProvider;
import org.pentaho.reporting.engine.classic.extensions.datasources.pmd.PmdDataFactory;
import org.pentaho.reporting.platform.plugin.connection.PentahoPmdConnectionProvider;
import pt.webdetails.cda.CdaEngine;
import pt.webdetails.cda.connections.ConnectionCatalog.ConnectionType;
import pt.webdetails.cda.connections.InvalidConnectionException;
import pt.webdetails.cda.connections.metadata.MetadataConnection;
import pt.webdetails.cda.settings.UnknownConnectionException;

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

  private static final ConnectionType connectionType = ConnectionType.MQL;

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

  public MqlDataAccess() {
  }

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

    final PmdDataFactory returnDataFactory = new PmdDataFactory();
    returnDataFactory.setXmiFile(connection.getMetadataConnectionInfo().getXmiFile());
    returnDataFactory.setDomainId(connection.getMetadataConnectionInfo().getDomainId());
    if (CdaEngine.getInstance().isStandalone()) {
      returnDataFactory.setConnectionProvider(new PmdConnectionProvider());
    } else {
      returnDataFactory.setConnectionProvider(new PentahoPmdConnectionProvider());
    }
    returnDataFactory.setQuery("query", getQuery());

    return returnDataFactory;
  }

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

  @Override
  public ConnectionType getConnectionType() {
    return ConnectionType.MQL;
  }
}

@@ -1765,24 +1765,190 @@
 tQuery());%0A%0A
+    try %7B%0A%09%09%09returnDataFactory.open();%0A%09%09%7D catch (ReportDataFactoryException e) %7B%0A%09%09%09throw new InvalidConnectionException(%22Error opening mql data factory.%22, e);%0A%09%09%7D%0A%0A
     return r

call open before parseQuery gets called to avoid a null ref