/**
 * 
 */
package org.intermine.webservice.server.output;

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

/**
 * @author Alex Kalderimis
 *
 */
public class JSONObjectFormatter extends Formatter {
	
	public static final String KEY_VIEWS = "views";
	public static final String KEY_ROOT_CLASS = "rootClass";
	public static final String KEY_MODEL_NAME = "modelName";
	public static final String KEY_TIME = "executionTime";
	
	/**
	 * Constructor
	 */
	public JSONObjectFormatter() {
		// empty constructor
	}

	/**
	 * Closes the remaining open brackets (the root class array, and the 
	 * overall result set object).
	 * @see org.intermine.webservice.server.output.Formatter#formatFooter()
	 */
	@Override
	public String formatFooter() {
		return "]}";
	}

	/* (non-Javadoc)
	 * @see org.intermine.webservice.server.output.Formatter#formatHeader(java.util.Map)
	 */
	@Override
	public String formatHeader(Map<String, String> attributes) {
		String rootClass = attributes.get("rootClass");
		String views = attributes.get("views");
		String model = attributes.get("modelName");
		String time  = attributes.get("executionTime");
		return "{" +
					"'views':" + views + "," +
					"'model':'" + model + "'," +
					"'executed_at':'" + time + "'," + 
					"'root_class':'" + rootClass + "'," + 
					"'results':[";
	}

	/**
	 * In normal cases a list with a single json string item is expected. 
	 * But just in case, this formatter will simply join any strings
	 * it gets given, delimiting with a comma. It is the responsibility of 
	 * whoever is feeding me these lines to add any necessary commas between
	 * them.
	 * @see org.intermine.webservice.server.output.Formatter#formatResult(java.util.List)
	 */
	@Override
	public String formatResult(List<String> resultRow) {
		if (resultRow.isEmpty()) return "";
		Iterator<String> iter = resultRow.iterator();
		StringBuffer buffer = new StringBuffer(iter.next());
		while (iter.hasNext()) {
			buffer.append(",").append(iter.next());
		}
		return buffer.toString();	
	}

}

@@ -1,16 +1,4 @@
-/**%0A * %0A */%0A
 pack
@@ -46,1991 +46,507 @@
 t;%0A%0A
-import java.util.Iterator;%0Aimport java.util.List;%0Aimport java.util.Map;%0A%0A/**%0A * @author Alex Kalderimis%0A *%0A */%0Apublic class JSONObjectFormatter extends Formatter %7B%0A%09%0A%09public static final String KEY_VIEWS = %22views%22;%0A%09public static final String KEY_ROOT_CLASS = %22rootClass%22;%0A%09public static final String KEY_MODEL_NAME = %22modelName%22;%0A%09public static final String KEY_TIME = %22executionTime%22;%0A%09%0A%09/**%0A%09 * Constructor%0A%09 */%0A%09public JSONObjectFormatter() %7B%0A%09%09// empty constructor%0A%09%7D%0A%0A%09/**%0A%09 * Closes the remaining open brackets (the root class array, and the %0A%09 * overall result set object).%0A%09 * @see org.intermine.webservice.server.output.Formatter#formatFooter()%0A%09 */%0A%09@Override%0A%09public String formatFooter() %7B%0A%09%09return %22%5D%7D%22;%0A%09%7D%0A%0A%09/* (non-Javadoc)%0A%09 * @see org.intermine.webservice.server.output.Formatter#formatHeader(java.util.Map)%0A%09 */%0A%09@Override%0A%09public String formatHeader(Map%3CString, String%3E attributes) %7B%0A%09%09String rootClass = attributes.get(%22rootClass%22);%0A%09%09String views = attributes.get(%22views%22);%0A%09%09String model = attributes.get(%22modelName%22);%0A%09%09String time  = attributes.get(%22executionTime%22);%0A%09%09return %22%7B%22 +%0A%09%09%09%09%09%22'views':%22 + views + %22,%22 +%0A%09%09%09%09%09%22'model':'%22 + model + %22',%22 +%0A%09%09%09%09%09%22'executed_at':'%22 + time + %22',%22 + %0A%09%09%09%09%09%22'root_class':'%22 + rootClass + %22',%22 + %0A%09%09%09%09%09%22'results':%5B%22;%0A%09%7D%0A%0A%09
+/*%0A * Copyright (C) 2002-2011 FlyMine%0A *%0A * This code may be freely distributed and modified under the%0A * terms of the GNU Lesser General Public Licence.  This should%0A * be distributed with the code.  See the LICENSE file for more%0A * information or http://www.gnu.org/copyleft/lesser.html.%0A *%0A */%0A%0A
 /**%0A
-%09
  * 
-In normal cases a list with a single json string item is expected. %0A%09 * But just in
+@author Alexis Kalderimis%0A *%0A */%0Apublic
  c
+l
 as
-e, this formatter will simply join any strings%0A%09 * it gets given, delimiting with a comma. It is the responsibility of %0A%09 * whoever is feeding me these lines to add any necessary commas between%0A%09 * them.%0A%09 * @see org.intermine.webservice.server.output.Formatter#formatResult(java.util.List)%0A%09 */%0A%09@Override%0A%09public String formatResult(List%3CString%3E resultRow) %7B%0A%09%09if (resultRow.isEmpty()) return %22%22;%0A%09%09Iterator%3CString%3E iter = resultRow.iterator();%0A%09%09StringBuffer buffer = new StringBuffer(iter.next());%0A%09%09while (iter.hasNext()) %7B%0A%09%09%09buffer.append(%22,%22).append(iter.next());%0A%09%09%7D%0A%09%09return buffer.toString();%09%0A%09
+s JSONObjectFormatter extends JSONFormatter%0A%7B%0A%0A    /**%0A     * Constructor%0A     */%0A    public JSONObjectFormatter() %7B%0A        // empty constructor%0A    
 %7D%0A%0A%7D

Uses the abstract class JSONFormatter