package org.easycassandra.persistence.cassandra;

import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.ResultSetFuture;

/**
 * util to async process.
 * @author otaviojava
 */
enum AsyncResult {
    INSTANCE;

    private static final int THREAD_NUMBER = 10;
    private ExecutorService service = Executors.newFixedThreadPool(THREAD_NUMBER);

    public <T> void runSelect(ResultCallBack<List<T>> callBack,
            ResultSetFuture result, Class<?> class1) {

        result.addListener(new RunSelectRunnable<>(callBack, result, class1),
                service);
    }

    public void runUpdate(ResultCallBack<Boolean> callBack, ResultSetFuture resultSet) {
        resultSet.addListener(new RunUpdateRunnable<>(callBack), service);
    }
    /**
     * to run select async.
     * @author otaviojava
     * @param <T>
     */
    class RunSelectRunnable<T> implements Runnable {

        private ResultCallBack<List<T>> callBack;
        private ResultSetFuture resultSetFuture;
        private Class<?> beanClass;

        public RunSelectRunnable(ResultCallBack<List<T>> callBack,
                ResultSetFuture resultSetFuture, Class<?> beanClass) {
            this.callBack = callBack;
            this.resultSetFuture = resultSetFuture;
            this.beanClass = beanClass;
        }

        @Override
        public void run() {
            ResultSet result;
            try {
                result = resultSetFuture.get();
                @SuppressWarnings("unchecked")
                List<T> beans = (List<T>) RecoveryObject.INTANCE.recoverObjet(beanClass, result);
                callBack.result(beans);
            } catch (InterruptedException | ExecutionException e) {
                e.printStackTrace();
            }
        }

    }
    /**
     * to run update, insert, delete async.
     * @author otaviojava
     * @param <T>
     */
    class RunUpdateRunnable<T> implements Runnable {

        private ResultCallBack<Boolean> callBack;

        public RunUpdateRunnable(ResultCallBack<Boolean> callBack) {
            this.callBack = callBack;
        }

        @Override
        public void run() {
            callBack.result(Boolean.TRUE);
        }

    }
}

@@ -1864,16 +1864,24 @@
 eption e
+xception
 ) %7B%0A    
@@ -1896,26 +1896,140 @@
     
-e.printStackTrace(
+throw new AsyncExecption(%0A                        %22a problem trying to retrieve the select query%22,%0A                        exception
 );%0A 
@@ -2481,20 +2481,449 @@
 %0A        %7D%0A%0A
+    %7D%0A    /**%0A     * execption to async process.%0A     * @author otaviojava%0A     */%0A    public class AsyncExecption extends RuntimeException %7B%0A        private static final long serialVersionUID = 1L;%0A%0A        /**%0A         * The Constructor for Exception.%0A         * @param message information to log%0A         */%0A        public AsyncExecption(String message, Throwable throwable) %7B%0A            super(message, throwable);%0A        %7D%0A
     %7D%0A%7D%0A

add exception