package org.saiku.service.olap.totals.aggregators;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import mondrian.util.Format;

import org.olap4j.Cell;
import org.olap4j.OlapException;
import org.olap4j.metadata.Measure;
import org.saiku.olap.util.SaikuProperties;

public abstract class TotalAggregator {
	private static final Map<String, TotalAggregator> all;
	static {
		Map<String, TotalAggregator> tmp = new HashMap<String, TotalAggregator>();
		tmp.put("sum", new SumAggregator(null));
		tmp.put("max", new MaxAggregator(null));
		tmp.put("min", new MinAggregator(null));
		tmp.put("avg", new AvgAggregator(null));
		all = Collections.unmodifiableMap(tmp);
	}
	private String formattedValue;
	protected final Format format;
	
	protected TotalAggregator(Format format) {
		this.format = format;
	}
	
	public void addData(Cell cell) {
		Object value = cell.getValue();
		if (value instanceof Number) {
			double doubleVal;
			try {
				doubleVal = cell.getDoubleValue();
				addData(doubleVal);
			} catch (OlapException e) {
				throw new RuntimeException(e);
			}
		}
	}
	
	protected abstract void addData(double data);
	public abstract Double getValue();
	public abstract TotalAggregator newInstance(Format format, Measure measure);
	
	public String getFormattedValue() {
		if (formattedValue != null)
			return formattedValue;
		else {
			Double value = getValue();
			if (value != null)
				return format.format(value);
			return "";
		}
	}
	public void setFormattedValue(String formattedValue) {
		this.formattedValue = formattedValue;
	}
	
	public TotalAggregator newInstance() {
		return newInstance("");
	}
	
	public TotalAggregator newInstance(String formatString) {
		return newInstance(new Format(formatString, SaikuProperties.locale), null);
	}
	
	public static TotalAggregator newInstanceByFunctionName(final String functionName) {
		return all.get(functionName);
	}
}
@@ -196,29 +196,32 @@
 .olap4j.
-OlapException
+metadata.Measure
 ;%0Aimport
@@ -233,39 +233,40 @@
 olap4j.metadata.
-Measure
+Property
 ;%0Aimport org.sai
@@ -739,22 +739,16 @@
 otected 
-final 
 Format f
@@ -863,16 +863,405 @@
 cell) %7B%0A
+%09%09try %7B%0A%09%09// FIXME - maybe we should try to do fetch the format here, but seems to cause some issues? infinite loop? make sure we try this only once to override existing format?%0A//%09%09if (format == null) %7B%0A//%09%09%09String formatString = (String) cell.getPropertyValue(Property.StandardCellProperty.FORMAT_STRING);%0A//%09%09%09this.format = Format.get(formatString, SaikuProperties.locale);%0A//%09%09%09%0A//%09%09%7D%0A
 %09%09Object
@@ -1344,17 +1344,9 @@
 al;%0A
-%09%09%09try %7B
 %0A
+
 %09%09%09%09
@@ -1405,16 +1405,19 @@
 eVal);%0A%09
+%09%7D%0A
 %09%09%7D catc
@@ -1423,12 +1423,8 @@
 ch (
-Olap
 Exce
@@ -1434,17 +1434,16 @@
 on e) %7B%0A
-%09
 %09%09%09throw
@@ -1470,22 +1470,18 @@
 n(e);%0A%09%09
-%09
 %7D%0A
-%09%09%7D
 %0A%09%7D%0A%09%0A%09p

add potential fix for measure format defined in query etc. - using cell format to set the format