/*
 *  Copyright 2010 salaboy.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */

package org.drools.grid;


import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

import junit.framework.Assert;

import org.drools.KnowledgeBase;
import org.drools.KnowledgeBaseFactory;
import org.drools.KnowledgeBaseFactoryService;
import org.drools.base.MapGlobalResolver;
import org.drools.builder.JPAKnowledgeFactoryService;
import org.drools.builder.KnowledgeBuilder;
import org.drools.builder.KnowledgeBuilderFactoryService;
import org.drools.builder.ResourceType;
import org.drools.io.ResourceFactory;
import org.drools.persistence.jpa.grid.JPAKnowledgeProviderLocalClient;
import org.drools.runtime.Environment;
import org.drools.runtime.EnvironmentName;
import org.drools.runtime.StatefulKnowledgeSession;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import bitronix.tm.TransactionManagerServices;
import bitronix.tm.resource.jdbc.PoolingDataSource;
import org.drools.grid.local.LocalDirectoryConnector;
import org.drools.grid.local.LocalNodeConnector;
import org.drools.grid.strategies.ReturnAlwaysTheFirstSelectionStrategy;

/**
 *
 * @author salaboy
 */
public class LocalExecutionNodeTest extends ExecutionNodeBase{

    private static EntityManagerFactory entityManagerFactory;
    private static PoolingDataSource dataSource;
    private GenericConnection connection;
    public LocalExecutionNodeTest() {
    }

    @BeforeClass
    public static void initializeEntityManager() {
        System.out.println("Initializing Entity Manager");
        entityManagerFactory = Persistence.createEntityManagerFactory("org.drools.persistence.jpa");
    }

    @BeforeClass
    public static void initializeDataSource() {
        System.out.println("Initializing Datasource");
        dataSource = new PoolingDataSource();
        dataSource.setUniqueName( "jdbc/testDS1" );
        dataSource.setClassName( "org.h2.jdbcx.JdbcDataSource" );
        dataSource.setMaxPoolSize( 3 );
        dataSource.setAllowLocalTransactions( true );
        dataSource.getDriverProperties().put( "user",
                                       "sa" );
        dataSource.getDriverProperties().put( "password",
                                       "sasa" );
        dataSource.getDriverProperties().put( "URL",
                                       "jdbc:h2:mem:mydb" );
        dataSource.init();
    }

    @Before
    public void configureNode() throws ConnectorException {
        
        connection = new GridConnection();
        connection.addExecutionNode(new LocalNodeConnector());
        connection.addDirectoryNode(new LocalDirectoryConnector());
        node = connection.getExecutionNode(new ReturnAlwaysTheFirstSelectionStrategy());
        node.set(JPAKnowledgeFactoryService.class, new JPAKnowledgeProviderLocalClient());
    }
    
    private Environment newEnvironment() {
        Environment environment = KnowledgeBaseFactory.newEnvironment();
        environment.set(EnvironmentName.ENTITY_MANAGER_FACTORY, entityManagerFactory);
        environment.set(EnvironmentName.GLOBALS, new MapGlobalResolver());
        environment.set(EnvironmentName.TRANSACTION_MANAGER, TransactionManagerServices.getTransactionManager());

        return environment;
    }

    @Test
    public void persistenceTest(){
        Environment env = newEnvironment();
         String str = "";
        str += "package org.drools \n";
        str += "global java.util.List list \n";
        str += "rule rule1 \n";
        str += "    dialect \"java\" \n";
        str += "when \n";
        str += "then \n";
        str += "    System.out.println( \"hello1!!!\" ); \n";
        str += "end \n";
        str += "rule rule2 \n";
        str += "    dialect \"java\" \n";
        str += "when \n";
        str += "then \n";
        str += "    System.out.println( \"hello2!!!\" ); \n";
        str += "end \n";


        KnowledgeBuilder kbuilder =
                node.get(KnowledgeBuilderFactoryService.class).newKnowledgeBuilder();
        kbuilder.add( ResourceFactory.newByteArrayResource( str.getBytes() ),
                      ResourceType.DRL );

        if ( kbuilder.hasErrors() ) {
            System.out.println( "Errors: " + kbuilder.getErrors() );
        }

        KnowledgeBase kbase =
                node.get(KnowledgeBaseFactoryService.class).newKnowledgeBase();
        Assert.assertNotNull(kbase);

        kbase.addKnowledgePackages( kbuilder.getKnowledgePackages() );


        StatefulKnowledgeSession ksession = node
                                                .get(JPAKnowledgeFactoryService.class)
                                                .newStatefulKnowledgeSession(kbase, null, env);

        Assert.assertNotNull(ksession);

    }
   


}
@@ -934,62 +934,8 @@
 er;%0A
-import org.drools.builder.JPAKnowledgeFactoryService;%0A
 impo
@@ -970,24 +970,24 @@
 dgeBuilder;%0A
+
 import org.d
@@ -1114,80 +1114,8 @@
 ry;%0A
-import org.drools.persistence.jpa.grid.JPAKnowledgeProviderLocalClient;%0A
 impo
@@ -1597,16 +1597,82 @@
 trategy;
+%0Aimport org.drools.persistence.jpa.impl.KnowledgeStoreServiceImpl;
 %0A%0A/**%0A *
@@ -3287,19 +3287,16 @@
 ode.set(
-JPA
 Knowledg
@@ -3292,38 +3292,40 @@
 et(Knowledge
-Factory
+Store
 Service
+Impl
 .class, new 
@@ -3324,19 +3324,16 @@
 ss, new 
-JPA
 Knowledg
@@ -3337,27 +3337,24 @@
 edge
-ProviderLocalClient
+StoreServiceImpl
 ());
@@ -5112,11 +5112,8 @@
 get(
-JPA
 Know
@@ -5117,30 +5117,32 @@
 nowledge
-Factory
+Store
 Service
+Impl
 .class)%0A

Remove duplicate interface JPAKnowledgeFactoryService - done