package org.intermine.dataconversion;

/*
 * Copyright (C) 2002-2004 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.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.intermine.model.fulldata.Item;
import org.intermine.objectstore.ObjectStore;
import org.intermine.objectstore.ObjectStoreException;
import org.intermine.objectstore.query.Query;
import org.intermine.objectstore.query.QueryClass;
import org.intermine.objectstore.query.SingletonResults;

/**
 * Provides an interface between a DataTranslator and the source Item ObjectStore which it wishes to
 * read.
 *
 * @author Matthew Wakeling
 * @author Richard Smith
 */
public class ObjectStoreItemReader implements ItemReader
{
    private ObjectStoreItemPathFollowingImpl os;

    /**
     * Constructs a new ObjectStoreItemReader.
     *
     * @param os the ObjectStore
     */
    public ObjectStoreItemReader(ObjectStore os) {
        this.os = new ObjectStoreItemPathFollowingImpl(os);
    }

    /**
     * Constructs a new ObjectStoreItemReader with the given path info.
     *
     * @param os the ObjectStore
     * @param paths the paths
     */
    public ObjectStoreItemReader(ObjectStore os, Map paths) {
        this.os = new ObjectStoreItemPathFollowingImpl(os, paths);
    }

    /**
     * @see ItemReader#itemIterator
     */
    public Iterator itemIterator() throws ObjectStoreException {
        Query q = new Query();
        // database has a hard time selecting distinct on object xml
        q.setDistinct(false);
        QueryClass qc = new QueryClass(Item.class);
        q.addFrom(qc);
        q.addToSelect(qc);
        SingletonResults sr = new SingletonResults(q, os, os.getSequence());
        sr.setBatchSize(1000);
        return sr.iterator();
    }

    /**
     * @see ItemReader#getItemById
     */
    public Item getItemById(String objectId) throws ObjectStoreException {
        List results = getItemsByDescription(Collections.singleton(
                    new FieldNameAndValue("identifier", objectId, false)));
        if (results.size() > 1) {
            throw new IllegalStateException("Multiple Items in the objectstore with identifier "
                    + objectId + ", size = " + results.size()
                    + (results instanceof SingletonResults ? "query = " + ((SingletonResults)
                            results).getQuery() : ""));
        } else if (results.size() == 1) {
            return (Item) results.get(0);
        }
        return null;
    }

    /**
     * @see ItemReader#getItemsByDescription
     */
    public List getItemsByDescription(Set constraints) throws ObjectStoreException {
        return os.getItemsByDescription(constraints);
    }
}

@@ -1570,107 +1570,120 @@
   * 
-@see ItemReader#itemIterator%0A     */%0A    public Iterator itemIterator() throws ObjectStoreException
+Returns the default itemIterator Query.%0A     *%0A     * @return a Query%0A     */%0A    public Query getDefaultQuery()
  %7B%0A 
@@ -1909,24 +1909,726 @@
 Select(qc);%0A
+        return q;%0A    %7D%0A%0A    /**%0A     * @see ItemReader#itemIterator%0A     */%0A    public Iterator itemIterator() throws ObjectStoreException %7B%0A        SingletonResults sr = new SingletonResults(getDefaultQuery(), os, os.getSequence());%0A        sr.setBatchSize(1000);%0A        return sr.iterator();%0A    %7D%0A%0A    /**%0A     * Returns an iterator through the set of Items present in the source Item ObjectStore for the%0A     * given Query.%0A     *%0A     * @param q the Query - must have one element in the SELECT list, which must be an Item Object%0A     * @return an Iterator%0A     * @throws ObjectStoreException if anything goes wrong%0A     */%0A    public Iterator itemIterator(Query q) throws ObjectStoreException %7B%0A
         Sing
@@ -3000,31 +3000,86 @@
 lue(
-%22identifier%22, objectId,
+ObjectStoreItemPathFollowingImpl.IDENTIFIER, objectId,%0A                       
  fal

Allow different queries for the ObjectStoreItemReader