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.util.Collection;
import java.util.Iterator;

import org.flymine.model.FlyMineBusinessObject;
import org.flymine.model.fulldata.Item;
import org.flymine.objectstore.ObjectStoreException;
import org.flymine.objectstore.ObjectStoreWriter;

import org.apache.log4j.Logger;
/**
 * Stores Items in an objectstore.
 *
 * @author Matthew Wakeling
 * @author Mark Woodbridge
 */
public class ObjectStoreItemWriter implements ItemWriter
{
    private ObjectStoreWriter osw;
    private int transactionCounter = 0;
    private static final int TRANSACTION_BATCH_SIZE = 10000;
    protected static final Logger LOG = Logger.getLogger(DataTranslator.class);
    /**
     * Constructs the ItemWriter with an ObjectStoreWriter.
     *
     * @param osw the ObjectStoreWriter in which to store the Items
     * @throws ObjectStoreException if the ObjectStore is already in a transaction
     */
    public ObjectStoreItemWriter(ObjectStoreWriter osw) throws ObjectStoreException {
        this.osw = osw;
        osw.beginTransaction();
    }

    /**
     * @see ItemWriter#store
     */
    public void store(Item item) throws ObjectStoreException {
        //LOG.error("storing: " + item.getIdentifier() + ", " + item.getClassName());
        for (Iterator i = item.getAttributes().iterator(); i.hasNext();) {
            osw.store((FlyMineBusinessObject) i.next());
            transactionCounter++;
        }
        for (Iterator i = item.getReferences().iterator(); i.hasNext();) {
            osw.store((FlyMineBusinessObject) i.next());
            transactionCounter++;
        }
        for (Iterator i = item.getCollections().iterator(); i.hasNext();) {
            osw.store((FlyMineBusinessObject) i.next());
            transactionCounter++;
        }
        osw.store(item);
        transactionCounter++;
        if (transactionCounter >= TRANSACTION_BATCH_SIZE) {
            osw.commitTransaction();
            osw.beginTransaction();
            transactionCounter = 0;
        }
    }

    /**
     * @see ItemWriter#storeAll
     */
    public void storeAll(Collection items) throws ObjectStoreException {
        Iterator i = items.iterator();
        while (i.hasNext()) {
            store((Item) i.next());
            i.remove(); // ?
        }
    }

    /**
     * @see ItemWriter#close
     */
    public void close() throws ObjectStoreException {
        osw.commitTransaction();
        osw.close();
    }
}



@@ -581,40 +581,8 @@
 r;%0A%0A
-import org.apache.log4j.Logger;%0A
 /**%0A
@@ -877,88 +877,8 @@
 00;%0A
-    protected static final Logger LOG = Logger.getLogger(DataTranslator.class);%0A
     
@@ -1368,94 +1368,8 @@
 n %7B%0A
-        //LOG.error(%22storing: %22 + item.getIdentifier() + %22, %22 + item.getClassName());%0A
     
@@ -2372,16 +2372,18 @@
         
+//
 i.remove
@@ -2385,19 +2385,16 @@
 emove();
- //
  ?%0A     

revert to version 1.1, checked in with log messages in error