package org.intermine.webservice.server.output;

/*
 * Copyright (C) 2002-2011 FlyMine
 *
 * This code may be freely distributed and modified under the
 * terms of the GNU Lesser General Public Licence.  This should
 * be distributed with the code.  See the LICENSE file for more
 * information or http://www.gnu.org/copyleft/lesser.html.
 *
 */

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

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.StringEscapeUtils;

import org.intermine.api.InterMineAPI;
import org.intermine.api.results.ExportResultsIterator;
import org.intermine.api.results.ResultElement;
import org.intermine.model.InterMineObject;
import org.intermine.pathquery.Path;
import org.intermine.web.logic.PortalHelper;
import org.json.JSONArray;
import org.json.JSONObject;

/**
 * @author Alexis Kalderimis
 *
 */
public class MinimalJsonIterator implements Iterator<JSONArray>
{
    private final ExportResultsIterator subIter;
    private final InterMineAPI im;

    /**
     * Constructor
     * @param it An ExportResultsIterator that will be used internally to process the data.
     * @param im A reference to the the API settings bundle.
     */
    public MinimalJsonIterator(ExportResultsIterator it, InterMineAPI im) {
        this.subIter = it;
        this.im = im;
    }

    public JSONArray next() {
        List<ResultElement> row = subIter.next();
        List<Object> jsonRow = new ArrayList<Object>();
        for (int i = 0; i < row.size(); i++) {
            ResultElement re = row.get(i);
            jsonRow.add(re.getField());
        }
        JSONArray next = new JSONArray(jsonRow);
        return next;
    }

    public boolean hasNext() {
        return subIter.hasNext();
    }

    public void remove() {
        throw new UnsupportedOperationException("Remove is not supported for this implementation");
    }
}

@@ -372,34 +372,8 @@
 st;%0A
-import java.util.HashMap;%0A
 impo
@@ -421,125 +421,8 @@
 ist;
-%0Aimport java.util.Map;%0A%0Aimport org.apache.commons.lang.StringUtils;%0Aimport org.apache.commons.lang.StringEscapeUtils;
 %0A%0Aim
@@ -577,176 +577,22 @@
 org.
-intermine.model.InterMineObject;%0Aimport org.intermine.pathquery.Path;%0Aimport org.intermine.web.logic.PortalHelper;%0Aimport org.json.JSONArray;%0Aimport org.json.JSONObject
+json.JSONArray
 ;%0A%0A/
@@ -1327,16 +1327,162 @@
 get(i);%0A
+            if (re == null) %7B%0A                // In the case of flattened outerjoins.%0A                jsonRow.add(null);%0A            %7D else %7B%0A    
         
@@ -1513,16 +1513,30 @@
 eld());%0A
+            %7D%0A
         

Handle outerjoins with grace and wit