package org.saiku.olap.query;

import java.util.Properties;

import org.olap4j.Axis;

public class QueryProperties {

	public abstract class QueryProperty {

		protected String key;
		protected String value;
		protected OlapQuery query;

		public static final String KEY_NONEMPTY_ROWS	= "saiku.olap.query.nonempty.rows"; //$NON-NLS-1$
		public static final String KEY_NONEMPTY_COLUMNS = "saiku.olap.query.nonempty.columns"; //$NON-NLS-1$
		public final String[] KEYS = new String[] { KEY_NONEMPTY_ROWS, KEY_NONEMPTY_COLUMNS };

		public QueryProperty(OlapQuery query, String key, String value) {
			this.key = key;
			this.value = value;
			this.query = query;
		}

		public abstract void handle();
		public abstract Properties getProperties();

	}
	public class DummyProperty extends QueryProperty {

		public DummyProperty(OlapQuery query, String key, String value) {
			super(query,key,value);
		}

		@Override
		public void handle() {	}

		@Override
		public Properties getProperties() { return new Properties(); }

	}


	public class NonEmptyRowsProperty extends QueryProperty {

		public NonEmptyRowsProperty(OlapQuery query, String key, String value) {
			super(query,key,value);
		}

		@Override
		public void handle() {
			Boolean nonEmpty = Boolean.parseBoolean(NonEmptyRowsProperty.this.value);
			query.getAxis(Axis.ROWS).setNonEmpty(nonEmpty);
		}

		@Override
		public Properties getProperties() {
			String key = this.key;
			String value = Boolean.toString(query.getAxis(Axis.ROWS).isNonEmpty());
			Properties props = new Properties();
			props.put(key, value);
			return props;
		}

	}

	public class NonEmptyColumnsProperty extends QueryProperty {

		public NonEmptyColumnsProperty(OlapQuery query, String key, String value) {
			super(query,key,value);
		}

		@Override
		public void handle() {
			Boolean nonEmpty = Boolean.parseBoolean(NonEmptyColumnsProperty.this.value);
			query.getAxis(Axis.COLUMNS).setNonEmpty(nonEmpty);
		}
		
		@Override
		public Properties getProperties() {
			String key = this.key;
			String value = Boolean.toString(query.getAxis(Axis.COLUMNS).isNonEmpty());
			Properties props = new Properties();
			props.put(key, value);
			return props;
		}

	}
	
	public static class QueryPropertyFactory {
		private static QueryProperties q = new QueryProperties();
		public static QueryProperty getProperty(final String key, final String value, final OlapQuery query) {
			if (QueryProperty.KEY_NONEMPTY_ROWS.equals(key)) {
				return q.new NonEmptyRowsProperty(query, key, value);
			}
			if (QueryProperty.KEY_NONEMPTY_COLUMNS.equals(key)) {
				return q.new NonEmptyColumnsProperty(query, key, value);
			}
			return q.new DummyProperty(query, key, value);
		}
		
		public static Properties forQuery(OlapQuery query) {
			Properties props = new Properties();
			props.putAll(getProperty(QueryProperty.KEY_NONEMPTY_ROWS, null, query).getProperties());
			props.putAll(getProperty(QueryProperty.KEY_NONEMPTY_COLUMNS, null, query).getProperties());
			return props;
		}
	}
	

	
}

@@ -231,16 +231,105 @@
 query;%0A%0A
+%09%09public static final String KEY_NONEMPTY%09%09%09= %22saiku.olap.query.nonempty%22; //$NON-NLS-1$%0A
 %09%09public
@@ -409,32 +409,32 @@
 ; //$NON-NLS-1$%0A
-
 %09%09public static 
@@ -1683,16 +1683,694 @@
 s;%0A%09%09%7D%0A%0A
+%09%7D%0A%09%0A%09public class NonEmptyProperty extends QueryProperty %7B%0A%0A%09%09public NonEmptyProperty(OlapQuery query, String key, String value) %7B%0A%09%09%09super(query,key,value);%0A%09%09%7D%0A%0A%09%09@Override%0A%09%09public void handle() %7B%0A%09%09%09Boolean nonEmpty = Boolean.parseBoolean(NonEmptyProperty.this.value);%0A%09%09%09query.getAxis(Axis.ROWS).setNonEmpty(nonEmpty);%0A%09%09%09query.getAxis(Axis.COLUMNS).setNonEmpty(nonEmpty);%0A%09%09%7D%0A%0A%09%09@Override%0A%09%09public Properties getProperties() %7B%0A%09%09%09String key = this.key;%0A%09%09%09String value = Boolean.toString(%0A%09%09%09%09%09query.getAxis(Axis.COLUMNS).isNonEmpty() &&%0A%09%09%09%09%09query.getAxis(Axis.ROWS).isNonEmpty());%0A%09%09%09Properties props = new Properties();%0A%09%09%09props.put(key, value);%0A%09%09%09return props;%0A%09%09%7D%0A%0A
 %09%7D%0A%0A%09pub
@@ -3338,32 +3338,32 @@
 .equals(key)) %7B%0A
-
 %09%09%09%09return q.new
@@ -3412,16 +3412,124 @@
 );%0A%09%09%09%7D%0A
+%09%09%09if (QueryProperty.KEY_NONEMPTY.equals(key)) %7B%0A%09%09%09%09return q.new NonEmptyProperty(query, key, value);%0A%09%09%09%7D%0A
 %09%09%09retur

add property for both axis NON EMPTY