package org.flymine.dataconversion;

/*
 * Copyright (C) 2002-2003 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.io.InputStream;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import org.flymine.metadata.Model;
import org.flymine.model.FlyMineBusinessObject;
import org.flymine.objectstore.ObjectStore;
import org.flymine.objectstore.ObjectStoreWriter;
import org.flymine.objectstore.ObjectStoreWriterFactory;
import org.flymine.objectstore.query.Query;
import org.flymine.objectstore.query.QueryClass;
import org.flymine.objectstore.query.SingletonResults;
import org.flymine.objectstore.translating.Translator;
import org.flymine.objectstore.translating.ObjectStoreTranslatingImpl;
import org.flymine.xml.full.Item;
import org.flymine.xml.full.FullRenderer;
import org.flymine.xml.full.FullParser;

import junit.framework.TestCase;

public class ItemToObjectTranslatorFunctionalTest extends TestCase
{
    ObjectStoreWriter osw;
    List items;
    
    public ItemToObjectTranslatorFunctionalTest(String arg) {
        super(arg);
    }

    public void setUp() throws Exception {
        InputStream is = getClass().getClassLoader().getResourceAsStream("test/testmodel_data.xml");
        items = FullParser.parse(is);
        osw = ObjectStoreWriterFactory.getObjectStoreWriter("osw.fulldatatest");
        ItemStore itemStore = new ItemStore(osw);
        for (Iterator i = items.iterator(); i.hasNext();) {
            itemStore.store((Item) i.next());
        }
    }

    public void tearDown() throws Exception {
        Query q = new Query();
        QueryClass qc = new QueryClass(FlyMineBusinessObject.class);
        q.addToSelect(qc);
        q.addFrom(qc);
        Collection toDelete = new SingletonResults(q, osw.getObjectStore(), osw.getObjectStore()
                .getSequence());
        Iterator iter = toDelete.iterator();
        osw.beginTransaction();
        while (iter.hasNext()) {
            FlyMineBusinessObject obj = (FlyMineBusinessObject) iter.next();
            System.out.println("Deleting " + obj);
            osw.delete(obj);
        }
        osw.commitTransaction();
        osw.close();
    }
 
    public void testTranslation() throws Exception {
        Model model = Model.getInstanceByName("testmodel");
        Translator translator = new ItemToObjectTranslator(model);
        ObjectStore os = new ObjectStoreTranslatingImpl(model, osw, translator);

        Query q = new Query();
        QueryClass qc = new QueryClass(FlyMineBusinessObject.class);
        q.addFrom(qc);
        q.addToSelect(qc);
        List objects = new SingletonResults(q, os, os.getSequence());

        assertEquals(items, FullRenderer.toItems(objects, model));
    }
}

@@ -563,16 +563,67 @@
 tStore;%0A
+import org.flymine.objectstore.ObjectStoreFactory;%0A
 import o
@@ -1110,16 +1110,56 @@
 lParser;
+%0Aimport org.flymine.xml.full.ItemHelper;
 %0A%0Aimport
@@ -1259,35 +1259,8 @@
 e%0A%7B%0A
-    ObjectStoreWriter osw;%0A
     
@@ -1555,16 +1555,34 @@
         
+ObjectStoreWriter 
 osw = Ob
@@ -1658,43 +1658,68 @@
     
-ItemStore itemStore = new ItemStore
+ObjectStoreItemWriter itemWriter = new ObjectStoreItemWriter
 (osw
@@ -1801,19 +1801,39 @@
 item
-Store.store
+Writer.store(ItemHelper.convert
 ((It
@@ -1845,16 +1845,17 @@
 .next())
+)
 ;%0A      
@@ -1854,24 +1854,52 @@
 ;%0A        %7D%0A
+        itemWriter.close();%0A
     %7D%0A%0A    p
@@ -2081,32 +2081,131 @@
  q.addFrom(qc);%0A
+        ObjectStoreWriter osw = ObjectStoreWriterFactory.getObjectStoreWriter(%22osw.fulldatatest%22);%0A
         Collecti
@@ -2900,19 +2900,64 @@
 (model, 
-osw
+ObjectStoreFactory.getObjectStore(%22os.unittest%22)
 , transl

Update to use ItemWriter