/**
   * @param type
   * @param fileName
   * @return
   * @throws BirtException
   */
  private DataEngineContext newContext(int type, String fileName) throws BirtException {

    switch (type) {
      case DataEngineContext.MODE_GENERATION:
        {
          try {
            archiveWriter = new FileArchiveWriter(fileName);
            archiveWriter.initialize();
          } catch (IOException e) {
            throw new IllegalArgumentException(e.getMessage());
          }
          return DataEngineContext.newInstance(
              DataEngineContext.MODE_GENERATION, null, null, archiveWriter);
        }
      case DataEngineContext.MODE_PRESENTATION:
        {
          try {
            archiveReader = new FileArchiveReader(fileName);
            archiveReader.open();
          } catch (IOException e) {
            throw new IllegalArgumentException(e.getMessage());
          }
          return DataEngineContext.newInstance(
              DataEngineContext.MODE_PRESENTATION, null, archiveReader, null);
        }
      default:
        throw new IllegalArgumentException("" + type);
    }
  }
 /*
  * @see junit.framework.TestCase#setUp()
  */
 @Before
 public void mirrorCursorModelSetUp() throws Exception {
   this.scope = new ImporterTopLevel();
   DataEngineContext context =
       DataEngineContext.newInstance(DataEngineContext.DIRECT_PRESENTATION, scope, null, null);
   context.setTmpdir(this.getTempDir());
   de = (DataEngineImpl) DataEngine.newDataEngine(context);
   this.creator = new CubeUtility();
   creator.createCube(de);
   creator.createCube1(de);
   cube1 = creator.getCube(CubeUtility.cubeName, de);
   cube2 = creator.getCube(CubeUtility.timeCube, de);
 }
  /**
   * @param name
   * @param dataType
   * @param se
   * @throws BirtException
   */
  private void basicTestNoDataSet(String[] name, int[] dataType, ScriptExpression[] se)
      throws BirtException {
    DataEngine dataEngine =
        new DataEngineImpl(
            DataEngineContext.newInstance(DataEngineContext.DIRECT_PRESENTATION, null, null, null));
    QueryDefinition queryDefn = new QueryDefinition();

    for (int i = 0; i < name.length; i++) queryDefn.addBinding(new Binding(name[i], se[i]));

    IResultIterator ri = dataEngine.prepare(queryDefn).execute(null).getResultIterator();
    if (ri.next()) {
      String str = "";
      for (int i = 0; i < name.length; i++) {
        Object value = ri.getValue(name[i]);
        str += value;

        if (i < name.length - 1) str += ", ";

        if (dataType[i] == DataType.INTEGER_TYPE)
          assertTrue(value.getClass().equals(Integer.class));
        else if (dataType[i] == DataType.DOUBLE_TYPE)
          assertTrue(value.getClass().equals(Double.class));
        else if (dataType[i] == DataType.DATE_TYPE) assertTrue(value.getClass().equals(Date.class));
      }
      testPrintln(str);
    }
  }
  /** @throws Exception */
  @Test
  public void testNoDataSetWithSubQuery() throws Exception {
    // outer query without data set
    int[] dataType = new int[] {DataType.DATE_TYPE};
    String[] name = new String[] {"testColumn1"};

    IResultIterator ri2 = null;
    {
      ScriptExpression[] se =
          new ScriptExpression[] {
            new ScriptExpression("new Date()", dataType[0]),
          };

      QueryDefinition queryDefn = new QueryDefinition();
      for (int i = 0; i < name.length; i++) queryDefn.addBinding(new Binding(name[i], se[i]));

      // sub query
      String subQueryName = "TEST";
      SubqueryDefinition subQueryDefn = new SubqueryDefinition(subQueryName, queryDefn);
      for (int i = 0; i < name.length; i++)
        subQueryDefn.addBinding(
            new Binding(name[i], new ScriptExpression("row._outer." + name[i], dataType[i])));
      queryDefn.addSubquery(subQueryDefn);

      DataEngine myDataEngine =
          new DataEngineImpl(
              DataEngineContext.newInstance(
                  DataEngineContext.DIRECT_PRESENTATION, null, null, null));
      IResultIterator ri = myDataEngine.prepare(queryDefn).execute(null).getResultIterator();
      ri.next();
      ri2 = ri.getSecondaryIterator(subQueryName, null);
    }

    if (ri2.next()) {
      String str = "";
      for (int i = 0; i < name.length; i++) {
        Object value = ri2.getValue(name[i]);
        str += value;

        if (i < name.length - 1) str += ", ";

        if (dataType[0] == DataType.DATE_TYPE) assertTrue(value.getClass().equals(Date.class));
      }
      testPrintln(str);
    }
    ri2.close();
  }
  /** @throws Exception */
  @Test
  public void testNoDataSetWithNestedQuery() throws Exception {
    // outer query without data set
    String[] name = new String[] {"testColumn1"};
    IQueryResults queryResult = null;

    {
      int[] dataType = new int[] {DataType.DATE_TYPE};
      ScriptExpression[] se =
          new ScriptExpression[] {
            new ScriptExpression("new Date()", dataType[0]),
          };

      DataEngine myDataEngine =
          new DataEngineImpl(
              DataEngineContext.newInstance(
                  DataEngineContext.DIRECT_PRESENTATION, null, null, null));
      QueryDefinition queryDefn = new QueryDefinition();

      for (int i = 0; i < name.length; i++) queryDefn.addBinding(new Binding(name[i], se[i]));

      IResultIterator ri = myDataEngine.prepare(queryDefn).execute(null).getResultIterator();
      queryResult = ri.getQueryResults();
    }

    // inner query with data set
    QueryDefinition queryDefn2 = this.newReportQuery();
    for (int i = 0; i < name.length; i++)
      queryDefn2.addBinding(new Binding(name[i], new ScriptExpression("row._outer." + name[i])));

    IResultIterator ri2 =
        this.dataEngine.prepare(queryDefn2).execute(queryResult, null).getResultIterator();
    if (ri2.next()) {
      String str = "";
      for (int i = 0; i < name.length; i++) {
        Object value = ri2.getValue(name[i]);
        str += value;

        if (i < name.length - 1) str += ", ";
      }
      testPrintln(str);
    }
    ri2.close();
  }
  /**
   * Adapts a model oda data set handle
   *
   * @param modelDataSet model handle
   * @param propBindingScope Javascript scope in which to evaluate property binding expressions. If
   *     null, property binding is not resolved
   * @throws BirtException
   */
  public OdaDataSetAdapter(
      OdaDataSetHandle modelDataSet,
      Scriptable propBindingScope,
      ModelAdapter adapter,
      DataEngineContext dtContext)
      throws BirtException {
    super(modelDataSet.getQualifiedName());

    // TODO: event handler

    // Adapt base class properties
    DataAdapterUtil.adaptBaseDataSet(modelDataSet, this, adapter);

    // Adapt extended data set elements

    // Set query text; if binding exists, use its result; otherwise
    // use static design
    Expression expression = modelDataSet.getPropertyBindingExpression(OdaDataSet.QUERY_TEXT_PROP);
    org.eclipse.birt.data.engine.api.querydefn.ScriptExpression script =
        adapter.adaptExpression(expression);

    if (propBindingScope != null
        && script != null
        && DataSessionContext.MODE_UPDATE != dtContext.getMode()) {
      String queryText =
          JavascriptEvalUtil.evaluateScript(
                  null, propBindingScope, script.getText(), ScriptExpression.defaultID, 0)
              .toString();
      setQueryText(queryText);
    } else {
      setQueryText(modelDataSet.getQueryText());
    }

    // type of extended data set
    setExtensionID(modelDataSet.getExtensionID());

    // result set name
    setPrimaryResultSetName(modelDataSet.getResultSetName());

    if (modelDataSet.getPropertyHandle(IOdaDataSetModel.RESULT_SET_NUMBER_PROP).isSet())
      setPrimaryResultSetNumber(modelDataSet.getResultSetNumber());
    // static ROM properties defined by the ODA driver extension
    Map staticProps =
        DataAdapterUtil.getExtensionProperties(
            modelDataSet, modelDataSet.getExtensionPropertyDefinitionList());
    if (staticProps != null && !staticProps.isEmpty()) {
      Iterator propNamesItr = staticProps.keySet().iterator();
      while (propNamesItr.hasNext()) {
        String propName = (String) propNamesItr.next();
        assert (propName != null);
        String propValue;
        String bindingExpr = modelDataSet.getPropertyBinding(propName);
        if (propBindingScope != null && bindingExpr != null && bindingExpr.length() > 0) {
          propValue =
              JavascriptEvalUtil.evaluateScript(
                      null, propBindingScope, bindingExpr, ScriptExpression.defaultID, 0)
                  .toString();
        } else {
          propValue = (String) staticProps.get(propName);
        }
        addPublicProperty((String) propName, propValue);
      }
    }

    // private driver properties / private runtime data
    Iterator elmtIter = modelDataSet.privateDriverPropertiesIterator();
    if (elmtIter != null) {
      while (elmtIter.hasNext()) {
        ExtendedPropertyHandle modelProp = (ExtendedPropertyHandle) elmtIter.next();
        addPrivateProperty(modelProp.getName(), modelProp.getValue());
      }
    }
  }