/*
 * Copyright (c) 2009-2013 Topi Nieminen
 */
package fi.tnie.db;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Types;
import java.util.Arrays;
import java.util.Set;

import fi.tnie.db.expr.DeleteStatement;
import fi.tnie.db.expr.ddl.SQLType;
import fi.tnie.db.meta.BaseTable;
import fi.tnie.db.meta.Catalog;
import fi.tnie.db.meta.Column;
import fi.tnie.db.meta.DBMetaTestCase;
import fi.tnie.db.meta.DataType;
import fi.tnie.db.build.Builder;
import fi.tnie.db.build.BuilderTest;
import fi.tnie.db.build.CatalogContext;
import fi.tnie.db.build.pub.AbstractContinent;
import fi.tnie.db.build.pub.Continent;
import fi.tnie.db.build.pub.ContinentImpl;
import fi.tnie.db.build.pub.DefaultContinent;
import fi.tnie.db.build.pub.PublicFactory;
import fi.tnie.db.build.pub.Continent.Attribute;
import fi.tnie.db.build.pub.Continent.Query;
import fi.tnie.db.build.pub.Continent.Reference;

public class PersistenceManagerTest extends DBMetaTestCase  {

    public void testPersistenceManager() 
        throws Exception {
        
        Connection c = getConnection();
        assertFalse(c.getAutoCommit());
        
        Catalog cat = getCatalog();
        
        testCatalog(cat, c);        
        TableMapper tm = getTableMapper();
        
        // ClassLoader gcl = createClassLoaderForGenerated();
        ClassLoader gcl = null;
        
        CatalogContext cc = new CatalogContext(cat, gcl, tm);
                        
        assertEquals(cat, cc.boundTo());                
        assertFalse(cc.getMetaMap().isEmpty());
                       
        BaseTable ct = getContinentTable(cat);
        assertNotNull(ct);
        
        logger().debug("bound tables: " + cc.getMetaMap().keySet());
        logger().debug("key-table: " + ct);
        logger().debug("key-table-loader: " + ct.getClass().getClassLoader());
        
        logger().debug("bound-table-loader: " + cc.getMetaMap().keySet().iterator().next().getClass().getClassLoader());
        
        assertTrue(cc.getMetaMap().containsKey(ct));               
                        
        EntityMetaData<?, ?, ?, ?> meta = cc.getMetaData(ct);
        assertNotNull(meta);
                
        EntityFactory<?, ?, ?, ?> ef = meta.getFactory();
        assertNotNull(ef);
                
        PublicFactory pf = cc.newPublicFactory();
        DefaultContinent cont = pf.newAbstractContinent();
        
        
        PersistenceManager<Attribute, Reference, Query, Entity<Attribute, Reference, Query, ? extends Continent>> pm = cont.createPersistentManager();
                
        cont.setId(8);
        cont.setName("asdf");
                               
        pm.merge(c);
        c.commit();        
        pm.delete(c);
        c.commit();
        pm.insert(c);
        c.commit();
        pm.update(c);
        c.commit();
        pm.delete(c);
        c.commit();
        
        DefaultContinent newCont = pf.newAbstractContinent();
        newCont.setName("asdf");
        pm = newCont.createPersistentManager();
        pm.merge(c);
        c.commit();
    }

}

@@ -94,185 +94,8 @@
 on;%0A
-import java.sql.SQLException;%0Aimport java.sql.Types;%0Aimport java.util.Arrays;%0Aimport java.util.Set;%0A%0Aimport fi.tnie.db.expr.DeleteStatement;%0Aimport fi.tnie.db.expr.ddl.SQLType;%0A
 impo
@@ -160,39 +160,8 @@
 og;%0A
-import fi.tnie.db.meta.Column;%0A
 impo
@@ -217,306 +217,117 @@
 .db.
-meta.DataType;%0Aimport fi.tnie.db.build.Builder;%0Aimport fi.tnie.db.build.BuilderTest;%0Aimport fi.tnie.db.build.CatalogContext;%0Aimport fi.tnie.db.build.pub.AbstractContinent;%0Aimport fi.tnie.db.build.pub.Continent;%0Aimport fi.tnie.db.build.pub.ContinentImpl;%0Aimport fi.tnie.db.build.pub.DefaultContinent
+build.CatalogContext;%0Aimport fi.tnie.db.build.personal.DefaultPerson;%0Aimport fi.tnie.db.build.personal.Person
 ;%0Aim
@@ -353,17 +353,24 @@
 ld.p
-ub.Public
+ersonal.Personal
 Fact
@@ -399,28 +399,30 @@
 .build.p
-ub.Continent
+ersonal.Person
 .Attribu
@@ -450,28 +450,30 @@
 .build.p
-ub.Continent
+ersonal.Person
 .Query;%0A
@@ -501,20 +501,22 @@
 ld.p
-ub.Continent
+ersonal.Person
 .Ref
@@ -1933,21 +1933,23 @@
        P
-ublic
+ersonal
 Factory 
@@ -1960,21 +1960,23 @@
  cc.newP
-ublic
+ersonal
 Factory(
@@ -1997,48 +1997,31 @@
 ault
-Continent cont = pf.newAbstractContinent
+Person p = pf.newPerson
 ();%0A
@@ -2020,33 +2020,32 @@
 rson();%0A        
-%0A
         %0A       
@@ -2143,25 +2143,22 @@
 nds 
-Continent
+Person
 %3E%3E pm = 
 cont
@@ -2153,20 +2153,17 @@
 %3E%3E pm = 
-cont
+p
 .createP
@@ -2203,28 +2203,28 @@
    %0A        
-cont
+// p
 .setId(8);%0A 
@@ -2230,20 +2230,20 @@
         
-cont
+// p
 .setName
@@ -2244,32 +2244,97 @@
 etName(%22asdf%22);%0A
+        p.setFirstName(%22a%22);%0A        p.setLastName(%22b%22);%0A        
                 
@@ -2567,32 +2567,34 @@
 mit();%0A        %0A
+//
         DefaultC
@@ -2635,24 +2635,26 @@
 ontinent();%0A
+//
         newC
@@ -2666,32 +2666,34 @@
 etName(%22asdf%22);%0A
+//
         pm = new
@@ -2716,32 +2716,34 @@
 stentManager();%0A
+//
         pm.merge
@@ -2739,32 +2739,34 @@
    pm.merge(c);%0A
+//
         c.commit

Test with different DB schema