package org.intermine.webservice.server.output;


import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.intermine.api.results.ResultElement;
import org.json.JSONObject;

public class JSONSummaryProcessor extends JSONResultProcessor {

	@Override
	protected Iterator<? extends Object> getResultsIterator(Iterator<List<ResultElement>> it) {
		return new SummaryIterator(it);
	}
	
	private static class SummaryIterator implements Iterator<JSONObject>{
		
		private final Iterator<List<ResultElement>> it;
		
		public SummaryIterator(Iterator<List<ResultElement>> it) {
			this.it = it;
		}

		@Override
		public boolean hasNext() {
			return it.hasNext();
		}

		@Override
		public JSONObject next() {
			List<ResultElement> row = it.next();
			Map<String, Object> dict = new HashMap<String, Object>();
			// 4 = numeric, 2 = string...
			if (row.size() == 4) {
				dict.put("min", row.get(0).getField());
				dict.put("max", row.get(1).getField());
				dict.put("average", row.get(2).getField());
				dict.put("stdev", row.get(3).getField());
			} else if (row.size() == 2) {
				dict.put("item", row.get(0).getField());
				dict.put("count", row.get(1).getField());
			}
			return new JSONObject(dict);
		}

		@Override
		public void remove() {
			throw new UnsupportedOperationException();
		}
	}

}

@@ -878,16 +878,44 @@
 = string
+, 7 = numeric with histogram
 ...%0A%09%09%09i
@@ -928,17 +928,17 @@
 .size() 
-=
+%3E
 = 4) %7B%0A%09
@@ -1126,14 +1126,193 @@
 %09%09%09%7D
- else 
+%0A%09%09%09if (row.size() == 7) %7B%0A%09%09%09    dict.put(%22buckets%22, row.get(4).getField());%0A%09%09%09    dict.put(%22bucket%22, row.get(5).getField());%0A%09%09%09    dict.put(%22count%22, row.get(6).getField());%0A%09%09%09%7D%0A%09%09%09
 if (

Handle histogram data