/*******************************************************************************
 * Copyright (c) 2004, 2005 Actuate Corporation. All rights reserved. This
 * program and the accompanying materials are made available under the terms of
 * the Eclipse Public License v1.0 which accompanies this distribution, and is
 * available at http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors: Actuate Corporation - initial API and implementation
 ******************************************************************************/

package org.eclipse.birt.tests.data.engine.acceptance;

import org.eclipse.birt.data.engine.api.IBaseExpression;
import org.eclipse.birt.data.engine.api.querydefn.ConditionalExpression;
import org.eclipse.birt.data.engine.api.querydefn.FilterDefinition;
import org.eclipse.birt.data.engine.api.querydefn.ScriptExpression;

import testutil.APITestCase;
import testutil.ConfigText;

/**
 * DtE features test.
 */
public class FilterTest extends APITestCase {

	/*
	 * @see org.eclipse.birt.data.engine.api.APITestCase#getDataSourceInfo()
	 */
	protected DataSourceInfo getDataSourceInfo() {
		return new DataSourceInfo(ConfigText
				.getString("Api.TestData.TableName"), ConfigText
				.getString("Api.TestData.TableSQL"), ConfigText
				.getString("Api.TestData.TestDataFileName"));
	}

	/**
	 * Test Integer input parameter is supported if set it in data set dialog
	 * and check all the data which are qualified will list in preview tab
	 */
	public void test1_Integer_input_parameter() throws Exception {

		String[] bindingNameRow = new String[4];

		bindingNameRow[0] = "ROW_COUNTRY";
		bindingNameRow[1] = "ROW_CITY";
		bindingNameRow[2] = "ROW_SALE_DATE";
		bindingNameRow[3] = "ROW_AMOUNT";

		IBaseExpression[] bindingExprRow = new IBaseExpression[] {
				new ScriptExpression("dataSetRow.COUNTRY", 0),
				new ScriptExpression("dataSetRow.CITY", 0),
				new ScriptExpression("dataSetRow.SALE_DATE", 0),
				new ScriptExpression("dataSetRow.AMOUNT", 0) };

		String[] bindingNameFilter = new String[1];
		bindingNameFilter[0] = "FILTER_AMOUNT";
		IBaseExpression[] bindingExprFilter = new IBaseExpression[1];
		bindingExprFilter[0] = new ScriptExpression("dataSetRow.AMOUNT");

		FilterDefinition[] filters = new FilterDefinition[] { new FilterDefinition(
				new ConditionalExpression("row.FILTER_AMOUNT",
						ConditionalExpression.OP_GE, "500")) };

		createAndRunQuery(null, null, null, null, null, null,
				bindingNameFilter, bindingExprFilter, filters, bindingNameRow,
				bindingExprRow);

		checkOutputFile();
	}

	/**
	 * Test String input parameter is supported if set it in data set dialog and
	 * check all the data which are qualified will list in preview tab
	 */
	public void test2_String_input_parameter() throws Exception {

		String[] bindingNameFilter = new String[1];
		bindingNameFilter[0] = "FILTER_CITY";
		IBaseExpression[] bindingExprFilter = new IBaseExpression[1];
		bindingExprFilter[0] = new ScriptExpression("dataSetRow.CITY");

		FilterDefinition[] filters = new FilterDefinition[] { new FilterDefinition(
				new ConditionalExpression("row.FILTER_CITY",
						ConditionalExpression.OP_EQ, "\"Chicago\"")) };

		String[] bindingNameRow = new String[4];

		bindingNameRow[0] = "ROW_COUNTRY";
		bindingNameRow[1] = "ROW_CITY";
		bindingNameRow[2] = "ROW_SALE_DATE";
		bindingNameRow[3] = "ROW_AMOUNT";

		IBaseExpression[] bindingExprRow = new IBaseExpression[] {
				new ScriptExpression("dataSetRow.COUNTRY", 0),
				new ScriptExpression("dataSetRow.CITY", 0),
				new ScriptExpression("dataSetRow.SALE_DATE", 0),
				new ScriptExpression("dataSetRow.AMOUNT", 0) };

		createAndRunQuery(null, null, null, null, null, null,
				bindingNameFilter, bindingExprFilter, filters, bindingNameRow,
				bindingExprRow);

		checkOutputFile();
	}

