package test.com.barkingbrain.data.domain.simple;

import org.apache.ojb.broker.PersistenceBrokerFactory;
import org.apache.ojb.broker.singlevm.PersistenceBrokerConfiguration;
import org.apache.ojb.broker.util.configuration.ConfigurationException;
import org.apache.ojb.broker.util.ui.AsciiSplash;
import org.apache.ojb.odmg.OJB;
import org.odmg.*;

import com.barkingbrain.data.domain.simple.DeliverableStatusTypesVO;
import com.barkingbrain.ojbtest.Product;

public class UpdateTest {
    private static String databaseName;
    private Implementation odmg;
    private Database db;

    public UpdateTest() {
        setUp();
    }

    /** Sets up the fixture, for example, open a network connection. This method is called before a test is executed. */
    protected void setUp() {
        AsciiSplash as =  new  AsciiSplash();
		System.out.println(as.getSplashArt());

         try
        {
            databaseName =
                ((PersistenceBrokerConfiguration) PersistenceBrokerFactory
                    .getConfigurator()
                    .getConfigurationFor(null))
                    .getRepositoryFilename();
            System.out.println(">>>Got DB Name from PersistenceBrokerFactory");
        }
        catch (ConfigurationException e)
        {
            System.out.println(">>>Failed to get DB Name from PersistenceBrokerFactory, using default");
            databaseName = "repository.xml";
        }

        // get facade instance
        odmg = OJB.getInstance();
        db = odmg.newDatabase();
        //open database
        try
        {
            System.out.println(">>>Opening DB \"" + databaseName + "\"...");
            db.open(databaseName, Database.OPEN_READ_WRITE);
        }
        catch (ODMGException ex)
        {
            System.out.println(">>>Exception opening DB: " + ex.getMessage());
            ex.printStackTrace();
        }
    }

    public void testAdding() {
        // now perform persistence operations
        Transaction tx = null;
        // 3. open transaction
        tx = odmg.newTransaction();
        tx.begin();
        // 4. acquire write lock on new object

        DeliverableStatusTypesVO dvo = new DeliverableStatusTypesVO();
        dvo.setDescription("Original Description");
        dvo.setTypeName("Original Name");

        tx.lock(dvo, Transaction.WRITE);

        // 5. commit transaction
        tx.commit();

        testUpdating(dvo);
        testUpdatingWithQuery(dvo);

    }

    public void testUpdating(DeliverableStatusTypesVO dvo) {

        System.out.println("attempting to update with out a query");

        dvo.setDescription("Updated description");
        dvo.setTypeName("Updated name");

        // We don't have a reference to the selected Product.
        // So first we have to lookup the object.

        // 1. build oql query to select product by id:
        String oqlQuery = "select * from " + DeliverableStatusTypesVO.class.getName() + " where id = " + dvo.getID();

        Database db = odmg.getDatabase(null); // the current DB
        Transaction tx = null;
        try
        {
            // 2. start transaction
            tx = odmg.newTransaction();
            tx.begin();

            // 4. lock the product for write access
            tx.lock(dvo, tx.WRITE);

            // 6. commit transaction
            tx.commit();
        }
        catch (Throwable t)
        {
            // rollback in case of errors
            tx.abort();
            t.printStackTrace();
        }

    }

    public void testUpdatingWithQuery(DeliverableStatusTypesVO dvo) {

        System.out.println("attempting to update");

        dvo.setDescription("Updated description");
        dvo.setTypeName("Updated name");

        // We don't have a reference to the selected Product.
        // So first we have to lookup the object.

        // 1. build oql query to select product by id:
        String oqlQuery = "select * from " + DeliverableStatusTypesVO.class.getName() + " where id = " + dvo.getID();

        Database db = odmg.getDatabase(null); // the current DB
        Transaction tx = null;
        try
        {
            // 2. start transaction
            tx = odmg.newTransaction();
            tx.begin();

            // 3. lookup the product specified by query
            OQLQuery query = odmg.newOQLQuery();
            query.create(oqlQuery);
            DList result = (DList) query.execute();
            DeliverableStatusTypesVO toBeEdited = (DeliverableStatusTypesVO) result.get(0);

            // 4. lock the product for write access
            tx.lock(dvo, tx.WRITE);

            // 6. commit transaction
            tx.commit();
        }
        catch (Throwable t)
        {
            // rollback in case of errors
            tx.abort();
            t.printStackTrace();
        }

    }


    /** Tears down the fixture, for example, close a network connection. This method is called after a test is executed. */
    protected void tearDown() {
        // Write your code here
    }

    public static void main(String[] args) {
        UpdateTest t = new UpdateTest();

            t.testAdding();
    }
}