	/**
	 * Test Large than operator is supported by string type filter and check all
	 * the data which are qualified will list in preview tab
	 */
	public void test3_Large_than_operator() throws Exception {

		String[] bindingNameFilter = new String[1];
		bindingNameFilter[0] = "FILTER_CITY";
		IBaseExpression[] bindingExprFilter = new IBaseExpression[1];
		bindingExprFilter[0] = new ScriptExpression("dataSetRow.CITY");

		FilterDefinition[] filters = new FilterDefinition[] { new FilterDefinition(
				new ConditionalExpression("row.FILTER_CITY",
						ConditionalExpression.OP_GE, "\"New York\"")) };

		String[] bindingNameRow = new String[4];

		bindingNameRow[0] = "ROW_COUNTRY";
		bindingNameRow[1] = "ROW_CITY";
		bindingNameRow[2] = "ROW_SALE_DATE";
		bindingNameRow[3] = "ROW_AMOUNT";

		IBaseExpression[] bindingExprRow = new IBaseExpression[] {
				new ScriptExpression("dataSetRow.COUNTRY", 0),
				new ScriptExpression("dataSetRow.CITY", 0),
				new ScriptExpression("dataSetRow.SALE_DATE", 0),
				new ScriptExpression("dataSetRow.AMOUNT", 0) };

		createAndRunQuery(null, null, null, null, null, null,
				bindingNameFilter, bindingExprFilter, filters, bindingNameRow,
				bindingExprRow);
		checkOutputFile();
	}

	/**
	 * Test NOT_NULL is supported by string type filter and check all the data
	 * which are qualified will list in preview tab
	 */
	public void test4_NOT_NULL() throws Exception {

		String[] bindingNameFilter = new String[1];
		bindingNameFilter[0] = "FILTER_CITY";
		IBaseExpression[] bindingExprFilter = new IBaseExpression[1];
		bindingExprFilter[0] = new ScriptExpression("dataSetRow.CITY");

		FilterDefinition[] filters = new FilterDefinition[] { new FilterDefinition(
				new ConditionalExpression("row.FILTER_CITY",
						ConditionalExpression.OP_NOT_NULL)) };

		String[] bindingNameRow = new String[4];

		bindingNameRow[0] = "ROW_COUNTRY";
		bindingNameRow[1] = "ROW_CITY";
		bindingNameRow[2] = "ROW_SALE_DATE";
		bindingNameRow[3] = "ROW_AMOUNT";

		IBaseExpression[] bindingExprRow = new IBaseExpression[] {
				new ScriptExpression("dataSetRow.COUNTRY", 0),
				new ScriptExpression("dataSetRow.CITY", 0),
				new ScriptExpression("dataSetRow.SALE_DATE", 0),
				new ScriptExpression("dataSetRow.AMOUNT", 0) };

		createAndRunQuery(null, null, null, null, null, null,
				bindingNameFilter, bindingExprFilter, filters, bindingNameRow,
				bindingExprRow);
		checkOutputFile();
	}

	public void test5_FilteronDatetime() throws Exception {

		String[] bindingNameRow = new String[4];

		bindingNameRow[0] = "ROW_COUNTRY";
		bindingNameRow[1] = "ROW_CITY";
		bindingNameRow[2] = "ROW_SALE_DATE";
		bindingNameRow[3] = "ROW_AMOUNT";

		IBaseExpression[] bindingExprRow = new IBaseExpression[] {
				new ScriptExpression("dataSetRow.COUNTRY", 0),
				new ScriptExpression("dataSetRow.CITY", 0),
				new ScriptExpression("dataSetRow.SALE_DATE", 0),
				new ScriptExpression("dataSetRow.AMOUNT", 0) };

		String[] bindingNameFilter = new String[1];
		bindingNameFilter[0] = "FILTER_SALE_DATE";
		IBaseExpression[] bindingExprFilter = new IBaseExpression[1];
		bindingExprFilter[0] = new ScriptExpression("dataSetRow.SALE_DATE");
		FilterDefinition[] filters = new FilterDefinition[] { new FilterDefinition(
				new ConditionalExpression("row.FILTER_SALE_DATE",
						ConditionalExpression.OP_GE,
						"'May 10, 2004 12:00:00 PM'")) };

		createAndRunQuery(null, null, null, null, null, null,
				bindingNameFilter, bindingExprFilter, filters, bindingNameRow,
				bindingExprRow);
		checkOutputFile();
	}

}